-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-spc-notify-email.js
executable file
·123 lines (109 loc) · 4.09 KB
/
node-spc-notify-email.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
#!/usr/bin/env node
/*
* SPC notify template
*/
/* Accept self signed certificate */
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var config = require('./config.json');
var websocket_client = require('websocket').client;
var email = require('emailjs')
// Connect to spc_web_gateway websocket interface
var ws_client = new websocket_client();
ws_client.connect(config['spc_gw_ws'] + '/ws/spc' + config['auth']);
ws_client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
ws_client.on('connect', function(connection) {
console.log('SPC WebSocket client connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
manageSiaEvent(message.utf8Data);
}
});
});
/**********************************************************************
* manageSiaEvent
**********************************************************************/
function manageSiaEvent(message){
data = JSON.parse(message);
if (data['status'] === 'success'){
timestamp = data['data']['sia'].timestamp;
sia_code = data['data']['sia'].sia_code;
sia_address = data['data']['sia'].sia_address;
description = data['data']['sia'].description;
flags = data['data']['sia'].flags;
verification_id = data['data']['sia'].verification_id;
// Convert timestamp to milliseconds and repack json string
json = JSON.stringify(data['data']['sia']);
// Filter SIA events
switch (sia_code){
case 'BA': /* Burglar Alarm */
case 'BR': /* Burglar Alarm Restore */
case 'BB': /* Inhibited or Isolated */
case 'BU': /* Deinhibited or Deisolated */
case 'CL': /* Area Activated (Full Set) */
case 'NL': /* Area Activated (Part Set) */
case 'OP': /* Area Deactivated */
case 'ZC': /* Zone Closed */
case 'ZO': /* Zone Opened */
sendSiaEvent(json);
break;
}
}
}
/**********************************************************************
* getTimeString - Convert timestamp to string format
**********************************************************************/
function getTimeString(seconds){
var d = new Date(seconds*1000);
var timestr = ( d.getFullYear() + '-' +
('0' + (d.getMonth() + 1)).slice(-2) + '-' +
('0' + d.getDate()).slice(-2) + ' ' +
('0' + d.getHours()).slice(-2) + ':' +
('0' + d.getMinutes()).slice(-2) + ':' +
('0' + d.getSeconds()).slice(-2));
return timestr;
}
/**********************************************************************
* sendSiaEvent - Format and send the SIA message
**********************************************************************/
function sendSiaEvent(json) {
data = JSON.parse(json);
var timestr = getTimeString(parseInt(data['timestamp']));
var text = 'Date: ' + timestr + ' SPC Event: ' + data['sia_code'] +
' ' + data['description'];
var subject = "My SPC SIA Event";
console.log(subject + ": " + text);
mailMessage(subject, text);
}
/**********************************************************************
* mailMessage - Mail the message
**********************************************************************/
function mailMessage(subject, text){
// Connect to Email server
var server = email.server.connect({
user: config['email_server_user'],
password:config['email_server_password'],
host: config['email_server_host'],
ssl: config['email_server_ssl']
});
// Send message
server.send({
from: config['email_from'],
to: config['email_to'],
cc: '',
subject: subject,
text: text
},
function(err, message) {
if (err) {
console.log(err);
}
});
}