-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
186 lines (172 loc) · 5.35 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
/*
* Created with @iobroker/create-adapter v1.25.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const adapterName = require('./package.json').name.split('.').pop();
const Rtl_433 = require('./lib/rtl_433.js');
const BrokerInterface = require('./lib/brokerInterface');
const AdminUtility = require('./lib/adminUtility');
const MAX_RESTART_DURATION = 10*60; // 10 minutes
const BASE_RESTART_DURATION = 20; // 20 seconds
const INC_RESTART_DURATION = 20; // 20 seconds
class rtl_433 extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'rtl_433',
});
this.brokerInterface = null;
this.server = null;
this.restartDuration = 20;
this.on('ready', this.onReady.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
this.on('message', this.onMessage.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
const rtl_433Server = () => {
const server = new Rtl_433({
config: this.config,
log: this.log
});
let connected = false;
server.on('connectionChange', (connectState) => {
this.setState('info.connection', connectState, true);
if (!connectState && !connected) {
this.log.error(`rtl_433 disconnected, reconnecting in ${this.restartDuration}s ...`);
this.timeout = setTimeout(() => {
this.server = rtl_433Server();
if (this.restartDuration < MAX_RESTART_DURATION) {
this.restartDuration += INC_RESTART_DURATION;
}
else {
this.restartDuration = MAX_RESTART_DURATION;
}
}, this.restartDuration*1000);
}
else {
this.restartDuration = BASE_RESTART_DURATION;
}
connected = connectState;
});
server.on('data', data => {
this.log.debug(`${adapterName}:${data}`);
this.brokerInterface && this.brokerInterface.handleIncomingObject(data);
});
}
this.server = rtl_433Server();
this.brokerInterface = new BrokerInterface({
adapter: this,
});
this.adminUtils = new AdminUtility({
adapter: this,
});
await this.subscribeObjectsAsync("*");
};
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.brokerInterface && this.brokerInterface.cleanUp();
this.log.info('cleaned everything up...');
this.timeout && clearTimeout(this.timeout);
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.debug(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
const regex = /^rtl_433\.\d+\.[\w-]+-\d+$/;
if (regex.test(id)) {
this.log.debug(`object ${id} deleted`);
this.brokerInterface && this.brokerInterface.getDevices();
}
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.debug(`state ${id} deleted`);
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
/**
* Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
* Using this method requires "common.messagebox" property to be set to true in io-package.json
* @param {ioBroker.Message} obj
*/
async onMessage(obj) {
const respond = (response) => {
if (obj.callback)
this.sendTo(obj.from, obj.command, response, obj.callback);
};
if (typeof obj === 'object') {
if (obj.command === 'rtl_433') {
try {
respond(this.adminUtils ? await this.adminUtils.rtl_433(obj.message) : null);
}
catch(e) {
respond(e);
}
}
if (obj.command === 'listSerial') {
try {
respond(this.adminUtils ? await this.adminUtils.listSerial() : null);
}
catch(e) {
respond(e);
}
}
if (obj.command === 'adapterVersion') {
try {
respond(this.adminUtils ? this.adminUtils.adapterVersion() : null);
}
catch(e) {
respond(e);
}
}
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new rtl_433(options);
} else {
// otherwise start the instance directly
new rtl_433();
}