-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerStream.js
136 lines (102 loc) · 3.76 KB
/
rerStream.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
"use strict";
// trigger the debugger so that you can easily set breakpoints
debugger;
var VectorWatch = require('vectorwatch-browser');
var request = require('request');
var Schedule = require('node-schedule');
var StorageProvider = require('vectorwatch-storageprovider');
var vectorWatch = new VectorWatch();
var storageProvider = new StorageProvider();
vectorWatch.setStorageProvider(storageProvider);
var logger = vectorWatch.logger;
vectorWatch.on('config', function(event, response) {
// your stream was just dragged onto a watch face
logger.info('on config');
response.send();
});
vectorWatch.on('subscribe', function(event, response) {
var streamText;
var settings = event.getUserSettings().settings;
try {
getTrafic().then(function(body) {
var rers = body.response.rers;
if(rers[0].title === "Trafic normal" && rers[1].title === "Trafic normal"){
streamText = "Trafic normal";
} else if(rers[0].title !== "Trafic normal"){
streamText = "RER A - KO";
} else if(rers[1].title !== "Trafic normal"){
streamText = "RER B - KO";
}
response.setValue(streamText);
response.send();
}).catch(function(e) {
response.setValue("ERROR 1");
response.send();
});
} catch(err) {
response.setValue("ERROR 2");
response.send();
}
});
function getTrafic() {
return new Promise(function (resolve, reject) {
var url = "url_to_ratp";
request(url, function (error, httpResponse, body) {
if (error) {
reject('REST call error: ' + error.message + ' for ' + url);
return;
}
if (httpResponse && httpResponse.statusCode != 200) {
reject('REST call error: ' + httpResponse.statusCode + ' for ' + url);
return;
}
try {
body = JSON.parse(body);
resolve(body);
} catch(err) {
reject('Malformed JSON response from ' + url + ': ' + err.message);
}
});
});
}
vectorWatch.on('unsubscribe', function(event, response) {
// your stream was removed from a watch face
logger.info('on unsubscribe');
response.send();
});
function pushUpdates() {
var streamText;
try {
getTrafic().then(function(body) {
var rers = body.response.rers;
if(rers[0].title === "Trafic normal" && rers[1].title === "Trafic normal"){
streamText = "Traffffic normal";
} else if(rers[0].title !== "Trafic normal"){
streamText = "RER A - KO";
} else if(rers[1].title !== "Trafic normal"){
streamText = "RER B - KO";
}
storageProvider.getAllUserSettingsAsync().then(function(records) {
records.forEach(function(record) {
// record.userSettings
vectorWatch.pushStreamValue(record.channelLabel, streamText);
});
});
}).catch(function(e) {
//logger.error(e);
response.setValue("ERROR");
response.send();
});
//logger.info('on subscribe: ' + streamText);
} catch(err) {
//logger.error('on subscribe - malformed user setting: ' + err.message);
response.setValue("ERROR");
response.send();
}
}
function scheduleJob() {
var scheduleRule = new Schedule.RecurrenceRule();
scheduleRule.minute = [0,5,10,15,20,25,30,35,40,45,50,55]; // will execute at :15 and :45 every hour
Schedule.scheduleJob(scheduleRule, pushUpdates);
}
vectorWatch.createServer(scheduleJob);