Browse Source

1.1.0: Multi controller

Also removed the legacy methods
martijndierckx 5 years ago
parent
commit
24b0ac3dc5

+ 27 - 2
README.md

@@ -5,6 +5,7 @@
 This is a client for the [Hydrawise API](https://support.hydrawise.com/hc/en-us/articles/360008965753-Hydrawise-API-Information). [Hydrawise](https://hydrawise.com) is an internet-controlled home irrigation system.
 
 On a very basic level, it allows you to do:
+* [Get controllers](#get-controllers)
 * [Get zones](#get-zones)
 * [Run a command on a zone](#run-a-command-on-a-zone) (run/stop/suspend)
 * [Command all zones at once](#command-all-zones-at-once)
@@ -35,16 +36,41 @@ You can also provide a *user* parameter, but this should be 'admin' in most case
 
 ## Basic usage
 
+### Get Controllers
+
+If you have multiple controllers you can use this function to retrieve them all. Once you know your controllers, you're able to get and/or command the connected zones to that controller.
+
+```js
+myHydrawise.getControllers()
+  .then(controllers => console.log(controllers))
+  .catch(error => console.log(error));
+```
+
 ### Get Zones
 
 Get all zones and see their status.
 
+If you only have a single controller, you can get the list of zones without first retrieving your controller details:
+
 ```js
 myHydrawise.getZones()
   .then(zones => console.log(zones))
   .catch(error => console.log(error));
 ```
 
+If you have multiple controllers you should first get your list of controllers and request the zones list for that specific controller (if you don't you only get the zones of your 'default' controller.):
+
+```js
+myHydrawise.getControllers()
+  .then((controllers) => {
+    // Get the zones for the first returned controller
+    controllers[0].getZones()
+    .then(zones => console.log(zones))
+    .catch(error => console.log(error));
+  })
+  .catch(error => console.log(error));
+```
+
 This will return an array of HydrawiseZone objects containing the following info:
 
 ```js
@@ -58,8 +84,6 @@ This will return an array of HydrawiseZone objects containing the following info
 {number} remainingRunningTime - Remaining run time in seconds when isRunning = true
 ```
 
-By default only the active zones are returned, you can change this behaviour by calling getZones(false) instead.
-
 ### Run a command on a zone
 
 You can execute a couple of basic commands on each zone: `run()`, `suspend()` or `stop()`
@@ -67,6 +91,7 @@ You can execute a couple of basic commands on each zone: `run()`, `suspend()` or
 ```js
 myHydrawise.getZones()
   .then(zones => {
+    // Run the first returned zone  
     zone[0].run().then((info) => {
       console.log('success');
     }).catch(error => console.log(error));

+ 22 - 41
dist/Hydrawise.d.ts

@@ -42,7 +42,7 @@ export declare class Hydrawise {
      * @param {string} action - The required command to be executed for the given zone/relay: run, suspend, stop
      * @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
      * @param {number} [duration] - How long should the command be executed (only applicable for run & suspend)
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
+     * @todo Allow using a controller id instead of HydrawiseController object.
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
     commandZone(action: string, zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any>;
@@ -53,52 +53,53 @@ export declare class Hydrawise {
      * @todo Check whether controller_id needs to sent when the account contains multiple zones
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    commandAllZones(action: string, duration?: number): Promise<any>;
+    commandAllZones(action: string, controller?: HydrawiseController | number, duration?: number): Promise<any>;
     /**
      * Sends the run command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    runZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any>;
+    runZone(zoneOrRelay: HydrawiseZone | number, duration?: number): Promise<any>;
     /**
      * Sends the run command to all zones/relays
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    runAllZones(duration?: number): Promise<any>;
+    runAllZones(controller?: HydrawiseController, duration?: number): Promise<any>;
     /**
      * Sends the suspend command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    suspendZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any>;
+    suspendZone(zoneOrRelay: HydrawiseZone | number, duration?: number): Promise<any>;
     /**
-     * Sends the suspend command to all zones/relays
+     * Sends the suspend command to all zones/relays for a specific controller
      * @param {number} [duration] - How long should the command be executed
+     * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    suspendAllZones(duration?: number): Promise<any>;
+    suspendAllZones(controller?: HydrawiseController, duration?: number): Promise<any>;
     /**
      * Sends the stop command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    stopZone(zoneOrRelay: number | HydrawiseZone): Promise<any>;
+    stopZone(zoneOrRelay: HydrawiseZone | number): Promise<any>;
     /**
      * Sends the stop command to all zones/relays
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    stopAllZones(): Promise<any>;
+    stopAllZones(controller?: HydrawiseController): Promise<any>;
     /**
      * Retrieves all zones/relays known to the server
-     * @param {boolean} [onlyConfigured = true] - Only return zones/relays which have been configured
+     * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when all zones have been retrieved
      */
-    getZones(): Promise<HydrawiseZone[]>;
+    getZones(controller?: HydrawiseController | number): Promise<HydrawiseZone[]>;
     /**
-     * Retrieves all controllers known to the Hydrawise cloud
+     * Retrieves all controllers known to the Hydrawise cloud or returns a single dummy one for a local connection
      * @return {Promise} A Promise which will be resolved when all controllers have been retrieved
      */
     getControllers(): Promise<HydrawiseController[]>;
@@ -110,41 +111,21 @@ export declare class Hydrawise {
     getCustomerDetails(type: string): Promise<any>;
     /**
      * Gets the status and schedule of the locally connected controller or all controllers in the cloud
-     * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
+     * @param {number} [controller] - Return the status and schedule for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
      */
-    getStatusAndSchedule(tag?: string, hours?: string): Promise<any>;
+    getStatusAndSchedule(controller?: number): Promise<any>;
     /**
      * Sends an action request to a specific or all zones
      * @param {object} params - Parameters object containing all parameters to be sent along with the request
+     * @param {string} [params.relay_id] - The id of the relay which needs to be targetted. Not needed for runall, suspendall, stopall
      * @param {string} params.action - The action to be executed: run, stop, suspend, runall, suspendall, stopall
+     * @param {number} [params.custom] - The amount of seconds the action needs to be run. Only for run, suspend, runall, suspendall
+     * @param {number} [controller] - Needs to be specified if you have multiple controllers (cloud only)
      * @todo Complete params documentation
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
-     * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-     */
-    setZone(this: Hydrawise, params?: any): Promise<any>;
-    /**
-     * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @param {string} [type = controllers] - Defines the type of customer details to be retrieved alongside the customer ID
-     * @alias getCustomerDetails
-     * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
-     */
-    customerdetails(type?: string): Promise<any>;
-    /**
-     * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @alias getStatusAndSchedule
-     * @deprecated since version 1.0.0. Please use getZones()
-     * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-     */
-    statusschedule(tag?: string, hours?: string): Promise<any>;
-    /**
-     * Does the same as setZone, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @alias setZone
-     * @deprecated since version 1.0.0. Please use runZone(), suspendZone(), stopZone(), runAllZones(), suspendAllZones(), stopAllZones() or the run(), suspend(), stop() commands on a HydrawiseZone object.
      * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
      */
-    setzone(params?: any): Promise<any>;
+    setZone(params?: any, controller?: number): Promise<any>;
 }
 export {};
 //# sourceMappingURL=Hydrawise.d.ts.map

File diff suppressed because it is too large
+ 0 - 0
dist/Hydrawise.d.ts.map


+ 95 - 84
dist/Hydrawise.js

@@ -78,7 +78,7 @@ class Hydrawise {
      * @param {string} action - The required command to be executed for the given zone/relay: run, suspend, stop
      * @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
      * @param {number} [duration] - How long should the command be executed (only applicable for run & suspend)
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
+     * @todo Allow using a controller id instead of HydrawiseController object.
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
     commandZone(action, zoneOrRelay, duration) {
@@ -91,16 +91,20 @@ class Hydrawise {
             };
             // Set Relay number for local binding
             if (that.type == HydrawiseConnectionType_1.HydrawiseConnectionType.LOCAL) {
-                opts.relay = typeof zoneOrRelay == 'object' ? zoneOrRelay.zone : zoneOrRelay; // A zone object, as returned by getZones, or just the relayID can be sent
+                opts.relay = zoneOrRelay instanceof HydrawiseZone_1.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 = typeof zoneOrRelay == 'object' ? zoneOrRelay.relayID : zoneOrRelay; // A zone object, as returned by getZones, or just the relayID can be sent
+                opts.relay_id = zoneOrRelay instanceof HydrawiseZone_1.HydrawiseZone ? zoneOrRelay.relayID : zoneOrRelay; // A zone object, as returned by getZones, or just the relayID can be sent
             }
             // Custom duration?
             if (duration !== undefined) {
                 opts.custom = duration;
             }
+            // Set controller if one was provided (only for cloud)
+            if (that.type == HydrawiseConnectionType_1.HydrawiseConnectionType.CLOUD && zoneOrRelay instanceof HydrawiseZone_1.HydrawiseZone && zoneOrRelay.controller !== undefined && zoneOrRelay.controller instanceof HydrawiseController_1.HydrawiseController) {
+                opts.controller_id = zoneOrRelay.controller.id;
+            }
             // Execute command
             that.setZone(opts).then((data) => {
                 resolve(data);
@@ -117,7 +121,7 @@ class Hydrawise {
      * @todo Check whether controller_id needs to sent when the account contains multiple zones
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    commandAllZones(action, duration) {
+    commandAllZones(action, controller, duration) {
         let that = this;
         // Get started
         let promise = new Promise((resolve, reject) => {
@@ -129,6 +133,15 @@ class Hydrawise {
             if (duration !== undefined) {
                 opts.custom = duration;
             }
+            // Specific controller? (only cloud)
+            if (that.type == HydrawiseConnectionType_1.HydrawiseConnectionType.CLOUD && controller !== undefined && controller !== null) {
+                if (controller instanceof HydrawiseController_1.HydrawiseController) {
+                    opts.controller_id = controller.id;
+                }
+                else {
+                    opts.controller_id = controller;
+                }
+            }
             that.setZone(opts).then(data => {
                 resolve(data);
             }).catch((err) => {
@@ -139,7 +152,7 @@ class Hydrawise {
     }
     /**
      * Sends the run command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
@@ -151,12 +164,12 @@ class Hydrawise {
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    runAllZones(duration) {
-        return this.commandAllZones('runall', duration);
+    runAllZones(controller, duration) {
+        return this.commandAllZones('runall', controller, duration);
     }
     /**
      * Sends the suspend command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @param {number} [duration] - How long should the command be executed
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
@@ -164,16 +177,17 @@ class Hydrawise {
         return this.commandZone('suspend', zoneOrRelay, duration);
     }
     /**
-     * Sends the suspend command to all zones/relays
+     * Sends the suspend command to all zones/relays for a specific controller
      * @param {number} [duration] - How long should the command be executed
+     * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    suspendAllZones(duration) {
-        return this.commandAllZones('suspendall', duration);
+    suspendAllZones(controller, duration) {
+        return this.commandAllZones('suspendall', controller, duration);
     }
     /**
      * Sends the stop command to a single zone/relay
-     * @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
+     * @param {(HydrawiseZone|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
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
     stopZone(zoneOrRelay) {
@@ -183,20 +197,30 @@ class Hydrawise {
      * Sends the stop command to all zones/relays
      * @return {Promise} A Promise which will be resolved when the command has been executed.
      */
-    stopAllZones() {
-        return this.commandAllZones('stopall');
+    stopAllZones(controller) {
+        return this.commandAllZones('stopall', controller);
     }
     /**
      * Retrieves all zones/relays known to the server
-     * @param {boolean} [onlyConfigured = true] - Only return zones/relays which have been configured
+     * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when all zones have been retrieved
      */
-    getZones() {
+    getZones(controller) {
         let that = this;
         // Get started
         let promise = new Promise((resolve, reject) => {
+            // Controller set?
+            let controllerID;
+            if (controller !== undefined && controller !== null) {
+                if (controller instanceof HydrawiseController_1.HydrawiseController) {
+                    controllerID = controller.id;
+                }
+                else {
+                    controllerID = controller;
+                }
+            }
             // Get relays
-            that.getStatusAndSchedule().then((data) => {
+            that.getStatusAndSchedule(controllerID).then((data) => {
                 let zones = [];
                 // Check every returned relay
                 data.relays.map((z) => {
@@ -212,8 +236,12 @@ class Hydrawise {
                             nextRunDuration: z.run || z.run_seconds,
                             isSuspended: z.suspended !== undefined && z.suspended == 1,
                             isRunning: false,
-                            remainingRunningTime: 0,
+                            remainingRunningTime: 0
                         };
+                        // Link controller to the zones if it was provided when calling the method
+                        if (controller !== undefined && controller !== null && controller instanceof HydrawiseController_1.HydrawiseController) {
+                            zone.controller = controller;
+                        }
                         // Only available data for local connections
                         if (that.type == HydrawiseConnectionType_1.HydrawiseConnectionType.LOCAL) {
                             zone.defaultRunDuration = z.normalRuntime * 60;
@@ -239,32 +267,45 @@ class Hydrawise {
         return promise;
     }
     /**
-     * Retrieves all controllers known to the Hydrawise cloud
+     * Retrieves all controllers known to the Hydrawise cloud or returns a single dummy one for a local connection
      * @return {Promise} A Promise which will be resolved when all controllers have been retrieved
      */
     getControllers() {
         let that = this;
         // Get started
         let promise = new Promise((resolve, reject) => {
-            // Get Controllers
-            this.getCustomerDetails('controllers').then(data => {
-                let controllers = [];
-                // Check every returned relay
-                data.controllers.map((c) => {
-                    // Zone
-                    let controller = {
-                        id: c.controller_id,
-                        name: c.name,
-                        serialNumber: c.serial_number,
-                        lastContactWithCloud: new Date(c.last_contact * 1000),
-                        status: c.status
-                    };
-                    controllers.push(new HydrawiseController_1.HydrawiseController(controller));
+            // Cloud
+            if (that.type == HydrawiseConnectionType_1.HydrawiseConnectionType.CLOUD) {
+                // Get Controllers
+                this.getCustomerDetails('controllers').then(data => {
+                    let controllers = [];
+                    // Check every returned relay
+                    data.controllers.map((c) => {
+                        // Controller
+                        let controller = {
+                            apiBinding: that,
+                            id: c.controller_id,
+                            name: c.name,
+                            serialNumber: c.serial_number,
+                            lastContactWithCloud: new Date(c.last_contact * 1000),
+                            status: c.status
+                        };
+                        controllers.push(new HydrawiseController_1.HydrawiseController(controller));
+                    });
+                    resolve(controllers);
+                }).catch((err) => {
+                    reject(err);
                 });
-                resolve(controllers);
-            }).catch((err) => {
-                reject(err);
-            });
+            }
+            // Local
+            else {
+                // Controller
+                let controller = {
+                    apiBinding: that,
+                    name: that.url
+                };
+                resolve([new HydrawiseController_1.HydrawiseController(controller)]);
+            }
         });
         return promise;
     }
@@ -285,67 +326,37 @@ class Hydrawise {
     }
     /**
      * Gets the status and schedule of the locally connected controller or all controllers in the cloud
-     * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
+     * @param {number} [controller] - Return the status and schedule for a specific controller. If not specified, the zones of the deault controller are returned.
      * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
      */
-    getStatusAndSchedule(tag = '', hours = '168') {
+    getStatusAndSchedule(controller) {
         let uri = (this.type == HydrawiseConnectionType_1.HydrawiseConnectionType.LOCAL ? 'get_sched_json.php' : 'statusschedule.php');
-        return this.request(uri, { tag, hours });
-    }
-    /*setController(controllerID: number): Promise<any> {
-        // Cloud only API
-        if (this.type  == HydrawiseConnectionType.LOCAL) {
-            return new Promise((resolve, reject) => {
-                reject(new HydrawiseCommandException('Calling Cloud API function on a Local Binding'));
-            });
+        let params = {};
+        // Was a controller set?
+        if (controller !== undefined && controller !== null) {
+            params.controller_id = controller;
         }
-        
-        return this.request('setcontroller.php', { controllerID, json: true });
-    }*/
+        // If no controller was set
+        return this.request(uri, params);
+    }
     /**
      * Sends an action request to a specific or all zones
      * @param {object} params - Parameters object containing all parameters to be sent along with the request
+     * @param {string} [params.relay_id] - The id of the relay which needs to be targetted. Not needed for runall, suspendall, stopall
      * @param {string} params.action - The action to be executed: run, stop, suspend, runall, suspendall, stopall
+     * @param {number} [params.custom] - The amount of seconds the action needs to be run. Only for run, suspend, runall, suspendall
+     * @param {number} [controller] - Needs to be specified if you have multiple controllers (cloud only)
      * @todo Complete params documentation
-     * @todo Check whether controller_id needs to sent when the account contains multiple zones
      * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
      */
-    setZone(params = {}) {
+    setZone(params = {}, controller) {
         let uri = (this.type == HydrawiseConnectionType_1.HydrawiseConnectionType.LOCAL ? 'set_manual_data.php' : 'setzone.php');
+        // Was a controller set?
+        if (controller !== undefined && controller !== null) {
+            params.controller_id = controller;
+        }
         return this.request(uri, params);
     }
-    /* -------- Original 0.1.0 function names for backwards compatibility -------- */
-    /**
-     * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @param {string} [type = controllers] - Defines the type of customer details to be retrieved alongside the customer ID
-     * @alias getCustomerDetails
-     * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
-     */
-    customerdetails(type = 'controllers') {
-        return this.getCustomerDetails(type);
-    }
-    /**
-     * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @alias getStatusAndSchedule
-     * @deprecated since version 1.0.0. Please use getZones()
-     * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-     */
-    statusschedule(tag = '', hours = '168') {
-        return this.getStatusAndSchedule(tag, hours);
-    }
-    /*setcontroller(controllerID) {
-        return this.setController(controllerID);
-    }*/
-    /**
-     * Does the same as setZone, and is only kept to be backwards compatible with version 0.1.0 of this module
-     * @alias setZone
-     * @deprecated since version 1.0.0. Please use runZone(), suspendZone(), stopZone(), runAllZones(), suspendAllZones(), stopAllZones() or the run(), suspend(), stop() commands on a HydrawiseZone object.
-     * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-     */
-    setzone(params = {}) {
-        return this.setZone(params);
-    }
 }
 exports.Hydrawise = Hydrawise;
 //# sourceMappingURL=Hydrawise.js.map

File diff suppressed because it is too large
+ 0 - 0
dist/Hydrawise.js.map


+ 25 - 0
dist/HydrawiseController.d.ts

@@ -1,8 +1,10 @@
 /**
  * @author Martijn Dierckx
  */
+import { Hydrawise, HydrawiseZone } from ".";
 /** Class representing a Hydrawise controller */
 export declare class HydrawiseController {
+    apiBinding: Hydrawise;
     id: number;
     name: string;
     serialNumber: string;
@@ -11,6 +13,7 @@ export declare class HydrawiseController {
     /**
      * Create a new instance of a HydrawiseController
      * @param {object} options - Options object containing all parameters
+     * @param {Hydrawise} options.apiBinding - The API binding which can be used to execute commands on the zone
      * @param {number} options.id - The unique identifier of the controller
      * @param {string} options.name - The name of the controller
      * @param {string} options.serialNumber - The serial number of the controller
@@ -18,5 +21,27 @@ export declare class HydrawiseController {
      * @param {string} options.status - The status as returned by the Hydrawise cloud
      */
     constructor(options: any);
+    /**
+     * Retrieves all zones/relays known to the server for this controller
+     * @return {Promise} A Promise which will be resolved when all zones have been retrieved
+     */
+    getZones(): Promise<HydrawiseZone[]>;
+    /**
+     * Sends the run command to all the zones/relays of the controller
+     * @param {number} [duration] - How long should the command be executed
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    runAllZones(duration?: number): Promise<any>;
+    /**
+     * Sends the stop command to all the zones/relays of the controller
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    stopAllZones(): Promise<any>;
+    /**
+     * Sends the suspend command to all the zones/relays of the controller
+     * @param {number} [duration] - How long should the command be executed
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    suspendAllZones(duration?: number): Promise<any>;
 }
 //# sourceMappingURL=HydrawiseController.d.ts.map

+ 1 - 1
dist/HydrawiseController.d.ts.map

@@ -1 +1 @@
-{"version":3,"file":"HydrawiseController.d.ts","sourceRoot":"","sources":["../src/HydrawiseController.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,gDAAgD;AAChD,qBAAa,mBAAmB;IAExB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IAEtB;;;;;;;;OAQG;gBACS,OAAO,EAAE,GAAG;CAOxB"}
+{"version":3,"file":"HydrawiseController.d.ts","sourceRoot":"","sources":["../src/HydrawiseController.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,GAAG,CAAC;AAE7C,gDAAgD;AAChD,qBAAa,mBAAmB;IAExB,UAAU,EAAE,SAAS,CAAC;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;OASG;gBACS,OAAO,EAAE,GAAG;IASxB;;;OAGG;IACI,QAAQ,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;IAI3C;;;;OAIG;IACI,WAAW,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAInD;;;OAGG;IACI,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAInC;;;;OAIG;IACI,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CAGvD"}

+ 32 - 0
dist/HydrawiseController.js

@@ -8,6 +8,7 @@ class HydrawiseController {
     /**
      * Create a new instance of a HydrawiseController
      * @param {object} options - Options object containing all parameters
+     * @param {Hydrawise} options.apiBinding - The API binding which can be used to execute commands on the zone
      * @param {number} options.id - The unique identifier of the controller
      * @param {string} options.name - The name of the controller
      * @param {string} options.serialNumber - The serial number of the controller
@@ -15,12 +16,43 @@ class HydrawiseController {
      * @param {string} options.status - The status as returned by the Hydrawise cloud
      */
     constructor(options) {
+        this.apiBinding = options.apiBinding;
         this.id = options.id;
         this.name = options.name;
         this.serialNumber = options.serialNumber;
         this.lastContactWithCloud = options.lastContactWithCloud;
         this.status = options.status;
     }
+    /**
+     * Retrieves all zones/relays known to the server for this controller
+     * @return {Promise} A Promise which will be resolved when all zones have been retrieved
+     */
+    getZones() {
+        return this.apiBinding.getZones(this);
+    }
+    /**
+     * Sends the run command to all the zones/relays of the controller
+     * @param {number} [duration] - How long should the command be executed
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    runAllZones(duration) {
+        return this.apiBinding.commandAllZones('runall', this, duration);
+    }
+    /**
+     * Sends the stop command to all the zones/relays of the controller
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    stopAllZones() {
+        return this.apiBinding.commandAllZones('stopall', this);
+    }
+    /**
+     * Sends the suspend command to all the zones/relays of the controller
+     * @param {number} [duration] - How long should the command be executed
+     * @return {Promise} A Promise which will be resolved when the command has been executed.
+     */
+    suspendAllZones(duration) {
+        return this.apiBinding.commandAllZones('suspendall', this, duration);
+    }
 }
 exports.HydrawiseController = HydrawiseController;
 //# sourceMappingURL=HydrawiseController.js.map

+ 1 - 1
dist/HydrawiseController.js.map

@@ -1 +1 @@
-{"version":3,"file":"HydrawiseController.js","sourceRoot":"","sources":["../src/HydrawiseController.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,gDAAgD;AAChD,MAAa,mBAAmB;IAQ/B;;;;;;;;OAQG;IACH,YAAY,OAAY;QACvB,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACzC,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,CAAC;CACD;AAxBD,kDAwBC"}
+{"version":3,"file":"HydrawiseController.js","sourceRoot":"","sources":["../src/HydrawiseController.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAIH,gDAAgD;AAChD,MAAa,mBAAmB;IAS/B;;;;;;;;;OASG;IACH,YAAY,OAAY;QACvB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACrC,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACzC,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACI,QAAQ;QACd,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,QAAiB;QACnC,OAAO,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAClE,CAAC;IAED;;;OAGG;IACI,YAAY;QAClB,OAAO,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACzD,CAAC;IAED;;;;OAIG;IACI,eAAe,CAAC,QAAiB;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,YAAY,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IACtE,CAAC;CACD;AA7DD,kDA6DC"}

+ 3 - 0
dist/HydrawiseZone.d.ts

@@ -2,6 +2,7 @@
  * @author Martijn Dierckx
  */
 import { Hydrawise } from "./Hydrawise";
+import { HydrawiseController } from "./HydrawiseController";
 /** Class representing a Hydrawise zone */
 export declare class HydrawiseZone {
     apiBinding: Hydrawise;
@@ -13,6 +14,7 @@ export declare class HydrawiseZone {
     isSuspended: boolean;
     isRunning: boolean;
     remainingRunningTime: number;
+    controller: HydrawiseController;
     /**
      * Create a new instance of a HydrawiseZone
      * @param {object} options - Options object containing all parameters
@@ -25,6 +27,7 @@ export declare class HydrawiseZone {
      * @param {boolean} options.isSuspended - Returns true when the zoneis currently suspended
      * @param {boolean} options.isRunning - Returns true when the zone is actively running
      * @param {number} options.remainingRunningTime - Remaining run time in seconds when isRunning = true
+     * @param {HydrawiseController} [options.controller] - The controller linked to the zone
      */
     constructor(options: any);
     /**

+ 1 - 1
dist/HydrawiseZone.d.ts.map

@@ -1 +1 @@
-{"version":3,"file":"HydrawiseZone.d.ts","sourceRoot":"","sources":["../src/HydrawiseZone.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,0CAA0C;AAC1C,qBAAa,aAAa;IAElB,UAAU,EAAE,SAAS,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,OAAO,CAAC;IACnB,oBAAoB,EAAE,MAAM,CAAC;IAEpC;;;;;;;;;;;;OAYG;gBACS,OAAO,EAAE,GAAG;IAYxB;;;;OAIG;IACH,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAIpC;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC;IAIpB;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CAGxC"}
+{"version":3,"file":"HydrawiseZone.d.ts","sourceRoot":"","sources":["../src/HydrawiseZone.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,0CAA0C;AAC1C,qBAAa,aAAa;IAElB,UAAU,EAAE,SAAS,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,OAAO,CAAC;IACnB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,UAAU,EAAE,mBAAmB,CAAC;IAEvC;;;;;;;;;;;;;OAaG;gBACS,OAAO,EAAE,GAAG;IAaxB;;;;OAIG;IACI,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAI3C;;;OAGG;IACI,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC;IAI3B;;;;OAIG;IACI,OAAO,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CAG/C"}

+ 2 - 0
dist/HydrawiseZone.js

@@ -17,6 +17,7 @@ class HydrawiseZone {
      * @param {boolean} options.isSuspended - Returns true when the zoneis currently suspended
      * @param {boolean} options.isRunning - Returns true when the zone is actively running
      * @param {number} options.remainingRunningTime - Remaining run time in seconds when isRunning = true
+     * @param {HydrawiseController} [options.controller] - The controller linked to the zone
      */
     constructor(options) {
         this.apiBinding = options.apiBinding;
@@ -28,6 +29,7 @@ class HydrawiseZone {
         this.isSuspended = options.isSuspended;
         this.isRunning = options.isRunning;
         this.remainingRunningTime = options.remainingRunningTime;
+        this.controller = options.controller;
     }
     /**
      * Sends the run command to the zone/relay

+ 1 - 1
dist/HydrawiseZone.js.map

@@ -1 +1 @@
-{"version":3,"file":"HydrawiseZone.js","sourceRoot":"","sources":["../src/HydrawiseZone.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAIH,0CAA0C;AAC1C,MAAa,aAAa;IAYzB;;;;;;;;;;;;OAYG;IACH,YAAY,OAAY;QACvB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACrC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;QAC/C,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAC1D,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,QAAiB;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;IAED;;;OAGG;IACH,IAAI;QACH,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,QAAiB;QACxB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC/D,CAAC;CACD;AA9DD,sCA8DC"}
+{"version":3,"file":"HydrawiseZone.js","sourceRoot":"","sources":["../src/HydrawiseZone.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAKH,0CAA0C;AAC1C,MAAa,aAAa;IAazB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAAY;QACvB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACrC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;QAC/C,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;QACzD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,QAAiB;QAC3B,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;IAED;;;OAGG;IACI,IAAI;QACV,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACI,OAAO,CAAC,QAAiB;QAC/B,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC/D,CAAC;CACD;AAjED,sCAiEC"}

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "hydrawise-api",
-  "version": "1.0.1",
+  "version": "1.1.0",
   "description": "Library for accessing the Hydrawise API via the cloud or directly on the local network",
   "repository": {
     "type": "git",

+ 111 - 99
src/Hydrawise.ts

@@ -8,6 +8,7 @@ import { HydrawiseZone } from './HydrawiseZone';
 import { HydrawiseController } from './HydrawiseController';
 import { HydrawiseCommandException } from './HydrawiseCommandException';
 import Axios from 'axios';
+import { ifError } from 'assert';
 
 interface HydrawiseConfiguration {
 	type : HydrawiseConnectionType,
@@ -101,7 +102,7 @@ export class Hydrawise {
 	 * @param {string} action - The required command to be executed for the given zone/relay: run, suspend, stop
 	 * @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
 	 * @param {number} [duration] - How long should the command be executed (only applicable for run & suspend)
-	 * @todo Check whether controller_id needs to sent when the account contains multiple zones
+	 * @todo Allow using a controller id instead of HydrawiseController object.
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
 	public commandZone(action: string, zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
@@ -116,11 +117,11 @@ export class Hydrawise {
 
 			// Set Relay number for local binding
 			if(that.type == HydrawiseConnectionType.LOCAL) {
-				opts.relay = typeof zoneOrRelay == 'object' ? zoneOrRelay.zone : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
+				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 = typeof zoneOrRelay == 'object' ? zoneOrRelay.relayID : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
+				opts.relay_id = zoneOrRelay instanceof HydrawiseZone ? zoneOrRelay.relayID : zoneOrRelay // A zone object, as returned by getZones, or just the relayID can be sent
 			}
 
 			// Custom duration?
@@ -128,6 +129,11 @@ export class Hydrawise {
 				opts.custom = duration;
 			}
 
+			// Set controller if one was provided (only for cloud)
+			if(that.type == HydrawiseConnectionType.CLOUD && zoneOrRelay instanceof HydrawiseZone && zoneOrRelay.controller !== undefined && zoneOrRelay.controller instanceof HydrawiseController) {
+				opts.controller_id = zoneOrRelay.controller.id;
+			}
+
 			// Execute command
 			that.setZone(opts).then((data: any) => {
 				resolve(data);
@@ -146,7 +152,7 @@ export class Hydrawise {
 	 * @todo Check whether controller_id needs to sent when the account contains multiple zones
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public commandAllZones(action: string, duration?: number): Promise<any> {
+	public commandAllZones(action: string, controller?: HydrawiseController | number, duration?: number): Promise<any> {
 		let that = this;
 
 		// Get started
@@ -161,6 +167,16 @@ export class Hydrawise {
 				opts.custom = duration;
 			}
 
+			// Specific controller? (only cloud)
+			if(that.type == HydrawiseConnectionType.CLOUD && controller !== undefined && controller !== null) {
+				if(controller instanceof HydrawiseController) {
+					opts.controller_id = controller.id;
+				}
+				else {
+					opts.controller_id = controller;
+				}
+			}
+
 			that.setZone(opts).then(data => {
 				resolve(data);
 			}).catch((err) => {
@@ -173,11 +189,11 @@ export class Hydrawise {
 
 	/**
 	 * Sends the run command to a single zone/relay
-	 * @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
+	 * @param {(HydrawiseZone|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
 	 * @param {number} [duration] - How long should the command be executed
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public runZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
+	public runZone(zoneOrRelay: HydrawiseZone | number, duration?: number): Promise<any> {
 		return this.commandZone('run', zoneOrRelay, duration);
 	}
 
@@ -186,35 +202,36 @@ export class Hydrawise {
 	 * @param {number} [duration] - How long should the command be executed
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public runAllZones(duration?: number): Promise<any> {
-		return this.commandAllZones('runall', duration);
+	public runAllZones(controller?: HydrawiseController, duration?: number): Promise<any> {
+		return this.commandAllZones('runall', controller, duration);
 	}
 
 	/**
 	 * Sends the suspend command to a single zone/relay
-	 * @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
+	 * @param {(HydrawiseZone|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
 	 * @param {number} [duration] - How long should the command be executed
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public suspendZone(zoneOrRelay: number | HydrawiseZone, duration?: number): Promise<any> {
+	public suspendZone(zoneOrRelay: HydrawiseZone | number, duration?: number): Promise<any> {
 		return this.commandZone('suspend', zoneOrRelay, duration);
 	}
 
 	/**
-	 * Sends the suspend command to all zones/relays
+	 * Sends the suspend command to all zones/relays for a specific controller
 	 * @param {number} [duration] - How long should the command be executed
+	 * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned. 
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public suspendAllZones(duration?: number): Promise<any> {
-		return this.commandAllZones('suspendall', duration);
+	public suspendAllZones(controller?: HydrawiseController, duration?: number): Promise<any> {
+		return this.commandAllZones('suspendall', controller, duration);
 	}
 
 	/**
 	 * Sends the stop command to a single zone/relay
-	 * @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
+	 * @param {(HydrawiseZone|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
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public stopZone(zoneOrRelay: number | HydrawiseZone): Promise<any> {
+	public stopZone(zoneOrRelay: HydrawiseZone | number): Promise<any> {
 		return this.commandZone('stop', zoneOrRelay);
 	}
 
@@ -222,23 +239,34 @@ export class Hydrawise {
 	 * Sends the stop command to all zones/relays
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	public stopAllZones(): Promise<any> {
-		return this.commandAllZones('stopall');
+	public stopAllZones(controller?: HydrawiseController): Promise<any> {
+		return this.commandAllZones('stopall', controller);
 	}
 
 	/**
 	 * Retrieves all zones/relays known to the server
-	 * @param {boolean} [onlyConfigured = true] - Only return zones/relays which have been configured
+	 * @param {HydrawiseController|number} [controller] - Return zones for a specific controller. If not specified, the zones of the deault controller are returned. 
 	 * @return {Promise} A Promise which will be resolved when all zones have been retrieved
 	 */
-	public getZones(): Promise<HydrawiseZone[]> {
+	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) {
+				if(controller instanceof HydrawiseController) {
+					controllerID = controller.id;
+				}
+				else {
+					controllerID = controller;
+				}
+			}
+
 			// Get relays
-			that.getStatusAndSchedule().then((data: any) => {
+			that.getStatusAndSchedule(controllerID).then((data: any) => {
 				let zones:HydrawiseZone[] = [];
 				
 				// Check every returned relay
@@ -257,9 +285,14 @@ export class Hydrawise {
 							nextRunDuration: z.run || z.run_seconds,
 							isSuspended: z.suspended !== undefined && z.suspended == 1,
 							isRunning: false,
-							remainingRunningTime: 0,
+							remainingRunningTime: 0
 						};
 
+						// Link controller to the zones if it was provided when calling the method
+						if(controller !== undefined && controller !== null && controller instanceof HydrawiseController) {
+							zone.controller = controller;
+						}
+
 						// Only available data for local connections
 						if(that.type == HydrawiseConnectionType.LOCAL) {
 							zone.defaultRunDuration = z.normalRuntime * 60;
@@ -290,7 +323,7 @@ export class Hydrawise {
 	}
 
 	/**
-	 * Retrieves all controllers known to the Hydrawise cloud
+	 * Retrieves all controllers known to the Hydrawise cloud or returns a single dummy one for a local connection
 	 * @return {Promise} A Promise which will be resolved when all controllers have been retrieved
 	 */
 	public getControllers(): Promise<HydrawiseController[]> {
@@ -299,29 +332,44 @@ export class Hydrawise {
 		// Get started
 		let promise: Promise<HydrawiseController[]> = new Promise((resolve, reject) => {
 			
-			// Get Controllers
-			this.getCustomerDetails('controllers').then(data => {
-				let controllers: HydrawiseController[] = [];
+			// Cloud
+			if(that.type == HydrawiseConnectionType.CLOUD) {
 				
-				// Check every returned relay
-				data.controllers.map((c: any) => {
+				// Get Controllers
+				this.getCustomerDetails('controllers').then(data => {
+					let controllers: HydrawiseController[] = [];
 					
-					// Zone
-					let controller: any = {
-						id: c.controller_id,
-						name: c.name,
-						serialNumber: c.serial_number,
-						lastContactWithCloud: new Date(c.last_contact * 1000),
-						status: c.status
-					};
-					
-					controllers.push(new HydrawiseController(controller));
-				});
+					// Check every returned relay
+					data.controllers.map((c: any) => {
+						
+						// Controller
+						let controller: any = {
+							apiBinding: that,
+							id: c.controller_id,
+							name: c.name,
+							serialNumber: c.serial_number,
+							lastContactWithCloud: new Date(c.last_contact * 1000),
+							status: c.status
+						};
+						
+						controllers.push(new HydrawiseController(controller));
+					});
 
-				resolve(controllers);
-			}).catch((err) => {
-				reject(err);
-			});
+					resolve(controllers);
+				}).catch((err) => {
+					reject(err);
+				});
+			}
+			// Local
+			else {
+				// Controller
+				let controller: any = {
+					apiBinding: that,
+					name: that.url
+				};
+				
+				resolve([new HydrawiseController(controller)]);
+			}
 
 		});
 
@@ -333,7 +381,7 @@ export class Hydrawise {
 	/* -------- Raw API calls -------- */
 
 	/**
-	 * Gets the customer ID & list of available controllers configured in the Hydrawise cloud. Only available in cloud binding. 
+	 * Gets the customer ID & list of available controllers configured in the Hydrawise cloud. Only available in cloud binding.
 	 * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
 	 * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
 	 */
@@ -350,76 +398,40 @@ export class Hydrawise {
 
 	/**
 	 * Gets the status and schedule of the locally connected controller or all controllers in the cloud 
-	 * @param {string} type - Defines the type of customer details to be retrieved alongside the customer ID
-	 * @todo Check whether controller_id needs to sent when the account contains multiple zones
+	 * @param {number} [controller] - Return the status and schedule for a specific controller. If not specified, the zones of the deault controller are returned. 
 	 * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
 	 */
-	public getStatusAndSchedule(tag: string = '', hours: string = '168'): Promise<any> {
-		let uri = (this.type == HydrawiseConnectionType.LOCAL ? 'get_sched_json.php' : 'statusschedule.php');
+	public getStatusAndSchedule(controller?: number): Promise<any> {
+		let uri: string = (this.type == HydrawiseConnectionType.LOCAL ? 'get_sched_json.php' : 'statusschedule.php');
+		let params: any = {};
 	
-		return this.request(uri, { tag, hours });
-	}
-
-	/*setController(controllerID: number): Promise<any> {
-		// Cloud only API
-		if (this.type  == HydrawiseConnectionType.LOCAL) { 
-			return new Promise((resolve, reject) => {
-				reject(new HydrawiseCommandException('Calling Cloud API function on a Local Binding'));
-			});
+		// Was a controller set?
+		if(controller !== undefined && controller !== null) {
+			params.controller_id = controller;
 		}
-		
-		return this.request('setcontroller.php', { controllerID, json: true });
-	}*/
+
+		// If no controller was set
+		return this.request(uri, params);
+	}
 
 	/**
 	 * Sends an action request to a specific or all zones
 	 * @param {object} params - Parameters object containing all parameters to be sent along with the request
+	 * @param {string} [params.relay_id] - The id of the relay which needs to be targetted. Not needed for runall, suspendall, stopall
 	 * @param {string} params.action - The action to be executed: run, stop, suspend, runall, suspendall, stopall
+	 * @param {number} [params.custom] - The amount of seconds the action needs to be run. Only for run, suspend, runall, suspendall
+	 * @param {number} [controller] - Needs to be specified if you have multiple controllers (cloud only)
 	 * @todo Complete params documentation
-	 * @todo Check whether controller_id needs to sent when the account contains multiple zones
 	 * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
 	 */
-	public setZone(this: Hydrawise, params: any = {}): Promise<any> {
+	public setZone(params: any = {}, controller?: number): Promise<any> {
 		let uri: string = (this.type == HydrawiseConnectionType.LOCAL ? 'set_manual_data.php' : 'setzone.php');
 		
-		return this.request(uri, params);
-	}
-
-
-
-	/* -------- Original 0.1.0 function names for backwards compatibility -------- */
-
-	/**
-	 * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-	 * @param {string} [type = controllers] - Defines the type of customer details to be retrieved alongside the customer ID
-	 * @alias getCustomerDetails
-	 * @return {Promise} A Promise which will be resolved when the request has returned from the cloud server.
-	 */
-	public customerdetails(type: string = 'controllers'): Promise<any> {
-		return this.getCustomerDetails(type);
-	}
-
-	/**
-	 * Does the same as getCustomerDetails, and is only kept to be backwards compatible with version 0.1.0 of this module
-	 * @alias getStatusAndSchedule
-	 * @deprecated since version 1.0.0. Please use getZones()
-	 * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-	 */
-	public statusschedule(tag: string = '', hours: string = '168'): Promise<any> {
-		return this.getStatusAndSchedule(tag, hours);
-	}
-
-	/*setcontroller(controllerID) {
-		return this.setController(controllerID);
-	}*/
+		// Was a controller set?
+		if(controller !== undefined && controller !== null) {
+			params.controller_id = controller;
+		}
 
-	/**
-	 * Does the same as setZone, and is only kept to be backwards compatible with version 0.1.0 of this module
-	 * @alias setZone
-	 * @deprecated since version 1.0.0. Please use runZone(), suspendZone(), stopZone(), runAllZones(), suspendAllZones(), stopAllZones() or the run(), suspend(), stop() commands on a HydrawiseZone object.
-	 * @return {Promise} A Promise which will be resolved when the request has returned from the local or cloud server.
-	 */
-	public setzone(params: any = {}): Promise<any> {
-		return this.setZone(params);
+		return this.request(uri, params);
 	}
 }

+ 39 - 0
src/HydrawiseController.ts

@@ -2,9 +2,12 @@
  * @author Martijn Dierckx
  */
 
+import { Hydrawise, HydrawiseZone } from ".";
+
 /** Class representing a Hydrawise controller */
 export class HydrawiseController {
 
+	public apiBinding: Hydrawise;
 	public id: number;
 	public name: string;
 	public serialNumber: string;
@@ -14,6 +17,7 @@ export class HydrawiseController {
 	/**
 	 * Create a new instance of a HydrawiseController
 	 * @param {object} options - Options object containing all parameters
+	 * @param {Hydrawise} options.apiBinding - The API binding which can be used to execute commands on the zone
 	 * @param {number} options.id - The unique identifier of the controller
 	 * @param {string} options.name - The name of the controller
 	 * @param {string} options.serialNumber - The serial number of the controller
@@ -21,10 +25,45 @@ export class HydrawiseController {
 	 * @param {string} options.status - The status as returned by the Hydrawise cloud
 	 */
 	constructor(options: any) {
+		this.apiBinding = options.apiBinding;
 		this.id = options.id;
 		this.name = options.name;
 		this.serialNumber = options.serialNumber;
 		this.lastContactWithCloud = options.lastContactWithCloud;
 		this.status = options.status;
 	}
+
+	/**
+	 * Retrieves all zones/relays known to the server for this controller
+	 * @return {Promise} A Promise which will be resolved when all zones have been retrieved
+	 */
+	public getZones(): Promise<HydrawiseZone[]> {
+		return this.apiBinding.getZones(this);
+	}
+
+	/**
+	 * Sends the run command to all the zones/relays of the controller
+	 * @param {number} [duration] - How long should the command be executed
+	 * @return {Promise} A Promise which will be resolved when the command has been executed.
+	 */
+	public runAllZones(duration?: number): Promise<any> {
+		return this.apiBinding.commandAllZones('runall', this, duration);
+	}
+
+	/**
+	 * Sends the stop command to all the zones/relays of the controller
+	 * @return {Promise} A Promise which will be resolved when the command has been executed.
+	 */
+	public stopAllZones(): Promise<any> {
+		return this.apiBinding.commandAllZones('stopall', this);
+	}
+
+	/**
+	 * Sends the suspend command to all the zones/relays of the controller
+	 * @param {number} [duration] - How long should the command be executed
+	 * @return {Promise} A Promise which will be resolved when the command has been executed.
+	 */
+	public suspendAllZones(duration?: number): Promise<any> {
+		return this.apiBinding.commandAllZones('suspendall', this, duration);
+	}
 }

+ 7 - 3
src/HydrawiseZone.ts

@@ -3,6 +3,7 @@
  */
 
 import { Hydrawise } from "./Hydrawise";
+import { HydrawiseController } from "./HydrawiseController";
 
 /** Class representing a Hydrawise zone */
 export class HydrawiseZone {
@@ -16,6 +17,7 @@ export class HydrawiseZone {
 	public isSuspended: boolean;
 	public isRunning: boolean;
 	public remainingRunningTime: number;
+	public controller: HydrawiseController;
 
 	/**
 	 * Create a new instance of a HydrawiseZone
@@ -29,6 +31,7 @@ export class HydrawiseZone {
 	 * @param {boolean} options.isSuspended - Returns true when the zoneis currently suspended
 	 * @param {boolean} options.isRunning - Returns true when the zone is actively running
 	 * @param {number} options.remainingRunningTime - Remaining run time in seconds when isRunning = true
+	 * @param {HydrawiseController} [options.controller] - The controller linked to the zone
 	 */
 	constructor(options: any) {
 		this.apiBinding = options.apiBinding;
@@ -40,6 +43,7 @@ export class HydrawiseZone {
 		this.isSuspended = options.isSuspended;
 		this.isRunning = options.isRunning;
 		this.remainingRunningTime = options.remainingRunningTime;
+		this.controller = options.controller;
 	}
 
 	/**
@@ -47,7 +51,7 @@ export class HydrawiseZone {
 	 * @param {number} [duration] - How long should the command be executed
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	run(duration?: number): Promise<any> {
+	public run(duration?: number): Promise<any> {
 		return this.apiBinding.commandZone('run', this, duration);
 	}
 
@@ -55,7 +59,7 @@ export class HydrawiseZone {
 	 * Sends the stop command to the zone/relay
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	stop(): Promise<any> {
+	public stop(): Promise<any> {
 		return this.apiBinding.commandZone('stop', this);
 	}
 
@@ -64,7 +68,7 @@ export class HydrawiseZone {
 	 * @param {number} [duration] - How long should the command be executed
 	 * @return {Promise} A Promise which will be resolved when the command has been executed.
 	 */
-	suspend(duration?: number): Promise<any> {
+	public suspend(duration?: number): Promise<any> {
 		return this.apiBinding.commandZone('suspend', this, duration);
 	}
 }

+ 24 - 14
test/test.js

@@ -8,36 +8,46 @@ else {
   hydrawise = new Hydrawise({ type: 'LOCAL', host: process.argv[2], password: process.argv[3] });
 }
 
-/* Get all zones -> run first zone -> get all zones -> stop first zone -> get all zones */
-hydrawise.getZones().then(function(data) {
+/* Get all controllers -> get all zones for first controller -> run first zone -> get all zones -> stop first zone -> get all zones */
+hydrawise.getControllers().then(function (data) {
   console.log(data);
   setTimeout(() => {
 
-    data[0].run().then(function(data) {
+    var controller = data[0];
+
+    controller.getZones().then(function (data) {
       console.log(data);
       setTimeout(() => {
-        
-        hydrawise.getZones().then(function(data) {
+
+        data[0].run().then(function (data) {
           console.log(data);
           setTimeout(() => {
-        
-            data[0].stop().then(function(data) {
+
+            controller.getZones().then(function (data) {
               console.log(data);
               setTimeout(() => {
-        
-                hydrawise.getZones().then(function(data) {
+
+                data[0].stop().then(function (data) {
                   console.log(data);
-        
-                  
+                  setTimeout(() => {
+
+                    controller.getZones().then(function (data) {
+                      console.log(data);
+
+                    }).catch((err) => {
+                      console.log(err);
+                    });
+
+                  }, 2000);
                 }).catch((err) => {
                   console.log(err);
                 });
-        
+
               }, 2000);
             }).catch((err) => {
               console.log(err);
             });
-    
+
           }, 2000);
         }).catch((err) => {
           console.log(err);
@@ -50,5 +60,5 @@ hydrawise.getZones().then(function(data) {
 
   }, 2000);
 }).catch((err) => {
-	console.log(err);
+  console.log(err);
 });

Some files were not shown because too many files changed in this diff