-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclock.js
More file actions
93 lines (80 loc) · 2.57 KB
/
Copy pathclock.js
File metadata and controls
93 lines (80 loc) · 2.57 KB
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
var mqtt;
function mqtt_connect() {
host = '192.168.99.999'
port = 1884;
console.log('connecting to '+ host +' '+ port)
t = new Date().getTime()
mqtt = new Paho.MQTT.Client(host,port,'client'+t)
options = {
timeout: 3,
userName: 'xxxxxx',
password: 'yyyyyy',
onSuccess: mqtt_onconnect,
onFailure: mqtt_onFailure,
}
mqtt.onMessageArrived = mqtt_onMessageArrived
mqtt.connect(options);
}
function mqtt_onFailure(message) {
console.log('mqtt connect failed');
setTimeout(mqtt_connect, 2000);
}
// Note, make sure mqtt publish retain bit is set, so these values get set correctly on reload
function mqtt_onMessageArrived(msg){
out_msg = 'mqtt message received: ' + msg.destinationName + ' => ' + msg.payloadString;
console.log(out_msg);
if (msg.destinationName == 'sensor/Outside Temperature') {
console.log('out temp update')
document.getElementById('tout').innerHTML = msg.payloadString
}
else if (msg.destinationName == 'sensor/Bedroom Temperature') {
document.getElementById('tbed').innerHTML = msg.payloadString
}
else if (msg.destinationName == 'sensor/Upstairs Temperature') {
document.getElementById('tup').innerHTML = msg.payloadString
}
else if (msg.destinationName == 'ha/speech') {
document.getElementById('speech').innerHTML = msg.payloadString
}
else if (msg.destinationName == 'mh/hvac/summary') {
document.getElementById('hvac').innerHTML = msg.payloadString
}
else if (msg.destinationName == 'ha/sunevent') {
// if (msg.payloadString == 'sunriseEnd' || msg.payloadString == 'goldenHourEnd') {
if (msg.payloadString == 'day') {
my_color = '#ffffff';
}
if (msg.payloadString == 'night') {
my_color = "#666666";
}
if (my_color) {
console.log('set color to ' + my_color);
let my_divs = document.querySelectorAll('div');
console.log('divs=' + my_divs);
for (let my_div of my_divs) {
console.log('div=' + my_div);
my_div.style.color = my_color;
}
}
}
}
function mqtt_onconnect() {
console.log('mqtt connected')
mqtt.subscribe('sensor/+')
mqtt.subscribe('ha/speech')
mqtt.subscribe('ha/sunevent')
mqtt.subscribe('mh/hvac/summary')
message = new Paho.MQTT.Message('Hello from clock.js')
message.destinationName = 'clock'
mqtt.send(message)
}
mqtt_connect()
function startTime() {
h = new Date().getHours()
// f = (h < 8 || h > 22) ? 'h:mm' : 'h:mm:ss'
f = 'h:mm';
t = moment().format(f)
document.getElementById('clock').innerHTML = t
t=setTimeout('startTime()', 1000)
}
startTime();