UNPKG

homebridge-cct-dim-motor

Version:

Plugin for controlling Zigbee CCT DIM light and motor lift in HomeKit.

121 lines (93 loc) 3.9 kB
const mqttClient = require('./mqtt'); // Import MQTT logic // Khởi tạo phụ kiện function CCTDimMotorAccessory(log, config) { this.log = log; this.name = config.name || 'CCTDimMotor'; this.topics = config.topics; // Kết nối MQTT this.client = mqttClient(config.mqttBroker, this.log); this.subscribeToTopics(); } // Đăng ký dịch vụ (Service) CCTDimMotorAccessory.prototype.getServices = function () { const services = []; // Đèn CCT DIM const lightService = new Service.Lightbulb(this.name + ' Light'); lightService .getCharacteristic(Characteristic.On) .on('set', (value, callback) => this.setLightOn(value, callback)); lightService .addCharacteristic(new Characteristic.Brightness()) .on('set', (value, callback) => this.setBrightness(value, callback)); lightService .addCharacteristic(new Characteristic.ColorTemperature()) .on('set', (value, callback) => this.setColorTemperature(value, callback)); services.push(lightService); // Motor nâng hạ const motorService = new Service.WindowCovering(this.name + ' Motor'); motorService .getCharacteristic(Characteristic.TargetPosition) .on('set', (value, callback) => this.setMotorTargetPosition(value, callback)); motorService .getCharacteristic(Characteristic.CurrentPosition) .on('get', (callback) => this.getMotorCurrentPosition(callback)); motorService .getCharacteristic(Characteristic.PositionState) .on('get', (callback) => this.getMotorPositionState(callback)); services.push(motorService); return services; }; // Hàm điều khiển CCTDimMotorAccessory.prototype.setLightOn = function (value, callback) { this.client.publish(this.topics.setLightOn, value ? 'ON' : 'OFF'); callback(); }; CCTDimMotorAccessory.prototype.setBrightness = function (value, callback) { this.client.publish(this.topics.setBrightness, String(value)); callback(); }; CCTDimMotorAccessory.prototype.setColorTemperature = function (value, callback) { this.client.publish(this.topics.setColorTemperature, String(value)); callback(); }; CCTDimMotorAccessory.prototype.setMotorTargetPosition = function (value, callback) { this.client.publish(this.topics.setMotorTargetPosition, String(value)); callback(); }; CCTDimMotorAccessory.prototype.getMotorCurrentPosition = function (callback) { this.client.publish(this.topics.getMotorCurrentPosition, '', () => { callback(null, 50); // Giá trị mặc định (ví dụ: 50%). }); }; CCTDimMotorAccessory.prototype.getMotorPositionState = function (callback) { // Trạng thái nâng/hạ: Đang di chuyển, đứng yên callback(null, 2); // 2 = Đứng yên }; CCTDimMotorAccessory.prototype.subscribeToTopics = function () { Object.values(this.topics).forEach((topic) => { this.client.subscribe(topic, (err) => { if (err) { this.log(`Failed to subscribe to topic: ${topic}`); } else { this.log(`Subscribed to topic: ${topic}`); } }); }); }; CCTDimMotorAccessory.prototype.getServices = function () { const services = []; // Định nghĩa dịch vụ Lightbulb (bóng đèn) const lightService = new Service.Lightbulb(this.name + ' Light'); lightService .getCharacteristic(Characteristic.On) .on('set', (value, callback) => this.setLightOn(value, callback)); services.push(lightService); // Định nghĩa dịch vụ WindowCovering (motor nâng hạ) const motorService = new Service.WindowCovering(this.name + ' Motor'); motorService .getCharacteristic(Characteristic.TargetPosition) .on('set', (value, callback) => this.setMotorTargetPosition(value, callback)); services.push(motorService); return services; }; module.exports = CCTDimMotorAccessory;