changing to esm .mjs module from .js using @std/esm
parent
c3e94b465e
commit
0290ec99a7
8
index.js
8
index.js
|
@ -1,8 +0,0 @@
|
|||
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)
|
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
|
@ -1,11 +1,10 @@
|
|||
{
|
||||
"name": "@uci/device",
|
||||
"name": "@uci/esmtesting",
|
||||
"version": "0.1.0",
|
||||
"description": "Mutli Level/Transport Message/Event Classes",
|
||||
"description": "Test for esm",
|
||||
"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"
|
||||
"start": "node -r @std/esm test"
|
||||
},
|
||||
"author": "David Kebler",
|
||||
"license": "MIT",
|
||||
|
@ -23,18 +22,11 @@
|
|||
"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"
|
||||
"@std/esm": "^0.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"json-ipc-lib": "^1.0.2"
|
||||
}
|
||||
}
|
||||
|
|
11
readme.md
11
readme.md
|
@ -1,10 +1,5 @@
|
|||
# uCOMmandIt Message and Event Classes
|
||||
## Native ESM Testing
|
||||
|
||||
<!-- 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)
|
||||
`npm start`
|
||||
|
||||
Multi-level Protocol Messaging/Event base class for all UCI devices hardware and software including buses and user frontend, backend, etc.
|
||||
esm module loader testing for module written with es modules but using .js as it was meant to be transpiled
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
// 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
|
|
@ -1,76 +0,0 @@
|
|||
'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
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
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()
|
|
@ -1,41 +0,0 @@
|
|||
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)
|
||||
}
|
||||
)
|
|
@ -0,0 +1,12 @@
|
|||
import ipc from 'json-ipc-lib/src'
|
||||
|
||||
console.log('clinet\n',[...getAllMethodNames(new ipc.client )])
|
||||
|
||||
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
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
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))
|
||||
// })
|
||||
})
|
Loading…
Reference in New Issue