0.1.16 update utility dependencies, improve the fio example so mqtt work correctly

master
David Kebler 2019-02-14 14:01:21 -08:00
parent a5fa20b74a
commit 0a86774b17
4 changed files with 35 additions and 58 deletions

View File

@ -1,12 +0,0 @@
language: node_js
node_js:
- '7.10'
- 'node'
sudo: false
script: npm test
after_success:
- bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"

View File

@ -6,18 +6,22 @@ const socketfuncs = {
switch: { switch: {
on: function(packet){ on: function(packet){
return new Promise( async (resolve) => { return new Promise( async (resolve) => {
packet.cmd='status/on' packet.cmd='switch/status'
packet.status='on'
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
this.push(packet) // push to all active socket servers this.push(packet) // push to all active socket servers
let res = { response:'status pushed on to all clients'} let res = { response:'status pushed on to all clients'}
return resolve(res) return resolve(res) // this goes back to sender
}) })
}, },
off: function(packet){ off: function(packet){
return new Promise( async (resolve) => { return new Promise( async (resolve) => {
packet.cmd='status/off' packet.cmd='switch/status'
packet.status='off'
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
this.push(packet) // push to all active socket servers this.push(packet) // push to all active socket servers
let res = { response:'status pushed off to all clients'} let res = { response:'status pushed off to all clients'}
return resolve(res) return resolve(res) // this goes back to sender
}) })
}, },
toggle: function(packet){ toggle: function(packet){
@ -25,46 +29,34 @@ const socketfuncs = {
// would check status before deciding what to push // would check status before deciding what to push
if (packet.status===null) packet.status = Math.random()>=.5 ? 'on' : 'off' if (packet.status===null) packet.status = Math.random()>=.5 ? 'on' : 'off'
else packet.status = (packet.status==='on' ? 'off' : 'on') else packet.status = (packet.status==='on' ? 'off' : 'on')
packet.cmd ='status/'+ packet.status packet.cmd ='switch/status'
packet.sender= packet.sender || (packet._header ? packet._header.sender.name : 'unknown')
this.push(packet) // push to all active socket servers this.push(packet) // push to all active socket servers
let res = { cmd:'reply', response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`} let res = { response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`}
return resolve(res) return resolve(res) // this goes back to sender
}) })
} }
} }
} }
const clientfuncs = {
reply: packet => { function status(state,consumer) {
return (packet) => {
return new Promise( async (resolve) => { return new Promise( async (resolve) => {
console.log('==============Reply Processor at Consumer==========') // console.log('complete packet', packet, consumer)
console.dir(packet.response) console.log(`Switch ${packet.id} is ${packet.status}, sent by ${packet.sender || packet.data.sender} to ${consumer}`)
return resolve() return resolve()
}) })
},
status: {
on: packet => {
return new Promise( async (resolve) => {
console.log('==============Pushed to Consumer==========')
console.log(`Switch ${packet.id} is on`)
return resolve()
})
},
off: packet => {
return new Promise( async (resolve) => {
console.log('==============Pushed to Consumer==========')
console.log(`Switch ${packet.id} is off`)
return resolve()
})
}
} }
} }
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,mqtts#s>m,webs#s>w', tc:{port:8100}, ts:{port:8100}, webs:{ port:8090 }, mqtts:{ topics:['switch/#']}, id:'four-in-one'}) let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,mqtts#s>m,mqtt#c>m,webs#s>w', tc:{port:8100}, ts:{port:8100}, webs:{ port:8090 }, mqtts:{ topics:['switch/on','switch/off','switch/toggle']}, mqtt:{ topics:['switch/status']}})
fio.s = socketfuncs fio.s = socketfuncs
fio.c = clientfuncs fio.c = { reply: () => {return Promise.resolve()} }
fio.cn = {switch:{status:status('on','named')}}
fio.ct = {switch:{status:status('on','tcp')}}
fio.cm = {switch:{status:status('on','mqtt')}}
; ;
(async () => { (async () => {
@ -75,10 +67,10 @@ fio.c = clientfuncs
let packet = {} let packet = {}
console.log('=============sending============') packet = {cmd:'switch/toggle', id:'1'}
packet = {cmd:'switch/on', id:'1'} console.log('=====sending to socket uc======',packet)
console.log('sending to socket uc',packet) console.log('this should get pushed to all listening')
await fio.send('uc',packet) console.log('response back to uc: ',(await fio.send('uc',packet)).response)
})().catch(err => { })().catch(err => {

View File

@ -1,11 +1,11 @@
{ {
"name": "@uci/base", "name": "@uci/base",
"version": "0.1.15", "version": "0.1.16",
"description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes", "description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes",
"main": "src/base", "main": "src/base",
"scripts": { "scripts": {
"deve": "./node_modules/.bin/nodemon -r esm examples/four-in-one", "fiod": "./node_modules/.bin/nodemon -r esm examples/four-in-one",
"fio": "UCI_ENV=dev node -r esm examples/four-in-one", "fio": "node -r esm examples/four-in-one",
"dy": "node -r esm examples/dynamic", "dy": "node -r esm examples/dynamic",
"web": "UCI_DEV=true nodemon -r esm examples/web", "web": "UCI_DEV=true nodemon -r esm examples/web",
"mqtt": "node -r esm examples/mqtt", "mqtt": "node -r esm examples/mqtt",
@ -31,19 +31,16 @@
"homepage": "https://github.com/uCOMmandIt/message#readme", "homepage": "https://github.com/uCOMmandIt/message#readme",
"devDependencies": { "devDependencies": {
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"codecov": "^3.2.0",
"esm": "^3.0.84", "esm": "^3.0.84",
"istanbul": "^0.4.5",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"nodemon": "^1.18.6" "nodemon": "^1.18.6"
}, },
"dependencies": { "dependencies": {
"@uci-utils/bind-funcs": "^0.2.3", "@uci-utils/bind-funcs": "^0.2.3",
"@uci-utils/logger": "0.0.12", "@uci-utils/logger": "0.0.13",
"@uci/mqtt": "^0.1.7", "@uci/mqtt": "^0.1.8",
"@uci/socket": "^0.2.6", "@uci/socket": "^0.2.7",
"@uci/websocket": "^0.3.3", "@uci/websocket": "^0.3.4",
"p-settle": "^2.1.0" "p-settle": "^2.1.0"
} }
} }

View File

@ -9,9 +9,9 @@ import Socket from '@uci/socket' // tcp or named pipe
import MQTT from '@uci/mqtt' // requires broker import MQTT from '@uci/mqtt' // requires broker
import WebSocket from '@uci/websocket' // server only - client is for web browser only import WebSocket from '@uci/websocket' // server only - client is for web browser only
// UCI helpers // UCI helpers
import { bindFuncs } from '@uci/utils/src/function' import { bindFuncs } from '@uci-utils/bind-funcs'
// UCI logger // UCI logger
import logger from '@uci/logger' import logger from '@uci-utils/logger'
let log = {} // declare module wide log to be set during construction let log = {} // declare module wide log to be set during construction
// Community dependencies // Community dependencies