Skip to content

Commit 28aadcf

Browse files
committed
Change rest api to return JSON and update template.html to read JSON
1 parent 9d7e010 commit 28aadcf

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

html/template.html

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@
4242
}
4343
setInterval(function() {
4444
$("[data-ajaxurl]").each(function(index) {
45+
console.log("test");
4546
var ele = $(this);
46-
$.ajax(ele.data("ajaxurl")).success(function (data) {
47-
if(data.toUpperCase() == "OPEN") {
48-
ele.html("<span class=\"label label-success\">" + data + "</span>");
49-
} else if(data.toUpperCase() == "CLOSE") {
50-
ele.html("<span class=\"label label-danger\">" + data + "</span>");
51-
} else {
52-
ele.html(data);
53-
}
54-
});
47+
$.getJSON( ele.data("ajaxurl"), function (data) {
48+
console.log(data.value);
49+
var eleValue = ele.find(".panel-body")[0];
50+
if(data.value.toUpperCase() == "OPEN") {
51+
eleValue.html("<span class=\"label label-success\">" + data.value + "</span>");
52+
} else if(data.value.toUpperCase() == "CLOSE") {
53+
eleValue.html("<span class=\"label label-danger\">" + data.value + "</span>");
54+
} else {
55+
eleValue.html(data.value);
56+
}
57+
});
5558
});
5659
}, 1000);
5760
</script>
@@ -91,16 +94,16 @@
9194

9295
<!-- split:sensor -->
9396
<div class="col-md-4">
94-
<div class="panel panel-info">
97+
<div class="panel panel-info" data-ajaxurl="{path}">
9598
<div class="panel-heading">
9699
<strong>
97100
{name}
98101
</strong>
99102
</div>
100-
<div class="panel-body" data-ajaxurl="{path}" style="font-size: 24px;">
103+
<div class="panel-body" style="font-size: 24px;">
101104
{value}
102105
</div>
103-
<div class="panel-footer" style="padding: 5px 15px; font-size: 85%"><strong>Last updated:</strong> {last_updated}</div>
106+
<div class="panel-footer" style="padding: 5px 15px; font-size: 85%"><strong>Last updated:</strong> <span>{last_updated}</span></div>
104107
</div>
105108
</div>
106109
<!-- split:sensor -->
@@ -129,7 +132,7 @@
129132
<div class="panel panel-default">
130133
<div class="panel-heading" style="background-image: linear-gradient(to bottom,#444 0,#aaa 100%); color: #fff;"><strong>{name}</strong></div>
131134
<ul class="list-group">
132-
<li class="list-group-item" style="font-size: 24px;" data-ajaxurl="{path}">
135+
<li class="list-group-item" style="font-size: 24px;">
133136
{value}
134137
</li>
135138
<li class="list-group-item">

src/WebPortal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void WebPortal::handleRoot() {
108108
void WebPortal::handleRest() {
109109
Entry* entry = &mqtt->get(webServer->uri().substring(6).c_str());
110110
if(webServer->method() == HTTP_GET && entry->isIn()) {
111-
webServer->send(200, "text/plain", entry->getValue());
111+
webServer->send(200, "application/json", "{\"value\":\"" + entry->getValue() + "\",\"updated\":\"" + time(entry->getLastUpdate()) + "\"}");
112112
} else if(webServer->method() == HTTP_POST && entry->isOut()) {
113113
entry->update(webServer->arg("plain"));
114114
webServer->send(200, "text/plain", webServer->uri() + " Update");

0 commit comments

Comments
 (0)