-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.js
executable file
·125 lines (106 loc) · 3.14 KB
/
listener.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
#!/usr/bin/node
// Demo software for Iridium Node.JS library
// Version 1.5 (2012-12-28)
// (C) 2012 Razvan Dragomirescu <[email protected]>
var iridium = require("./iridium.js");
var sys = require('sys');
var zlib = require('zlib');
var dateFormat = require('dateformat');
var execSync = require('exec-sync');
var redis = require("redis"),
redis_client = redis.createClient();
var pending = 0;
var lock = 0;
sys.log("Welcome to the Ellipsis Global Interconnecting Gateway")
iridium.open({
debug: 0,
port: "/dev/ttyUSB0"
});
/*
IRIDIUM LISTENERS
*/
iridium.on('initialized', function() {
sys.log("[SBD] IRIDIUM INITIALIZED");
iridium.getSystemTime(function(err, ctime) {
sys.log("Current Iridium time is "+ctime);
var fdate = dateFormat(ctime, "mmddHHMMyyyy.ss");
execSync("sudo date "+fdate);
sys.log("Date set from Iridium time");
})
});
iridium.on('ringalert', function() {
sys.log("[SBD] RING ALERT");
mailboxCheck();
});
iridium.on('newmessage', function(message, queued) {
sys.log("[SBD] Received new message "+message);
mt_client = redis.createClient();
mt_client.publish("mt", message);
sys.log("[SBD] There are "+queued+" messages still waiting");
pending = queued;
});
/*
REDIS Listeners
*/
redis_client.on("ready", function () {
redis_client.subscribe("mo");
});
redis_client.on("message", function (channel, message) {
console.log(channel + ":" + message);
if (channel == "mo"){
sendMessage(message);
}
});
/*
METHODS TO HANDLE EVENTS
*/
// mailboxCheck
function mailboxCheck() {
if (lock) {
pending++;
} else {
sendMessage("");
}
}
// send a plain text message
function sendMessage(text) {
lock=1;
iridium.sendMessage(text, function(err, momsn) {
if (err==null) {
if (text) sys.log("[SBD] Message sent successfully, assigned MOMSN "+momsn);
// check to see if there are other messages pending - if there are, send a new mailbox check to fetch them in 1 second
if (pending>0) setTimeout(function() {
sendMessage("");
}, 1000);
else {
lock=0;
}
} else {
sys.log("[SBD] Iridium returned error "+err+", will retry in 20s");
setTimeout(function() {
sendMessage(text);
}, 20000);
}
});
}
// send a binary message
function sendBinaryMessage(buffer) {
lock=1;
iridium.sendBinaryMessage(buffer, function(err, momsn) {
if (err==null) {
if (buffer) sys.log("[SBD] Binary message sent successfully, assigned MOMSN "+momsn);
// check to see if there are other messages pending - if there are, send a new mailbox check to fetch them in 1 second
if (pending>0) setTimeout(function() {
sendMessage("");
}, 1000);
else {
lock=0;
}
} else {
sys.log("[SBD] Iridium returned error "+err+", will retry in 20s");
setTimeout(function() {
sendBinaryMessage(buffer);
}, 20000);
}
});
}