zigbee-herdsman-zigate
Version:
An open source ZigBee gateway solution with node.js.
164 lines • 7.98 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Zcl = __importStar(require("../zcl"));
const utils_1 = require("../utils");
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default('zigbee-herdsman:controller:touchlink');
const scanChannels = [11, 15, 20, 25, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26];
class Touchlink {
constructor(adapter) {
this.adapter = adapter;
this.locked = false;
}
lock(lock) {
if (lock && this.locked) {
throw new Error(`Touchlink operation already in progress`);
}
this.locked = lock;
}
scan() {
return __awaiter(this, void 0, void 0, function* () {
this.lock(true);
const result = [];
try {
for (const channel of scanChannels) {
debug(`Set InterPAN channel to '${channel}'`);
yield this.adapter.setChannelInterPAN(channel);
try {
// TODO: multiple responses are not handled yet.
const response = yield this.adapter.sendZclFrameInterPANBroadcast(this.createScanRequestFrame(), 500);
debug(`Got scan response on channel '${channel}' of '${response.address}'`);
utils_1.AssertString(response.address);
result.push({ ieeeAddr: response.address, channel });
}
catch (error) {
debug(`Scan request failed or was not answered: '${error}'`);
}
}
}
finally {
debug(`Restore InterPAN channel`);
yield this.adapter.restoreChannelInterPAN();
this.lock(false);
}
return result;
});
}
identify(ieeeAddr, channel) {
return __awaiter(this, void 0, void 0, function* () {
this.lock(true);
try {
debug(`Set InterPAN channel to '${channel}'`);
yield this.adapter.setChannelInterPAN(channel);
yield this.adapter.sendZclFrameInterPANBroadcast(this.createScanRequestFrame(), 500);
debug(`Got scan response on channel '${channel}'`);
debug(`Identifying '${ieeeAddr}'`);
yield this.adapter.sendZclFrameInterPANToIeeeAddr(this.createIdentifyRequestFrame(), ieeeAddr);
}
finally {
debug(`Restore InterPAN channel`);
yield this.adapter.restoreChannelInterPAN();
this.lock(false);
}
});
}
factoryReset(ieeeAddr, channel) {
return __awaiter(this, void 0, void 0, function* () {
this.lock(true);
try {
debug(`Set InterPAN channel to '${channel}'`);
yield this.adapter.setChannelInterPAN(channel);
yield this.adapter.sendZclFrameInterPANBroadcast(this.createScanRequestFrame(), 500);
debug(`Got scan response on channel '${channel}'`);
debug(`Identifying '${ieeeAddr}'`);
yield this.adapter.sendZclFrameInterPANToIeeeAddr(this.createIdentifyRequestFrame(), ieeeAddr);
yield utils_1.Wait(2000);
debug(`Reset to factory new '${ieeeAddr}'`);
yield this.adapter.sendZclFrameInterPANToIeeeAddr(this.createResetFactoryNewRequestFrame(), ieeeAddr);
}
finally {
debug(`Restore InterPAN channel`);
yield this.adapter.restoreChannelInterPAN();
this.lock(false);
}
return true;
});
}
factoryResetFirst() {
return __awaiter(this, void 0, void 0, function* () {
let done = false;
try {
for (const channel of scanChannels) {
debug(`Set InterPAN channel to '${channel}'`);
yield this.adapter.setChannelInterPAN(channel);
try {
const response = yield this.adapter.sendZclFrameInterPANBroadcast(this.createScanRequestFrame(), 500);
debug(`Got scan response on channel '${channel}'`);
utils_1.AssertString(response.address);
// Device answered (if not it will fall in the catch below),
// identify it (this will make e.g. the bulb flash)
debug(`Identifying`);
yield this.adapter.sendZclFrameInterPANToIeeeAddr(this.createIdentifyRequestFrame(), response.address);
yield utils_1.Wait(2000);
debug(`Reset to factory new`);
yield this.adapter.sendZclFrameInterPANToIeeeAddr(this.createResetFactoryNewRequestFrame(), response.address);
done = true;
}
catch (error) {
debug(`Scan request failed or was not answered: '${error}'`);
}
if (done)
break;
}
}
finally {
debug(`Restore InterPAN channel`);
yield this.adapter.restoreChannelInterPAN();
this.lock(false);
}
return done;
});
}
createScanRequestFrame() {
return Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, Zcl.Direction.CLIENT_TO_SERVER, true, null, 0, 'scanRequest', Zcl.Utils.getCluster('touchlink').ID, { transactionID: 1, zigbeeInformation: 4, touchlinkInformation: 18 });
}
createIdentifyRequestFrame() {
return Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, Zcl.Direction.CLIENT_TO_SERVER, true, null, 0, 'identifyRequest', Zcl.Utils.getCluster('touchlink').ID, { transactionID: 1, duration: 65535 });
}
createResetFactoryNewRequestFrame() {
return Zcl.ZclFrame.create(Zcl.FrameType.SPECIFIC, Zcl.Direction.CLIENT_TO_SERVER, true, null, 0, 'resetToFactoryNew', Zcl.Utils.getCluster('touchlink').ID, { transactionID: 1 });
}
}
exports.default = Touchlink;
//# sourceMappingURL=touchlink.js.map