diff --git a/mock-api/server.js b/mock-api/server.js index 3ea2ca635..90dfcc99e 100644 --- a/mock-api/server.js +++ b/mock-api/server.js @@ -1,30 +1,30 @@ -const express = require('express') -const compression = require('compression') -const SSE = require('express-sse') -const path = require('path') -const msgpack = require('@msgpack/msgpack') -const WebSocket = require('ws') +const express = require('express'); +const compression = require('compression'); +const SSE = require('express-sse'); +const path = require('path'); +const msgpack = require('@msgpack/msgpack'); +const WebSocket = require('ws'); // REST API -const rest_server = express() -const port = 3080 -const REST_ENDPOINT_ROOT = '/rest/' -rest_server.use(compression()) -rest_server.use(express.static(path.join(__dirname, '../interface/build'))) -rest_server.use(express.json()) +const rest_server = express(); +const port = 3080; +const REST_ENDPOINT_ROOT = '/rest/'; +rest_server.use(compression()); +rest_server.use(express.static(path.join(__dirname, '../interface/build'))); +rest_server.use(express.json()); // API endpoint -const API_ENDPOINT_ROOT = '/api/' +const API_ENDPOINT_ROOT = '/api/'; // LOG -const LOG_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'logSettings' +const LOG_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'logSettings'; log_settings = { level: 6, max_messages: 50, - compact: false, -} + compact: false +}; -const FETCH_LOG_ENDPOINT = REST_ENDPOINT_ROOT + 'fetchLog' +const FETCH_LOG_ENDPOINT = REST_ENDPOINT_ROOT + 'fetchLog'; const fetch_log = { events: [ { @@ -32,67 +32,67 @@ const fetch_log = { l: 3, i: 1, n: 'system', - m: 'this is message 3', + m: 'this is message 3' }, { t: '000+00:00:00.002', l: 4, i: 2, n: 'ntp', - m: 'this is message 4', + m: 'this is message 4' }, { t: '000+00:00:00.002', l: 5, i: 3, n: 'mqtt', - m: 'this is message 5', + m: 'this is message 5' }, { t: '000+00:00:00.002', l: 6, i: 444, n: 'command', - m: 'this is message 6', + m: 'this is message 6' }, { t: '000+00:00:00.002', l: 7, i: 5555, n: 'emsesp', - m: 'this is message 7', + m: 'this is message 7' }, { t: '000+00:00:00.002', l: 8, i: 666666, n: 'thermostat', - m: 'this is message 8', - }, - ], -} + m: 'this is message 8' + } + ] +}; // NTP -const NTP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'ntpStatus' -const NTP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'ntpSettings' -const TIME_ENDPOINT = REST_ENDPOINT_ROOT + 'time' +const NTP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'ntpStatus'; +const NTP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'ntpSettings'; +const TIME_ENDPOINT = REST_ENDPOINT_ROOT + 'time'; ntp_settings = { enabled: true, server: 'time.google.com', tz_label: 'Europe/Amsterdam', - tz_format: 'CET-1CEST,M3.5.0,M10.5.0/3', -} + tz_format: 'CET-1CEST,M3.5.0,M10.5.0/3' +}; const ntp_status = { status: 1, utc_time: '2021-04-01T14:25:42Z', local_time: '2021-04-01T16:25:42', server: 'time.google.com', - uptime: 856, -} + uptime: 856 +}; // AP -const AP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'apSettings' -const AP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'apStatus' +const AP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'apSettings'; +const AP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'apStatus'; ap_settings = { provision_mode: 1, ssid: 'ems-esp', @@ -102,20 +102,20 @@ ap_settings = { subnet_mask: '255.255.255.0', channel: 1, ssid_hidden: true, - max_clients: 4, -} + max_clients: 4 +}; const ap_status = { status: 1, ip_address: '192.168.4.1', mac_address: '3C:61:05:03:AB:2D', - station_num: 0, -} + station_num: 0 +}; // NETWORK -const NETWORK_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'networkSettings' -const NETWORK_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'networkStatus' -const SCAN_NETWORKS_ENDPOINT = REST_ENDPOINT_ROOT + 'scanNetworks' -const LIST_NETWORKS_ENDPOINT = REST_ENDPOINT_ROOT + 'listNetworks' +const NETWORK_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'networkSettings'; +const NETWORK_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'networkStatus'; +const SCAN_NETWORKS_ENDPOINT = REST_ENDPOINT_ROOT + 'scanNetworks'; +const LIST_NETWORKS_ENDPOINT = REST_ENDPOINT_ROOT + 'listNetworks'; network_settings = { ssid: 'myWifi', password: 'myPassword', @@ -132,8 +132,8 @@ network_settings = { gateway_ip: '', subnet_mask: '', dns_ip_1: '', - dns_ip_2: '', -} + dns_ip_2: '' +}; const network_status = { status: 3, local_ip: '10.10.10.101', @@ -145,8 +145,8 @@ const network_status = { subnet_mask: '255.255.255.0', gateway_ip: '10.10.10.1', dns_ip_1: '10.10.10.1', - dns_ip_2: '0.0.0.0', -} + dns_ip_2: '0.0.0.0' +}; const list_networks = { networks: [ { @@ -154,71 +154,71 @@ const list_networks = { ssid: '', bssid: 'FC:EC:DA:FD:B4:68', channel: 11, - encryption_type: 3, + encryption_type: 3 }, { rssi: -41, ssid: 'home', bssid: '02:EC:DA:FD:B4:68', channel: 11, - encryption_type: 3, + encryption_type: 3 }, { rssi: -42, ssid: '', bssid: '06:EC:DA:FD:B4:68', channel: 11, - encryption_type: 3, + encryption_type: 3 }, { rssi: -73, ssid: '', bssid: 'FC:EC:DA:17:D4:7E', channel: 1, - encryption_type: 3, + encryption_type: 3 }, { rssi: -73, ssid: 'office', bssid: '02:EC:DA:17:D4:7E', channel: 1, - encryption_type: 3, + encryption_type: 3 }, { rssi: -75, ssid: 'Erica', bssid: 'C8:D7:19:9A:88:BD', channel: 2, - encryption_type: 3, + encryption_type: 3 }, { rssi: -75, ssid: '', bssid: 'C6:C9:E3:FF:A5:DE', channel: 2, - encryption_type: 3, + encryption_type: 3 }, { rssi: -76, ssid: 'Bruin', bssid: 'C0:C9:E3:FF:A5:DE', channel: 2, - encryption_type: 3, - }, - ], -} + encryption_type: 3 + } + ] +}; // OTA -const OTA_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'otaSettings' +const OTA_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'otaSettings'; ota_settings = { enabled: true, port: 8266, - password: 'ems-esp-neo', -} + password: 'ems-esp-neo' +}; // MQTT -const MQTT_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'mqttSettings' -const MQTT_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'mqttStatus' +const MQTT_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'mqttSettings'; +const MQTT_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'mqttStatus'; mqtt_settings = { enabled: true, host: '192.168.1.4', @@ -243,8 +243,8 @@ mqtt_settings = { nested_format: 1, discovery_prefix: 'homeassistant', send_response: true, - publish_single: false, -} + publish_single: false +}; const mqtt_status = { enabled: true, connected: true, @@ -252,21 +252,21 @@ const mqtt_status = { disconnect_reason: 0, mqtt_fails: 0, mqtt_queued: 1, - connect_count: 2, -} + connect_count: 2 +}; // SYSTEM -const FEATURES_ENDPOINT = REST_ENDPOINT_ROOT + 'features' -const VERIFY_AUTHORIZATION_ENDPOINT = REST_ENDPOINT_ROOT + 'verifyAuthorization' -const SYSTEM_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'systemStatus' -const SECURITY_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'securitySettings' -const RESTART_ENDPOINT = REST_ENDPOINT_ROOT + 'restart' -const FACTORY_RESET_ENDPOINT = REST_ENDPOINT_ROOT + 'factoryReset' -const UPLOAD_FILE_ENDPOINT = REST_ENDPOINT_ROOT + 'uploadFile' -const SIGN_IN_ENDPOINT = REST_ENDPOINT_ROOT + 'signIn' -const GENERATE_TOKEN_ENDPOINT = REST_ENDPOINT_ROOT + 'generateToken' +const FEATURES_ENDPOINT = REST_ENDPOINT_ROOT + 'features'; +const VERIFY_AUTHORIZATION_ENDPOINT = REST_ENDPOINT_ROOT + 'verifyAuthorization'; +const SYSTEM_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'systemStatus'; +const SECURITY_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'securitySettings'; +const RESTART_ENDPOINT = REST_ENDPOINT_ROOT + 'restart'; +const FACTORY_RESET_ENDPOINT = REST_ENDPOINT_ROOT + 'factoryReset'; +const UPLOAD_FILE_ENDPOINT = REST_ENDPOINT_ROOT + 'uploadFile'; +const SIGN_IN_ENDPOINT = REST_ENDPOINT_ROOT + 'signIn'; +const GENERATE_TOKEN_ENDPOINT = REST_ENDPOINT_ROOT + 'generateToken'; const system_status = { - emsesp_version: '3.5.0-demo', + emsesp_version: '3.6.0-demo', esp_platform: 'ESP32', max_alloc_heap: 89, psram_size: 0, @@ -280,45 +280,45 @@ const system_status = { fs_free: 24, app_used: 1863, app_free: 121, - uptime: '000+00:15:42.707', -} + uptime: '000+00:15:42.707' +}; security_settings = { jwt_secret: 'naughty!', users: [ { username: 'admin', password: 'admin', admin: true }, - { username: 'guest', password: 'guest', admin: false }, - ], -} + { username: 'guest', password: 'guest', admin: false } + ] +}; const features = { project: true, security: true, mqtt: true, ntp: true, ota: true, - upload_firmware: true, -} -const verify_authentication = { access_token: '1234' } + upload_firmware: true +}; +const verify_authentication = { access_token: '1234' }; const signin = { access_token: - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiYWRtaW4iOnRydWUsInZlcnNpb24iOiIzLjAuMmIwIn0.MsHSgoJKI1lyYz77EiT5ZN3ECMrb4mPv9FNy3udq0TU', -} -const generate_token = { token: '1234' } + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiYWRtaW4iOnRydWUsInZlcnNpb24iOiIzLjAuMmIwIn0.MsHSgoJKI1lyYz77EiT5ZN3ECMrb4mPv9FNy3udq0TU' +}; +const generate_token = { token: '1234' }; // EMS-ESP Project specific -const EMSESP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'settings' -const EMSESP_CORE_DATA_ENDPOINT = REST_ENDPOINT_ROOT + 'coreData' -const EMSESP_SENSOR_DATA_ENDPOINT = REST_ENDPOINT_ROOT + 'sensorData' -const EMSESP_DEVICES_ENDPOINT = REST_ENDPOINT_ROOT + 'devices' -const EMSESP_SCANDEVICES_ENDPOINT = REST_ENDPOINT_ROOT + 'scanDevices' -const EMSESP_DEVICEDATA_ENDPOINT = REST_ENDPOINT_ROOT + 'deviceData' -const EMSESP_DEVICEENTITIES_ENDPOINT = REST_ENDPOINT_ROOT + 'deviceEntities' -const EMSESP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'status' -const EMSESP_BOARDPROFILE_ENDPOINT = REST_ENDPOINT_ROOT + 'boardProfile' -const EMSESP_WRITE_VALUE_ENDPOINT = REST_ENDPOINT_ROOT + 'writeValue' -const EMSESP_WRITE_SENSOR_ENDPOINT = REST_ENDPOINT_ROOT + 'writeSensor' -const EMSESP_WRITE_ANALOG_ENDPOINT = REST_ENDPOINT_ROOT + 'writeAnalog' -const EMSESP_CUSTOM_ENTITIES_ENDPOINT = REST_ENDPOINT_ROOT + 'customEntities' -const EMSESP_RESET_CUSTOMIZATIONS_ENDPOINT = REST_ENDPOINT_ROOT + 'resetCustomizations' +const EMSESP_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'settings'; +const EMSESP_CORE_DATA_ENDPOINT = REST_ENDPOINT_ROOT + 'coreData'; +const EMSESP_SENSOR_DATA_ENDPOINT = REST_ENDPOINT_ROOT + 'sensorData'; +const EMSESP_DEVICES_ENDPOINT = REST_ENDPOINT_ROOT + 'devices'; +const EMSESP_SCANDEVICES_ENDPOINT = REST_ENDPOINT_ROOT + 'scanDevices'; +const EMSESP_DEVICEDATA_ENDPOINT = REST_ENDPOINT_ROOT + 'deviceData'; +const EMSESP_DEVICEENTITIES_ENDPOINT = REST_ENDPOINT_ROOT + 'deviceEntities'; +const EMSESP_STATUS_ENDPOINT = REST_ENDPOINT_ROOT + 'status'; +const EMSESP_BOARDPROFILE_ENDPOINT = REST_ENDPOINT_ROOT + 'boardProfile'; +const EMSESP_WRITE_VALUE_ENDPOINT = REST_ENDPOINT_ROOT + 'writeValue'; +const EMSESP_WRITE_SENSOR_ENDPOINT = REST_ENDPOINT_ROOT + 'writeSensor'; +const EMSESP_WRITE_ANALOG_ENDPOINT = REST_ENDPOINT_ROOT + 'writeAnalog'; +const EMSESP_CUSTOM_ENTITIES_ENDPOINT = REST_ENDPOINT_ROOT + 'customEntities'; +const EMSESP_RESET_CUSTOMIZATIONS_ENDPOINT = REST_ENDPOINT_ROOT + 'resetCustomizations'; settings = { locale: 'en', @@ -354,8 +354,8 @@ settings = { bool_format: 1, bool_dashboard: 1, enum_format: 1, - fahrenheit: false, -} + fahrenheit: false +}; const emsesp_devices = { devices: [ @@ -363,22 +363,22 @@ const emsesp_devices = { i: 1, s: 'Thermostat (RC20/Moduline 300)', t: 4, - tn: 'thermostat', + tn: 'thermostat' }, { i: 2, s: 'Boiler (Nefit GBx72/Trendline/Cerapur/Greenstar Si/27i)', t: 3, - tn: 'boiler', + tn: 'boiler' }, { i: 4, s: 'Thermostat (RC100/Moduline 1000/1010)', t: 4, - tn: 'thermostat', - }, - ], -} + tn: 'thermostat' + } + ] +}; const emsesp_coredata = { connected: true, @@ -393,7 +393,7 @@ const emsesp_coredata = { d: 8, p: 123, v: '06.01', - e: 68, + e: 68 }, { id: '1', @@ -404,7 +404,7 @@ const emsesp_coredata = { d: 23, p: 77, v: '03.03', - e: 5, + e: 5 }, { id: '4', @@ -415,30 +415,30 @@ const emsesp_coredata = { d: 16, p: 165, v: '04.01', - e: 3, - }, + e: 3 + } ], s_n: 'Sensors', active_sensors: 8, - analog_enabled: true, -} + analog_enabled: true +}; const emsesp_sensordata = { sensors: [ { id: '28-233D-9497-0C03', n: 'Dallas 1', t: 25.7, o: 1.2, u: 1 }, { id: '28-243D-7437-1E3A', n: 'Dallas 2 outside', t: 26.1, o: 0, u: 1 }, { id: '28-243E-7437-1E3B', n: 'Zolder', t: 27.1, o: 0, u: 16 }, - { id: '28-183D-1892-0C33', n: 'Roof', o: 2, u: 1 }, + { id: '28-183D-1892-0C33', n: 'Roof', o: 2, u: 1 } ], // sensors: [], analogs: [ { id: '1', g: 36, n: 'motor', v: 0, u: 0, o: 17, f: 0, t: 0 }, { id: '2', g: 37, n: 'External switch', v: 13, u: 0, o: 17, f: 0, t: 1 }, { id: '3', g: 39, n: 'Pulse count', v: 144, u: 0, o: 0, f: 0, t: 2 }, - { id: '4', g: 40, n: 'Pressure', v: 16, u: 17, o: 0, f: 0, t: 3 }, - ], + { id: '4', g: 40, n: 'Pressure', v: 16, u: 17, o: 0, f: 0, t: 3 } + ] // analogs: [], -} +}; const status = { status: 0, @@ -454,9 +454,9 @@ const status = { { id: '3', s: 56506, f: 11, q: 100 }, { id: '4', s: 0, f: 0, q: 100 }, { id: '5', s: 12, f: 10, q: 20 }, - { id: '6', s: 0, f: 0, q: 0 }, - ], -} + { id: '6', s: 0, f: 0, q: 0 } + ] +}; // Dashboard data const emsesp_devicedata_1 = { @@ -465,32 +465,32 @@ const emsesp_devicedata_1 = { { v: '(0)', u: 0, - id: '08my custom error code', + id: '08my custom error code' }, { v: '14:54:39 06/06/2021', u: 0, - id: '00date/time', + id: '00date/time' }, { v: 18, u: 1, id: '00hc1 selected room temperature', - c: 'hc1/seltemp', + c: 'hc1/seltemp' }, { v: 22.6, u: 1, - id: '00hc1 current room temperature', + id: '00hc1 current room temperature' }, { v: 'auto', u: 0, id: '00hc1 mode', - c: 'hc1/mode', - }, - ], -} + c: 'hc1/mode' + } + ] +}; const emsesp_devicedata_2 = { label: 'Boiler: Nefit GBx72/Trendline/Cerapur/Greenstar Si/27i', @@ -553,7 +553,7 @@ const emsesp_devicedata_2 = { u: 0, id: '00dhw circulation pump mode', c: 'wwcircmode', - l: ['off', '1x3min', '2x3min', '3x3min', '4x3min', '5x3min', '6x3min', 'continuous'], + l: ['off', '1x3min', '2x3min', '3x3min', '4x3min', '5x3min', '6x3min', 'continuous'] }, { v: 'false', u: 0, id: '00dhw circulation active', c: 'wwcirc', l: ['off', 'on'] }, { v: 47.3, u: 1, id: '00dhw current intern temperature' }, @@ -569,9 +569,9 @@ const emsesp_devicedata_2 = { { v: 'true', u: 0, id: '00dhw 3way valve active' }, { v: 0, u: 3, id: '00dhw set pump power' }, { v: 288768, u: 0, id: '00dhw starts' }, - { v: 102151, u: 8, id: '00dhw active time' }, - ], -} + { v: 102151, u: 8, id: '00dhw active time' } + ] +}; const emsesp_devicedata_4 = { label: 'Thermostat: RC100/Moduline 1000/1010', @@ -580,22 +580,22 @@ const emsesp_devicedata_4 = { v: 16, u: 1, id: '08hc2 selected room temperature', - c: 'hc2/seltemp', + c: 'hc2/seltemp' }, { v: 18.6, u: 1, id: '02hc2 current room temperature', - c: '', + c: '' }, { v: 'off', u: 0, id: '02hc2 mode', - c: 'hc2/mode', - }, - ], -} + c: 'hc2/mode' + } + ] +}; // CUSTOMIZATIONS @@ -606,14 +606,14 @@ const emsesp_deviceentities_1 = [ cn: 'my custom error code', id: 'errorcode', m: 8, - w: false, + w: false }, { v: '14:54:39 06/06/2021', n: 'date/time', id: 'datetime', m: 0, - w: false, + w: false }, { v: 18.2, @@ -622,23 +622,23 @@ const emsesp_deviceentities_1 = [ m: 0, mi: 5, ma: 52, - w: true, + w: true }, { v: 22.6, n: 'hc1 current room temperature', id: 'hc1/curtemp', m: 0, - w: false, + w: false }, { v: 'auto', n: 'hc1 mode', id: 'hc1/mode', m: 0, - w: true, - }, -] + w: true + } +]; const emsesp_deviceentities_2 = [ { u: 0, n: '!reset', id: 'reset', m: 8, w: false }, @@ -720,8 +720,8 @@ const emsesp_deviceentities_2 = [ { n: 'dhw mixer temperature', id: 'wwmixertemp', m: 2 }, { n: 'dhw cylinder middle temperature (TS3)', id: 'wwcylmiddletemp', m: 2 }, { v: 288768, n: 'dhw starts', id: 'wwstarts', m: 0, w: false }, - { v: 102151, n: 'dhw active time', id: 'wwworkm', m: 0, w: false }, -] + { v: 102151, n: 'dhw active time', id: 'wwworkm', m: 0, w: false } +]; const emsesp_deviceentities_4 = [ { @@ -729,338 +729,338 @@ const emsesp_deviceentities_4 = [ n: 'hc2 selected room temperature', id: 'hc2/seltemp', m: 8, - w: true, + w: true }, { v: 18.5, n: 'hc2 current room temperature', id: 'hc2/curtemp', m: 2, - w: false, + w: false }, { v: 'off', n: 'hc2 mode', id: 'hc2/mode', m: 2, - w: true, - }, -] + w: true + } +]; // LOG rest_server.get(FETCH_LOG_ENDPOINT, (req, res) => { - const encoded = msgpack.encode(fetch_log) - res.write(encoded, 'binary') - res.end(null, 'binary') -}) + const encoded = msgpack.encode(fetch_log); + res.write(encoded, 'binary'); + res.end(null, 'binary'); +}); rest_server.get(LOG_SETTINGS_ENDPOINT, (req, res) => { - res.json(log_settings) -}) + res.json(log_settings); +}); rest_server.post(LOG_SETTINGS_ENDPOINT, (req, res) => { - log_settings = req.body - console.log(JSON.stringify(log_settings)) - res.json(log_settings) -}) + log_settings = req.body; + console.log(JSON.stringify(log_settings)); + res.json(log_settings); +}); // NETWORK rest_server.get(NETWORK_STATUS_ENDPOINT, (req, res) => { - res.json(network_status) -}) + res.json(network_status); +}); rest_server.get(NETWORK_SETTINGS_ENDPOINT, (req, res) => { - res.json(network_settings) -}) + res.json(network_settings); +}); rest_server.post(NETWORK_SETTINGS_ENDPOINT, (req, res) => { - network_settings = req.body - console.log(JSON.stringify(network_settings)) - res.json(network_settings) -}) + network_settings = req.body; + console.log(JSON.stringify(network_settings)); + res.json(network_settings); +}); rest_server.get(LIST_NETWORKS_ENDPOINT, (req, res) => { - res.json(list_networks) -}) + res.json(list_networks); +}); rest_server.get(SCAN_NETWORKS_ENDPOINT, (req, res) => { - res.sendStatus(202) -}) + res.sendStatus(202); +}); // AP rest_server.get(AP_SETTINGS_ENDPOINT, (req, res) => { - res.json(ap_settings) -}) + res.json(ap_settings); +}); rest_server.get(AP_STATUS_ENDPOINT, (req, res) => { - res.json(ap_status) -}) + res.json(ap_status); +}); rest_server.post(AP_SETTINGS_ENDPOINT, (req, res) => { - ap_status = req.body - console.log(JSON.stringify(ap_settings)) - res.json(ap_settings) -}) + ap_status = req.body; + console.log(JSON.stringify(ap_settings)); + res.json(ap_settings); +}); // OTA rest_server.get(OTA_SETTINGS_ENDPOINT, (req, res) => { - res.json(ota_settings) -}) + res.json(ota_settings); +}); rest_server.post(OTA_SETTINGS_ENDPOINT, (req, res) => { - ota_settings = req.body - console.log(JSON.stringify(ota_settings)) - res.json(ota_settings) -}) + ota_settings = req.body; + console.log(JSON.stringify(ota_settings)); + res.json(ota_settings); +}); // MQTT rest_server.get(MQTT_SETTINGS_ENDPOINT, (req, res) => { - res.json(mqtt_settings) -}) + res.json(mqtt_settings); +}); rest_server.post(MQTT_SETTINGS_ENDPOINT, (req, res) => { - mqtt_settings = req.body - console.log(JSON.stringify(mqtt_settings)) - res.json(mqtt_settings) -}) + mqtt_settings = req.body; + console.log(JSON.stringify(mqtt_settings)); + res.json(mqtt_settings); +}); rest_server.get(MQTT_STATUS_ENDPOINT, (req, res) => { - res.json(mqtt_status) -}) + res.json(mqtt_status); +}); // NTP rest_server.get(NTP_SETTINGS_ENDPOINT, (req, res) => { - res.json(ntp_settings) -}) + res.json(ntp_settings); +}); rest_server.post(NTP_SETTINGS_ENDPOINT, (req, res) => { - ntp_settings = req.body - console.log(JSON.stringify(ntp_settings)) - res.json(ntp_settings) -}) + ntp_settings = req.body; + console.log(JSON.stringify(ntp_settings)); + res.json(ntp_settings); +}); rest_server.get(NTP_STATUS_ENDPOINT, (req, res) => { - res.json(ntp_status) -}) + res.json(ntp_status); +}); rest_server.post(TIME_ENDPOINT, (req, res) => { - res.sendStatus(200) -}) + res.sendStatus(200); +}); // SYSTEM rest_server.get(SYSTEM_STATUS_ENDPOINT, (req, res) => { - res.json(system_status) -}) + res.json(system_status); +}); rest_server.get(SECURITY_SETTINGS_ENDPOINT, (req, res) => { - res.json(security_settings) -}) + res.json(security_settings); +}); rest_server.post(SECURITY_SETTINGS_ENDPOINT, (req, res) => { - security_settings = req.body - console.log(JSON.stringify(security_settings)) - res.json(security_settings) -}) + security_settings = req.body; + console.log(JSON.stringify(security_settings)); + res.json(security_settings); +}); rest_server.get(FEATURES_ENDPOINT, (req, res) => { - res.json(features) -}) + res.json(features); +}); rest_server.get(VERIFY_AUTHORIZATION_ENDPOINT, (req, res) => { - res.json(verify_authentication) -}) + res.json(verify_authentication); +}); rest_server.post(RESTART_ENDPOINT, (req, res) => { - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(FACTORY_RESET_ENDPOINT, (req, res) => { - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(UPLOAD_FILE_ENDPOINT, (req, res) => { - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(SIGN_IN_ENDPOINT, (req, res) => { - console.log('Signed in as ' + req.body.username) - res.json(signin) -}) + console.log('Signed in as ' + req.body.username); + res.json(signin); +}); rest_server.get(GENERATE_TOKEN_ENDPOINT, (req, res) => { - res.json(generate_token) -}) + res.json(generate_token); +}); // EMS-ESP Project stuff rest_server.post(EMSESP_RESET_CUSTOMIZATIONS_ENDPOINT, (req, res) => { - console.log('Removing all customizations...') - res.sendStatus(200) -}) + console.log('Removing all customizations...'); + res.sendStatus(200); +}); rest_server.get(EMSESP_SETTINGS_ENDPOINT, (req, res) => { - console.log('Get settings: ' + JSON.stringify(settings)) - res.json(settings) -}) + console.log('Get settings: ' + JSON.stringify(settings)); + res.json(settings); +}); rest_server.post(EMSESP_SETTINGS_ENDPOINT, (req, res) => { - settings = req.body - console.log('Write settings: ' + JSON.stringify(settings)) - res.status(202).json(settings) // restart needed + settings = req.body; + console.log('Write settings: ' + JSON.stringify(settings)); + res.status(202).json(settings); // restart needed // res.status(200).json(settings); // no restart needed -}) +}); rest_server.get(EMSESP_CORE_DATA_ENDPOINT, (req, res) => { - console.log('send back core data...') - res.json(emsesp_coredata) -}) + console.log('send back core data...'); + res.json(emsesp_coredata); +}); rest_server.get(EMSESP_SENSOR_DATA_ENDPOINT, (req, res) => { - console.log('send back sensor data...') - res.json(emsesp_sensordata) -}) + console.log('send back sensor data...'); + res.json(emsesp_sensordata); +}); rest_server.get(EMSESP_DEVICES_ENDPOINT, (req, res) => { - res.json(emsesp_devices) -}) + res.json(emsesp_devices); +}); rest_server.post(EMSESP_SCANDEVICES_ENDPOINT, (req, res) => { - console.log('Scan devices...') - res.sendStatus(200) -}) + console.log('Scan devices...'); + res.sendStatus(200); +}); rest_server.get(EMSESP_STATUS_ENDPOINT, (req, res) => { - res.json(status) -}) + res.json(status); +}); rest_server.post(EMSESP_DEVICEDATA_ENDPOINT, (req, res) => { - const id = req.body.id - console.log('send back device data for ' + id) + const id = req.body.id; + console.log('send back device data for ' + id); if (id === 1) { - const encoded = msgpack.encode(emsesp_devicedata_1) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_devicedata_1); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } if (id === 2) { - const encoded = msgpack.encode(emsesp_devicedata_2) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_devicedata_2); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } if (id === 4) { - const encoded = msgpack.encode(emsesp_devicedata_4) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_devicedata_4); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } -}) +}); rest_server.post(EMSESP_DEVICEENTITIES_ENDPOINT, (req, res) => { - const id = req.body.id + const id = req.body.id; if (id === 1) { - const encoded = msgpack.encode(emsesp_deviceentities_1) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_deviceentities_1); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } if (id === 2) { - const encoded = msgpack.encode(emsesp_deviceentities_2) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_deviceentities_2); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } if (id === 4) { - const encoded = msgpack.encode(emsesp_deviceentities_4) - res.write(encoded, 'binary') - res.end(null, 'binary') + const encoded = msgpack.encode(emsesp_deviceentities_4); + res.write(encoded, 'binary'); + res.end(null, 'binary'); } -}) +}); function updateMask(entity, de, dd) { - const current_mask = parseInt(entity.slice(0, 2), 16) + const current_mask = parseInt(entity.slice(0, 2), 16); // strip of any min/max ranges - const shortname_with_customname = entity.slice(2).split('>')[0] + const shortname_with_customname = entity.slice(2).split('>')[0]; - const shortname = shortname_with_customname.split('|')[0] - const new_custom_name = shortname_with_customname.split('|')[1] + const shortname = shortname_with_customname.split('|')[0]; + const new_custom_name = shortname_with_customname.split('|')[1]; // find in de - de_objIndex = de.findIndex((obj) => obj.id === shortname) + de_objIndex = de.findIndex((obj) => obj.id === shortname); if (de_objIndex !== -1) { if (de[de_objIndex].cn) { - fullname = de[de_objIndex].cn + fullname = de[de_objIndex].cn; } else { - fullname = de[de_objIndex].n + fullname = de[de_objIndex].n; } // find in dd, either looking for fullname or custom name - dd_objIndex = dd.data.findIndex((obj) => obj.id.slice(2) === fullname) + dd_objIndex = dd.data.findIndex((obj) => obj.id.slice(2) === fullname); if (dd_objIndex !== -1) { - let changed = new Boolean(false) + let changed = new Boolean(false); // see if the mask has changed - const old_mask = parseInt(dd.data[dd_objIndex].id.slice(0, 2), 16) + const old_mask = parseInt(dd.data[dd_objIndex].id.slice(0, 2), 16); if (old_mask !== current_mask) { - changed = true - console.log('mask has changed to ' + current_mask.toString(16)) + changed = true; + console.log('mask has changed to ' + current_mask.toString(16)); } // see if the custom name has changed - const old_custom_name = dd.data[dd_objIndex].cn - console.log('comparing old ' + old_custom_name + ' with new ' + new_custom_name) + const old_custom_name = dd.data[dd_objIndex].cn; + console.log('comparing old ' + old_custom_name + ' with new ' + new_custom_name); if (old_custom_name !== new_custom_name) { - changed = true - new_fullname = new_custom_name - console.log('name has changed to ' + new_custom_name) + changed = true; + new_fullname = new_custom_name; + console.log('name has changed to ' + new_custom_name); } else { - new_fullname = fullname + new_fullname = fullname; } if (changed) { console.log( - 'Updating ' + dd.data[dd_objIndex].id + ' -> ' + current_mask.toString(16).padStart(2, '0') + new_fullname, - ) - de[de_objIndex].m = current_mask - de[de_objIndex].cn = new_fullname - dd.data[dd_objIndex].id = current_mask.toString(16).padStart(2, '0') + new_fullname - dd.data[dd_objIndex].cn = new_fullname + 'Updating ' + dd.data[dd_objIndex].id + ' -> ' + current_mask.toString(16).padStart(2, '0') + new_fullname + ); + de[de_objIndex].m = current_mask; + de[de_objIndex].cn = new_fullname; + dd.data[dd_objIndex].id = current_mask.toString(16).padStart(2, '0') + new_fullname; + dd.data[dd_objIndex].cn = new_fullname; } - console.log('new dd:') - console.log(dd.data[dd_objIndex]) - console.log('new de:') - console.log(de[de_objIndex]) + console.log('new dd:'); + console.log(dd.data[dd_objIndex]); + console.log('new de:'); + console.log(de[de_objIndex]); } else { - console.log('error, dd not found') + console.log('error, dd not found'); } } else { - console.log("can't locate record for shortname " + shortname) + console.log("can't locate record for shortname " + shortname); } } rest_server.post(EMSESP_CUSTOM_ENTITIES_ENDPOINT, (req, res) => { - const id = req.body.id - console.log('customization id = ' + id) - console.log(req.body.entity_ids) + const id = req.body.id; + console.log('customization id = ' + id); + console.log(req.body.entity_ids); for (const entity of req.body.entity_ids) { if (id === 1) { - updateMask(entity, emsesp_deviceentities_1, emsesp_devicedata_1) + updateMask(entity, emsesp_deviceentities_1, emsesp_devicedata_1); } else if (id === 2) { - updateMask(entity, emsesp_deviceentities_2, emsesp_devicedata_2) + updateMask(entity, emsesp_deviceentities_2, emsesp_devicedata_2); } else if (id === 4) { - updateMask(entity, emsesp_deviceentities_4, emsesp_devicedata_4) + updateMask(entity, emsesp_deviceentities_4, emsesp_devicedata_4); } } - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(EMSESP_WRITE_VALUE_ENDPOINT, (req, res) => { - const devicevalue = req.body.devicevalue - const id = req.body.id + const devicevalue = req.body.devicevalue; + const id = req.body.id; if (id === 1) { - console.log('Write device value for Thermostat: ' + JSON.stringify(devicevalue)) - objIndex = emsesp_devicedata_1.data.findIndex((obj) => obj.c == devicevalue.c) - emsesp_devicedata_1.data[objIndex] = devicevalue + console.log('Write device value for Thermostat: ' + JSON.stringify(devicevalue)); + objIndex = emsesp_devicedata_1.data.findIndex((obj) => obj.c == devicevalue.c); + emsesp_devicedata_1.data[objIndex] = devicevalue; } if (id === 2) { - console.log('Write device value for Boiler: ' + JSON.stringify(devicevalue)) - objIndex = emsesp_devicedata_2.data.findIndex((obj) => obj.c == devicevalue.c) - emsesp_devicedata_2.data[objIndex] = devicevalue + console.log('Write device value for Boiler: ' + JSON.stringify(devicevalue)); + objIndex = emsesp_devicedata_2.data.findIndex((obj) => obj.c == devicevalue.c); + emsesp_devicedata_2.data[objIndex] = devicevalue; } if (id === 4) { - console.log('Write device value for Thermostat2: ' + JSON.stringify(devicevalue)) - objIndex = emsesp_devicedata_4.data.findIndex((obj) => obj.c == devicevalue.c) - emsesp_devicedata_4.data[objIndex] = devicevalue + console.log('Write device value for Thermostat2: ' + JSON.stringify(devicevalue)); + objIndex = emsesp_devicedata_4.data.findIndex((obj) => obj.c == devicevalue.c); + emsesp_devicedata_4.data[objIndex] = devicevalue; } - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(EMSESP_WRITE_SENSOR_ENDPOINT, (req, res) => { - const sensor = req.body - console.log('Write sensor: ' + JSON.stringify(sensor)) - objIndex = emsesp_sensordata.sensors.findIndex((obj) => obj.id == sensor.id) + const sensor = req.body; + console.log('Write sensor: ' + JSON.stringify(sensor)); + objIndex = emsesp_sensordata.sensors.findIndex((obj) => obj.id == sensor.id); if (objIndex !== -1) { - emsesp_sensordata.sensors[objIndex].n = sensor.name - emsesp_sensordata.sensors[objIndex].o = sensor.offset + emsesp_sensordata.sensors[objIndex].n = sensor.name; + emsesp_sensordata.sensors[objIndex].o = sensor.offset; } else { - console.log('not found') + console.log('not found'); } - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(EMSESP_WRITE_ANALOG_ENDPOINT, (req, res) => { - const analog = req.body - console.log('Write analog: ' + JSON.stringify(analog)) - objIndex = emsesp_sensordata.analogs.findIndex((obj) => obj.g == analog.gpio) + const analog = req.body; + console.log('Write analog: ' + JSON.stringify(analog)); + objIndex = emsesp_sensordata.analogs.findIndex((obj) => obj.g == analog.gpio); if (objIndex === -1) { - console.log('new analog') + console.log('new analog'); emsesp_sensordata.analogs.push({ id: analog.i.toString(), g: analog.gpio, @@ -1068,27 +1068,27 @@ rest_server.post(EMSESP_WRITE_ANALOG_ENDPOINT, (req, res) => { f: analog.factor, o: analog.offset, u: analog.uom, - t: analog.type, - }) + t: analog.type + }); } else { if (analog.type === -1) { - console.log('removing analog gpio' + analog.gpio + ' index ' + objIndex) - emsesp_sensordata.analogs[objIndex].t = -1 + console.log('removing analog gpio' + analog.gpio + ' index ' + objIndex); + emsesp_sensordata.analogs[objIndex].t = -1; } else { - console.log('updating analog gpio' + analog.gpio + ' index ' + objIndex) - emsesp_sensordata.analogs[objIndex].n = analog.name - emsesp_sensordata.analogs[objIndex].f = analog.factor - emsesp_sensordata.analogs[objIndex].o = analog.offset - emsesp_sensordata.analogs[objIndex].u = analog.uom - emsesp_sensordata.analogs[objIndex].t = analog.type + console.log('updating analog gpio' + analog.gpio + ' index ' + objIndex); + emsesp_sensordata.analogs[objIndex].n = analog.name; + emsesp_sensordata.analogs[objIndex].f = analog.factor; + emsesp_sensordata.analogs[objIndex].o = analog.offset; + emsesp_sensordata.analogs[objIndex].u = analog.uom; + emsesp_sensordata.analogs[objIndex].t = analog.type; } } - res.sendStatus(200) -}) + res.sendStatus(200); +}); rest_server.post(EMSESP_BOARDPROFILE_ENDPOINT, (req, res) => { - const board_profile = req.body.board_profile + const board_profile = req.body.board_profile; const data = { led_gpio: settings.led_gpio, @@ -1099,125 +1099,125 @@ rest_server.post(EMSESP_BOARDPROFILE_ENDPOINT, (req, res) => { phy_type: settings.phy_type, eth_power: settings.eth_power, eth_phy_addr: settings.eth_phy_addr, - eth_clock_mode: settings.eth_clock_mode, - } + eth_clock_mode: settings.eth_clock_mode + }; if (board_profile == 'S32') { // BBQKees Gateway S32 - data.led_gpio = 2 - data.dallas_gpio = 18 - data.rx_gpio = 23 - data.tx_gpio = 5 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 2; + data.dallas_gpio = 18; + data.rx_gpio = 23; + data.tx_gpio = 5; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'E32') { // BBQKees Gateway E32 - data.led_gpio = 2 - data.dallas_gpio = 4 - data.rx_gpio = 5 - data.tx_gpio = 17 - data.pbutton_gpio = 33 - data.phy_type = 1 - data.eth_power = 16 - data.eth_phy_addr = 1 - data.eth_clock_mode = 0 + data.led_gpio = 2; + data.dallas_gpio = 4; + data.rx_gpio = 5; + data.tx_gpio = 17; + data.pbutton_gpio = 33; + data.phy_type = 1; + data.eth_power = 16; + data.eth_phy_addr = 1; + data.eth_clock_mode = 0; } else if (board_profile == 'MH-ET') { // MH-ET Live D1 Mini - data.led_gpio = 2 - data.dallas_gpio = 18 - data.rx_gpio = 23 - data.tx_gpio = 5 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 2; + data.dallas_gpio = 18; + data.rx_gpio = 23; + data.tx_gpio = 5; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'NODEMCU') { // NodeMCU 32S - data.led_gpio = 2 - data.dallas_gpio = 18 - data.rx_gpio = 23 - data.tx_gpio = 5 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 2; + data.dallas_gpio = 18; + data.rx_gpio = 23; + data.tx_gpio = 5; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'LOLIN') { // Lolin D32 - data.led_gpio = 2 - data.dallas_gpio = 18 - data.rx_gpio = 17 - data.tx_gpio = 16 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 2; + data.dallas_gpio = 18; + data.rx_gpio = 17; + data.tx_gpio = 16; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'OLIMEX') { // Olimex ESP32-EVB (uses U1TXD/U1RXD/BUTTON, no LED or Dallas) - data.led_gpio = 0 - data.dallas_gpio = 0 - data.rx_gpio = 36 - data.tx_gpio = 4 - data.pbutton_gpio = 34 - data.phy_type = 1 - data.eth_power = -1 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 0; + data.dallas_gpio = 0; + data.rx_gpio = 36; + data.tx_gpio = 4; + data.pbutton_gpio = 34; + data.phy_type = 1; + data.eth_power = -1; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'OLIMEXPOE') { // Olimex ESP32-POE - data.led_gpio = 0 - data.dallas_gpio = 0 - data.rx_gpio = 36 - data.tx_gpio = 4 - data.pbutton_gpio = 34 - data.phy_type = 1 - data.eth_power = 12 - data.eth_phy_addr = 0 - data.eth_clock_mode = 3 + data.led_gpio = 0; + data.dallas_gpio = 0; + data.rx_gpio = 36; + data.tx_gpio = 4; + data.pbutton_gpio = 34; + data.phy_type = 1; + data.eth_power = 12; + data.eth_phy_addr = 0; + data.eth_clock_mode = 3; } else if (board_profile == 'C3MINI') { // Lolin C3 mini - data.led_gpio = 7 - data.dallas_gpio = 1 - data.rx_gpio = 4 - data.tx_gpio = 5 - data.pbutton_gpio = 9 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 7; + data.dallas_gpio = 1; + data.rx_gpio = 4; + data.tx_gpio = 5; + data.pbutton_gpio = 9; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'S2MINI') { // Lolin C3 mini - data.led_gpio = 15 - data.dallas_gpio = 7 - data.rx_gpio = 11 - data.tx_gpio = 12 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 15; + data.dallas_gpio = 7; + data.rx_gpio = 11; + data.tx_gpio = 12; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } else if (board_profile == 'S3MINI') { // Liligo S3 mini - data.led_gpio = 17 - data.dallas_gpio = 18 - data.rx_gpio = 8 - data.tx_gpio = 5 - data.pbutton_gpio = 0 - data.phy_type = 0 - data.eth_power = 0 - data.eth_phy_addr = 0 - data.eth_clock_mode = 0 + data.led_gpio = 17; + data.dallas_gpio = 18; + data.rx_gpio = 8; + data.tx_gpio = 5; + data.pbutton_gpio = 0; + data.phy_type = 0; + data.eth_power = 0; + data.eth_phy_addr = 0; + data.eth_clock_mode = 0; } - console.log('boardProfile POST. Sending back, profile: ' + board_profile + ', ' + 'data: ' + JSON.stringify(data)) + console.log('boardProfile POST. Sending back, profile: ' + board_profile + ', ' + 'data: ' + JSON.stringify(data)); - res.send(data) -}) + res.send(data); +}); // EMS-ESP API specific const emsesp_info = { @@ -1227,7 +1227,7 @@ const emsesp_info = { 'uptime (seconds)': 110434, freemem: 131, 'reset reason': 'Software reset CPU / Software reset CPU', - 'Dallas sensors': 3, + 'Dallas sensors': 3 }, Network: { connection: 'Ethernet', @@ -1235,7 +1235,7 @@ const emsesp_info = { MAC: 'A8:03:2A:62:64:CF', 'IPv4 address': '192.168.1.134/255.255.255.0', 'IPv4 gateway': '192.168.1.1', - 'IPv4 nameserver': '192.168.1.1', + 'IPv4 nameserver': '192.168.1.1' }, Status: { 'bus status': 'connected', @@ -1251,128 +1251,128 @@ const emsesp_info = { 'MQTT publishes': 46336, 'MQTT publish fails': 0, 'Dallas reads': 22086, - 'Dallas fails': 0, + 'Dallas fails': 0 }, Devices: [ { type: 'Boiler', name: 'Nefit GBx72/Trendline/Cerapur/Greenstar Si/27i (DeviceID:0x08 ProductID:123, Version:06.01)', handlers: - '0x10 0x11 0xC2 0x14 0x15 0x1C 0x18 0x19 0x1A 0x35 0x16 0x33 0x34 0x26 0x2A 0xD1 0xE3 0xE4 0xE5 0xE6 0xE9 0xEA', + '0x10 0x11 0xC2 0x14 0x15 0x1C 0x18 0x19 0x1A 0x35 0x16 0x33 0x34 0x26 0x2A 0xD1 0xE3 0xE4 0xE5 0xE6 0xE9 0xEA' }, { type: 'Thermostat', name: 'RC20/Moduline 300 (DeviceID:0x17, ProductID:77, Version:03.03)', - handlers: '0xA3 0x06 0xA2 0x12 0x91 0xA8', - }, - ], -} + handlers: '0xA3 0x06 0xA2 0x12 0x91 0xA8' + } + ] +}; rest_server.post(API_ENDPOINT_ROOT, (req, res) => { - console.log('Generic API POST') - console.log(req.body) + console.log('Generic API POST'); + console.log(req.body); if (req.body.device === 'system') { if (req.body.entity === 'info') { - console.log('sending system info: ' + JSON.stringify(emsesp_info)) + console.log('sending system info: ' + JSON.stringify(emsesp_info)); } else if (req.body.entity === 'settings') { - console.log('sending system settings: ' + JSON.stringify(settings)) - res.json(settings) + console.log('sending system settings: ' + JSON.stringify(settings)); + res.json(settings); } else { - res.sendStatus(200) + res.sendStatus(200); } } else { - res.sendStatus(200) + res.sendStatus(200); } -}) +}); rest_server.get(API_ENDPOINT_ROOT, (req, res) => { - console.log('Generic API GET') - res.sendStatus(200) -}) + console.log('Generic API GET'); + res.sendStatus(200); +}); -const SYSTEM_INFO_ENDPOINT = API_ENDPOINT_ROOT + 'system/info' +const SYSTEM_INFO_ENDPOINT = API_ENDPOINT_ROOT + 'system/info'; rest_server.post(SYSTEM_INFO_ENDPOINT, (req, res) => { - console.log('System Info POST: ' + JSON.stringify(req.body)) - res.sendStatus(200) -}) + console.log('System Info POST: ' + JSON.stringify(req.body)); + res.sendStatus(200); +}); rest_server.get(SYSTEM_INFO_ENDPOINT, (req, res) => { - console.log('System Info GET') - res.json(emsesp_info) -}) + console.log('System Info GET'); + res.json(emsesp_info); +}); -const GET_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'getSettings' +const GET_SETTINGS_ENDPOINT = REST_ENDPOINT_ROOT + 'getSettings'; rest_server.get(GET_SETTINGS_ENDPOINT, (req, res) => { - console.log('System Settings:') - res.json(settings) -}) + console.log('System Settings:'); + res.json(settings); +}); -const GET_CUSTOMIZATIONS_ENDPOINT = REST_ENDPOINT_ROOT + 'getCustomizations' +const GET_CUSTOMIZATIONS_ENDPOINT = REST_ENDPOINT_ROOT + 'getCustomizations'; rest_server.get(GET_CUSTOMIZATIONS_ENDPOINT, (req, res) => { - console.log('Customizations:') + console.log('Customizations:'); // not implemented yet - res.sendStatus(200) -}) + res.sendStatus(200); +}); // start server const expressServer = rest_server.listen(port, () => - console.log(`EMS-ESP REST API server running on http://localhost:${port}/api`), -) + console.log(`EMS-ESP REST API server running on http://localhost:${port}/api`) +); // start websocket server const websocketServer = new WebSocket.Server({ noServer: true, - path: '/ws', -}) -console.log('WebSocket server is listening to /ws') + path: '/ws' +}); +console.log('WebSocket server is listening to /ws'); expressServer.on('upgrade', (request, socket, head) => { websocketServer.handleUpgrade(request, socket, head, (websocket) => { - websocketServer.emit('connection', websocket, request) - }) -}) + websocketServer.emit('connection', websocket, request); + }); +}); websocketServer.on('connection', function connection(websocketConnection, connectionRequest) { - const [_path, params] = connectionRequest?.url?.split('?') - console.log(params) + const [_path, params] = connectionRequest?.url?.split('?'); + console.log(params); websocketConnection.on('message', (message) => { - const parsedMessage = JSON.parse(message) - console.log(parsedMessage) - }) -}) + const parsedMessage = JSON.parse(message); + console.log(parsedMessage); + }); +}); -var count = 8 -var log_index = 0 -const ES_ENDPOINT_ROOT = '/es/' -const ES_LOG_ENDPOINT = ES_ENDPOINT_ROOT + 'log' +var count = 8; +var log_index = 0; +const ES_ENDPOINT_ROOT = '/es/'; +const ES_LOG_ENDPOINT = ES_ENDPOINT_ROOT + 'log'; rest_server.get(ES_LOG_ENDPOINT, function (req, res) { - res.setHeader('Content-Type', 'text/event-stream') - res.setHeader('Cache-Control', 'no-cache') - res.setHeader('Access-Control-Allow-Origin', '*') - res.setHeader('Connection', 'keep-alive') + res.setHeader('Content-Type', 'text/event-stream'); + res.setHeader('Cache-Control', 'no-cache'); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Connection', 'keep-alive'); // res.setHeader('Content-Encoding', 'deflate') // res.setHeader('X-Accel-Buffering', 'no') - res.flushHeaders() + res.flushHeaders(); var timer = setInterval(function () { - count += 1 - log_index += 1 + count += 1; + log_index += 1; const data = { t: '000+00:00:00.000', l: 3, // error i: count, n: 'system', - m: 'incoming message #' + count + '/' + log_index, - } - const sseFormattedResponse = `data: ${JSON.stringify(data)}\n\n` + m: 'incoming message #' + count + '/' + log_index + }; + const sseFormattedResponse = `data: ${JSON.stringify(data)}\n\n`; // console.log('sending log #' + count) - res.write(sseFormattedResponse) - res.flush() // this is important + res.write(sseFormattedResponse); + res.flush(); // this is important // if buffer full start over if (log_index > 50) { - fetch_log.events = [] - log_index = 0 + fetch_log.events = []; + log_index = 0; } - fetch_log.events.push(data) // append to buffer - }, 1000) -}) + fetch_log.events.push(data); // append to buffer + }, 1000); +});