forked from arminstr/reremeter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (86 loc) · 3.36 KB
/
main.js
File metadata and controls
99 lines (86 loc) · 3.36 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
var mainApp = {};
var lastPredictID = null;
(function(){
var firebase = app_fireBase;
firebase.auth().onAuthStateChanged(function(user) {
if(user){
//user signed in.
uid = user.uid;
var path = 'devices/your_device_id/control';
app_fireBase.databaseApi.listen(path, controlCallBack);
var path = 'devices/your_device_id/control/';
var data = {measure: 'false'};
app_fireBase.databaseApi.update(path, data, messageHandler);
hidePlasticIcon();
}else{
//redirect to login page
uid = null;
window.location.replace("login.html");
}
});
function logOut(){
firebase.auth().signOut();
}
function messageHandler(err){
if(!!err){
console.log(err);
}
}
function scan(){
var path = 'devices/your_device_id/control/';
var data = {measure: 'true'};
app_fireBase.databaseApi.update(path, data, messageHandler);
hidePlasticIcon();
document.getElementById("scan_button").disabled = true;
}
var updatePlasticIcon = function(id, text) {
if(id == 0){
hidePlasticIcon();
document.getElementById("plasticText").style.display = 'block';
document.getElementById("loaderCircle").style.display = 'none';
var plasticText = document.getElementById("plasticText");
plasticText.innerHTML = text;
} else {
// html items of plastic Icon
showPlasticIcon();
var plasticText = document.getElementById("plasticText");
var plasticNumber = document.getElementById("plasticNumber");
plasticText.innerHTML = text;
plasticNumber.innerHTML = id;
}
};
function controlCallBack(value){
if(!!value){
if(value.measure == 'false' && value.predictID.name != lastPredictID){
lastPredictID = value.predictID.name;
var path = 'devices/your_device_id/detection/measurements/' + value.predictID.name;
//attach listener to get informed about prediction upload
app_fireBase.databaseApi.listen(path, dataUpdateCallback)
}
}
}
function dataUpdateCallback(value){
if(!!value.PlasticID || value.PlasticID == 0){
console.log(value);
showPlasticIcon();
updatePlasticIcon(value.PlasticID, value.PlasticType);
document.getElementById("scan_button").disabled = false;
}else{
console.log('no PlasticID available');
}
}
function hidePlasticIcon() {
document.getElementById("plasticText").style.display = 'none';
document.getElementById("plasticNumber").style.display = 'none';
document.getElementById("recyclingIcon").style.display = 'none';
document.getElementById("loaderCircle").style.display = 'block';
}
function showPlasticIcon() {
document.getElementById("plasticText").style.display = 'block';
document.getElementById("plasticNumber").style.display = 'block';
document.getElementById("recyclingIcon").style.display = 'block';
document.getElementById("loaderCircle").style.display = 'none';
}
mainApp.scan = scan;
mainApp.logOut = logOut;
})()