homebridge-luoshengfan 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +248 -0
  2. package/package.json +14 -0
package/index.js ADDED
@@ -0,0 +1,248 @@
1
+ var Service, Characteristic, Homebridge,recv_Value,Log
2
+ recv_Value=0;
3
+ var statePower=0; // init state, default is OFF
4
+ var statePowerTemp=0; // init state, default is OFF
5
+ var stateSpeed=58; // init speed, default is 2
6
+ var stateSpeedTemp=56; // init speed, default is 2
7
+ var lastSpeed=0;
8
+ const Net = require('net')
9
+ module.exports = function(homebridge)
10
+ {
11
+ Homebridge = homebridge;
12
+ Service = homebridge.hap.Service;
13
+ Characteristic = homebridge.hap.Characteristic;
14
+ homebridge.registerAccessory("homebridge-luoshengfan","luoshengfan",luosheng);
15
+
16
+ function luosheng(log, config)
17
+ {
18
+ this.log = log;
19
+ this.name = config["name"];
20
+ this.area = config["area"];
21
+ this.channel = config["channel"];
22
+ this.statePower = 0; // init state, default is OFF
23
+ this.stateSpeed = 0; // init speed, default is 0
24
+ this.log("luosheng_Fan");
25
+
26
+ this.ip = {
27
+ 'host': config.ip || '127.0.0.1',
28
+ 'port': config.port || 502
29
+ }
30
+ this.socket = new Net.Socket();
31
+ // Connect
32
+
33
+ //this.socket.connect(this.ip);
34
+ //this.socket = Net.createConnection({
35
+ // "port": config.port,
36
+ //"host": config.ip,
37
+ //"noDelay": true,
38
+ // "keepAlive": true
39
+ //});
40
+
41
+ Homebridge.on("didFinishLaunching", () => {
42
+ this.socket.connect(this.ip);
43
+ this.log("Connecting to", this.ip.host);
44
+ });
45
+ this.socket.on('connect', () => {
46
+ log("Socket connected");
47
+ });
48
+
49
+ this.socket.on('error', (error) => {
50
+ if (error) {
51
+ log(error);
52
+ }
53
+ log("error-Connection error");
54
+
55
+ setTimeout(() => {
56
+ //this.socket = Net.createConnection({
57
+ // "port": config.port,
58
+ // "host": config.ip,
59
+ // "noDelay": true,
60
+ // "keepAlive": true
61
+ //});
62
+ //this.socket.connect(this.ip);���dz���
63
+ log("error-Reconnecting to",);
64
+ }, 10000);
65
+ });
66
+
67
+ this.socket.on('data', (data) => {
68
+
69
+ var temp_value = data[0]
70
+ if (temp_value & 0x80 ) {
71
+
72
+ temp_value =temp_value &0x7f;
73
+ if (temp_value <= 100 && temp_value >= 0){
74
+ lastSpeed =recv_Value;
75
+ recv_Value =temp_value;
76
+
77
+ this.stateSpeed = recv_Value;
78
+ this.log("socket-data:%s", recv_Value);
79
+ this.virtualService.getCharacteristic(Characteristic.RotationSpeed).updateValue( this.stateSpeed );
80
+ if (this.stateSpeed ==0)
81
+ {
82
+ this.virtualService.getCharacteristic(Characteristic.On).updateValue(0);
83
+ }
84
+ else{
85
+ this.virtualService.getCharacteristic(Characteristic.On).updateValue(1);
86
+ }
87
+ }
88
+
89
+ }
90
+ });
91
+
92
+ this.socket.on('close', (error) => {
93
+ if (error) {
94
+ this.log(error);
95
+ }
96
+ this.log("Connection lost");
97
+ this.flagscontect = false;
98
+
99
+ setTimeout(() => {
100
+ //this.socket = Net.createConnection({
101
+ // "port": config.port,
102
+ // "host": config.ip,
103
+ // "noDelay": true,
104
+ // "keepAlive": true
105
+ //});
106
+ this.socket.connect(this.ip);
107
+ this.log("Reconnecting to", this.ip.host);
108
+ }, 9000);
109
+ });
110
+ }
111
+
112
+ Virtual.prototype.getServices = function()
113
+ {
114
+ this.virtualService = new Service.Fan();
115
+ //this.virtualService.getCharacteristic(Characteristic.Active)
116
+ this.virtualService.getCharacteristic(Characteristic.On)
117
+
118
+ .on('get', this.getOn.bind(this))
119
+ .on('set', this.setOn.bind(this));
120
+
121
+ this.virtualService.getCharacteristic(Characteristic.RotationSpeed)
122
+ .setProps({minValue: 0, maxValue: 100, minStep: 1,})
123
+ .on('get', this.getSpeed.bind(this))
124
+ .on('set', this.setSpeed.bind(this));
125
+
126
+ this.virtualInfoService = new Service.AccessoryInformation();
127
+
128
+ this.virtualInfoService
129
+ .setCharacteristic(Characteristic.Manufacturer, "luosheng")
130
+ .setCharacteristic(Characteristic.Model, "fan")
131
+ .setCharacteristic(Characteristic.SerialNumber, "0");
132
+
133
+ return [this.virtualService, this.virtualInfoService];
134
+ }
135
+
136
+ /************************************************************/
137
+ Virtual.prototype.getOn = function(callback)
138
+ {
139
+ //var power = this.statePower > 0;
140
+ // this.log("/ status / fan power / state=%s / area=%s / %s", this.statePower, this.area, this.name);
141
+ //const currentValue = this.Characteristic.Active.INACTIVE;
142
+
143
+ // return currentValue;
144
+ if (recv_Value== 0)
145
+ {
146
+
147
+ callback(null,0);
148
+ }
149
+ else{
150
+ callback(null,1);
151
+ }
152
+
153
+ // let isFanOn = false;
154
+ // if (this.fanDevice && this.fanDevice.isFanConnected()) {
155
+ // isFanOn = this.fanDevice.isPowerOn();
156
+ // }
157
+ //callback(null, isFanOn ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE);
158
+ }
159
+
160
+ Virtual.prototype.setOn = function(value1, callback)
161
+ {
162
+ this.statePowerTemp = value1;
163
+ //this.log("/debug s$ton/ %s / %s / %s", this.statePowerTemp, this.area, this.name);
164
+ switch(this.statePowerTemp)
165
+ {
166
+ //when number is said, comes here then goes to set speed. We want it to
167
+ case true:
168
+ if(this.statePower == false)
169
+ {
170
+ this.statePower = value1;
171
+ this.log("/ set / fan power / fan=power:on:%s / %s", this.area, this.name);
172
+ // this.stateSpeed = 100;
173
+ //lets set to what it was on before
174
+ }
175
+ this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name);
176
+ if(this.stateSpeed == 0)
177
+ {
178
+ if (lastSpeed < 48)
179
+ {
180
+ lastSpeed = 48;
181
+ }
182
+ arr = [ lastSpeed + 0x1c];
183
+ }
184
+ else {
185
+ arr = [ this.stateSpeed + 0x1c];
186
+ }
187
+ this.socket.write(new Uint8Array(arr));
188
+ this.log("/ set / fan speed / fan=speed:%s", this.stateSpeed)
189
+ callback(null);
190
+ this.virtualService.getCharacteristic(Characteristic.RotationSpeed).updateValue( lastSpeed);
191
+ break;
192
+
193
+ case false:
194
+ if(this.statePower == true)
195
+ {
196
+ this.statePower = value1;
197
+ this.log("/ set / fan power / fan=power:off:%s / %s", this.area, this.name);
198
+ //this.stateSpeed = 0;
199
+ }
200
+ this.log("/ set / fan speed / fan=speed:0:%s / %s", this.area, this.name);
201
+ arr = [ 0x1c];
202
+ this.socket.write(new Uint8Array(arr));
203
+
204
+ callback(null);
205
+ break;
206
+
207
+ //default:
208
+ // this.log("/ debug / error / statePower error");
209
+ // this.stateSpeed = 0;
210
+ // this.statePower = false;
211
+ // arr = [ 30 + 0x1c];
212
+ // this.socket.write(new Uint8Array(arr));
213
+ // callback(null);
214
+ // break;
215
+ }
216
+
217
+
218
+ }
219
+
220
+ /************************************************************/
221
+
222
+ Virtual.prototype.getSpeed = function(callback)
223
+ {
224
+ var speed = this.stateSpeed;
225
+ this.stateSpeed = recv_Value;
226
+ this.log("/ status / fan speed / state=%s / area=%s / %s", this.stateSpeed, this.area, this.name);
227
+ callback(null, this.stateSpeed);
228
+ //if (this.stateSpeed == 0)
229
+ // {
230
+
231
+ // this.log("masm1111111");
232
+ // this.virtualService.getCharacteristic(Characteristic.On).updateValue(0);
233
+
234
+ // }
235
+ this.virtualService.getCharacteristic(Characteristic.RotationSpeed).updateValue( this.stateSpeed );
236
+ }
237
+
238
+ Virtual.prototype.setSpeed = function(value2, callback)
239
+ {
240
+ this.stateSpeedTemp = value2;
241
+ arr = [ value2 + 0x1c];
242
+ this.socket.write(new Uint8Array(arr));
243
+ this.log("/debug s$tspeed/ %s / %s / %s", this.stateSpeedTemp, this.area, this.name);
244
+ callback(null);
245
+
246
+
247
+ }
248
+ /************************************************************/
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "homebridge-luoshengfan",
3
+ "version": "1.0.0",
4
+ "description": "change ac hz",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "fan"
11
+ ],
12
+ "author": "",
13
+ "license": "MIT"
14
+ }