|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +const ENetClient_1 = __importDefault(require("./ENetClient")); |
| 7 | +/** |
| 8 | + * @description Abstraction over ENetClient. |
| 9 | + */ |
| 10 | +class ENet { |
| 11 | + constructor(hostname, token = null) { |
| 12 | + this.hostname = hostname; |
| 13 | + this.token = token; |
| 14 | + } |
| 15 | + /** |
| 16 | + * Authenticate with eNet server. |
| 17 | + * |
| 18 | + * @param {string} username |
| 19 | + * @param {string} password |
| 20 | + * @returns |
| 21 | + */ |
| 22 | + authenticate(username, password) { |
| 23 | + this.username = username; |
| 24 | + this.password = password; |
| 25 | + var client = new ENetClient_1.default(this.hostname, this.token); |
| 26 | + return new Promise(async (resolve, reject) => { |
| 27 | + const hostname = this.hostname; |
| 28 | + if (typeof hostname !== "string") |
| 29 | + throw new Error("No hostname"); |
| 30 | + try { |
| 31 | + const { response, body } = await client.request('management', 'getDigestAuthentificationInfos', { auth: false, response: true }, null); |
| 32 | + const token = response.headers["x-clientcredentials-sessionid"]; |
| 33 | + client = new ENetClient_1.default(this.hostname, token); |
| 34 | + const user = await client.userLoginDigest(username, password, body.result); |
| 35 | + if (user.result.userName === username) { |
| 36 | + await client.setRemember(); |
| 37 | + this.token = token; |
| 38 | + resolve(token); |
| 39 | + } |
| 40 | + else { |
| 41 | + reject("login failed"); |
| 42 | + } |
| 43 | + } |
| 44 | + catch (error) { |
| 45 | + if (error && error.statusCode) { |
| 46 | + reject(error.statusMessage); |
| 47 | + } |
| 48 | + else { |
| 49 | + reject(error); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + /** |
| 55 | + * Shuts down the IMB service and invalidates the token. |
| 56 | + * |
| 57 | + * @param {String} token |
| 58 | + * @returns |
| 59 | + */ |
| 60 | + deauthenticate(token = null) { |
| 61 | + const sessionID = token || this.token; |
| 62 | + return new Promise(async (resolve, reject) => { |
| 63 | + try { |
| 64 | + if (sessionID === null) |
| 65 | + throw new Error("Missing token"); |
| 66 | + resolve(); |
| 67 | + } |
| 68 | + catch (error) { |
| 69 | + reject(error.message); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + /** |
| 74 | + * Reboots the ENet server. |
| 75 | + * |
| 76 | + * @returns |
| 77 | + */ |
| 78 | + reboot() { |
| 79 | + return new Promise(async (resolve, reject) => { |
| 80 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 81 | + try { |
| 82 | + await client.restartBox(); |
| 83 | + resolve(); |
| 84 | + } |
| 85 | + catch (error) { |
| 86 | + reject(error.message); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + /** |
| 91 | + * Location list |
| 92 | + * |
| 93 | + * @param {Boolean} flat |
| 94 | + * @returns |
| 95 | + */ |
| 96 | + getLocations(flat = false) { |
| 97 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 98 | + return new Promise(async (resolve, reject) => { |
| 99 | + try { |
| 100 | + const response = await client.getLocations(); |
| 101 | + if (flat) { |
| 102 | + resolve(locationsFlat(response)); |
| 103 | + } |
| 104 | + else { |
| 105 | + resolve(response); |
| 106 | + } |
| 107 | + } |
| 108 | + catch (error) { |
| 109 | + reject(error.message); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + /** |
| 114 | + * |
| 115 | + * @returns |
| 116 | + */ |
| 117 | + getDevices(deviceLsIncludeState = false) { |
| 118 | + return new Promise(async (resolve, reject) => { |
| 119 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 120 | + try { |
| 121 | + var data = []; |
| 122 | + const response = await this.getLocations(true); |
| 123 | + for (const location of response) { |
| 124 | + for (let device of location.deviceUIDs) { |
| 125 | + device.locationName = location.name; |
| 126 | + if (deviceLsIncludeState) { |
| 127 | + var functions = parseDeviceFunctions(await client.getDevicesWithParameterFilter([device.deviceUID])); |
| 128 | + for (const func of functions) { |
| 129 | + if (func.typeID.includes(".IOO") && func.io === "OUT") { |
| 130 | + const values = await client.getCurrentValuesFromOutputDeviceFunction(func.uid) || []; |
| 131 | + if (values.length > 0 && typeof values[0].value === "boolean") { |
| 132 | + device.state = values[0].value; |
| 133 | + } |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + data.push(device); |
| 139 | + } |
| 140 | + } |
| 141 | + resolve(data); |
| 142 | + } |
| 143 | + catch (error) { |
| 144 | + console.log(error); |
| 145 | + reject(error.message); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + /** |
| 150 | + * |
| 151 | + * @param {string[]} deviceUIDs |
| 152 | + * @returns |
| 153 | + */ |
| 154 | + getDevicesInfo(deviceUIDs) { |
| 155 | + return new Promise(async (resolve, reject) => { |
| 156 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 157 | + try { |
| 158 | + const response = await client.getDevicesWithParameterFilter(deviceUIDs); |
| 159 | + resolve(response); |
| 160 | + } |
| 161 | + catch (error) { |
| 162 | + reject(error.message); |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | + /** |
| 167 | + * |
| 168 | + * @param {string} deviceUID |
| 169 | + * @returns |
| 170 | + */ |
| 171 | + getDeviceInfo(deviceUID) { |
| 172 | + return new Promise(async (resolve, reject) => { |
| 173 | + this.getDevicesInfo([deviceUID]) |
| 174 | + .then(response => { resolve(response[0]); }) |
| 175 | + .catch(reject); |
| 176 | + }); |
| 177 | + } |
| 178 | + /** |
| 179 | + * |
| 180 | + * @param {string} deviceUID |
| 181 | + * @param {*} state |
| 182 | + * @returns |
| 183 | + */ |
| 184 | + async setDevicePrimaryState(deviceUID, state) { |
| 185 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 186 | + try { |
| 187 | + const response = await client.getDevicesWithParameterFilter([deviceUID]); |
| 188 | + var data = parseDeviceFunctions(response); |
| 189 | + for (const func of data) { |
| 190 | + if (func.typeID.includes(".SOO") && func.io === "IN") { |
| 191 | + await client.callInputDeviceFunction(func.uid, state); |
| 192 | + return; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + catch (error) { |
| 197 | + throw error; |
| 198 | + } |
| 199 | + } |
| 200 | + /** |
| 201 | + * |
| 202 | + * @param {string} deviceUID |
| 203 | + * @returns |
| 204 | + */ |
| 205 | + getDevicePrimaryState(deviceUID) { |
| 206 | + return new Promise(async (resolve, reject) => { |
| 207 | + const client = new ENetClient_1.default(this.hostname, this.token); |
| 208 | + try { |
| 209 | + const response = await client.getDevicesWithParameterFilter([deviceUID]); |
| 210 | + var data = parseDeviceFunctions(response); |
| 211 | + for (const func of data) { |
| 212 | + if (func.typeID.includes(".IOO") && func.io === "OUT") { |
| 213 | + const values = await client.getCurrentValuesFromOutputDeviceFunction(func.uid); |
| 214 | + resolve(values[0].value); |
| 215 | + return; |
| 216 | + } |
| 217 | + } |
| 218 | + reject(); |
| 219 | + } |
| 220 | + catch (error) { |
| 221 | + reject(error.message); |
| 222 | + } |
| 223 | + }); |
| 224 | + } |
| 225 | +} |
| 226 | +/** |
| 227 | + * Helper function |
| 228 | + * |
| 229 | + * @param {*} data |
| 230 | + * @returns |
| 231 | + */ |
| 232 | +function locationsFlat(data) { |
| 233 | + const array = []; |
| 234 | + for (const location of data) { |
| 235 | + const { childLocations } = location; |
| 236 | + if (childLocations) { |
| 237 | + for (const child of locationsFlat(childLocations)) |
| 238 | + array.push(child); |
| 239 | + } |
| 240 | + delete location.childLocations; |
| 241 | + array.push(location); |
| 242 | + } |
| 243 | + return array; |
| 244 | +} |
| 245 | +/** |
| 246 | + * Helper function |
| 247 | + * |
| 248 | + * @param devices |
| 249 | + * @returns |
| 250 | + */ |
| 251 | +function parseDeviceFunctions(devices) { |
| 252 | + const functions = []; |
| 253 | + for (const device of devices) { |
| 254 | + for (const configurationGroup of device.deviceChannelConfigurationGroups) { |
| 255 | + for (const deviceChannel of configurationGroup.deviceChannels) { |
| 256 | + const inputFunctions = deviceChannel.inputDeviceFunctions; |
| 257 | + const outputFunctions = deviceChannel.outputDeviceFunctions; |
| 258 | + for (const func of inputFunctions) |
| 259 | + if (typeof func === "object") { |
| 260 | + functions.push({ |
| 261 | + uid: func.uid, |
| 262 | + typeID: func.typeID, |
| 263 | + active: func.active, |
| 264 | + currentValues: func.currentValues, |
| 265 | + deviceFunctionDependency: func.deviceFunctionDependency, |
| 266 | + io: "IN" |
| 267 | + }); |
| 268 | + } |
| 269 | + for (const func of outputFunctions) |
| 270 | + if (typeof func === "object") { |
| 271 | + functions.push({ |
| 272 | + uid: func.uid, |
| 273 | + typeID: func.typeID, |
| 274 | + active: func.active, |
| 275 | + currentValues: func.currentValues, |
| 276 | + deviceFunctionDependency: func.deviceFunctionDependency, |
| 277 | + io: "OUT" |
| 278 | + }); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + return functions; |
| 284 | +} |
| 285 | +exports.default = ENet; |
0 commit comments