forked from openWB/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessAllMqttMsg.js
More file actions
161 lines (155 loc) · 5.81 KB
/
processAllMqttMsg.js
File metadata and controls
161 lines (155 loc) · 5.81 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
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
var credentialsFetched = false;
function setIframeSource() {
if (allTopicsReceived()) {
const startup = document.querySelector("#notReady");
const iframe = document.querySelector("#displayTarget");
if (!data["openWB/system/boot_done"]) {
addLog("backend still booting");
startup.classList.remove("hide");
iframe.classList.add("hide");
return;
}
if (data["openWB/system/update_in_progress"]) {
addLog("update in progress");
startup.classList.remove("hide");
iframe.classList.add("hide");
return;
}
var host = "";
var query = new URLSearchParams();
var destination = "";
if (data["openWB/general/extern"] === true) {
// load secondary display (from secondary openWB)
switch (data["openWB/general/extern_display_mode"]) {
case "local":
// host = location.host;
// ...
// break;
// ToDo, fallback to primary
addLog("Local display in secondary mode not yet supported! fallback to primary display");
case "primary":
default:
// retrieve display theme from primary
host = data["openWB/internal_chargepoint/global_data"]["parent_ip"];
const queryObject = {
// we need our own ip address for status information
localIp: data["openWB/system/ip_address"],
// we need to know the current branch, commit and version
localBranch: data["openWB/system/current_branch"],
localCommit: data["openWB/system/current_commit"],
localVersion: data["openWB/system/version"],
// we need to know how to map local charge points to primary
parentChargePoint1: data["openWB/internal_chargepoint/0/data/parent_cp"],
parentChargePoint2: data["openWB/internal_chargepoint/1/data/parent_cp"]
}
query.append("data", JSON.stringify(queryObject));
break;
}
// load display from primary or local
destination = `${location.protocol}//${host}/openWB/web/display/?${query.toString()}`;
addLog(`all done, loading theme from primary`);
// no iframe here as this would result in another nesting with the wrapper on primary
setTimeout(() => {
location.href = destination;
}, 2000);
} else {
// load primary display (from primary or secondary openWB)
host = location.host;
const theme = data["openWB/optional/int_display/theme"].type;
const searchParams = new URLSearchParams(location.search);
if (searchParams.has("data")) {
console.warn("Detected query parameters! Forwarding data to display theme:", searchParams.get("data"));
query.append("data", searchParams.get("data"));
}
if (credentialsFetched) {
query.append("hide_login", "1");
}
destination = `${location.protocol}//${host}/openWB/web/display/themes/${theme}/?${query.toString()}`;
var request = new XMLHttpRequest();
request.onload = function () {
if (this.readyState == 4) {
if (this.status == 200) {
addLog(`theme '${theme}' is valid`)
if (destination != iframe.src) {
addLog(`all done, starting theme '${theme}' with url '${destination}'`);
iframe.src = destination;
}
setTimeout(() => {
startup.classList.add("hide");
iframe.classList.remove("hide");
}, 2000);
} else {
addLog(`theme '${theme}' not found on server!`);
}
}
};
request.ontimeout = function () {
console.warn("onTimeout", this.readyState, this.status);
addLog(`check for theme '${theme}' timed out!`);
};
request.timeout = 2000;
console.debug("checking url:", destination);
request.open("GET", destination, true);
request.send();
}
} else {
console.debug("some topics still missing");
}
}
function addLog(message, forceDisplay = false) {
const logElement = document.getElementById('log');
let displayedMessages = logElement.innerHTML.split("\n");
if (displayedMessages.length > 25) {
displayedMessages.shift();
}
displayedMessages.push(message);
logElement.innerHTML = displayedMessages.join("\n");
if (forceDisplay) {
logElement.classList.remove("hide");
}
logElement.scrollTo(0, logElement.scrollHeight); // Scroll to the last element
}
function handleMessage(topic, payload) {
addLog(`Topic: ${topic} Payload: ${payload}`);
// receives all topics and calls respective function to process them
if (!data["openWB/system/boot_done"]) {
document.getElementById("boot").classList.remove("hide");
} else {
document.getElementById("boot").classList.add("hide");
}
if (data["openWB/system/update_in_progress"]) {
document.getElementById("update").classList.remove("hide");
} else {
document.getElementById("update").classList.add("hide");
}
if (topic === "openWB/system/security/user_management_active") {
if (data["openWB/system/security/user_management_active"] === true) {
console.debug("user management is active, fetching mqtt credentials from storage");
var xhr = new XMLHttpRequest();
xhr.open("GET", "/openWB/runs/dynsec_helper/display.php", false); // synchroner Request
xhr.send();
if (xhr.status === 200) {
try {
var credentials = JSON.parse(xhr.responseText);
setCookie("mqtt", `${credentials.username}:${credentials.password}`);
credentialsFetched = true;
addLog(`Using mqtt credentials from storage: ${credentials.username.charAt(0)}... / ${credentials.password.charAt(0)}...`);
if (credentials.username === "admin" && credentials.password === "openwb") {
console.warn("Using default mqtt credentials!");
addLog("Warnung: Es werden die Standard MQTT Anmeldedaten verwendet!", true);
}
} catch (e) {
console.debug("Fehler beim Parsen der Credentials:", e);
deleteCookie("mqtt");
}
} else {
console.debug("no credentials for client found, using existing cookie if available");
}
} else {
deleteCookie("mqtt");
credentialsFetched = false;
addLog("User management inactive, using anonymous mqtt connection");
}
}
setIframeSource();
} // end handleMessage