initial commit

master
David Kebler 2017-12-21 15:47:42 -08:00
parent 6b9b399ee5
commit c3e94b465e
13 changed files with 1539 additions and 0 deletions

33
.eslintrc.js Normal file
View File

@ -0,0 +1,33 @@
module.exports = {
"ecmaFeatures": {
"modules": true,
"spread" : true,
"restParams" : true
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"no-console": 0,
"semi": ["error", "never"],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
]
}
}

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/node_modules/
/coverage/
/syncd/

4
.npmignore Normal file
View File

@ -0,0 +1,4 @@
tests/
test/
*.test.js
testing/

12
.travis.yml Normal file
View File

@ -0,0 +1,12 @@
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"

8
index.js Normal file
View File

@ -0,0 +1,8 @@
let opts = {
dirname: __dirname + '/src',
// http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string
filter: /^(?!index)([^\.].*)\.js?$/,
recursive: false,
merge: true // remove or comment to have each file in /lib be a prop/key in library...see node-require-all
}
module.exports = require('@uci/require-all')(opts)

1207
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

40
package.json Normal file
View File

@ -0,0 +1,40 @@
{
"name": "@uci/device",
"version": "0.1.0",
"description": "Mutli Level/Transport Message/Event Classes",
"main": "index.js",
"scripts": {
"testw": "node -r @std/esm ./node_modules/.bin/_mocha ./test --compilers=mjs:@std/esm --reporter list --recursive --watch",
"test": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true"
},
"author": "David Kebler",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/uCOMmandIt/message.git"
},
"keywords": [
"node.js",
"i2c",
"rpi",
"raspberrypi"
],
"bugs": {
"url": "https://github.com/uCOMmandIt/message/issues"
},
"homepage": "https://github.com/uCOMmandIt/message#readme",
"dependencies": {
"@uci/require-all": "^2.x",
"aggregation": "^1.2.1",
"node-ipc": "^9.1.1"
},
"@std/esm": "cjs",
"devDependencies": {
"@std/esm": "^0.18.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"codecov": "^3.0.0",
"istanbul": "^0.4.5",
"mocha": "^4.0.1"
}
}

10
readme.md Normal file
View File

