-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
182 lines (146 loc) · 4.85 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var firstRun = localStorage.firstRun || true,
runTests = false,
sources = {}, // keyed off source id
triggers = {}, // keyed off source id, then signal id
cache = {}; // keyed off source id, then signal id
const MAX_LEN_SPARKBLOCK = 30;
function init() {
// force the screen to stay on forever
if (navigator.requestWakeLock) {
navigator.requestWakeLock('screen');
}
else {
console.log('No ability to requestWakeLock');
}
[ ambientLightSource,
bluetoothSource,
cameraSource, // TODO: implement me
deviceMotionSource,
deviceOrientationSource,
powerSource,
proximitySource,
soundSource, // TODO: implement activity
speechSource, // TODO: implement me
].forEach(function(d) {
// based on feature detection
if (d.enabled) {
// initialize source
d.start();
// cache source
sources[d.id] = d;
// display all registered sources
render(d);
// listent for data from the source
document.addEventListener(d.id, processEvent);
}
});
}
window.addEventListener('DOMContentLoaded', init);
function registerTrigger(trigger, action, notification) {
if (!triggers[trigger.source])
triggers[trigger.source] = [];
if (!triggers[trigger.source][trigger.signal])
triggers[trigger.source][trigger.signal] = [];
triggers[trigger.source][trigger.signal].push({
trigger: trigger,
action: action,
notification: notification
});
}
function processEvent(e) {
var source = sources[e.type],
sourceId = source.id,
signalId = e.detail.id;
// cache source data by source id, then by signal id
// TODO: persistentize this?
// TOOD: cap this
if (!cache[sourceId])
cache[sourceId] = []
if (!cache[sourceId][signalId])
cache[sourceId][signalId] = []
cache[sourceId][signalId].push(e.detail.value);
//notifyIFTTT(signalId, e.detail.label, e.detail.value) {
// render the data
render(source, e.detail, cache[sourceId][signalId]);
// find matching triggers
if (triggers[sourceId] && triggers[sourceId][signalId]) {
var matched = triggers[sourceId][signalId];
console.log('matched?', matched)
matched.forEach(function(match) {
if (!match.trigger['value'] ||
match.trigger.value == e.detail.value) {
processNotification(match.trigger, match.notification, e.detail);
}
});
}
else {
//console.log('no trigger+signal matches')
}
}
function processNotification(trigger, notification, data) {
if (!("Notification" in window)) {
console.error("This browser does not support desktop notification");
}
Notification.requestPermission(function (permission) {
if (permission === "granted") {
new Notification(notification.title, {
body: notification.description,
icon: notification.icon
});
}
});
}
// Data renderer
function render(source, data, cachedData) {
// get the container for this source
var container = document.querySelector('#' + source.id);
// if the signal container doesn't exist yet, create it
if (!container) {
var containerTpl = document.querySelector('#template-signal')
// set the title
containerTpl.content.querySelector('h2').innerText = source.title;
// enabled?
if (!source.enabled) {
containerTpl.content.querySelector('h2').innerText += ' (Not supported by device)';
}
// add to document
var containerClone = document.importNode(containerTpl.content, true);
containerTpl.parentNode.appendChild(containerClone);
// get newly added element and give it an id
container = containerTpl.parentNode.lastElementChild;
container.setAttribute('id', source.id);
}
if (data) {
var dataContainer = container.querySelector('.data'),
entryNode = dataContainer.querySelector('.' + data.id);
if (!entryNode) {
var entryTpl = document.querySelector('#template-signal-entry');
entryNode = document.importNode(entryTpl.content, true).firstElementChild;
entryNode.classList.add(data.id);
}
if (data.type == 'row') {
entryNode = entryNode.cloneNode(true);
}
var label = entryNode.querySelector('.label');
label.textContent = data.label;
var value = entryNode.querySelector('.value');
if (data.type == 'scalar' || data.type == 'row') {
value.textContent = data.value;
}
else if (data.type == 'stream') {
var start = cachedData.length < MAX_LEN_SPARKBLOCK ? 0 : (cachedData.length - MAX_LEN_SPARKBLOCK);
var show = cachedData.slice(start, cachedData.length);
value.textContent = sparkline.generate(show, MAX_LEN_SPARKBLOCK);
}
dataContainer.appendChild(entryNode);
}
}
function notifyIFTTT(value1, value2, value3) {
var url = 'https://maker.ifttt.com/trigger/yourAppName/with/key/yourIFTTTKey';
var msg = {
value1: value1,
value2: value2,
value3: value3
};
sendJSON(url, msg);
}