From 51139a3b357f4b2bb58073f1d1093150bcc1bd7b Mon Sep 17 00:00:00 2001 From: joyja Date: Mon, 23 Mar 2020 19:22:31 +0000 Subject: [PATCH] added subscriptions for service and device updates (mqtt updates on log) others update on connect & disconnect --- src/device/ethernetip.js | 6 ++++++ src/device/modbus.js | 6 ++++++ src/relations.js | 4 +++- src/resolvers/Subscription/index.js | 4 ++-- src/schema.graphql | 3 ++- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/device/ethernetip.js b/src/device/ethernetip.js index 2cdd62a..cacd3d2 100644 --- a/src/device/ethernetip.js +++ b/src/device/ethernetip.js @@ -32,11 +32,17 @@ class EthernetIP extends Model { } else { this.connected = false } + this.pubsub.publish('deviceUpdate', { + deviceUpdate: this.device + }) } } async disconnect() { this.client.destroy() this.connected = false + this.pubsub.publish('deviceUpdate', { + deviceUpdate: this.device + }) } get host() { this.checkInit() diff --git a/src/device/modbus.js b/src/device/modbus.js index e70e690..169662c 100644 --- a/src/device/modbus.js +++ b/src/device/modbus.js @@ -81,6 +81,9 @@ class Modbus extends Model { `Connection failed to modbus device ${this.device.name}, host: ${this.host}, port: ${this.port}, with error: ${this.error}.` ) } + this.pubsub.publish('deviceUpdate', { + deviceUpdate: this.device + }) } } async disconnect() { @@ -100,6 +103,9 @@ class Modbus extends Model { } }) this.connected = false + this.pubsub.publish('deviceUpdate', { + deviceUpdate: this.device + }) } get host() { this.checkInit() diff --git a/src/relations.js b/src/relations.js index ca3ed28..6cb4d9a 100644 --- a/src/relations.js +++ b/src/relations.js @@ -400,7 +400,9 @@ MqttSource.prototype.log = async function(scanClassId) { sql = `${sql} VALUES (?,?);` params = [host.id, result.lastID] await this.constructor.executeUpdate(sql, params) - this.pubsub.publish('mqttUpdate', { mqttUpdate: this.mqtt.service }) + this.pubsub.publish('serviceUpdate', { + serviceUpdate: this.mqtt.service + }) } resolve() }) diff --git a/src/resolvers/Subscription/index.js b/src/resolvers/Subscription/index.js index 7817799..6d5f048 100644 --- a/src/resolvers/Subscription/index.js +++ b/src/resolvers/Subscription/index.js @@ -1,7 +1,7 @@ const tag = require('./tag') -const mqttUpdate = require('./mqttUpdate') +const service = require('./service') module.exports = { - ...mqttUpdate, + ...service, ...tag } diff --git a/src/schema.graphql b/src/schema.graphql index 17cec34..29b9e58 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -357,5 +357,6 @@ type Mutation { type Subscription { tagUpdate: Tag, - mqttUpdate: Service + deviceUpdate: Device, + serviceUpdate: Service } \ No newline at end of file