@ -0,0 +1,10 @@
# uCOMmandIt Message and Event Classes
<!-- find and replace the package name to match -->
[![Build Status](https://img.shields.io/travis/uCOMmandIt/uci-pkg-template.svg?branch=master)](https://travis-ci.org/uCOMmandIt/uci-pkg-template)
[![Inline docs](http://inch-ci.org/github/uCOMmandIt/uci-pkg-template.svg?branch=master)](http://inch-ci.org/github/uCOMmandIt/uci-pkg-template)
[![Dependencies](https://img.shields.io/david/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template)
[![devDependencies](https://img.shields.io/david/dev/uCOMmandIt/uci-pkg-template.svg)](https://david-dm.org/uCOMmandIt/uci-pkg-template?type=dev)
[![codecov](https://img.shields.io/codecov/c/github/uCOMmandIt/uci-pkg-template/master.svg)](https://codecov.io/gh/uCOMmandIt/uci-pkg-template)
Multi-level Protocol Messaging/Event base class for all UCI devices hardware and software including buses and user frontend, backend, etc.

41
src/device.mjs Normal file
View File

@ -0,0 +1,41 @@
// Every UCI device whether it be hardware or strictly software device has in common
// A means of communication to other devices whether they be on the same machine host, across a network or even within itself
// To
import EventEmitter from 'events'
import IPC from 'node-ipc/services/IPC'
import aggregate from 'aggregation/es6'
console.log(IPC)
export default class Device extends aggregate(EventEmitter,IPC) {
constructor(id, opts) {
super()
this.id = id //must be unique in entire messaging system
if (opts) {
this.transports = opts.transports
}
}
async init() {}
async listen(message) {
console.log(message)
}
async send(message) {
console.log(message)
}
}
// # dc|intra|native|unix|tcp|mqtt
// # dc = direct call without messaging.
// # intra = within same process (standard node event emitter)
// # native = node child process using pipes
// # unix = unix socket on same host/machine
// # tcp = using
// # tls = secure TCP
// # udp
// # mqtt

76
src/i2c-device.js Normal file
View File

@ -0,0 +1,76 @@
'use strict'
// **********************************
class Device {
// bus is i2c-bus bus object
constructor(bus, address, opts) {
this.bus = bus
this.address = address
if (opts) {
this.id = opts.id // must be unique within a bus
this.desc = opts.desc
this.channel = opts.channel // if using TAC9546A channel number on which device is attached
}
}
async _setChannel() {
// console.log('before set',this.address,this.id,this.channel, this.bus.getState)
if (this.channel) {
if (!this.bus.address) { return Promise.reject('Channel set but no mux on bus')}
return this.bus.set(this.channel)
}
return Promise.resolve() // no channel for device either no mux or device is attached to mux bypass
}
// for devices that need just a simple send of a byte without a register command
async receive() {
await this._setChannel()
return this.bus.receive(this.address)
}
async send(cmd, byte) {
await this._setChannel()
return this.bus.send(this.address, cmd, byte)
}
// for devices needing a buffer/stream
async readRaw(length, buffer) {
await this._setChannel()
return this.bus.readRaw(this.address, length, buffer)
}
async writeRaw(length, buffer) {
await this._setChannel()
return this.bus.writeRaw(this.address, length, buffer)
}
// both cmd and byte should be a single byte as a decimal or hex
async read(cmd) {
await this._setChannel()
// console.log('after set before read',this.address,this.id,this.channel,this.bus.getState)
return this.bus.read(this.address, cmd)
}
async write(cmd, byte) {
await this._setChannel()
// console.log('after set, before write',this.address,this.id,this.channel,this.bus.getState)
return this.bus.write(this.address, cmd, byte)
}
// for I2C devices that use a word length packackage
async read2(cmd) {
await this._setChannel()
return this.bus.read2(this.address, cmd)
}
async write2(cmd, bytes) {
await this._setChannel()
return this.bus.write2(this.address, cmd, bytes)
}
}
module.exports = {
Device
}

29
src/mbroker.mjs Normal file
View File

@ -0,0 +1,29 @@
import ipc from 'node-ipc'
// const ipc = require('node-ipc')
ipc.config.id = 'broker'
ipc.config.retry= 1500
ipc.serve(
function(){
ipc.server.on(
'device.req',
function(data,socket){
if (data.message === 'understand') {
ipc.server.emit(
socket,
'server.res',
{
id : ipc.config.id,
message : `Message from ${data.id} understood`
}
)
}
}
)
}
)
ipc.server.start()

41
src/message-bus.mjs Normal file
View File

@ -0,0 +1,41 @@
import ipc from 'node-ipc'
const BROKER_ID = 'broker'
ipc.config.id = 'somedevice'
ipc.config.retry = 1000
ipc.connectTo(
BROKER_ID,
function(){
ipc.of[BROKER_ID].on(
'connect',
function(){
ipc.log(`connected to ${BROKER_ID}`)
ipc.of[BROKER_ID].emit(
'device.req',
{
id : ipc.config.id,
message : 'understand'
}
)
}
)
// ipc.of.bus.on(
// 'disconnect',
// function(){
// ipc.log('disconnected from bus')
// }
// )
// listen for bus
ipc.of[BROKER_ID].on(
'server.res',
function(data){
ipc.log('got a response from server : ', data)
}
)
// console.log(ipc.of.bus.destroy)
}
)

35
test/device.test.mjs Normal file
View File

@ -0,0 +1,35 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import Device from '../src/device'
chai.use(chaiAsPromised)
const expect = chai.expect
let device = new Device('test')
describe('Device Class - ', function () {
it('Has properties', function () {
expect(device.id).to.equal('test')
})
// function getAllMethodNames(obj) {
// let methods = new Set()
// while (obj = Reflect.getPrototypeOf(obj)) {
// let keys = Reflect.ownKeys(obj)
// keys.forEach((k) => methods.add(k))
// }
// return methods
// }
//
// let methods = getAllMethodNames(device)
// console.log([...methods])
// it('Can write and read to actual device', function () {
//
// device.write(0x09, SET).then(expect(device.read(0x0A)).to.eventually.equal(SET))
// .then(setTimeout(() => device.write(0x09, 0), 3000))
// .catch(err => console.log('an error', err))
// })
})