-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaincopy.js
184 lines (151 loc) · 6.18 KB
/
maincopy.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
'use strict';
/*
* Created with @iobroker/create-adapter v1.17.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');
// Load your modules here, e.g.:
// const fs = require("fs");
/**
* The adapter instance
* @type {ioBroker.Adapter}
*/
let adapter;
/**
* Starts the adapter instance
* @param {Partial<ioBroker.AdapterOptions>} [options]
*/
init();
function startAdapter(options) {
// Create the adapter and define its methods
return adapter = utils.adapter(Object.assign({}, options, {
name: 'dali',
// The ready callback is called when databases are connected and adapter received configuration.
// start here!
ready: main, // Main method defined below for readability
// is called when adapter shuts down - callback has to be called under any circumstances!
unload: (callback) => {
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
},
// is called if a subscribed object changes
objectChange: (id, obj) => {
if (obj) {
// The object was changed
adapter.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
adapter.log.info(`object ${id} deleted`);
}
},
// is called if a subscribed state changes
stateChange: (id, state) => {
if (state) {
// The state was changed
adapter.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
adapter.log.info(`state ${id} deleted`);
}
},
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
// requires "common.message" property to be set to true in io-package.json
// message: (obj) => {
// if (typeof obj === 'object' && obj.message) {
// if (obj.command === 'send') {
// // e.g. send email or pushover or whatever
// adapter.log.info('send command');
// // Send response in callback if required
// if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
// }
// }
// },
}));
}
function main() {
// Reset the connection indicator during startup
//this.setState('info.connection', false, true);
this.setState('info.connection', false, true);
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config:
adapter.log.info('config option2: ');
this.log.info('config option1: ' + this.config.host);
adapter.log.info('config option2: ' + adapter.config.port);
/*
For every state in the system there has to be also an object of type state
Here a simple template for a boolean variable named "testVariable"
Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
*/
adapter.setObject('testVariable', {
type: 'state',
common: {
name: 'testVariable',
type: 'boolean',
role: 'indicator',
read: true,
write: true,
},
native: {},
});
// in this template all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
/*
setState examples
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
adapter.setState('testVariable', true);
// same thing, but the value is flagged "ack"
// ack should be always set to true if the value is received from or acknowledged from the target system
adapter.setState('testVariable', { val: true, ack: true });
// same thing, but the state is deleted after 30s (getState will return null afterwards)
adapter.setState('testVariable', { val: true, ack: true, expire: 30 });
// examples for the checkPassword/checkGroup functions
adapter.checkPassword('admin', 'iobroker', (res) => {
adapter.log.info('check user admin pw ioboker: ' + res);
});
adapter.checkGroup('admin', 'admin', (res) => {
adapter.log.info('check group user admin group admin: ' + res);
});
//sendMessage(adapter.config.host, adapter.config.port)
/*
function sendMessage(host, port) {
var hex_data = ["0x00", "0x00", "0x00", "0x00", "0x00", "0x17", "0x01", "0x17", "0x00", "0x65", "0x00", "0x05", "0x00", "0x64", "0x00", "0x06", "0x0c", "0x12", "0x00", "0x00", "0x03", "0x00", "0x00", "0x01", "0xa0", "0x00", "0x00", "0x00", "0x00"]
var data = Buffer.from(hex_data);
var client = net.connect({host: host, port: port}, function () {
log('connected to server!');
client.write(data);
});
client.on('data', function (data) {
log("Response ")
client.end();
this.setState('info.connection', {val: true, ack: true});
//return callback(true);
});
client.on('end', function () {
log('disconnected from server');
});
client.on('error', function (error) {
console.error('error: ' + error);
client.end();
this.setState('info.connection', {val: false, ack: true});
//return callback(true);
});
}
*/
}
function init() {
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export startAdapter in compact mode
module.exports = startAdapter;
} else {
// otherwise start the instance directly
startAdapter();
}
}