-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (77 loc) · 2.77 KB
/
index.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
"use strict";
// trigger the debugger so that you can easily set breakpoints
//debugger;
var VectorWatch = require('vectorwatch-browser');
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;
var hours = ['twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven'];
var minutes = ['', 'five', 'ten', 'quarter', 'twenty', 'twenty five', 'half'];
vectorWatch.on('config', function(event, response) {
// your stream was just dragged onto a watch face
//logger.info('on config');
//logger.info(event);
response.send();
});
vectorWatch.on('subscribe', function(event, response) {
// your stream was added to a watch face
//logger.info('on subscribe');
logger.info(event.userSettings);
var time = getCurrentTime();
response.setValue(time);
response.send();
});
vectorWatch.on('unsubscribe', function(event, response) {
// your stream was removed from a watch face
logger.info('on unsubscribe');
response.send();
});
function getCurrentTime() {
var date = new Date();
var hour = date.getHours()%12;
var minute = Math.round(date.getMinutes()/5);
if (minute == 12) {
minute = 0;
hour = (hour+1)%12;
}
var time;
if (minute === 0) {
time = hours[hour] + " o'clock";
} else if (minute <= 6) {
time = minutes[minute] + ' past ' + hours[hour];
} else{
time = minutes[12-minute] + ' to ' + hours[(hour+1)%12];
}
console.log(time);
return time;
}
function pushUpdates() {
var streamText = getCurrentTime();
storageProvider.getAllUserSettingsAsync().then(function(records) {
for (var i=0; i<records.length; i++) {
vectorWatch.pushStreamValue(records[i].channelLabel, streamText);
}
});
}
function keepAlive() {
//A request to Vector server on the webhook endpoint will trigger an outside request for the current application
var url = 'https://endpoint.vector.watch/VectorCloud/rest/v1/stream/'+process.env.STREAM_UUID+'/webhook';
request(url);
}
function scheduleJob() {
var scheduleRule = new Schedule.RecurrenceRule();
var times = [];
for (var i=0;i<60;i+=5) {
times.push(i);
}
scheduleRule.minute = times; // will execute every 5 minutes
Schedule.scheduleJob(scheduleRule, pushUpdates);
// Custom rule in order to keep the heroku server alive.
var scheduleRuleHeroku = new Schedule.RecurrenceRule();
scheduleRuleHeroku.minute = [0,30];
Schedule.scheduleJob(scheduleRuleHeroku, keepAlive);
}
vectorWatch.createServer(scheduleJob);