|
|
@@ -22,14 +22,14 @@ interface HydrawiseConfiguration {
|
|
|
|
|
|
/** Class representing a Hydrawise local or cloud based API binding */
|
|
|
export class Hydrawise {
|
|
|
-
|
|
|
+
|
|
|
private readonly cloudUrl: string = 'https://app.hydrawise.com/api/v1/';
|
|
|
public type: HydrawiseConnectionType;
|
|
|
public url: string;
|
|
|
public localAuthUsername: string;
|
|
|
public localAuthPassword: string;
|
|
|
public cloudAuthAPIkey: string;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Create a new instance of the Hydrawise API binding
|
|
|
* @param {object} options - Options object containing all parameters
|
|
|
@@ -42,7 +42,7 @@ export class Hydrawise {
|
|
|
constructor(options: HydrawiseConfiguration) {
|
|
|
this.type = options.type || HydrawiseConnectionType.CLOUD; // CLOUD or LOCAL
|
|
|
this.url = (this.type == HydrawiseConnectionType.LOCAL ? 'http://'+options.host+'/' : this.cloudUrl);
|
|
|
-
|
|
|
+
|
|
|
// Local Auth
|
|
|
this.localAuthUsername = options.user || 'admin';
|
|
|
this.localAuthPassword = options.password || '';
|
|
|
@@ -67,7 +67,7 @@ export class Hydrawise {
|
|
|
params : params,
|
|
|
json : true
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// Basic auth for local binding
|
|
|
if(this.type == HydrawiseConnectionType.LOCAL) {
|
|
|
let authBuffer = Buffer.from(this.localAuthUsername + ':' + this.localAuthPassword);
|
|
|
@@ -82,7 +82,7 @@ export class Hydrawise {
|
|
|
|
|
|
// Send request
|
|
|
Axios(options).then((response: any) => {
|
|
|
-
|
|
|
+
|
|
|
//Check for errors
|
|
|
if(response.data.messageType == 'error') {
|
|
|
reject(new HydrawiseCommandException(response.data.message));
|
|
|
@@ -120,7 +120,7 @@ export class Hydrawise {
|
|
|
// Set Relay number for local binding
|
|
|
if(that.type == HydrawiseConnectionType.LOCAL) {
|
|
|
opts.relay = zoneOrRelay instanceof HydrawiseZone ? zoneOrRelay.zone : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
|
|
|
- }
|
|
|
+ }
|
|
|
// Set Relay ID for cloud binding
|
|
|
else {
|
|
|
opts.relay_id = zoneOrRelay instanceof HydrawiseZone ? zoneOrRelay.relayID : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
|
|
|
@@ -143,7 +143,7 @@ export class Hydrawise {
|
|
|
reject(err);
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return promise;
|
|
|
}
|
|
|
|
|
|
@@ -185,7 +185,7 @@ export class Hydrawise {
|
|
|
reject(err);
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return promise;
|
|
|
}
|
|
|
|
|
|
@@ -252,10 +252,10 @@ export class Hydrawise {
|
|
|
*/
|
|
|
public getZones(controller?: HydrawiseController | number): Promise<HydrawiseZone[]> {
|
|
|
let that = this;
|
|
|
-
|
|
|
+
|
|
|
// Get started
|
|
|
let promise: Promise<HydrawiseZone[]> = new Promise((resolve, reject) => {
|
|
|
-
|
|
|
+
|
|
|
// Controller set?
|
|
|
let controllerID;
|
|
|
if(controller !== undefined && controller !== null) {
|
|
|
@@ -270,14 +270,14 @@ export class Hydrawise {
|
|
|
// Get relays
|
|
|
that.getStatusAndSchedule(controllerID).then((data: any) => {
|
|
|
let zones:HydrawiseZone[] = [];
|
|
|
-
|
|
|
+
|
|
|
// Check every returned relay
|
|
|
data.relays.map((z: any) => {
|
|
|
-
|
|
|
+
|
|
|
// Only configured zones
|
|
|
// Commented out because it may drive some zones unusable from time to time by filtering them out
|
|
|
// if(that.type == HydrawiseConnectionType.CLOUD || z.lastwaterepoch != 0){
|
|
|
-
|
|
|
+
|
|
|
// Zone
|
|
|
let zone: any = {
|
|
|
apiBinding: that,
|
|
|
@@ -317,7 +317,7 @@ export class Hydrawise {
|
|
|
zone.isRunning = true;
|
|
|
zone.remainingRunTime = z.run;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
zones.push(new HydrawiseZone(zone));
|
|
|
// }
|
|
|
});
|
|
|
@@ -327,7 +327,7 @@ export class Hydrawise {
|
|
|
reject(err);
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return promise;
|
|
|
}
|
|
|
|
|
|
@@ -337,20 +337,20 @@ export class Hydrawise {
|
|
|
*/
|
|
|
public getControllers(): Promise<HydrawiseController[]> {
|
|
|
let that = this;
|
|
|
-
|
|
|
+
|
|
|
// Get started
|
|
|
let promise: Promise<HydrawiseController[]> = new Promise((resolve, reject) => {
|
|
|
-
|
|
|
+
|
|
|
// Cloud
|
|
|
if(that.type == HydrawiseConnectionType.CLOUD) {
|
|
|
-
|
|
|
+
|
|
|
// Get Controllers
|
|
|
this.getCustomerDetails('controllers').then(data => {
|
|
|
let controllers: HydrawiseController[] = [];
|
|
|
-
|
|
|
+
|
|
|
// Check every returned relay
|
|
|
data.controllers.map((c: any) => {
|
|
|
-
|
|
|
+
|
|
|
// Controller
|
|
|
let controller: any = {
|
|
|
apiBinding: that,
|
|
|
@@ -360,7 +360,7 @@ export class Hydrawise {
|
|
|
lastContactWithCloud: new Date(c.last_contact * 1000),
|
|
|
status: c.status
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
controllers.push(new HydrawiseController(controller));
|
|
|
});
|
|
|
|
|
|
@@ -376,7 +376,7 @@ export class Hydrawise {
|
|
|
apiBinding: that,
|
|
|
name: that.url
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
resolve([new HydrawiseController(controller)]);
|
|
|
}
|
|
|
|
|
|
@@ -385,8 +385,6 @@ export class Hydrawise {
|
|
|
return promise;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/* -------- Raw API calls -------- */
|
|
|
|
|
|
/**
|
|
|
@@ -401,7 +399,7 @@ export class Hydrawise {
|
|
|
reject(new HydrawiseCommandException('Calling Cloud API function on a Local Binding'));
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return this.request('customerdetails.php', { type: type });
|
|
|
}
|
|
|
|
|
|
@@ -413,7 +411,7 @@ export class Hydrawise {
|
|
|
public getStatusAndSchedule(controller?: number): Promise<any> {
|
|
|
let uri: string = (this.type == HydrawiseConnectionType.LOCAL ? 'get_sched_json.php' : 'statusschedule.php');
|
|
|
let params: any = {};
|
|
|
-
|
|
|
+
|
|
|
// Was a controller set?
|
|
|
if(controller !== undefined && controller !== null) {
|
|
|
params.controller_id = controller;
|
|
|
@@ -435,7 +433,7 @@ export class Hydrawise {
|
|
|
*/
|
|
|
public setZone(params: any = {}, controller?: number): Promise<any> {
|
|
|
let uri: string = (this.type == HydrawiseConnectionType.LOCAL ? 'set_manual_data.php' : 'setzone.php');
|
|
|
-
|
|
|
+
|
|
|
// Was a controller set?
|
|
|
if(controller !== undefined && controller !== null) {
|
|
|
params.controller_id = controller;
|