Skip to content

Commit

Permalink
Updated interface connections with REM and dashPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrouse committed Sep 5, 2021
1 parent 9f82735 commit 9c18573
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion controller/nixie/chemistry/ChemController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ export class NixieChemProbePh extends NixieChemProbe {
deviceBinding: this.probe.deviceBinding,
eventName: "chemController",
property: "pHLevel",
sendValue: 'pH',
sendValue: 'all',
isActive: data.remFeedEnabled,
sampling: 1,
changesOnly: false,
Expand Down
4 changes: 4 additions & 0 deletions defaultConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"interfaces": {
"smartThings": {
"name": "SmartThings",
"type": "rest",
"enabled": false,
"fileName": "smartThings-Hubitat.json",
"globals": {},
Expand All @@ -62,6 +63,7 @@
},
"hubitat": {
"name": "Hubitat",
"type": "rest",
"enabled": false,
"fileName": "smartThings-Hubitat.json",
"globals": {},
Expand All @@ -72,6 +74,7 @@
},
"vera": {
"name": "Vera",
"type": "rest",
"enabled": false,
"fileName": "vera.json",
"vars": {
Expand All @@ -83,6 +86,7 @@
}
},
"valveRelay": {
"type": "rest",
"name": "Valve Relays",
"enabled": false,
"fileName": "valveRelays.json",
Expand Down
20 changes: 14 additions & 6 deletions web/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as path from "path";
import * as fs from "fs";
import * as dns from "dns";
import express = require('express');
import { utils } from "../controller/Constants";
import { config } from "../config/Config";
Expand Down Expand Up @@ -101,6 +102,7 @@ export class WebServer {
let type = c.type || 'http';
logger.info(`Init ${type} interface: ${c.name}`);
switch (type) {
case 'rest':
case 'http':
int = new HttpInterfaceServer(c.name, type);
int.init(c);
Expand Down Expand Up @@ -182,7 +184,7 @@ export class WebServer {
if (this._servers[i].uuid === uuid) this._servers.splice(i, 1);
}
}
public async updateServerInterface(obj: any) {
public async updateServerInterface(obj: any): Promise<any> {
let int = config.setInterface(obj);
let srv = this.findServerByGuid(obj.uuid);
// if server is not enabled; stop & remove it from local storage
Expand All @@ -197,6 +199,7 @@ export class WebServer {
}
else srv.init(obj);
}
return config.getInterfaceByUuid(obj.uuid);
}
}
class ProtoServer {
Expand Down Expand Up @@ -935,16 +938,21 @@ export class REMInterfaceServer extends ProtoServer {
// First, send the connection info for njsPC and see if a connection exists.
let url = '/config/checkconnection/';
// can & should extend for https/username-password/ssl
let data: any = { type: "njspc", isActive: true, id: null, name: "njsPC - automatic", protocol: "http:", ipAddress: webApp.ip(), port: config.getSection('web').servers.http.port || 4200, userName: "", password: "", sslKeyFile: "", sslCertFile: "" }
let data: any = { type: "njspc", isActive: true, id: null, name: "njsPC - automatic", protocol: "http:", ipAddress: webApp.ip(), port: config.getSection('web').servers.http.port || 4200, userName: "", password: "", sslKeyFile: "", sslCertFile: "", hostnames: [] }
logger.info(`Checking REM Connection ${data.name} ${data.ipAddress}:${data.port}`);
try {
data.hostnames = await dns.promises.reverse(data.ipAddress);
} catch (err) { logger.error(`Error getting hostnames for njsPC REM connection`); }
let result = await this.putApiService(url, data, 5000);
// If the result code is > 200 we have an issue. (-1 is for timeout)
if (result.status.code > 200 || result.status.code < 0) return reject(new Error(`initConnection: ${result.error.message}`));
else { this.remoteConnectionId = result.obj.id };
else {
this.remoteConnectionId = result.obj.id;
};

// The passed connection has been setup/verified; now test for emit
// if this fails, it could be because the remote connection is disabled. We will not
// automatically re-enable it

url = '/config/checkemit'
data = { eventName: "checkemit", property: "result", value: 'success', connectionId: result.obj.id }
// wait for REM server to finish resetting
Expand All @@ -957,14 +965,14 @@ export class REMInterfaceServer extends ProtoServer {
// console.log(data);
clearTimeout(_tmr);
logger.info(`REM bi-directional communications established.`)
return resolve();
resolve();
});
result = await self.putApiService(url, data);
// If the result code is > 200 or -1 we have an issue.
if (result.status.code > 200 || result.status.code === -1) return reject(new Error(`initConnection: ${result.error.message}`));
else {
clearTimeout(_tmr);
return resolve();
resolve();
}
}
catch (err) { reject(new Error(`initConnection setTimeout: ${result.error.message}`)); }
Expand Down
13 changes: 8 additions & 5 deletions web/services/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,16 @@ export class ConfigRoute {
let opts = {
interfaces: config.getSection('web.interfaces'),
types: [
{name: 'rem', desc: 'Relay Equipment Manager'},
{name: 'mqtt', desc: 'MQTT'}
{ name: 'rest', desc: 'Rest' },
{ name: 'http', desc: 'Http' },
{ name: 'rem', desc: 'Relay Equipment Manager' },
{ name: 'mqtt', desc: 'MQTT' },
{ name: 'influx', desc: 'InfluxDB' }
],
protocols: [
{ val: 0, name: 'http://', desc: 'http://' },
{ val: 1, name: 'https://', desc: 'https://' },
{ val: 2, name: 'mqtt://', desc: 'mqtt://' },
{ val: 2, name: 'mqtt://', desc: 'mqtt://' }
]
}
return res.status(200).send(opts);
Expand Down Expand Up @@ -831,8 +834,8 @@ export class ConfigRoute {
});
app.put('/app/interface', async (req, res, next) => {
try{
await webApp.updateServerInterface(req.body);
return res.status(200).send('OK');
let iface = await webApp.updateServerInterface(req.body);
return res.status(200).send(iface);
}
catch (err) {next(err);}
});
Expand Down

0 comments on commit 9c18573

Please sign in to comment.