-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
256 lines (231 loc) · 7.71 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"use strict";
/*
* Created with @iobroker/create-adapter v2.3.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 fs = require("fs");
const WebSocket = require("ws");
const adapter = utils.Adapter("signalclirestapiclient");
const needle = require("needle");
// eslint-disable-next-line no-unused-vars
const { Adapter } = require("@iobroker/adapter-core");
let ws = null;
// Load your modules here, e.g.:
class Signalclirestapiclient extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "signalclirestapiclient",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
async onReady() {
// Initialize your adapter here
this.setState("info.connection", false, true);
ws = new WebSocket("ws://"+adapter.config.serverIP+":"+adapter.config.serverPort+"/v1/receive/"+adapter.config.signalNumber);
ws.on("open", () => {
adapter.log.debug("SignalRestAPI Webscocket: Connected");
adapter.setState("info.connection", true, true);
this.sendToAPI("get","/v1/groups/"+adapter.config.signalNumber);
});
ws.on("error", () => {
adapter.log.error("SignalRestAPI Webscocket: not Connected");
adapter.setState("info.connection", false, true);
adapter.disable();
});
ws.on("message", function message(jsonData) {
const parsedJSON = JSON.parse(jsonData.toString()).envelope;
if (typeof parsedJSON.dataMessage != "undefined") {
const message = parsedJSON.dataMessage.message;
const name = parsedJSON.sourceName;
handleMsg(name, message);
}
});
function handleMsg(name, msg) {
adapter.log.debug("Neue Nachricht: "+ msg);
adapter.setState("messages.message", msg, true);
adapter.setState("messages.from", name, true);
}
}
sendToAPI (type,path, body_sent) {
const options = {
headers: {"Content-Type": "application/json"}
};
needle(type,adapter.config.serverIP+":"+adapter.config.serverPort+path, body_sent, options)
.then((response) => resp(response))
.catch(function(err){
adapter.log.error("Senden der Anfrage fehlgeschlagen: "+err);
});
const resp = function(resp) {
switch(resp.statusCode){
case 200:
case 201:
adapter.log.debug(resp.statusCode+" Anfrage erfolgreich.");
if(Array.isArray(resp.body)){
if(resp.body[0].id != "undefined"){
const groups = {};
resp.body.forEach(item => groups[item.name] = item.id);
adapter.setState("info.groups", JSON.stringify(groups),true);
adapter.log.debug(JSON.stringify(groups));
}
}
break;
case 400:
case 401:
adapter.log.error(resp.statusCode + " Fehler bei der Kommunikation mit der API");
break;
case 500:
adapter.log.error(resp.statusCode+" Interner Serverfehler");
break;
}
};
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
// clearTimeout(timeout2);
// ...
// clearInterval(interval1);
ws.close();
callback();
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * 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.info(`object ${id} changed: ${JSON.stringify(obj)}`);
// } else {
// // The object was deleted
// this.log.info(`object ${id} deleted`);
// }
// }
/**
* 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.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
onMessage(obj) {
let body_sent;
if (typeof obj === "object" && obj.message) {
switch(obj.command){
case "send":
if(typeof obj.message.attachment !== "undefined"){
body_sent = {"message": obj.message.text,
"number": adapter.config.signalNumber,
"recipients": obj.message.numbers,
"base64_attachments": [fs.readFileSync(obj.message.attachment, "base64")]};
}else{
body_sent = {"message": obj.message.text,
"number": adapter.config.signalNumber,
"recipients": obj.message.numbers};
}
this.sendToAPI("post","/v2/send",body_sent);
break;
case "addGroup":
body_sent = {"name": obj.message.name,
"members": [obj.message.member]
};
this.sendToAPI("post", "/v1/groups/"+adapter.config.signalNumber, body_sent);
break;
case "getGroups":
this.sendToAPI("get","/v1/groups/"+adapter.config.signalNumber);
break;
case "sendGroup":{
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
let body_sent;
if(typeof obj.message.attachment !== "undefined"){
body_sent = {"message": obj.message.text,
"number": adapter.config.signalNumber,
"recipients": [gid],
"base64_attachments": [fs.readFileSync(obj.message.attachment, "base64")]};
}else{
body_sent = {"message": obj.message.text,
"number": adapter.config.signalNumber,
"recipients": [gid]};
}
this.sendToAPI("post","/v2/send",body_sent);
});
}
break;
case "addAdminGroup":
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
this.sendToAPI("post", "/v1/groups/"+adapter.config.signalNumber+"/"+gid+"/admins", {"admins": [obj.message.admin]});
});
break;
case "addMemberGroup":
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
this.sendToAPI("post", "/v1/groups/"+adapter.config.signalNumber+"/"+gid+"/members", {"members": [obj.message.member]});
});
break;
case "delAdminGroup":
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
this.sendToAPI("delete", "/v1/groups/"+adapter.config.signalNumber+"/"+gid+"/admins", {"admins": [obj.message.admin]});
});
break;
case "delMemberGroup":
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
this.sendToAPI("post", "/v1/groups/"+adapter.config.signalNumber+"/"+gid+"/members", {"members": [obj.message.admin]});
});
break;
case "delGroup":
adapter.getState("info.groups", (err, state) => {
// @ts-ignore
const gid = JSON.parse(state.val)[obj.message.group];
this.sendToAPI("delete", "/v1/groups/"+adapter.config.signalNumber+"/"+gid);
});
break;
}
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Signalclirestapiclient(options);
} else {
// otherwise start the instance directly
new Signalclirestapiclient();
}