-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-OS-Utilities.js
169 lines (149 loc) · 6.52 KB
/
MMM-OS-Utilities.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
//MMM-OS-Utilities.js
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getLocaleFromEpoch(epochDevTime, epochTime, defaultNull = "") {
if (epochTime) {
var myLocale = getDateFromEpoch(epochDevTime, epochTime);
return myLocale.toLocaleString();
} else {
return defaultNull;
}
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getMomentFromEpoch(epochDevTime, epochTime, defaultNull = "") {
if (epochTime) {
var myMoment = getDateFromEpoch(epochDevTime, epochTime);
return moment(myMoment).calendar();
} else {
return defaultNull;
}
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getDateFromEpoch(epochDevTime, epochTime) {
//OS poorly implemented epoch, uses local time coded as GMT-0
const now = new Date();
var correctEpoch = Math.round(now.getTime() / 1000) + (epochTime - epochDevTime);
var myDate = new Date(correctEpoch * 1000);
return myDate;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function secondsCountdownOld(futureSeconds) {
//var myVar = setInterval(myTimer(), 1000);
//var content = document.createElement("div");
// document.getElementById("sn1").innerHTML = "here";
return;
}
function myTimer(){
var d = new Date();
document.getElementById("sn1").innerHTML = "here";
//d.toLocaleTimeString();
}
function secondsCountdown(pid, futureSeconds) {
// Set the date we're counting down to
//var countDownDate = new Date("May 6, 2020 17:00:00").getTime();
var countDownDate = new Date().getTime();
countDownDate.setSeconds(countDownDate.getSeconds() + 75);
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
//Log.info('OD-cd: ' + distance);
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
if (days) {
document.getElementById("sn1").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
} else if (hours) {
document.getElementById("sn1").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
} else if (minutes) {
document.getElementById("sn1").innerHTML = minutes + "m " + seconds + "s ";
} else {
document.getElementById("sn1").innerHTML = seconds + "s ";
}
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById(pid).innerHTML = "EXPIRED";
}
}, 1000);
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getClearTime(minFromMidnight) {
var sd = new Date(0,0,0,0, minFromMidnight,0,0);
var sh = sd.getHours();
var sm = "0" + sd.getMinutes();
var smf = sm.substring(sm.length-2);
if (sh < 12) {
return sh + ':' + smf + " AM";
} else {
return sh-12 + ':' + smf + " PM";
}
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getProgramRunning (psps, pppd, pnstations) {
var i;
i=0;
for(i = 0; i < pnstations; i++) {
// return psps[i][0];
switch (psps[i][0]) {
case 0: //not here ignore
break;
case 254:
return 'Run once';
case 99:
return 'On demand';
default:
//Log.info('pRun: ' + psps[i][0]);
return pppd[psps[i][0]-1][5];
}
}
return 'Idle';
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
function getStationStatus (stationopen, stationcodes) {
var remaining = '';
var statdetail = '';
if (stationcodes[1] > 60) {
var remaining = parseFloat(stationcodes[1]/60).toFixed(0) + 'm ' + parseFloat(stationcodes[1]%60).toFixed(0) + 's';
} else if (stationcodes[1] == 0) {
var remaining = '0m';
} else {
var remaining = '0m ' + stationcodes[1] + 's';
}
Log.info('OD-gSS: ' + stationopen + '-' + stationcodes[1] + '-' + remaining);
var start = '';
//var start = stationcodes[2];//come back
if (stationopen == 1){//running <span id="on1")stationcodes[1]</span>
// statdetail = 'On - <span id="on1")' + remaining + '</span>';
statdetail = 'On - ' + remaining;
//call countdown
} else if (stationopen == 2) {//Station is closed or queued
if (stationcodes[0]) {//program running so inqueue
statdetail = 'Queued - ' + remaining;
} else {
statdetail = 'Off';
}
}
return statdetail;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------