This commit is contained in:
William Mantly 2024-01-23 23:15:29 -05:00
parent eecb79abb8
commit 3a0163fd42
7 changed files with 88 additions and 15 deletions

View File

@ -4,7 +4,6 @@ const io = ()=> app.io;
// We have to wait for the express HTTP server to be finished starting before we
// can use any of the socket.io stuff.
app.onListen.push(function(){
app.io.on('connection', (socket) => {
console.log('User connected via WebsSocket')
@ -12,7 +11,6 @@ app.onListen.push(function(){
console.log('User disconnect via WebsSocket')
});
});
});

View File

@ -10,6 +10,24 @@ app.util = (function (app) {
: decodeURIComponent(results[1].replace(/\+/g, " "));
}
function promisify(f) {
return function (...args) { // return a wrapper-function (*)
return new Promise((resolve, reject) => {
function callback(err, result) { // our custom callback for f (**)
if (err) {
reject(err);
} else {
resolve(result);
}
}
args.push(callback); // append our custom callback to the end of f arguments
f.call(this, ...args); // call the original function
});
};
}
function actionMessage(message, $target, type, callback) {
message = message || "";
$target = $target.closest("div.iot-card").find(".actionMessage");
@ -52,12 +70,13 @@ app.util = (function (app) {
};
return {
getUrlParameter: getUrlParameter,
actionMessage: actionMessage,
getUrlParameter,
actionMessage,
promisify
};
})(app);
app.api = (function (app) {
app.apiSync = (function (app) {
var baseURL = "/api/v0/";
function post(url, data, callback) {
@ -141,7 +160,21 @@ app.api = (function (app) {
});
}
return { post: post, get: get, put: put, delete: remove };
return {
post: app.util.promisify(post),
get: app.util.promisify(get),
put: app.util.promisify(put),
delete: app.util.promisify(remove),
};
})(app);
app.api = (function (app) {
return {
post: app.util.promisify(app.apiSync.post),
get: app.util.promisify(app.apiSync.get),
put: app.util.promisify(app.apiSync.put),
delete: app.util.promisify(app.apiSync.remove),
};
})(app);
//socket.io
@ -184,7 +217,7 @@ app.auth = (function (app) {
function isLoggedIn(callback) {
if (getToken()) {
return app.api.get("user/me", function (error, data) {
return app.apiSync.get("user/me", function (error, data) {
if (!error) app.auth.user = data;
//for navbar to show username
if (!location.pathname === "/login")
@ -268,7 +301,7 @@ app.auth = (function (app) {
app.user = (function (app) {
//delete profile
function deleteProfile() {
app.api.delete("user/delete", function (error, data) {
app.apiSync.delete("user/delete", function (error, data) {
if (error) {
app.util.actionMessage(error.message, $("#deleteProfile"), "danger");
} else {
@ -294,7 +327,7 @@ function formAJAX(btn, del) {
//console.log('Data being sent to', $form.attr('action'), formData)
app.api[method]($form.attr("action"), formData, function (error, data) {
app.apiSync[method]($form.attr("action"), formData, function (error, data) {
//console.log('Data back from the server', error, data)
app.util.actionMessage(data.message, $form, error ? "danger" : "success"); //re-populate table
if (!error) {

View File

@ -87,5 +87,9 @@ router.get("/api", function (req, res, next) {
res.render("api");
});
// sensor data
router.get("/sensor-data", function (req, res, next) {
res.render("sensor-data");
});
module.exports = router;

View File

@ -1,5 +1,5 @@
<%- include('top') %>
<link href="css/contact.css" rel="stylesheet" />
<link href="/css/contact.css" rel="stylesheet" />
<!-- full Title -->
<div class="full-title">

View File

@ -1,5 +1,5 @@
<%- include('top') %>
<link href="css/learnmore.css" rel="stylesheet" />
<link href="/css/learnmore.css" rel="stylesheet" />
<br>
<br>
@ -38,7 +38,7 @@
<br>
<br>
<script src="js/learnmore.js"></script>
<script src="/js/learnmore.js"></script>
<%- include('bot') %>

View File

@ -0,0 +1,41 @@
<%- include('top') %>
<style>
#sensorDataList{
padding-top: 2.5em;
}
</style>
<ul id="sensorDataList">
<li jq-repeat='sensorData'>
rowid: {{ id }}
sensorId: {{ sensorid }}
created: {{ createdAt }}
location: {{ locationid }}
<br/ >
co: {{ measurement.co }}
humidity: {{ measurement.humidity }}
no2: {{ measurement.no2 }}
o3: {{ measurement.o3 }}
psi: {{ measurement.psi }}
so2: {{ measurement.so2 }}
temperature: {{ measurement.temperature }}
windspeed: {{ measurement.windspeed }}
<hr />
</li>
<li jq-repeat-defualt='sensorData'>
Loading...
</li>
</ul>
<script type="text/javascript">
$(document).ready(async function(){
$.scope.sensorData.push(...await app.api.get('sensor-data/data?order=DESC&limit=40'));
app.socket.on('sensordata:new', function(data){
$.scope.sensorData.unshift(data);
});
})
</script>
<%- include('bot') %>

View File

@ -128,6 +128,3 @@
</div>
</div>
</nav>
</body>
</html>