1
0

index.js 14 KB

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