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' ) ;
1+ const os = require ( 'os' ) ;
2+ const ip = require ( 'ip' ) ;
3+ const async = require ( 'async' ) ;
4+ const express = require ( 'express' ) ;
5+ const bodyParser = require ( 'body-parser' )
6+ const bacnet = require ( 'bacstack' ) ;
7+ const debug = require ( 'debug' ) ( 'bacstack-browser' ) ;
78
89const utils = require ( './utils' ) ;
910
1011const app = express ( ) ;
1112
1213// Local stores
14+ const options = utils . getSettings ( ) ;
1315const settings = {
14- port : 47808 ,
15- nic : 12 ,
16- timeout : 4000 ,
17- language : 'en' ,
18- analytics : true
16+ port : options . port || 47808 ,
17+ nic : options . nic || 0 ,
18+ timeout : options . timeout || 4000 ,
19+ language : options . language || 'en' ,
20+ noAnalytics : options . noAnalytics
1921} ;
2022
2123const devices = { } ;
24+ const nics = [ ] ;
25+
26+ // NIC Stuff
27+ const getNics = ( ) => {
28+ const osNics = os . networkInterfaces ( ) ;
29+ nics . push ( { name : 'Default' } ) ;
30+ Object . keys ( osNics ) . forEach ( ( ifname ) => {
31+ osNics [ ifname ] . forEach ( ( iface ) => {
32+ if ( iface . interal === true ) return ;
33+ if ( iface . family !== 'IPv4' ) return ;
34+ nics . push ( {
35+ name : ifname ,
36+ address : iface . address ,
37+ broadcast : ip . subnet ( iface . address , iface . netmask ) . broadcastAddress
38+ } ) ;
39+ } ) ;
40+ } ) ;
41+ } ;
42+ getNics ( ) ;
2243
2344// BACNET Stuff
24- const client = new bacnet ( { adpuTimeout : 6000 } ) ;
45+ let client ;
2546
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- }
47+ const startBacnet = ( ) => {
48+ client = new bacnet ( {
49+ port : settings . port ,
50+ interface : ( nics [ settings . nic ] || { } ) . address ,
51+ broadcastAddress : ( nics [ settings . nic ] || { } ) . broadcast ,
52+ adpuTimeout : settings . timeout
4053 } ) ;
41- } ) ;
54+ client . on ( 'iAm' , ( device ) => {
55+ const id = `${ device . address } :${ device . deviceId } ` ;
56+ devices [ id ] = device ;
57+ devices [ id ] . id = id ;
58+ client . readPropertyMultiple ( device . address , [
59+ { objectId : { type : 8 , instance : 4194303 } , properties : [ { id : bacnet . enum . PropertyIds . PROP_OBJECT_NAME } , { id : bacnet . enum . PropertyIds . PROP_DESCRIPTION } ] }
60+ ] , ( err , value ) => {
61+ if ( err ) return ;
62+ if ( value && value . values && value . values [ 0 ] && value . values [ 0 ] . values ) {
63+ const tmp = { } ;
64+ value . values [ 0 ] . values . forEach ( data => tmp [ data . id ] = data . value [ 0 ] )
65+ devices [ id ] . name = tmp [ bacnet . enum . PropertyIds . PROP_OBJECT_NAME ] . value ;
66+ devices [ id ] . description = tmp [ bacnet . enum . PropertyIds . PROP_DESCRIPTION ] . value ;
67+ }
68+ } ) ;
69+ } ) ;
70+ }
71+
72+ const stopBacnet = ( ) => {
73+ if ( client ) client . close ( ) ;
74+ client = undefined ;
75+ }
4276
4377// Webserver Stuff
78+ app . use ( bodyParser . json ( ) ) ;
79+
4480app . use ( ( req , res , next ) => {
4581 res . header ( 'Access-Control-Allow-Origin' , '*' ) ;
4682 res . header ( 'Access-Control-Allow-Methods' , 'GET,PUT,POST,DELETE' ) ;
@@ -54,23 +90,20 @@ app.get('/api/settings', (req, res) => {
5490} ) ;
5591
5692app . post ( '/api/settings' , ( req , res ) => {
57- res . send ( req . body ) ;
93+ settings . port = req . body . port || settings . port ;
94+ settings . nic = req . body . nic || settings . nic ;
95+ settings . timeout = req . body . timeout || settings . timeout ;
96+ settings . language = req . body . language || settings . language ;
97+ settings . noAnalytics = req . body . noAnalytics || settings . noAnalytics ;
98+ utils . setSettings ( settings , ( err ) => {
99+ if ( err ) return res . sendStatus ( 400 ) ;
100+ stopBacnet ( ) ;
101+ startBacnet ( ) ;
102+ res . send ( settings ) ;
103+ } ) ;
58104} ) ;
59105
60106app . 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- } ) ;
74107 res . send ( nics ) ;
75108} ) ;
76109
@@ -141,3 +174,5 @@ app.get('/api/devices/:id/objects/:oid', (req, res) => {
141174app . listen ( 3000 , '127.0.0.1' , ( ) => {
142175 console . log ( 'Example app listening on port 3000!' ) ;
143176} ) ;
177+
178+ startBacnet ( ) ;
0 commit comments