-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
172 lines (151 loc) · 5.62 KB
/
main.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
let websocket = null;
let pluginUUID = null;
let settings = null;
let context = null;
let remainingMinutes = 0;
let timerId;
function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, inInfo) {
pluginUUID = inPluginUUID;
websocket = new WebSocket("ws://localhost:" + inPort);
websocket.onopen = () => registerPlugin(inRegisterEvent);
websocket.onmessage = (event) => handleMessage(event);
websocket.onerror = (event) => console.warn('Websocket error:', event, event.data);
websocket.onclose = (event) => unregisterPlugin(event);
}
function handleMessage(event) {
const eventObject = JSON.parse(event.data);
switch (eventObject['event']) {
case 'willAppear':
console.log('Stream-Deck appeared');
context = eventObject.context;
fetchWakaTimeStats();
break;
case 'willDisappear':
console.log('Stream-Deck disappeared');
break;
case 'sendToPlugin':
console.log('Data from PropertyInspector arrived.');
getGlobalSettings();
break;
case 'didReceiveGlobalSettings':
console.log('Did receive global settings.');
settings = eventObject.payload.settings;
fetchWakaTimeStats();
break;
case 'keyUp':
console.log('Pressed keyUp');
fetchWakaTimeStats();
break;
default:
console.log('Unknown Event: ' + eventObject.event);
break;
}
}
function fetchWakaTimeStats() {
if (!(settings.username && settings.apikey && settings.minutes)) {
showAlert();
return;
}
fetch(`https://wakatime.com/api/v1/users/${settings.username}/durations?date=today`, {
headers: new Headers({
'Authorization': 'Basic ' + btoa(settings.apikey),
})
}).then(response => {
return response.json();
}).then(data => {
remainingMinutes = calculateRemainingMinutes(data.data, settings.minutes);
setTitle(remainingMinutes);
}).catch(error => {
console.log(error);
});
}
function registerPlugin(inRegisterEvent) {
const json = {
"event": inRegisterEvent,
"uuid": pluginUUID
};
websocket.send(JSON.stringify(json));
getGlobalSettings();
startTimer();
}
function startTimer() {
timerId = setInterval(function () {
fetchWakaTimeStats();
}, 30 * 1000);
}
function showAlert() {
const json = {
"event": "showAlert",
"context": context,
};
websocket.send(JSON.stringify(json));
}
function getGlobalSettings() {
const json = {
"event": "getGlobalSettings",
"context": pluginUUID,
};
websocket.send(JSON.stringify(json));
}
function unregisterPlugin(event) {
const reason = getWebsocketReason(event);
console.warn('Websocket closed:', reason);
clearInterval(timerId);
}
function setTitle(title) {
const json = {
"event": "setTitle",
"context": context,
"payload": {
"title": "" + title,
"target": 0
}
};
websocket.send(JSON.stringify(json));
}
function calculateRemainingMinutes(durations, minutesToReach) {
let workedSeconds = 0;
for (const value of durations) {
workedSeconds += value.duration;
}
const remainingTime = minutesToReach - Math.floor(workedSeconds / 60);
return remainingTime > 0 ? remainingTime : 0;
}
function getWebsocketReason(event) {
let reason;
if (event.code === 1000) {
reason = 'Normal Closure. The purpose for which the connection was established has been fulfilled.';
} else if (event.code === 1001) {
reason = 'Going Away. An endpoint is "going away", ' +
'such as a server going down or a browser having navigated away from a page.';
} else if (event.code === 1002) {
reason = 'Protocol error. An endpoint is terminating the connection due to a protocol error';
} else if (event.code === 1003) {
reason = "Unsupported Data. An endpoint received a type of data it doesn't support.";
} else if (event.code === 1004) {
reason = '--Reserved--. The specific meaning might be defined in the future.';
} else if (event.code === 1005) {
reason = 'No Status. No status code was actually present.';
} else if (event.code === 1006) {
reason = 'Abnormal Closure. ' +
'The connection was closed abnormally, e.g., without sending or receiving a Close control frame';
} else if (event.code === 1007) {
reason = 'Invalid frame payload data. ' +
'The connection was closed, because the received data was not consistent with the type of the message.';
} else if (event.code === 1008) {
reason = 'Policy Violation. The connection was closed, because current message data "violates its policy". ';
} else if (event.code === 1009) {
reason = 'Message Too Big. Connection closed because the message is too big for it to process.';
} else if (event.code === 1010) {
reason = 'Mandatory Ext. Connection is terminated the connection because the server didn\'t ' +
'negotiate one or more extensions in the WebSocket handshake.' + event.reason;
} else if (event.code === 1011) {
reason = 'Internal Server Error. Connection closed because it encountered an unexpected ' +
'condition that prevented it from fulfilling the request.';
} else if (event.code === 1015) {
reason = 'TLS Handshake. The connection was closed due to a failure to perform a TLS handshake';
} else {
reason = 'Unknown reason';
}
return reason;
}