-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
157 lines (112 loc) · 3.17 KB
/
script.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
// Plotly.newPlot('chart', [{
// y: [0],
// type: 'line',
// layout: {
// xaxis: {
// title: 'Time'
// },
// yaxis: {
// title: 'Humidity (%)'
// },
// width: 200,
// height: 200,
// },
// scrollZoom: true
// }]);
var layout = {
showlegend: false,
};
var trace1 = {
y: [0],
type: 'line',
line: { color: '#f30242' }
};
var data = [trace1]
var config = {
displayModeBar: false,
scrollZoom: true,
responsive: true,
autosizable: true,
};
// Plotly.newPlot('chart', [{y: [0]}] , layout, {scrollZoom: true});
Plotly.newPlot('chart', data, layout, config);
var gateway = `ws://192.168.249.144/ws`;
var websocket;
window.addEventListener('load', onload);
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
const registration = await navigator.serviceWorker.register("./serviceWorker.js");
return registration;
}
}
const checkPermission = () => {
if(!("Notification" in window)){
console.log("Notification not supported in this browser");
alert("Notification not supported in this browser");
return;
}
}
const requestNotificationPermission = async () => {
const permission = await window.Notification.requestPermission();
if (permission !== "granted") {
alert("Permission not granted for Notification");
}
}
requestNotificationPermission();
const main = async () => {
checkPermission();
const reg = await registerServiceWorker();
reg.showNotification("Gas leak detected");
}
function onload(event) {
initWebSocket();
// registerServiceWorker();
// requestNotificationPermission();
}
window.addEventListener("load", onload)
//navigator.serviceWorker
//.register("/serviceWorker.js")
//.then(res => console.log("service worker registered"))
//.catch(err => console.log("service worker not registered", err))
function getReadings() {
websocket.send("getReadings");
}
function initWebSocket() {
console.log('Trying to open a WebSocket connection…');
websocket = new WebSocket(gateway);
console.log("hi");
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage;
}
function onOpen(event) {
console.log('Connection opened');
getReadings();
}
function onClose(event) {
console.log('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
console.log(event.data);
var myObj = JSON.parse(event.data);
var key = "message";
plotter(parseInt(myObj[key]));
document.getElementById("humidity").innerHTML = myObj[key];
if(parseInt(myObj[key]) > 50){
console.log("Gas Leak detected");
main();
}
}
function plotter(data) {
var cnt = 0;
Plotly.extendTraces('chart', { y: [[data]] }, [0]);
cnt++;
if (cnt > 500) {
Plotly.relayout('chart', {
xaxis: {
range: [cnt - 500, cnt]
}
});
}
}