-
Notifications
You must be signed in to change notification settings - Fork 1
/
smart_mirror.js
169 lines (155 loc) · 4.42 KB
/
smart_mirror.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
/*
SmartMirror Javascript to get content and display in the document.
Define following variables in a separate smart_config.js file
var mEndpoint = ...
var mSecurity = ...
*/
var mMonthNames = [ "Januar", "Februar", "M\u00e4rz", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember" ];
var mWeekNames = [ "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag",
"Freitag", "Samstag" ];
var mGarage = "undefined";
function initialize()
{
shortloop();
// Setup short refresh loop for 3 seconds
setInterval(shortloop, 1000 * 3);
}
function shortloop()
{
refreshDate();
refreshMusic();
refreshInformation();
refreshSwitches();
}
function refreshInformation()
{
var response = httpGet(mEndpoint + '/information/list?' + mSecurity);
for (const info of response)
{
if(info.key == "weather")
{
refreshWeather(info);
}
if(info.key == "sensor.rct_power_storage_generator_a_energy_production_day")
{
refreshPV(info);
}
if(info.key == "sensor.garage_door")
{
mGarage = info.state;
}
}
}
function refreshWeather(weather)
{
var temperature = document.getElementById('temperature');
var icon = document.getElementById('weather_img');
temperature.innerHTML = Math.round(weather.celsius) + '°';
var icon_text = '<img src="img/';
var icon_name = '';
// Respect sunrise-sunset
if (weather.day_night == 'Day')
icon_name = 'sun';
else
icon_name = 'moon';
// Respect clouds
if (weather.clouds >= 90)
icon_name = 'cloud';
else if (weather.clouds > 50)
icon_name += '_cloud';
else if (weather.clouds > 10)
icon_name += '_cloud_less';
// Respect rain and snow
if (weather.rain != null && weather.rain)
icon_name += '_rain';
else if (weather.snow != null && weather.snow)
icon_name += '_snow';
icon_text += icon_name + '.png"/>';
icon.innerHTML = icon_text;
}
function refreshPV(pv)
{
var sunContainer = document.getElementById('sun_power');
var sunContent = Math.round(Number(pv.state) / 100) / 10 + " kWh";
sunContainer.innerHTML = sunContent;
}
function refreshSwitches() {
var response = httpGet(mEndpoint + '/switch/list?' + mSecurity);
var container = document.getElementById('container_switches');
var content = '<table class="bottom">';
response.sort(function(a, b){return a.name.localeCompare(b.name)});
for (var i = 0; i < response.length; i++) {
var s = response[i];
if (s.state == "ON") {
content += '<tr><td><img src="img/lamp.png"></td><td>';
content += s.name;
content += '</td></tr>';
}
}
if (mGarage == "closed")
{
content += '<tr><td><img src="img/cover_closed.png"></td><td>';
content += "Garage geschlossen";
content += '</td></tr>';
}
if (mGarage == "open")
{
content += '<tr><td><img src="img/cover_open.png"></td><td>';
content += "Garage offen";
content += '</td></tr>';
}
content += '</table>';
container.innerHTML = content;
}
function refreshMusic() {
var response = httpGet(mEndpoint + '/mediaserver/list?' + mSecurity);
var artist = document.getElementById('artist');
var song = document.getElementById('song');
var container_music = document.getElementById('container_music');
if (artist != null && song != null && container_music != null) {
var playing = response[0].current_playing;
artist.innerHTML = '';
song.innerHTML = '';
if (playing != null) {
container_music.style.visibility = 'visible';
if (playing.artist != null)
artist.innerHTML = playing.artist;
if (playing.title != null){
if (playing.title.length > 30)
song.innerHTML = playing.title.substring(0,
30)
+ "...";
else
song.innerHTML = playing.title;
}
if (playing.artist == null && playing.title == null && playing.file != null){
song.innerHTML = playing.file;
}
} else {
container_music.style.visibility = 'hidden';
}
}
}
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false); // false for synchronous request
xmlHttp.send(null);
var response = xmlHttp.responseText;
return JSON.parse(response);
}
function refreshDate() {
var now = new Date();
var time = document.getElementById('time');
if (time != null) {
var text = now.getHours() + ':' + now.getMinutes();
if (now.getMinutes() < 10)
text = now.getHours() + ':0' + now.getMinutes();
time.innerHTML = text;
}
var date = document.getElementById('date');
if (date != null) {
date.innerHTML = mWeekNames[now.getDay()] + ', ' + now.getDate() + '. '
+ mMonthNames[now.getMonth()];
}
}