Skip to content

Commit

Permalink
fix: socket events constants
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Dec 7, 2020
1 parent 10143d6 commit 5619cb3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
1 change: 1 addition & 0 deletions lib/SocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = reqlib('/lib/debug')('Socket')

debug.color = 3

// FIXME: this constants are duplicated on /src/plugins/socket.js. When converting this to ES6 module that can be removed
// events from server ---> client
const socketEvents = {
init: 'INIT', // automatically sent when a new client connects to the socket
Expand Down
18 changes: 0 additions & 18 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
@showSnackbar="showSnackbar"
@showConfirm="confirm"
:socket="socket"
:socketEvents="socketEvents"
:socketActions="socketActions"
/>
</v-main>
</main>
Expand Down Expand Up @@ -222,22 +220,6 @@ export default {
{ icon: 'settings', title: 'Settings', path: '/settings' },
{ icon: 'share', title: 'Network graph', path: '/mesh' }
],
socketEvents: {
init: 'INIT',
controller: 'CONTROLLER_CMD',
connected: 'CONNECTED',
nodeRemoved: 'NODE_REMOVED',
nodeUpdated: 'NODE_UPDATED',
valueUpdated: 'VALUE_UPDATED',
valueRemoved: 'VALUE_REMOVED',
api: 'API_RETURN',
debug: 'DEBUG'
},
socketActions: {
init: 'INITED',
hass: 'HASS_API',
zwave: 'ZWAVE_API'
},
status: '',
statusColor: '',
drawer: false,
Expand Down
43 changes: 21 additions & 22 deletions src/components/ControlPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ import ValueID from '@/components/ValueId'
import AnsiUp from 'ansi_up'
import DialogSceneValue from '@/components/dialogs/DialogSceneValue'
import { socketEvents, inboundEvents as socketActions } from '@/plugins/socket'
const ansiUp = new AnsiUp()
Expand All @@ -668,9 +669,7 @@ const MAX_DEBUG_LINES = 300
export default {
name: 'ControlPanel',
props: {
socket: Object,
socketActions: Object,
socketEvents: Object
socket: Object
},
components: {
ValueID,
Expand Down Expand Up @@ -997,7 +996,7 @@ export default {
api: apiName,
args: args
}
this.socket.emit(this.socketActions.zwave, data)
this.socket.emit(socketActions.zwave, data)
} else {
this.showSnackbar('Socket disconnected')
}
Expand Down Expand Up @@ -1074,7 +1073,7 @@ export default {
'alert'
))
) {
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'delete',
device: device,
nodeId: this.selectedNode.id
Expand All @@ -1090,7 +1089,7 @@ export default {
'Are you sure you want to re-discover all node values?'
))
) {
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'rediscoverNode',
nodeId: this.selectedNode.id
})
Expand All @@ -1105,7 +1104,7 @@ export default {
'Are you sure you want to disable discovery of all values? In order to make this persistent remember to click on Store'
))
) {
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'disableDiscovery',
nodeId: this.selectedNode.id
})
Expand All @@ -1120,7 +1119,7 @@ export default {
'Are you sure you want to re-discover selected device?'
))
) {
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'discover',
device: device,
nodeId: this.selectedNode.id
Expand All @@ -1135,7 +1134,7 @@ export default {
this.selectedDevice.id,
updated
)
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'update',
device: updated,
nodeId: this.selectedNode.id
Expand All @@ -1145,15 +1144,15 @@ export default {
addDevice () {
if (!this.errorDevice) {
var newDevice = JSON.parse(this.deviceJSON)
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'add',
device: newDevice,
nodeId: this.selectedNode.id
})
}
},
storeDevices (remove) {
this.socket.emit(this.socketActions.hass, {
this.socket.emit(socketActions.hass, {
apiName: 'store',
devices: this.selectedNode.hassDevices,
nodeId: this.selectedNode.id,
Expand Down Expand Up @@ -1404,24 +1403,24 @@ export default {
this.nodeTableItems = !isNaN(itemsPerPage) ? itemsPerPage : 10
this.socket.on(this.socketEvents.controller, data => {
this.socket.on(socketEvents.controller, data => {
self.cnt_status = data
})
this.socket.on(this.socketEvents.connected, info => {
this.socket.on(socketEvents.connected, info => {
self.homeid = info.homeid
self.homeHex = info.name
self.ozwVersion = info.version
})
this.socket.on(this.socketEvents.nodeRemoved, node => {
this.socket.on(socketEvents.nodeRemoved, node => {
if (self.selectedNode && self.selectedNode.id === node.id) {
self.selectedNode = null
}
self.$set(self.nodes, node.id, node)
})
this.socket.on(this.socketEvents.debug, data => {
this.socket.on(socketEvents.debug, data => {
if (self.debugActive) {
data = ansiUp.ansi_to_html(data)
data = data.replace(/\n/g, '</br>')
Expand All @@ -1438,7 +1437,7 @@ export default {
}
})
this.socket.on(this.socketEvents.init, data => {
this.socket.on(socketEvents.init, data => {
// convert node values in array
var nodes = data.nodes
for (var i = 0; i < nodes.length; i++) {
Expand All @@ -1451,7 +1450,7 @@ export default {
self.ozwVersion = data.info.version
})
this.socket.on(this.socketEvents.nodeUpdated, data => {
this.socket.on(socketEvents.nodeUpdated, data => {
self.initNode(data)
if (!self.nodes[data.id] || self.nodes[data.id].failed) {
// add missing nodes
Expand All @@ -1470,7 +1469,7 @@ export default {
}
})
this.socket.on(this.socketEvents.valueRemoved, data => {
this.socket.on(socketEvents.valueRemoved, data => {
const valueId = self.getValue(data)
if (valueId) {
Expand All @@ -1483,7 +1482,7 @@ export default {
}
})
this.socket.on(this.socketEvents.valueUpdated, data => {
this.socket.on(socketEvents.valueUpdated, data => {
const valueId = self.getValue(data)
if (valueId) {
Expand All @@ -1504,7 +1503,7 @@ export default {
}
})
this.socket.on(this.socketEvents.api, async data => {
this.socket.on(socketEvents.api, async data => {
if (data.success) {
switch (data.api) {
case 'getAssociations':
Expand Down Expand Up @@ -1542,12 +1541,12 @@ export default {
}
})
this.socket.emit(this.socketActions.init, true)
this.socket.emit(socketActions.init, true)
},
beforeDestroy () {
if (this.socket) {
// unbind events
for (const event in this.socketEvents) {
for (const event in socketEvents) {
this.socket.off(event)
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/components/Mesh.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@
<script>
import D3Network from 'vue-d3-network'
import { socketEvents, inboundEvents as socketActions } from '@/plugins/socket'
export default {
name: 'Mesh',
props: {
socket: Object,
socketActions: Object,
socketEvents: Object
socket: Object
},
components: {
D3Network
Expand Down Expand Up @@ -221,13 +221,13 @@ export default {
api: apiName,
args: args
}
this.socket.emit(this.socketActions.zwave, data)
this.socket.emit(socketActions.zwave, data)
} else {
this.showSnackbar('Socket disconnected')
}
},
refresh () {
this.socket.emit(this.socketActions.zwave, {
this.socket.emit(socketActions.zwave, {
api: 'refreshNeighbors',
args: []
})
Expand All @@ -251,23 +251,23 @@ export default {
mounted () {
var self = this
this.socket.on(this.socketEvents.nodeRemoved, node => {
this.socket.on(socketEvents.nodeRemoved, node => {
self.$set(self.nodes, node.id, node)
})
this.socket.on(this.socketEvents.init, data => {
this.socket.on(socketEvents.init, data => {
var nodes = data.nodes
for (var i = 0; i < nodes.length; i++) {
self.nodes.push(self.convertNode(nodes[i]))
}
})
this.socket.on(this.socketEvents.nodeRemoved, node => {
this.socket.on(socketEvents.nodeRemoved, node => {
self.$set(self.nodes, node.id, node)
self.refresh()
})
this.socket.on(this.socketEvents.nodeUpdated, data => {
this.socket.on(socketEvents.nodeUpdated, data => {
var node = self.convertNode(data)
// node added
Expand All @@ -290,7 +290,7 @@ export default {
}
})
this.socket.on(this.socketEvents.api, data => {
this.socket.on(socketEvents.api, data => {
if (data.success) {
switch (data.api) {
case 'refreshNeighbors':
Expand All @@ -310,7 +310,7 @@ export default {
}
})
this.socket.emit(this.socketActions.init, true)
this.socket.emit(socketActions.init, true)
this.refresh()
// make properties window draggable
Expand Down Expand Up @@ -364,7 +364,7 @@ export default {
beforeDestroy () {
if (this.socket) {
// unbind events
for (const event in this.socketEvents) {
for (const event in socketEvents) {
this.socket.off(event)
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// FIXME: This comes from /lib/SocketManager.js. It cannot be imported as it's commonjs module and require isn't supported by webpack

// events from client ---> server
export const inboundEvents = {
init: 'INITED', // get all nodes
zwave: 'ZWAVE_API', // call a zwave api
hass: 'HASS_API' // call an hass api
}

// events from server ---> client
export const socketEvents = {
init: 'INIT', // automatically sent when a new client connects to the socket
controller: 'CONTROLLER_CMD', // controller status updates
connected: 'CONNECTED', // socket status
nodeRemoved: 'NODE_REMOVED',
nodeUpdated: 'NODE_UPDATED',
valueUpdated: 'VALUE_UPDATED',
valueRemoved: 'VALUE_REMOVED',
api: 'API_RETURN', // api results
debug: 'DEBUG'
}

0 comments on commit 5619cb3

Please sign in to comment.