@chrispyduck/homie-sensors
Version:
Sensor library intended for ues with homie-device
65 lines • 2.86 kB
JavaScript
;
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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.I2CDevice = void 0;
const winston = __importStar(require("winston"));
const lodash_1 = require("lodash");
const AHT20_1 = __importDefault(require("./AHT20"));
const events_1 = require("events");
const os_1 = __importDefault(require("os"));
class I2CDevice extends events_1.EventEmitter {
constructor(deviceType, configuration) {
super();
this.init = async () => {
if (os_1.default.platform() != "linux" || os_1.default.arch() != "arm") {
throw new Error("i2c is only supported on linux/arm");
}
this.logger.verbose(`Opening I2C bus #${this.configuration.busNumber}`);
const i2c_module = require("i2c-bus");
this.bus$ = await i2c_module.openPromisified(this.configuration.busNumber);
this.onInit();
};
this.delay = (ms) => new Promise(res => setTimeout(res, ms));
this.sendCommand = async (cmd) => {
var _a, _b;
if (cmd.args)
await ((_a = this.bus) === null || _a === void 0 ? void 0 : _a.writeI2cBlock(this.configuration.deviceId, cmd.id, cmd.args.length, cmd.args));
else
await ((_b = this.bus) === null || _b === void 0 ? void 0 : _b.sendByte(this.configuration.deviceId, cmd.id));
await this.delay(cmd.delay);
};
this.configuration = lodash_1.merge({}, AHT20_1.default.DefaultConfiguration, configuration);
this.logger = winston.child({
type: deviceType,
name: "default",
});
}
get bus() {
if (!this.bus$)
throw new Error("Bus has not been opened. Did you forget to call init()?");
return this.bus$;
}
}
exports.I2CDevice = I2CDevice;
//# sourceMappingURL=I2CDevice.js.map