-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
924 lines (784 loc) · 30.5 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
'use strict';
/*
* Created with @iobroker/create-adapter v2.0.1
*/
// 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 serialportgsm = require('serialport-gsm');
// Load your modules here, e.g.:
// const fs = require("fs");
const gsmModem = serialportgsm.Modem();
//
//
let port;
//var connectionMode;
//var pollinginterval;
//let polling;
let autoDeleteOnReceive = false;
let enableConcatenation = true;
let incomingCallIndication = false;
let incomingSMSIndication = true;
let pin = '';
let customInitCommand = '';
let cnmiModemOpen = '';
let cnmiModemClosed = '';
let getSignalQuality = false;
let pollinginterval = 30;
let baudRate = 19200;
let dataBits = 8;
let stopBits = 1;
let parity = 'none';
let rtscts = false;
let xon = false;
let xoff = false;
let xany = false;
let options;
const ownPhone = {
name: '',
number: ''
};
let opMode = 'PDU';
let messageRawJson = {
recipient: '',
message: '',
alert: false
};
let gettingSignals;
/*
var reinitialize = {
"indicator": false,
"id": '',
"state": ''
};
*/
//var highWaterMark = 16384; Prüfen ob als 'option'nötig: The size of the read and write buffers defaults to 16k. |
class Gsmsms extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'gsmsms',
});
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.on('log', this.onLog.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onMessage(obj) {
try {
if (obj.command === 'sendNotification') {
await this.processNotification(obj);
} else {
this.log.debug('Recieved Message, data: ' + JSON.stringify(obj));
const messageToSend = {
recipient: 'Number',
message: 'Yourtext',
alert: 'false'
};
messageToSend.recipient = obj.message.recipient;
messageToSend.message = obj.message.text;
messageToSend.alert = obj.message.alert;
await this.sending(messageToSend);
}
} catch (e) {
this.log.warn('Error onMessage' + e);
}
//Recieved Message, data: {"command":"send","message":{"text":"Testsms","number":"0798233954"},"from":"system.adapter.javascript.0","_id":70000864}
} //end onMessage
/*async onLog(logObject) {
// Here we have the log in "logObject" and can handle it accordingly.
// LogObject: {from:'testlog.0', message: 'testlog.0 (12504) adapter disabled', severity: 'error', ts:1585413238439}
if (logObject.severity == 'debug' && logObject.from == 'gsmsms.' + this.instance && logObject.message.includes('RSSI') == true) {
//this.log.info("RSSI - Message:" + logObject.message);
var sigQual = logObject.message.slice(-2);
this.log.info("RSSI - NEW:" + sigQual);
this.setState('info.signalQuality', parseInt(sigQual), true);
}
//const severity = logObject.severity; // the log level (severity): info, warn, error, etc.
// ....
} // end onLog
*/
async onReady() {
try {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
port = this.config.port;
//connectionMode = this.config.connectionMode;
//pollinginterval = this.config.pollinginterval * 60000;
autoDeleteOnReceive = this.config.autoDeleteOnReceive;
enableConcatenation = this.config.enableConcatenation;
incomingCallIndication = this.config.incomingCallIndication;
incomingSMSIndication = this.config.incomingSMSIndication;
pin = this.config.pin;
customInitCommand = this.config.customInitCommand;
cnmiModemOpen = 'AT+CNMI=' + this.config.cnmiModemOpen;
cnmiModemClosed = 'AT+CNMI=' + this.config.cnmiModemClosed;
getSignalQuality = this.config.getSignalQuality;
pollinginterval = this.config.pollinginterval * 1000;
baudRate = this.config.baudRate;
dataBits = this.config.dataBits;
stopBits = this.config.stopBits;
parity = this.config.parity;
rtscts = this.config.rtscts;
xon = this.config.xon;
xoff = this.config.xoff;
xany = this.config.xany;
this.log.debug('port' + port);
//this.log.debug('connectionMode: ' + connectionMode);
//this.log.debug('pollinginterval: ' + pollinginterval + " min");
this.log.debug('autoDeleteOnReceive: ' + autoDeleteOnReceive);
this.log.debug('enableConcatenation: ' + enableConcatenation);
this.log.debug('incomingCallIndication: ' + incomingCallIndication);
this.log.debug('incomingSMSIndication: ' + incomingSMSIndication);
this.log.debug('pin: ' + pin);
this.log.debug('customInitCommand: ' + customInitCommand);
this.log.debug('cnmiModemOpen: ' + cnmiModemOpen);
this.log.debug('cnmiModemClosed: ' + cnmiModemClosed);
this.log.debug('getSignalQuality: ' + getSignalQuality);
this.log.debug('pollinginterval: ' + pollinginterval);
this.log.debug('baudRate: ' + baudRate);
this.log.debug('dataBits: ' + dataBits);
this.log.debug('stopBits: ' + stopBits);
this.log.debug('parity: ' + parity);
this.log.debug('rtscts: ' + rtscts);
this.log.debug('xon: ' + xon);
this.log.debug('xoff: ' + xoff);
this.log.debug('xany: ' + xany);
this.subscribeStates('admin.*');
this.subscribeStates('sendSMS.send');
this.subscribeStates('sendSMS.messageRaw');
options = {
baudRate: baudRate,
dataBits: dataBits,
parity: parity,
stopBits: stopBits,
rtscts: rtscts,
xon: xon,
xoff: xoff,
xany: xany,
autoDeleteOnReceive: autoDeleteOnReceive,
enableConcatenation: enableConcatenation,
incomingCallIndication: incomingCallIndication,
incomingSMSIndication: incomingSMSIndication,
pin: pin,
customInitCommand: customInitCommand,
logger: this.log,
cnmiCommand: cnmiModemOpen
};
await this.modemInitialize();
/*
if (connectionMode == 'polling') {
polling = setInterval(() => { // poll states every [10] minute
this.log.debug("Initialize modem to check new messages")
this.modemInitialize(this.startup.bind(this));
}, pollingTime);
} // endIf
*/
} catch (e) {
this.log.warn('Error onReady' + e);
}
} //end onReady
async modemInitialize() {
gsmModem.on('open', () => {
this.log.debug('GSM-Modem successfully opened');
// now we initialize the GSM Modem
gsmModem.initializeModem((msg, err) => {
if (err) {
this.log.warn(`Error initializing GSM-modem - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
} else {
this.log.debug(`Modem initialized: ${JSON.stringify(msg)}`);
if (msg.status === 'success') {
this.log.info(JSON.stringify(msg.data));
this.setState('info.connection', true, true);
this.startup();
} else {
this.log.warn(`Error initializing GSM-modem - pls check: ${msg}`);
this.setState('info.error', JSON.stringify(msg), true);
}
}
});
});
gsmModem.on('onNewMessageIndicator', datanmi => {
//indicator for new message only (sender, timeSent)
this.log.debug(`Event New Message Indication: ` + JSON.stringify(datanmi));
});
gsmModem.on('onNewMessage', datanm => {
//whole message data
this.log.info(`Event New Message: ` + JSON.stringify(datanm));
this.log.debug('sender: ' + datanm[0]['sender']);
this.log.debug('text: ' + datanm[0]['message']);
this.setState('inbox.messageSender', '+' + datanm[0]['sender'], true);
this.setState('inbox.messageText', datanm[0]['message'], true);
this.setState('inbox.messageDate', (datanm[0]['dateTimeSent']).toString(), true);
this.setState('inbox.messageRaw', JSON.stringify(datanm[0]), true);
});
gsmModem.on('onSendingMessage', data => {
//whole message data
this.log.info(`Event Sending Message: ` + JSON.stringify(data));
});
///incomingCall auf false als standard!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/*
gsmModem.on('onNewIncomingCall', data => {
//whole message data
this.log.debug(`Event Incoming Call: ` + JSON.stringify(data));
});
*/
//löschen bei Empfang auf standard !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
gsmModem.on('onMemoryFull', data => {
//whole message data
this.log.debug(`Event Memory Full: ` + JSON.stringify(data));
});
gsmModem.on('close', data => {
//whole message data
this.log.info(`Event Close: ` + JSON.stringify(data));
try {
this.setState('info.connection', false, true);
} catch (err) {
// ignore
}
});
try {
//Fehler: Wenn funktion erfolgreich aber rückmeldung ein 'ERROR' in 'result', dann auffangen und auf connection.error setzen.
// this.setStateAsync('connection.error', err, true);
const msg = await gsmModem.open(port, options);
this.log.debug(`Modem msg: ${JSON.stringify(msg)}`);
if (msg.status === 'success') {
this.log.info('Modem opened successfully, Port: ' + JSON.stringify(msg.data.modem) + ' Status: ' + JSON.stringify(msg.data.status));
} else {
this.log.warn(`Problems opening, please check: ${JSON.stringify(msg)} `);
this.setState('info.error', JSON.stringify(msg), true);
}
} catch (err) {
this.log.warn(`Error opening - ${JSON.stringify(err)} - pls check your settings (port, serial connection) and restart the instance`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end modemInitialize()
async startup() {
try {
const opModeState = await this.getStateAsync('admin.opMode');
if (opModeState && (opModeState.val === 'PDU' || opModeState.val === 'SMS')) {
opMode = opModeState.val;
}
this.log.debug(`Configuring Modem for mode: ${opMode}`);
// set mode to PDU mode to handle SMS
let msg;
try {
msg = await gsmModem.setModemMode(undefined, opMode);
} catch (err) {
this.log.warn(`Error setting GSM-Modem mode - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
return;
}
this.log.debug(`Set operation mode: ${JSON.stringify(msg)}`);
if (msg.status === 'success') {
const returnedOpMode = msg.data.slice(0, 3);
if (returnedOpMode !== opMode) {
this.log.info('Operating mode changed to: ' + returnedOpMode);
}
opMode = returnedOpMode;
this.setState('info.opMode', opMode, true);
this.setState('admin.opMode', opMode, true);
} else {
this.log.warn('Setting operating mode not successful! Error: ' + JSON.stringify(msg));
this.setState('info.error', JSON.stringify(msg), true);
}
// get the Network signal strength & quality
await this.getSignals();
if (getSignalQuality && !gettingSignals) {
gettingSignals = setInterval(() => {
this.getSignals();
}, pollinginterval);
}
// get Modem Serial Number
try {
const result = await gsmModem.getModemSerial();
this.log.debug(`GSM modem Serial: ${JSON.stringify(result)}`);
this.setState('info.modemSerial', parseInt(result.data.modemSerial), true);
//this.execATSL('CNMI', cnmiModemOpen)
} catch (err) {
this.log.warn(`Error retrieving GSM modem serial - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
// get the Own Number of the Modem gsmModem.setOwnNumber('+41798233954');
await this.getNameNumber();
await this.readSIM();
} catch (e) {
this.log.warn('Error startup ' + e);
}
} //end startup()
async getSignals() {
try {
const result = await gsmModem.getNetworkSignal();
this.log.debug(`GSM signal strength: ${JSON.stringify(result)}`);
if (result.status === 'success') {
this.setState('info.signalQuality', parseInt(result.data.signalQuality), true);
this.setState('info.signalStrength', parseInt(result.data.signalStrength), true);
} else {
this.log.warn('Problem retrieving GSM signal strength: ' + JSON.stringify(result));
this.setState('info.error', JSON.stringify(result), true);
}
} catch (err) {
this.log.warn(`Error retrieving GSM signal strength - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end getSignals
async readSIM() {
try {
// get info about stored Messages on SIM card
const resultSimMem = await gsmModem.checkSimMemory();
this.log.debug(`Sim Memory Result: ${JSON.stringify(resultSimMem)}`);
} catch (err) {
this.log.warn(`Failed to get SimMemory ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
try {
// read the whole SIM card inbox
const result = await gsmModem.getSimInbox();
this.log.debug(`Sim Inbox Result: ${JSON.stringify(result)}`);
let messageNr = 0;
if (result.status === 'success' && result.data.length > 0) {
for (const message of result.data) {
this.setState('inbox.messageSender', message['sender'], true);
this.setState('inbox.messageText', message['message'], true);
this.setState('inbox.messageDate', message['dateTimeSent'].toString(), true);
this.setState('inbox.messageRaw', JSON.stringify(message), true);
this.log.info('saved message Nr ' + (messageNr + 1) + ': ' + JSON.stringify(message));
if (autoDeleteOnReceive) {
try {
await this.deleteMessage(message);
} catch (err) {
this.log.warn(`Error deleting message ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
}
messageNr++;
await (new Promise(resolve => setTimeout(resolve, 1000)));
}
this.log.info('All saved messages processed');
} else {
this.log.info('No saved messages to process');
}
// was passiert wenn sim voll??
//this.execATSL('CNMI', cnmiModemOpen)
} catch (err) {
this.log.warn(`Failed to get SimInbox ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end readSIM
async deleteAll() {
try {
const result = await gsmModem.deleteAllSimMessages();
this.log.debug(`Delete Result: ${JSON.stringify(result)}`);
if (result.status === 'success') {
this.log.info('All messages on SIM deleted');
} else {
this.log.warn('Deleting Messages not successful, pls check: ' + JSON.stringify(result));
this.setState('info.error', JSON.stringify(result), true);
}
} catch (err) {
this.log.warn(`Failed to delete Message ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end deleteAll
async deleteMessage(msg) {
try {
const result = await gsmModem.deleteMessage(msg);
this.log.debug(`Delete Result: ${JSON.stringify(result)}`);
if (result.status === 'success') {
this.log.info('Message deleted');
} else {
this.log.warn('Deleting Message not successful, pls check: ' + JSON.stringify(result));
this.setState('info.error', JSON.stringify(result), true);
}
} catch (err) {
this.log.warn(`Failed to delete Message ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end deleteMessage
/*
async storeMessage(data) {
await this.setObjectNotExistsAsync('inbox.SIM.' + data.index, {
type: 'state',
common: {
name: 'Message data',
type: 'string',
role: 'state',
read: true,
write: true,
},
native: {},
});
await this.setState('inbox.SIM.' + data.index, JSON.stringify(data), true);
this.log.debug('Message stored to: ' + data.index)
}//end storeMessage
*/
async phonebook(id, ownPhone) { //Check mit nachfolgender Abfrage einbauen
try {
if (ownPhone.name.length > 16) {
this.log.warn('ownName max length = 16 chars, please choose a shorter name');
return;
}
const result = await gsmModem.writeToPhonebook(ownPhone.number, ownPhone.name);
this.log.debug(`wrote to Phonebook: ${JSON.stringify(result)}`);
if (result.status === 'success') {
this.log.debug('Phonebook updated: ' + ownPhone.name + ', ' + ownPhone.number);
} else {
this.log.warn('Writing to phonebook not successful, pls check: ' + JSON.stringify(result));
this.setState('info.error', JSON.stringify(result), true);
}
await this.getNameNumber();
} catch (err) {
this.log.warn(`Error writing to phonebook - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end phonebook
async getNameNumber() {
try {
const result = await gsmModem.getOwnNumber();
this.log.debug(`Own number: ${JSON.stringify(result)}`);
this.log.info('Your number: ' + result.data.number + ', your name: ' + result.data.name);
if (result.data.number && result.data.name) {
this.setState('info.ownNumber', result.data.number, true);
this.setState('info.ownName', result.data.name, true);
this.setState('admin.ownNumber', result.data.number, true);
this.setState('admin.ownName', result.data.name, true);
}
} catch (err) {
this.log.warn(`Error retrieving own Number - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
}
async sending(messageRawJson) {
try {
// Finally send an SMS
const result = await gsmModem.sendSMS(messageRawJson.recipient, messageRawJson.message, messageRawJson.alert);
if (result.data.response === 'Message Successfully Sent') {
this.log.info(`Callback Send: Message ID: ${result.data.messageId},` +
`${result.data.response} To: ${result.data.recipient} ${JSON.stringify(result)}`);
const messageSent = {
'message': result.data.message,
'sentTo': result.data.recipient
};
this.setState('sendSMS.messageRawSent', JSON.stringify(messageSent), true);
if (messageRawJson.message.includes('ioB_notification')) {
return true;
};
} else if (result.data.response === 'Successfully Sent to Message Queue') {
this.log.debug('Message processing: ' + JSON.stringify(result));
if (messageRawJson.message.includes('ioB_notification')) {
return false;
};
} else {
this.log.warn('Error sending message: ' + JSON.stringify(result));
if (messageRawJson.message.includes('ioB_notification')) {
return false;
};
}
} catch (e) {
this.log.warn('Error sending message' + e);
if (messageRawJson.message.includes('ioB_notification')) {
return false;
};
}
} //end sending()
async setOpMode(id, state) {
try {
const msg = await gsmModem.setModemMode(undefined, state);
this.log.debug(`Set operation mode: ${JSON.stringify(msg)}`);
if (msg.status === 'success') {
const returnedOpMode = msg.data.slice(0, 3);
if (returnedOpMode !== opMode) {
this.log.info('Operating mode changed to: ' + returnedOpMode);
}
opMode = returnedOpMode;
this.setState('info.opMode', opMode, true);
this.setState('admin.opMode', opMode, true);
} else {
this.log.warn('Setting operating mode not successful! Error: ' + JSON.stringify(msg));
this.setState('info.error', JSON.stringify(msg), true);
}
} catch (err) {
this.log.warn(`Error setting GSM-Modem mode - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end setOpMode
async execATSL(id, atCmd) {
try {
this.log.debug('Execute AT+Command: ' + atCmd);
// execute a custom command - one line response normally is handled automatically
const result = await gsmModem.executeCommand(atCmd);
this.log.debug(`Result ${JSON.stringify(result)}`);
if (id !== 'CNMI') {
this.setState('admin.atCommandResponse', JSON.stringify(result), true);
this.log.info('AT+Command response (single / last line): ' + JSON.stringify(result.data.result));
}
} catch (err) {
this.log.debug(`Error - ${err}`);
this.setState('info.error', JSON.stringify(err), true);
}
} //end execATSL
async controlSIM(id, state) {
try {
const conn = ((await this.getStateAsync('info.connection')) || {}).val;
this.log.debug('connection: ' + conn);
switch (id) {
case 'admin.ownName':
if (conn) {
ownPhone.number = ((await this.getStateAsync('info.ownNumber')) || {}).val;
ownPhone.name = state.val;
this.log.debug('Phonebook new: ' + JSON.stringify(ownPhone));
await this.phonebook(id, ownPhone);
} else {
this.log.debug('Connection is closed, please restart adapter & try again');
}
break;
case 'admin.ownNumber':
if (conn) {
this.log.debug('set new own Number to: ' + state.val);
try {
const result = await gsmModem.setOwnNumber(state.val);
this.log.debug(`Own number set: ${JSON.stringify(result)}`);
this.setState('info.ownNumber', state.val, true);
} catch (err) {
this.log.warn(`Error setting own Number - ${err}`);
}
} else {
this.log.warn('Connection is closed, please restart adapter & try again');
}
break;
case 'admin.opMode':
opMode = state.val;
if (state.val === 'PDU' || state.val === 'SMS') {
if (conn) {
this.log.debug('Connection open, set new operation mode');
await this.setOpMode(id, state.val);
} else {
this.log.debug('Connection is closed, please restart adapter & try again');
}
} else {
this.log.warn("Operating mode has to be set to 'PDU' (default) or 'SMS', you entered " + state.val);
return;
}
break;
case 'admin.atCommandSLR':
if (conn) {
this.log.debug('Connection open, send command');
await this.execATSL(id, state.val);
} else {
this.log.debug('Connection is closed, please restart adapter & try again');
}
break;
/*
case 'admin.atCommandMLR':
if (conn == true) {
this.log.debug("Connection open, send command");
this.execATML(id, state.val)
} else {
this.log.debug("Connection is closed, please restart adapter & try again");
}
break;
*/
case 'sendSMS.messageRaw':
messageRawJson = JSON.parse(state);
await this.sending(messageRawJson);
break;
case 'sendSMS.send':
messageRawJson.recipient = ((await this.getStateAsync('sendSMS.recipient')) || {}).val;
messageRawJson.message = ((await this.getStateAsync('sendSMS.message')) || {}).val;
messageRawJson.alert = ((await this.getStateAsync('sendSMS.alert')) || {}).val;
await this.sending(messageRawJson);
break;
default:
this.log.debug('Error, triggering objectChange not recognized');
}
} catch (e) {
this.log.warn('controlSIM - error: ' + e);
}
} //end controlSIM
async processNotification(obj) {
this.log.debug(`New notification received from: ${JSON.stringify(obj)}`);
const messageToSend = {
recipient: 'Number',
message: 'Yourtext',
alert: 'false'
};
messageToSend.recipient = this.config.defaultrecipientnotifications;
messageToSend.message = 'ioB_notification from ' + obj.message.host + ': ' + obj.message.category.name;
messageToSend.alert = false;
this.log.debug(`Notification SMS: ${JSON.stringify(messageToSend)}`);
let notirep = await this.sending(messageToSend);
if (notirep = true) {
this.sendTo(obj.from, 'sendNotification', {
sent: true
}, obj.callback);
} else {
this.sendTo(obj.from, 'sendNotification', {
sent: false
}, obj.callback);
}
}
/*
modemClose() {
try {
this.log.debug("Setze CNMI auf Speichern");
if (cnmiModemClosed.length == 17) {
gsmModem.executeCommand(cnmiModemClosed, (result, err) => {
if (err) {
this.log.warn(`Error setting CNMI - ${err}`);
} else {
this.log.debug(`Result setting CNMI: ${JSON.stringify(result)}`);
gsmModem.executeCommand('AT+CNMI?', (result, err) => {
if (err) {
this.log.warn(`Error checking CNMI - ${err}`);
} else {
this.log.debug(`CNMI new: ${JSON.stringify(result)}`);
this.log.debug("Closing connection");
gsmModem.close((err, msg) => {
if (err) {
this.log.warn(`Error closing - ${JSON.stringify(err)}`);
} else {
this.log.debug(`Closing: ${JSON.stringify(msg)}`);
this.log.debug("connection closed");
}
});
this.setState('info.connection', false, true);
}
});
}
});
} else {
this.log.debug("Closing connection (without changing CNMI)");
gsmModem.close();
this.setState('info.connection', false, true);
}
gsmModem.on('close', data => {
//whole message data
this.log.debug(`Event Close onModemClose: ` + JSON.stringify(data));
});
} catch (e) {
this.log.warn("Error closing connection" + e);
}
} //end modemClose()
*/
/**
* 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);
clearInterval(gettingSignals);
//clearInterval(polling);
if (cnmiModemClosed.length === 17) {
this.log.debug('Setze CNMI auf Speichern');
gsmModem.executeCommand(cnmiModemClosed, (result, err) => {
if (err) {
this.log.warn(`Error setting CNMI - ${err}`);
this.log.debug('Closing connection ');
gsmModem.close(callback);
this.setState('info.connection', false, true);
this.log.info('stopping GSM-SMS - adapter');
} else {
this.log.debug(`Result setting CNMI: ${JSON.stringify(result)}`);
gsmModem.executeCommand('AT+CNMI?', (result, err) => {
if (err) {
this.log.warn(`Error checking CNMI - ${err}`);
} else {
this.log.debug(`CNMI new: ${JSON.stringify(result)}`);
}
this.log.debug('Closing connection ');
gsmModem.close(callback);
this.setState('info.connection', false, true);
this.log.info('stopping GSM-SMS - adapter');
});
}
});
} else {
this.log.debug('Closing connection (without changing CNMI)');
gsmModem.close(callback);
this.setState('info.connection', false, true);
this.log.info('stopping GSM-SMS - adapter');
}
} catch (e) {
this.log.warn('Error stopping (onUnload) GSM-SMS - adapter: ' + e);
callback();
}
} // end onUnload
// 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.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (!id || state.ack) return;
this.log.info(`state ${id} changed: ${state.val}`);
const instance = id.substring(0, this.namespace.length);
this.log.debug('Instanz: ' + instance);
id = id.substring(this.namespace.length + 1); // remove instance name and id
this.log.debug('id=' + id);
this.controlSIM(id, state);
} else {
// The state was deleted
this.log.info(`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
// */
// onMessage(obj) {
// if (typeof obj === 'object' && obj.message) {
// if (obj.command === 'send') {
// // e.g. send email or pushover or whatever
// this.log.info('send command');
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, 'Message received', obj.callback);
// }
// }
// }
} //end Class Gsmsms
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Gsmsms(options);
} else {
// otherwise start the instance directly
new Gsmsms();
}