From 0a86774b173f58c5a77cf37ac57c1c41ed269030 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Thu, 14 Feb 2019 14:01:21 -0800 Subject: [PATCH] 0.1.16 update utility dependencies, improve the fio example so mqtt work correctly --- .travis.yml | 12 --------- examples/four-in-one.js | 60 ++++++++++++++++++----------------------- package.json | 17 +++++------- src/base.js | 4 +-- 4 files changed, 35 insertions(+), 58 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5b0b93e..0000000 --- a/.travis.yml +++ /dev/null @@ -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" diff --git a/examples/four-in-one.js b/examples/four-in-one.js index 3c6cca9..6ab8242 100644 --- a/examples/four-in-one.js +++ b/examples/four-in-one.js @@ -6,18 +6,22 @@ const socketfuncs = { switch: { on: function(packet){ 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 let res = { response:'status pushed on to all clients'} - return resolve(res) + return resolve(res) // this goes back to sender }) }, off: function(packet){ 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 let res = { response:'status pushed off to all clients'} - return resolve(res) + return resolve(res) // this goes back to sender }) }, toggle: function(packet){ @@ -25,46 +29,34 @@ const socketfuncs = { // would check status before deciding what to push if (packet.status===null) packet.status = Math.random()>=.5 ? 'on' : 'off' 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 - let res = { cmd:'reply', response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`} - return resolve(res) + let res = { response:`command ${packet.cmd} with id:${packet.id} pushed to all consumers(clients)`} + return resolve(res) // this goes back to sender }) } } } -const clientfuncs = { - reply: packet => { + +function status(state,consumer) { + return (packet) => { return new Promise( async (resolve) => { - console.log('==============Reply Processor at Consumer==========') - console.dir(packet.response) + // console.log('complete packet', packet, consumer) + console.log(`Switch ${packet.id} is ${packet.status}, sent by ${packet.sender || packet.data.sender} to ${consumer}`) 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.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 () => { @@ -75,10 +67,10 @@ fio.c = clientfuncs let packet = {} - console.log('=============sending============') - packet = {cmd:'switch/on', id:'1'} - console.log('sending to socket uc',packet) - await fio.send('uc',packet) + packet = {cmd:'switch/toggle', id:'1'} + console.log('=====sending to socket uc======',packet) + console.log('this should get pushed to all listening') + console.log('response back to uc: ',(await fio.send('uc',packet)).response) })().catch(err => { diff --git a/package.json b/package.json index c26212e..d8b27d5 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "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", "main": "src/base", "scripts": { - "deve": "./node_modules/.bin/nodemon -r esm examples/four-in-one", - "fio": "UCI_ENV=dev node -r esm examples/four-in-one", + "fiod": "./node_modules/.bin/nodemon -r esm examples/four-in-one", + "fio": "node -r esm examples/four-in-one", "dy": "node -r esm examples/dynamic", "web": "UCI_DEV=true nodemon -r esm examples/web", "mqtt": "node -r esm examples/mqtt", @@ -31,19 +31,16 @@ "homepage": "https://github.com/uCOMmandIt/message#readme", "devDependencies": { "chai": "^4.2.0", - "chai-as-promised": "^7.1.1", - "codecov": "^3.2.0", "esm": "^3.0.84", - "istanbul": "^0.4.5", "mocha": "^5.2.0", "nodemon": "^1.18.6" }, "dependencies": { "@uci-utils/bind-funcs": "^0.2.3", - "@uci-utils/logger": "0.0.12", - "@uci/mqtt": "^0.1.7", - "@uci/socket": "^0.2.6", - "@uci/websocket": "^0.3.3", + "@uci-utils/logger": "0.0.13", + "@uci/mqtt": "^0.1.8", + "@uci/socket": "^0.2.7", + "@uci/websocket": "^0.3.4", "p-settle": "^2.1.0" } } diff --git a/src/base.js b/src/base.js index 2866749..ab71af6 100644 --- a/src/base.js +++ b/src/base.js @@ -9,9 +9,9 @@ import Socket from '@uci/socket' // tcp or named pipe import MQTT from '@uci/mqtt' // requires broker import WebSocket from '@uci/websocket' // server only - client is for web browser only // UCI helpers -import { bindFuncs } from '@uci/utils/src/function' +import { bindFuncs } from '@uci-utils/bind-funcs' // UCI logger -import logger from '@uci/logger' +import logger from '@uci-utils/logger' let log = {} // declare module wide log to be set during construction // Community dependencies