homebridge-switchbot-bluetooth-platform
Version:
A Homebridge platform Plugin for controlling SwitchBot bots using BLE (Bluetooth Low Energry)
83 lines • 4.22 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetadataClient = void 0;
const node_cache_1 = __importDefault(require("node-cache"));
const node_switchbot_1 = __importDefault(require("node-switchbot"));
const settings_1 = require("../settings");
const errorLogger_1 = require("../utils/errorLogger");
class MetadataClient {
constructor(log) {
this.client = new node_switchbot_1.default();
this.metaDataCache = new node_cache_1.default({
stdTTL: settings_1.CACHE_TTL,
checkperiod: settings_1.CHECK_CACHE_TTL_PERIOD,
// We want to store a clone of the metadata, since
// it is the recommended method of caching things using
// node-cache
useClones: true,
deleteOnExpire: true,
});
this.isScanningForMetadata = false;
/**
* Retreives the bot's operation mode.
* There are two possible modes of operation: switch and press.
* When the bot is set to switch mode, it behaves as a switch, this means it has "ON"/"OFF" states.
* When the bot is set to press mode, it has no "ON"/"OFF" state, but rather a single mode - press mode.
* Setting the bot's operation mode can be done via the offical SwitchBot application.
*/
this.getDeviceOperationMode = (address, scanDuration) => {
var _a, _b;
this.log.info(`Getting operation mode for device (address ${address})`);
const metaData = this.getDeviceMetaData(address, scanDuration);
const isSwitchMode = (_b = (_a = metaData === null || metaData === void 0 ? void 0 : metaData.serviceData) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : true;
return isSwitchMode ? 'switch' : 'press';
};
this.getDeviceBatteryStatus = (address, scanDuration) => {
var _a, _b;
this.log.info(`Getting Battery level for device (address ${address})`);
const metaData = this.getDeviceMetaData(address, scanDuration);
return (_b = (_a = metaData === null || metaData === void 0 ? void 0 : metaData.serviceData) === null || _a === void 0 ? void 0 : _a.battery) !== null && _b !== void 0 ? _b : settings_1.DEFAULT_BATTERY_LEVEL;
};
this.getDeviceMetaData = (address, scanDuration) => {
const metaData = this.getMetadataFromCache(address);
if (!metaData && !this.isScanningForMetadata) {
this.log.info(`No metadata was found on cache for device (address ${address})`);
this.scanForDeviceMetadata(address, scanDuration);
}
return metaData;
};
this.scanForDeviceMetadata = async (address, scanDuration) => {
try {
this.log.info('Scanning for device metadata');
this.isScanningForMetadata = true;
await this.client.startScan({ model: 'H' });
await this.client.wait(scanDuration);
this.client.stopScan();
this.isScanningForMetadata = false;
this.log.info(`Finished scanning for device metadata (address ${address})`);
}
catch (e) {
(0, errorLogger_1.logSwitchbotClientError)(this.log, e);
}
};
this.handleScannedMetadata = (data) => {
const isAlreadyCached = this.metaDataCache.has(data.address);
if (isAlreadyCached) {
return;
}
this.log.info(`Found device metadata during scan. setting on cache. (address ${data.address})`);
this.metaDataCache.set(data.address, data);
};
this.getMetadataFromCache = (address) => {
const metaData = this.metaDataCache.get(address);
return metaData;
};
this.log = log;
this.client.onadvertisement = this.handleScannedMetadata;
}
}
exports.MetadataClient = MetadataClient;
//# sourceMappingURL=metaDataClient.js.map