|
| 1 | +const os = require('os'); |
| 2 | +const ip = require('ip'); |
| 3 | +const async = require('async'); |
| 4 | +const express = require('express'); |
| 5 | +const bacnet = require('bacstack'); |
| 6 | +const debug = require('debug')('bacstack-browser'); |
| 7 | + |
| 8 | +const utils = require('./utils'); |
| 9 | + |
| 10 | +const app = express(); |
| 11 | + |
| 12 | +// Local stores |
| 13 | +const settings = { |
| 14 | + port: 47808, |
| 15 | + nic: 12, |
| 16 | + timeout: 4000, |
| 17 | + language: 'en', |
| 18 | + analytics: true |
| 19 | +}; |
| 20 | + |
| 21 | +const devices = {}; |
| 22 | + |
| 23 | +// BACNET Stuff |
| 24 | +const client = new bacnet({adpuTimeout: 6000}); |
| 25 | + |
| 26 | +client.on('iAm', (device) => { |
| 27 | + const id = `${device.address}:${device.deviceId}`; |
| 28 | + devices[id] = device; |
| 29 | + devices[id].id = id; |
| 30 | + client.readPropertyMultiple(device.address, [ |
| 31 | + {objectId: {type: 8, instance: 4194303}, properties: [{id: bacnet.enum.PropertyIds.PROP_OBJECT_NAME}, {id: bacnet.enum.PropertyIds.PROP_DESCRIPTION}]} |
| 32 | + ], (err, value) => { |
| 33 | + if (err) return; |
| 34 | + if (value && value.values && value.values[0] && value.values[0].values) { |
| 35 | + const tmp = {}; |
| 36 | + value.values[0].values.forEach(data => tmp[data.id] = data.value[0]) |
| 37 | + devices[id].name = tmp[bacnet.enum.PropertyIds.PROP_OBJECT_NAME].value; |
| 38 | + devices[id].description = tmp[bacnet.enum.PropertyIds.PROP_DESCRIPTION].value; |
| 39 | + } |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +// Webserver Stuff |
| 44 | +app.use((req, res, next) => { |
| 45 | + res.header('Access-Control-Allow-Origin', '*'); |
| 46 | + res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); |
| 47 | + res.header('Access-Control-Allow-Headers', 'Content-Type'); |
| 48 | + next(); |
| 49 | +}); |
| 50 | + |
| 51 | +// Settings |
| 52 | +app.get('/api/settings', (req, res) => { |
| 53 | + res.send(settings); |
| 54 | +}); |
| 55 | + |
| 56 | +app.post('/api/settings', (req, res) => { |
| 57 | + res.send(req.body); |
| 58 | +}); |
| 59 | + |
| 60 | +app.get('/api/settings/interfaces', (req, res) => { |
| 61 | + const nics = []; |
| 62 | + const osNics = os.networkInterfaces(); |
| 63 | + Object.keys(osNics).forEach((ifname) => { |
| 64 | + osNics[ifname].forEach((iface) => { |
| 65 | + if (iface.interal === true) return; |
| 66 | + if (iface.family !== 'IPv4') return; |
| 67 | + nics.push({ |
| 68 | + name: ifname, |
| 69 | + address: iface.address, |
| 70 | + broadcast: ip.subnet(iface.address, iface.netmask).broadcastAddress |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + res.send(nics); |
| 75 | +}); |
| 76 | + |
| 77 | +// Devices |
| 78 | +app.get('/api/devices', (req, res) => { |
| 79 | + res.send(Object.keys(devices).map((key) => devices[key])); |
| 80 | +}); |
| 81 | + |
| 82 | +app.get('/api/devices/search', (req, res) => { |
| 83 | + client.whoIs(); |
| 84 | + res.sendStatus(204); |
| 85 | +}); |
| 86 | + |
| 87 | +// Objects |
| 88 | +app.get('/api/devices/:id/objects', (req, res) => { |
| 89 | + const device = devices[req.params.id]; |
| 90 | + if (!device) return res.sendStatus(404); |
| 91 | + client.readPropertyMultiple(device.address, [ |
| 92 | + {objectId: {type: 8, instance: 4194303}, properties: [{id: bacnet.enum.PropertyIds.PROP_ALL}]} |
| 93 | + ], (err, value) => { |
| 94 | + if (err) return res.sendStatus(500); |
| 95 | + const tmp = {}; |
| 96 | + if (!(value && value.values && value.values[0] && value.values[0].values)) return res.sendStatus(500); |
| 97 | + value.values[0].values.forEach(data => tmp[data.id] = data.value) |
| 98 | + async.mapSeries(tmp[bacnet.enum.PropertyIds.PROP_OBJECT_LIST], (item, next) => { |
| 99 | + console.log(item); |
| 100 | + client.readPropertyMultiple(device.address, [ |
| 101 | + {objectId: {type: item.value.type, instance: item.value.instance}, properties: [{id: bacnet.enum.PropertyIds.PROP_ALL}]} |
| 102 | + ], (err, value) => { |
| 103 | + if (err) return next(null, {}); |
| 104 | + const tmp = {}; |
| 105 | + if (!(value && value.values && value.values[0] && value.values[0].values)) return next(null, {}); |
| 106 | + value.values[0].values.forEach(data => tmp[data.id] = data.value) |
| 107 | + next(null, { |
| 108 | + id: `${item.value.type}:${item.value.instance}`, |
| 109 | + type: item.value.type, |
| 110 | + name: tmp[bacnet.enum.PropertyIds.PROP_OBJECT_NAME] ? tmp[bacnet.enum.PropertyIds.PROP_OBJECT_NAME][0].value : '', |
| 111 | + description: tmp[bacnet.enum.PropertyIds.PROP_DESCRIPTION] ? tmp[bacnet.enum.PropertyIds.PROP_DESCRIPTION][0].value : '', |
| 112 | + value: tmp[bacnet.enum.PropertyIds.PROP_PRESENT_VALUE] ? tmp[bacnet.enum.PropertyIds.PROP_PRESENT_VALUE][0].value : null |
| 113 | + }); |
| 114 | + }); |
| 115 | + }, (err, values) => { |
| 116 | + res.send(values); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +app.get('/api/devices/:id/objects/:oid', (req, res) => { |
| 122 | + const device = devices[req.params.id]; |
| 123 | + if (!device) return res.sendStatus(404); |
| 124 | + const oid = req.params.oid.split(':'); |
| 125 | + if (!oid[0] || !oid[1]) return res.sendStatus(404); |
| 126 | + client.readPropertyMultiple(device.address, [ |
| 127 | + {objectId: {type: oid[0], instance: oid[1]}, properties: [{id: bacnet.enum.PropertyIds.PROP_ALL}]} |
| 128 | + ], (err, value) => { |
| 129 | + if (err) return res.sendStatus(500); |
| 130 | + if (!(value && value.values && value.values[0] && value.values[0].values)) return res.sendStatus(500); |
| 131 | + let properties = value.values[0].values; |
| 132 | + properties = properties.map(property => {return { |
| 133 | + id: property.id, |
| 134 | + name: utils.getPropertyName(property.id) || `Vendor Specific Property ${property.id}`, |
| 135 | + value: property.value |
| 136 | + }}); |
| 137 | + res.send(properties); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +app.listen(3000, '127.0.0.1', () => { |
| 142 | + console.log('Example app listening on port 3000!'); |
| 143 | +}); |
0 commit comments