diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index e0121b6..a565e23 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -103,7 +103,7 @@ function putObjectInstance(objectInstance, description) { } class ClientNodeInstance extends EventEmitter { - constructor(lifetime, manufacturer, model, queueMode, endpointClientName, serverURI, clientPort) { + constructor(options) { super(); this._state = 'stopped'; // eslint-disable-line no-underscore-dangle @@ -112,21 +112,29 @@ class ClientNodeInstance extends EventEmitter { this.updatesIterator = {}; this.observedResources = {}; this.registrationPath = '/rd'; - this.listeningPort = clientPort; + this.listeningPort = options.clientPort; this.updatesInterval = 10; // updates interval in seconds - this.endpointClientName = endpointClientName; + this.endpointClientName = options.endpointClientName; this.coapServer = coap.createServer({ type: 'udp6' }, (req, res) => { this.requestListener(req, res); }); - this.coapServer.listen(clientPort); + this.coapServer.listen(options.clientPort); this.coapAgent = new coap.Agent({ type: 'udp6', socket: this.coapServer._sock, // eslint-disable-line no-underscore-dangle }); + + this.coapAgent.on('error', (error) => { + this.emit('error', error); + }); + this.coapServer.on('error', (error) => { + this.emit('error', error); + }); + this.requestOptions = { - host: serverURI, - port: 5555, + host: options.serverURI, + port: options.serverPort, method: 'POST', confirmable: 'true', agent: this.coapAgent, @@ -134,10 +142,10 @@ class ClientNodeInstance extends EventEmitter { this.stateListener(); - this.initiateSecurityObject(serverURI); - this.initiateServerObject(lifetime, queueMode); + this.initiateSecurityObject(options.serverURI); + this.initiateServerObject(options.lifetime, options.queueMode); this.initiateAccessControlObject(); - this.initiateDeviceObject(manufacturer, model, queueMode); + this.initiateDeviceObject(options.manufacturer, options.model, options.queueMode); this.initiateConnectivityMonitoringObject(); this.initiateFirmwareObject(); this.initiateLocationObject(); @@ -787,6 +795,7 @@ class ClientNodeInstance extends EventEmitter { break; } case 'registered': { + this.emit('registered'); break; } default: { diff --git a/8dev_emulate/nodes/sensorInstances.js b/8dev_emulate/nodes/sensorInstances.js index 1204e2d..ba986ab 100644 --- a/8dev_emulate/nodes/sensorInstances.js +++ b/8dev_emulate/nodes/sensorInstances.js @@ -34,8 +34,8 @@ function humiditySensorHandle(averageHumidity = 70, fluctuation = 0.05) { } class Sensor3700 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_3700', true, UUID, serverIP, clientPort); + constructor(options) { + super(options); this.initialisePowerSourceVoltageResource(); this.initialisePowerMeasurementObject(); @@ -138,8 +138,8 @@ class Sensor3700 extends ClientNode { } class Sensor3800 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_3800', true, UUID, serverIP, clientPort); + constructor(options) { + super(options); this.initialisePowerSourceVoltageResource(); this.initialiseTemperatureSensorObject(); @@ -184,8 +184,8 @@ class Sensor3800 extends ClientNode { } class Sensor4400 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_4400', true, UUID, serverIP, clientPort); + constructor(options) { + super(options); this.initialisePowerSourceVoltageResource(); this.initialiseDigitalInputObject(); @@ -251,8 +251,8 @@ class Sensor4400 extends ClientNode { } class Sensor4500 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_4500', true, UUID, serverIP, clientPort); + constructor(options) { + super(options); this.initialisePowerSourceVoltageResource(); this.initialiseAnalogueInputOjbect(); diff --git a/package.json b/package.json index a876a1b..86eed15 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ }, "homepage": "https://github.com/8devices/restserver-api#readme", "dependencies": { + "coap": "git+https://github.com/mcollina/node-coap.git#v0.23.0", "body-parser": "^1.18.2", - "coap": "^0.21.0", "express": "^4.16.3", "ip": "^1.1.5", "node-rest-client": "^3.1.0"