forked from stasel/NodeGSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·70 lines (61 loc) · 2.48 KB
/
test.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
const GSM = require('./src/index')
const gsm = new GSM('/dev/ttyUSB0')
async function main() {
// Connect
console.log("Connecting to serial modem...")
await gsm.connect()
console.log("Connected to serial modem")
await gsm.check()
console.log("Modem OK\n")
// Print modem info
console.log("Modem Information:")
console.log(` Manufacturer: ${await gsm.getManufacturerInformation()}`)
console.log(` Model ID: ${await gsm.getModelIdentification()}`)
console.log(` Version: ${await gsm.getRevisionIdentification()}`)
console.log(` Serial: ${await gsm.getSerialNumber()}`)
console.log(`Character set: ${await gsm.getCharacterSet()}`)
console.log("")
// Print network info (Works only with a working SIM card and GSM signal)
console.log("Network Information:")
console.log(` Carrier: ${await gsm.getCurrentOperator()}`)
console.log(` Signal: ${(await gsm.getSignalQuality()).description }`)
console.log(` Subscriber ID: ${await gsm.getSubscriberId()}`)
console.log(` Phone Number: ${await getPhoneNumber(gsm) || "Unknown"}`)
console.log("start sending")
await gsm.sendSMS('+4367761943548', 'Hello from node-gsm-modem', 10000)
console.log("done sending")
// Polling on the list of unread messages and write all messages to stdout
while(true) {
let unreadMessages = await gsm.readSMS(GSM.MessageStorage.sim,GSM.MessageFilter.unread)
console.log(`Unread messages: ${unreadMessages.length}`)
if(unreadMessages.length > 0) {
for(let msg of unreadMessages) {
console.log(`From ${msg.sender} at ${msg.time.toISOString()}:`)
console.log(`${msg.text}\n\n`)
}
await gsm.deleteAllMessages(GSM.MessageStorage.sim, GSM.MessageDeleteFilter.readSentAndUnsent)
}
await timeout(10000)
}
}
async function getPhoneNumber(gsm) {
try {
return await gsm.getSubscriberNumber()
}
catch {
// Some operators store the subscriber phone number in the own number phone book. But it may fail
try {
const result = await gsm.readPhoneBook(GSM.PhoneBookStorage.ownNumber,1,1)
return result[0].number
}
catch {
return undefined
}
}
}
const timeout = (ms) => {
return new Promise(resolve => {
setTimeout(resolve,ms)
})
}
main().then(() => console.log("Bye")).catch(console.error).finally(() => { gsm.disconnect() })