Hydrawise.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @author Martijn Dierckx - Complete rewrite to service both the cloud & local API binding
  3. * @author Paul Molluzzo (https://paulmolluzzo.com) - Initial 0.1.0 version containing the cloud binding
  4. */
  5. import { HydrawiseConnectionType } from './HydrawiseConnectionType';
  6. import { HydrawiseZone } from './HydrawiseZone';
  7. import { HydrawiseController } from './HydrawiseController';
  8. import { HydrawiseCommandException } from './HydrawiseCommandException';
  9. import Axios from 'axios';
  10. interface HydrawiseConfiguration {
  11. type : HydrawiseConnectionType,
  12. host ?: string,
  13. user ?: string,
  14. password ?: string,
  15. key ?: string
  16. }
  17. /** Class representing a Hydrawise local or cloud based API binding */
  18. export class Hydrawise {
  19. private readonly cloudUrl: string = 'https://app.hydrawise.com/api/v1/';
  20. public type: HydrawiseConnectionType;
  21. public url: string;
  22. public localAuthUsername: string;
  23. public localAuthPassword: string;
  24. public cloudAuthAPIkey: string;
  25. /**
  26. * Create a new instance of the Hydrawise API binding
  27. * @param {object} options - Options object containing all parameters
  28. * @param {string} options.type - The type of binding you wish to make: 'CLOUD' or 'LOCAL'
  29. * @param {string} [options.host] - The hostname or ip address of the local host you wish to connect to. Only needed for local bindings.
  30. * @param {string} [options.user = admin] - The username of the local Hydrawise controller. Only needed for local bindings (falls back to the default 'admin' user).
  31. * @param {string} [options.password] - The password of the local Hydrawise controller. Only needed for local bindings.
  32. * @param {string} [options.key] - The API key of your Hydrawise cloud account. Only needed for cloud bindings.
  33. */
  34. constructor(options: HydrawiseConfiguration) {
  35. this.type = options.type || HydrawiseConnectionType.CLOUD; // CLOUD or LOCAL
  36. this.url = (this.type == HydrawiseConnectionType.LOCAL ? 'http://'+options.host+'/' : this.cloudUrl);
  37. // Local Auth
  38. this.localAuthUsername = options.user || 'admin';
  39. this.localAuthPassword = options.password || '';
  40. // Cloud Auth
  41. this.cloudAuthAPIkey = options.key || '';
  42. }
  43. /**
  44. * Private function that makes a GET request to the local or cloud Hydrawise server
  45. * @param {string} path - The path of the API endpoint
  46. * @param {object} [params] - Parameters to be added to the URL path
  47. * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
  48. */
  49. private request(path: string = '', params: any = {}): Promise<any> {
  50. let promise = new Promise((resolve, reject) => {
  51. // setup basic request
  52. let options: any = {
  53. method : 'get',
  54. url : this.url + path,
  55. params : params,
  56. json : true
  57. };
  58. // Basic auth for local binding
  59. if(this.type == HydrawiseConnectionType.LOCAL) {
  60. let authBuffer = Buffer.from(this.localAuthUsername + ':' + this.localAuthPassword);
  61. options.headers = {
  62. 'Authorization': 'Basic '+ authBuffer.toString('base64')
  63. };
  64. }
  65. // API key auth for cloud binding
  66. else {
  67. options.params.api_key = this.cloudAuthAPIkey;
  68. }
  69. // Send request
  70. Axios(options).then((response: any) => {
  71. //Check for errors
  72. if(response.data.messageType == 'error') {
  73. reject(new HydrawiseCommandException(response.data.message));
  74. }
  75. resolve(response.data);
  76. }).catch((err: Error) => {
  77. reject(err);
  78. });
  79. });
  80. // return request
  81. return promise;
  82. }
  83. /**
  84. * Sends a command to a single zone/relay
  85. * @param {string} action - The required command to be executed for the given zone/relay: run, suspend, stop
  86. * @param {(HydrawiseZone|number|number)} zoneOrRelay - The zone/relay you are targetting. Can be a zone object returned by getZones, a relay number (zone.zone) for local bindings or a relayID (zone.relayID) for cloud bindings
  87. * @param {number} [duration] - How long should the command be executed (only applicable for run & suspend)
  88. * @todo Check whether controller_id needs to sent when the account contains multiple zones
  89. * @return {Promise} A Promise which will be resolved when the command has been executed.
  90. */
  91. public commandZone(action: string, zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
  92. let that:Hydrawise = this;
  93. // Get started
  94. let promise = new Promise((resolve, reject) => {
  95. let opts: any = {
  96. period_id : 998,
  97. action: action,
  98. };
  99. // Set Relay number for local binding
  100. if(that.type == HydrawiseConnectionType.LOCAL) {
  101. opts.relay = typeof zoneOrRelay == 'object' ? zoneOrRelay.zone : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
  102. }
  103. // Set Relay ID for cloud binding
  104. else {
  105. opts.relay_id = typeof zoneOrRelay == 'object' ? zoneOrRelay.relayID : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
  106. }
  107. // Custom duration?
  108. if(duration !== undefined) {
  109. opts.custom = duration;
  110. }
  111. // Execute command
  112. that.setZone(opts).then((data: any) => {
  113. resolve(data);
  114. }).catch((err) => {
  115. reject(err);
  116. });
  117. });
  118. return promise;
  119. }
  120. /**
  121. * Sends a command to all zones/relays
  122. * @param {string} action - The required command to be executed: runall, suspendall, stopall
  123. * @param {number} [duration] - How long should the given command be executed (only applicable for runall & suspendall)
  124. * @todo Check whether controller_id needs to sent when the account contains multiple zones
  125. * @return {Promise} A Promise which will be resolved when the command has been executed.
  126. */
  127. public commandAllZones(action: string, duration?: number): Promise<any> {
  128. let that = this;
  129. // Get started
  130. let promise = new Promise((resolve, reject) => {
  131. let opts: any = {
  132. period_id : 998,
  133. action: action
  134. }
  135. // Custom duration?
  136. if(duration !== undefined) {
  137. opts.custom = duration;
  138. }
  139. that.setZone(opts).then(data => {
  140. resolve(data);
  141. }).catch((err) => {
  142. reject(err);
  143. });
  144. });
  145. return promise;
  146. }
  147. /**
  148. * Sends the run command to a single zone/relay
  149. * @param {(HydrawiseZone|number|number)} zoneOrRelay - The zone/relay you are targetting. Can be a zone object returned by getZones, a relay number (zone.zone) for local bindings or a relayID (zone.relayID) for cloud bindings
  150. * @param {number} [duration] - How long should the command be executed
  151. * @return {Promise} A Promise which will be resolved when the command has been executed.
  152. */
  153. public runZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
  154. return this.commandZone('run', zoneOrRelay, duration);
  155. }
  156. /**
  157. * Sends the run command to all zones/relays
  158. * @param {number} [duration] - How long should the command be executed
  159. * @return {Promise} A Promise which will be resolved when the command has been executed.
  160. */
  161. public runAllZones(duration?: number): Promise<any> {
  162. return this.commandAllZones('runall', duration);
  163. }
  164. /**
  165. * Sends the suspend command to a single zone/relay
  166. * @param {(HydrawiseZone|number|number)} zoneOrRelay - The zone/relay you are targetting. Can be a zone object returned by getZones, a relay number (zone.zone) for local bindings or a relayID (zone.relayID) for cloud bindings
  167. * @param {number} [duration] - How long should the command be executed
  168. * @return {Promise} A Promise which will be resolved when the command has been executed.
  169. */
  170. public suspendZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
  171. return this.commandZone('suspend', zoneOrRelay, duration);
  172. }
  173. /**
  174. * Sends the suspend command to all zones/relays
  175. * @param {number} [duration] - How long should the command be executed
  176. * @return {Promise} A Promise which will be resolved when the command has been executed.
  177. */
  178. public suspendAllZones(duration?: number): Promise<any> {
  179. return this.commandAllZones('suspendall', duration);
  180. }
  181. /**
  182. * Sends the stop command to a single zone/relay
  183. * @param {(HydrawiseZone|number|number)} zoneOrRelay - The zone/relay you are targetting. Can be a zone object returned by getZones, a relay number (zone.zone) for local bindings or a relayID (zone.relayID) for cloud bindings
  184. * @return {Promise} A Promise which will be resolved when the command has been executed.
  185. */
  186. public stopZone(zoneOrRelay: number | HydrawiseZone): Promise<any> {
  187. return this.commandZone('stop', zoneOrRelay);
  188. }
  189. /**
  190. * Sends the stop command to all zones/relays
  191. * @return {Promise} A Promise which will be resolved when the command has been executed.
  192. */
  193. public stopAllZones(): Promise<any> {
  194. return this.commandAllZones('stopall');
  195. }
  196. /**
  197. * Retrieves all zones/relays known to the server
  198. * @param {boolean} [onlyConfigured = true] - Only return zones/relays which have been configured
  199. * @return {Promise} A Promise which will be resolved when all zones have been retrieved
  200. */
  201. public getZones(): Promise<HydrawiseZone[]> {
  202. let that = this;
  203. // Get started
  204. let promise: Promise<HydrawiseZone[]> = new Promise((resolve, reject) => {
  205. // Get relays
  206. that.getStatusAndSchedule().then((data: any) => {
  207. let zones:HydrawiseZone[] = [];
  208. // Check every returned relay
  209. data.relays.map((z: any) => {
  210. // Only configured zones
  211. if(that.type == HydrawiseConnectionType.CLOUD || z.lastwaterepoch != 0){
  212. // Zone
  213. let zone: any = {
  214. apiBinding: that,
  215. relayID: z.relay_id,
  216. zone: z.relay,
  217. name: z.name,
  218. nextRunAt: new Date((data.time + z.time) * 1000),
  219. nextRunDuration: z.run || z.run_seconds,
  220. isSuspended: z.suspended !== undefined && z.suspended == 1,
  221. isRunning: false,
  222. remainingRunningTime: 0,
  223. };
  224. // Only available data for local connections
  225. if(that.type == HydrawiseConnectionType.LOCAL) {
  226. zone.defaultRunDuration = z.normalRuntime * 60;
  227. }
  228. // Running?
  229. if(data.running !== undefined) {
  230. let runningZone = data.running.find((x: any) => {
  231. return x.relay_id == z.relay_id;
  232. });
  233. if(runningZone != undefined && runningZone != null) {
  234. zone.isRunning = true;
  235. zone.remainingRunningTime = runningZone.time_left;
  236. }
  237. }
  238. zones.push(new HydrawiseZone(zone));
  239. }
  240. });
  241. resolve(zones);
  242. }).catch((err) => {
  243. reject(err);
  244. });
  245. });
  246. return promise;
  247. }
  248. /**
  249. * Retrieves all controllers known to the Hydrawise cloud
  250. * @return {Promise} A Promise which will be resolved when all controllers have been retrieved
  251. */
  252. public getControllers(): Promise<HydrawiseController[]> {
  253. let that = this;
  254. // Get started
  255. let promise: Promise<HydrawiseController[]> = new Promise((resolve, reject) => {
  256. // Get Controllers
  257. this.getCustomerDetails('controllers').then(data => {
  258. let controllers: HydrawiseController[] = [];
  259. // Check every returned relay
  260. data.controllers.map((c: any) => {
  261. // Zone
  262. let controller: any = {
  263. id: c.controller_id,
  264. name: c.name,
  265. serialNumber: c.serial_number,
  266. lastContactWithCloud: new Date(c.last_contact * 1000),
  267. status: c.status
  268. };
  269. controllers.push(new HydrawiseController(controller));
  270. });
  271. resolve(controllers);
  272. }).catch((err) => {
  273. reject(err);
  274. });
  275. });
  276. return promise;
  277. }
  278. /* -------- Raw API calls -------- */
  279. /**
  280. * Gets the customer ID & list of available controllers configured in the Hydrawise cloud. Only available in cloud binding.
  281. * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
  282. * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
  283. */
  284. public getCustomerDetails(type: string): Promise<any> {
  285. // Cloud only API
  286. if (this.type == HydrawiseConnectionType.LOCAL) {
  287. return new Promise((resolve, reject) => {
  288. reject(new HydrawiseCommandException('Calling Cloud API function on a Local Binding'));
  289. });
  290. }
  291. return this.request('customerdetails.php', { type: type });
  292. }
  293. /**
  294. * Gets the status and schedule of the locally connected controller or all controllers in the cloud
  295. * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
  296. * @todo Check whether controller_id needs to sent when the account contains multiple zones
  297. * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
  298. */
  299. public getStatusAndSchedule(tag: string = '', hours: string = '168'): Promise<any> {
  300. let uri = (this.type == HydrawiseConnectionType.LOCAL ? 'get_sched_json.php' : 'statusschedule.php');
  301. return this.request(uri, { tag, hours });
  302. }
  303. /*setController(controllerID: number): Promise<any> {
  304. // Cloud only API
  305. if (this.type == HydrawiseConnectionType.LOCAL) {
  306. return new Promise((resolve, reject) => {
  307. reject(new HydrawiseCommandException('Calling Cloud API function on a Local Binding'));
  308. });
  309. }
  310. return this.request('setcontroller.php', { controllerID, json: true });
  311. }*/
  312. /**
  313. * Sends an action request to a specific or all zones
  314. * @param {object} params - Parameters object containing all parameters to be sent along with the request
  315. * @param {string} params.action - The action to be executed: run, stop, suspend, runall, suspendall, stopall
  316. * @todo Complete params documentation
  317. * @todo Check whether controller_id needs to sent when the account contains multiple zones
  318. * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
  319. */
  320. public setZone(this: Hydrawise, params: any = {}): Promise<any> {
  321. let uri: string = (this.type == HydrawiseConnectionType.LOCAL ? 'set_manual_data.php' : 'setzone.php');
  322. return this.request(uri, params);
  323. }
  324. /* -------- Original 0.1.0 function names for backwards compatibility -------- */
  325. /**
  326. * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
  327. * @param {string} [type = controllers] - Defines the type of customer details to be retrieved alongside the customer ID
  328. * @alias getCustomerDetails
  329. * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
  330. */
  331. public customerdetails(type: string = 'controllers'): Promise<any> {
  332. return this.getCustomerDetails(type);
  333. }
  334. /**
  335. * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
  336. * @alias getStatusAndSchedule
  337. * @deprecated since version 1.0.0. Please use getZones()
  338. * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
  339. */
  340. public statusschedule(tag: string = '', hours: string = '168'): Promise<any> {
  341. return this.getStatusAndSchedule(tag, hours);
  342. }
  343. /*setcontroller(controllerID) {
  344. return this.setController(controllerID);
  345. }*/
  346. /**
  347. * Does the same as setZone, and is only kept to be backwards compatible with version 0.1.0 of this module
  348. * @alias setZone
  349. * @deprecated since version 1.0.0. Please use runZone(), suspendZone(), stopZone(), runAllZones(), suspendAllZones(), stopAllZones() or the run(), suspend(), stop() commands on a HydrawiseZone object.
  350. * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
  351. */
  352. public setzone(params: any = {}): Promise<any> {
  353. return this.setZone(params);
  354. }
  355. }