-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
269 lines (219 loc) · 9.56 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
257
258
259
260
261
262
263
264
265
266
267
268
269
"use strict";
/*
* Created with @iobroker/create-adapter v2.0.1
*/
/*
Ideas for the future:
text2command integration
group list attribute for every room to send multicast message to multiple rooms belonging to one group
smarthome/iobroker user gets a set of "botfather" functions as an alternative (password protected) way to configure this adapter - to create/organize new rooms/groups
Add filter to allow only nctalk group and/or 1to1 conversation types to be Used
New 1to1/group conversation will automatically be joined (ListenModeActive=true) either password protected or after request sent to the nextcloud talk admin was approved
Use iobroker state ack for queue handling in case of message bursts and not to miss any message
Create devices per group or chat room with own rxmessage and SendMessage state
Support for a jsonobject in rx/txmessage states
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
//process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
//const NextcloudTalk = require("./src/nctalkclient"); // For development - have nctalkclient sources locally
const NextcloudTalk = require("nctalkclient");
const iobTalk = require("./lib/iobtalk");
const webdavsupport = require("./lib/webdavsupport");
// Load your modules here, e.g.:
// const fs = require("fs");
// let debugadapter = undefined;
// global.ErrorLog = function ErrorLog(e) {
// if (typeof e === "object" && e !== null) {
// debugadapter.log.error("Error Event" + JSON.stringify(e));
// } else {
// debugadapter.log.error("Error Event " + e);
// }
// };
// global.DebugLog = function DebugLog(e) {
// if (typeof e === "object" && e !== null) {
// debugadapter.log.info("Debug Event" + JSON.stringify(e));
// } else {
// debugadapter.log.info("Debug Event " + e);
// }
// };
class Nctalk extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "nctalk",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.bind(this));
this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
this.grouplist = [];
// debugadapter = this;
}
_GroupListAdd(cfgstring, token) {
const gs = cfgstring.split(",");
gs.forEach(gselement => {
const index = this.grouplist.findIndex((glelement) => { return glelement.name == gselement; });
if (index != -1) {
console.log("Add element to group");
this.grouplist[index].tokenlist.push(token);
}
else {
console.log("Create new group with first token");
this.grouplist.push({ name: gselement, iobTalk: gselement, tokenlist: [token] });
}
});
}
CreateGroupList(AdminRooms) {
AdminRooms.forEach(element => {
console.log(element.groups);
if (element.groups)
this._GroupListAdd(element.groups, element.token);
});
this.grouplist.forEach(element => {
element.iobTalk = new iobTalk(this, element.name, element.tokenlist);
});
//this.grouplist[element].iobTalk = new iobTalk(this, element, null);
//this.grouplist
console.log(this.grouplist);
}
SetRoomsListenMode(AdminRooms) {
AdminRooms.forEach(element => {
if (element.active) {
this.Talk.RoomListenMode(element.token, true);
}
});
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
//this.createState;
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
// {"host":"nextcloud.domain.de","protocol":"https","port":443,"user":"user","password":"password",
// "AdminRooms": [{ "name": "room1", "token": "123", "type": "4", "groups": "glb,group1", "text2command": "", "active": false },
// { "name": "room2", "token": "234", "type": "2", "groups": "glb,group2", "text2command": "", "active": false },
// { "name": "room3", "token": "345", "type": "1", "groups": "glb,group1,group2", "text2command": "", "active": false }] }
this.Talk = new NextcloudTalk({
server: this.config.host,
user: this.config.user,
pass: this.config.password,
port: this.config.port,
debug: this.config.debuglog
});
this.log.info(`Create nctalkclient - Version: ${this.Talk.GetVersion()}`);
this.webdavsupport = new webdavsupport(this, this.config);
this.webdavsupport.CreateUploadPathIfnotExist();
this.Talk.start(500);
// Talk client is ready and has fetched all required information / data to handle conversations
this.Talk.on("Ready", (listofrooms) => {
this.log.info("Talk client is ready and has fetched capabilities and room information");
// console.log("Talk is ready make " + listofrooms[2].token + " active");
// this.Talk.SendMessage(listofrooms[2].token, "OnReady Test Nachricht");
this.CreateGroupList(this.config.AdminRooms);
this.SetRoomsListenMode(this.config.AdminRooms);
});
// Error
this.Talk.on("Error", (e) => {
if (typeof e === "object" && e !== null) {
this.log.error("Error Event" + JSON.stringify(e));
} else {
this.log.error("Error Event " + e);
}
});
// Debug
this.Talk.on("Debug", (e) => {
if (typeof e === "object" && e !== null) {
this.log.info("Debug Event" + JSON.stringify(e));
} else {
this.log.info("Debug Event " + e);
}
});
// this.webdavsupport.UploadFileFromURL("nctalk.jpg","https://raw.githubusercontent.com/jjqoie/iobroker.nctalk/main/img/nctalk-Push.jpg");
// this.webdavsupport.UploadFileFromURL("tNpm2qxWkZ9rAXo.jpg","http://openmediavault/s/tNpm2qxWkZ9rAXo/preview");
// INFO: States and subscribes are made with each group instance of iobTalk in CreateGroupList
}
/**
* 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);
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) {
const as = id.split(".");
const index = this.grouplist.findIndex((element) => { return element.name == as[2]; });
if (index != -1) {
// Group was found
this.grouplist[index].iobTalk.onStateChange(id, state);
}
}
// 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
*/
onMessage(obj) {
if (typeof obj === "object" && obj.message) {
if (obj.command === "send") {
// e.g. send email or pushover or whatever
this.log.info("my adapter send command");
// Send response in callback if required
if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
} else if (obj.command === "rooms") {
// e.g. send email or pushover or whatever
this.log.info("my adapter rooms command");
// Send response in callback if required
if (obj.callback) this.sendTo(obj.from, obj.command, JSON.stringify(this.Talk.rooms.getlistofrooms()), obj.callback);
}
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Nctalk(options);
} else {
// otherwise start the instance directly
new Nctalk();
}