generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (107 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const net = require('net');
const { Accessory, uuid, Service: HAPService, Characteristic: HAPCharacteristic } = require('homebridge');
module.exports = (api) => {
api.registerAccessory('homebridge-sr201netrelay', 'SR201NetRelay', SR201NetRelayAccessory);
}
class SR201NetRelayAccessory {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.index = parseInt(this.config.index);
if ( (this.index < 1) || this.index > 8 )
{
log('index parameter must be within [1;8], value is: ' + this.index);
return;
}
this.address = this.config.address;
this.port = 6722;
if (this.config.pulse == false)
{
this.service = new HAPService.StatelessProgrammableSwitch(this.config.name);
}
else
{
this.service = new HAPService.Switch(this.config.name);
}
this.client = new net.Socket();
this.client.on('data', this.handleData.bind(this));
this.client.on('close', this.handleClose.bind(this));
this.client.on('error', this.handleError.bind(this));
this.connect();
}
getServices()
{
/* Create a new information service. This just tells HomeKit about our accessory. */
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'No idea')
.setCharacteristic(Characteristic.Model, 'SR201NetRelay')
.setCharacteristic(Characteristic.SerialNumber, 'Unspecified');
if (this.config.pulse == false)
{
this.service.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.on('set', this.handleSwitchEvent.bind(this));
}
else
{
this.service.getCharacteristic(Characteristic.On)
.on('get', this.handleSwitchEvent.bind(this))
.on('set', this.handleSwitchEvent.bind(this))
}
/* Return both the main service (this.service) and the informationService */
return [informationService, this.service]
}
connect() {
this.client.connect(this.port, this.host, () => {
this.log('TCP connection established.');
});
}
handleData(data) {
// Handle the received data here
this.log('Received data:', data.toString());
}
handleClose() {
this.log('TCP connection closed. Reconnecting...');
this.connect();
}
handleError(error) {
this.log.error('TCP connection error:', error);
if (error.code === 'ETIMEDOUT' || error.code === 'ECONNRESET') {
this.log('TCP connection timeout or reset. Reconnecting...');
this.connect();
}
}
handleSwitchEvent(value, callback) {
var message;
if (config.pulse == true)
{
if (value === Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS) {
message = '1${index}*';
this.sendUDPMessage(message);
}
}
else
{
if (value == true)
{
message = '1${index}';
}
else
{
message = '2${index}';
}
this.sendUDPMessage(message);
}
callback(null);
}
sendTCPMessage(message) {
if (this.client.writable) {
this.client.write(message);
} else {
this.log('TCP client is not writable. Reconnecting...');
this.connect();
}
}
getServices() {
return [this.accessory];
}
}