UNPKG

homebridge-soma-smartshades2

Version:
253 lines (205 loc) 7.59 kB
//var request = require("request"); let PythonShell = require('python-shell'); var schedule = require('node-schedule'); var Service, Characteristic; module.exports = function(homebridge) { console.log("homebridge API version: " + homebridge.version); Service = homebridge.hap.Service; Characteristic = homebridge.hap.Characteristic; homebridge.registerAccessory("homebridge-soma-smartshades", "SmartShades", SmartShades); }; /* DO: - počkat na aktuální polohu - poslat na target position - get last TargetPosition - wait for 10s? - set new position - integrate stop function to py - use stop function on set new position */ function SmartShades(log, config) { this.service = new Service.WindowCovering(this.name); this.log = log; this.name = config.name || "Smart Shade"; this.id = config.id || 0; this.pythonScriptPath = config.pythonScriptPath; this.pythonScriptName = config.pythonScriptName; this.apiroute = config.apiroute; this.target = config.target || 'E3:19:51:66:9F:8B'; // Required Characteristics this.waitForcurrentPosition = null; this.currentPosition = null; this.targetPosition = null; this.targetPositionTimeout = null; //Characteristic.PositionState.DECREASING = 0; //Characteristic.PositionState.INCREASING = 1; //Characteristic.PositionState.STOPPED = 2; this.positionState = Characteristic.PositionState.STOPPED; this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED); // Optional Characteristics //this.holdPosition = Characteristic.HoldPosition; //this.targetHorizontalTiltAngle = Characteristic.TargetHorizontalTiltAngle; //this.targetVerticalTiltAngle = Characteristic.TargetVerticalTiltAngle; //this.currentHorizontalTiltAngle = Characteristic.CurrentHorizontalTiltAngle; //this.currentVerticalTiltAngle = Characteristic.CurrentVerticalTiltAngle; //this.obstructionDetected = Characteristic.ObstructionDetected; var j = schedule.scheduleJob('*/5 * * * *', function(){ console.log("test schedule"); }); } SmartShades.prototype = { //Start identify: function(callback) { this.log("identifyShades"); callback(null); }, // Required getCurrentPosition: function(callback) { this.log("getting currentPosition"); this.waitForcurrentPosition = new Promise((resolve) => { var options = {}; options.args = [ '-t', this.target, '-c', 'get_position' ] options.scriptPath = this.pythonScriptPath; var pyshell = new PythonShell(this.pythonScriptName, options); pyshell.on('message', (message) => { this.log(message); if(/get_position/.test(message)) { let number = message.replace("get_position ", "").trim(); this.currentPosition = 100-number; this.targetPosition = this.currentPosition; this.log("getCurrentPosition:", this.currentPosition); //this.service.getCharacteristic(Characteristic.TargetPosition).updateValue(100); //.updateCharacteristic(Characteristic.TargetPosition, 100); //this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED); callback(null, this.currentPosition); resolve(); } }); pyshell.end((err) => { if(err) { this.log(err); } }); }); }, getName: function(callback) { this.log("getName :", this.name); var error = null; callback(error, this.name); }, getTargetPosition: function (callback) { this.log("getting targetPosition"); if(this.targetPosition == null && this.waitForcurrentPosition != null) { this.waitForcurrentPosition.then(() => { callback(null, this.targetPosition); this.log("getTargetPosition :", this.targetPosition); }) } else { callback(null, this.targetPosition); this.log("getTargetPosition :", this.targetPosition); } }, setTargetPosition: function (value, callback) { this.targetPosition = value; clearTimeout(this.targetPositionTimeout) this.targetPositionTimeout = setTimeout(() =>{ let pos = 100-this.targetPosition; this.log("setTargetPosition to %s : %s", value, pos); /*var options1 = {}; options1.args = [ '-t', 'E3:19:51:66:9F:8B', '-c', 'move_stop' ] options1.scriptPath = this.pythonScriptPath; var pyshell1 = new PythonShell(this.pythonScriptName, options1); pyshell1.on('message', (message) => { console.log(message); }); pyshell1.end((err) => { this.log("finish stop");*/ var options = {}; options.args = [ '-t', this.target, '-c', 'move_target', '-a', pos ] options.scriptPath = this.pythonScriptPath; var pyshell = new PythonShell(this.pythonScriptName, options); pyshell.on('message', (message) => { console.log(message); }); pyshell.end((err) => { if(err) { this.log(err); } //if (err) console.log(err.message); /*console.log('Success ! Results: %j'); this.currentPosition = this.targetPosition; this.service.setCharacteristic(Characteristic.CurrentPosition, this.currentPosition); this.log("currentPosition is now %s", this.currentPosition); //this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED); callback(null); // success*/ }); //}); }, 2500); this.currentPosition = this.targetPosition; this.service.setCharacteristic(Characteristic.CurrentPosition, this.currentPosition); //this.log("currentPosition is now %s", this.currentPosition); callback(null); // success /*if(this.targetPosition > this.currentPosition) { this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.INCREASING); } else if(this.targetPosition < this.currentPosition) { this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.DECREASING); } else if(this.targetPosition = this.currentPosition) { this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED); }*/ }, getPositionState: function(value, callback) { console.log(value) console.log(callback); this.log("getPositionState :", this.positionState); var error = null; callback(error, this.positionState); }, getServices: function() { // you can OPTIONALLY create an information service if you wish to override // the default values for things like serial number, model, etc. var informationService = new Service.AccessoryInformation(); informationService .setCharacteristic(Characteristic.Manufacturer, "SOMA") .setCharacteristic(Characteristic.Model, "Smart Shades") .setCharacteristic(Characteristic.SerialNumber, this.target); this.service .getCharacteristic(Characteristic.Name) .on('get', this.getName.bind(this)); //this.service.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED); // Required Characteristics this.service .getCharacteristic(Characteristic.CurrentPosition) //.removeAllListeners('get') .on('get', this.getCurrentPosition.bind(this)); this.service .getCharacteristic(Characteristic.TargetPosition) //.removeAllListeners('set') .on('get', this.getTargetPosition.bind(this)) .on('set', this.setTargetPosition.bind(this)); this.service .getCharacteristic(Characteristic.PositionState) .on('get', this.getPositionState.bind(this)); // Optional Characteristics //TODO return [informationService, this.service]; } };