homebridge-platform-orbit
Version:
Orbit Irrigation System platform plugin for [HomeBridge](https://github.com/nfarina/homebridge).
231 lines • 10.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.OrbitDevice = exports.OrbitAPI = void 0;
var bent_1 = __importDefault(require("bent"));
var ws_1 = __importDefault(require("ws"));
var reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
var endpoint = 'https://api.orbitbhyve.com/v1';
var ws_endpoint = 'wss://api.orbitbhyve.com/v1';
var WSpingINTERVAL = 25000; // Websocket get's timed out after 30s, so ping every 25s
var OrbitAPI = /** @class */ (function () {
function OrbitAPI(log, email, password) {
this.log = log;
this.email = email;
this.password = password;
this.token = "";
}
OrbitAPI.prototype.login = function () {
return __awaiter(this, void 0, void 0, function () {
var postJSON, response, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
postJSON = bent_1["default"]('POST', 'json');
return [4 /*yield*/, postJSON(endpoint + "/session", {
"session": {
"email": this.email,
"password": this.password
}
}, {
"orbit-app-id": "Orbit Support Dashboard",
"orbit-api-key": "null"
})];
case 1:
response = _a.sent();
this.token = response['orbit_api_key'];
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
throw error_1;
case 3: return [2 /*return*/];
}
});
});
};
OrbitAPI.prototype.getDevices = function () {
return __awaiter(this, void 0, void 0, function () {
var devices, getJSON, response, error_2;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
devices = [];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
getJSON = bent_1["default"]('GET', 'json');
return [4 /*yield*/, getJSON(endpoint + "/devices", {}, {
"orbit-app-id": "Orbit Support Dashboard",
"orbit-api-key": this.token
})];
case 2:
response = _a.sent();
response.forEach(function (result) {
if (result['type'] == "sprinkler_timer") {
// Create the device
var device_1 = new OrbitDevice(_this.log, _this.token, result['id'], result['name'], result['hardware_version'], result['firmware_version'], result['is_connected']);
// Create zones
result['zones'].forEach(function (zone) {
device_1.addZone(zone['station'], zone['name']);
});
devices.push(device_1);
}
});
return [2 /*return*/, devices];
case 3:
error_2 = _a.sent();
throw error_2;
case 4: return [2 /*return*/];
}
});
});
};
return OrbitAPI;
}());
exports.OrbitAPI = OrbitAPI;
var OrbitDevice = /** @class */ (function () {
function OrbitDevice(log, token, id, name, hardware_version, firmware_version, is_connected) {
var _this = this;
this.log = log;
this.token = token;
this.id = id;
this.name = name;
this.hardware_version = hardware_version;
this.firmware_version = firmware_version;
this.is_connected = is_connected;
this.zones = [];
this.messageQueue = [];
// Create the Reconnecting Web Socket
this.rws = new reconnecting_websocket_1["default"](ws_endpoint + "/events", [], {
WebSocket: ws_1["default"],
connectionTimeout: 10000,
maxReconnectionDelay: 64000,
minReconnectionDelay: 2000,
reconnectionDelayGrowFactor: 2
});
// Intercept send events for logging and queuing
var origSend = this.rws.send.bind(this.rws);
this.rws.send = function (data) {
if (_this.rws.readyState === ws_1["default"].OPEN) {
_this.log.debug('TX', data);
origSend(data);
}
else {
_this.messageQueue.push(data);
}
};
// Ping
setInterval(function () {
_this.rws.send(JSON.stringify({ event: 'ping' }));
}, WSpingINTERVAL);
// On Open, process any queued messages
this.rws.onopen = function (openEvent) {
_this.log.debug('WebSocket', openEvent.type);
while (_this.messageQueue.length > 0) {
var data = _this.messageQueue.shift();
_this.log.debug('TX', data);
origSend(data);
}
};
// On Close
this.rws.onclose = function (closeEvent) {
_this.log.debug('WebSocket', closeEvent.type);
};
// On Error
this.rws.onerror = function (errorEvent) {
_this.log.error('WebSocket Error', errorEvent);
_this.rws.close();
};
}
OrbitDevice.prototype.addZone = function (station, name) {
this.zones.push({ "station": station, "name": name });
};
OrbitDevice.prototype.openConnection = function () {
this.log.debug('openConnection');
this.rws.send(JSON.stringify({
event: "app_connection",
orbit_session_token: this.token,
subscribe_device_id: this.id
}));
};
OrbitDevice.prototype.onMessage = function (listner) {
var _this = this;
this.log.debug('onMessage');
this.rws.onmessage = function (messageEvent) {
_this.log.debug('RX', messageEvent.data);
listner(messageEvent.data);
};
};
OrbitDevice.prototype.sync = function () {
this.log.debug('sync');
this.rws.send(JSON.stringify({
event: "sync",
device_id: this.id
}));
};
OrbitDevice.prototype.startZone = function (station, run_time) {
this.log.debug('startZone', station, run_time);
this.rws.send(JSON.stringify({
event: "change_mode",
mode: "manual",
device_id: this.id,
timestamp: new Date().toISOString(),
stations: [
{ "station": station, "run_time": run_time }
]
}));
};
OrbitDevice.prototype.stopZone = function () {
this.log.debug('stopZone');
this.rws.send(JSON.stringify({
event: "change_mode",
mode: "manual",
device_id: this.id,
timestamp: new Date().toISOString(),
stations: []
}));
};
return OrbitDevice;
}());
exports.OrbitDevice = OrbitDevice;
//# sourceMappingURL=orbitapi.js.map