-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (117 loc) · 4.05 KB
/
app.js
File metadata and controls
133 lines (117 loc) · 4.05 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
(function(){
// Initialize Firebase
var config = {
apiKey: "AIzaSyDB-W_rJpMGPyI_w4LB5Q6-T7HcHXGmhho",
authDomain: "testing-cf658.firebaseapp.com",
databaseURL: "https://testing-cf658.firebaseio.com",
storageBucket: "testing-cf658.appspot.com",
messagingSenderId: "611513984981"
};
firebase.initializeApp(config);
//get elements
const lightObject = document.getElementById('object1');
const rideObject = document.getElementById('object2');
const time = document.getElementById('time');
const record = document.getElementById('recordTime');
const allTimeLoops = document.getElementById('loop2');
// create references to the database
const lightData = firebase.database().ref().child('lightStatus');
const coasterMode = firebase.database().ref().child('coasterMode');
const coasterEstop = firebase.database().ref().child('coasterEstop');
const loopCount = firebase.database().ref().child('loopCount');
const coasterAlert = firebase.database().ref().child('coasterAlert');
const recordTime = firebase.database().ref().child('recordTime');
const timeElapsed = firebase.database().ref().child('timeElapsed');
const totalLoops = firebase.database().ref().child('totalLoops');
// keep the text on the page updated
lightData.on('value', snap => {
if(snap.val()===false){
lightObject.innerText = "Light Status: Off";
}
else{
lightObject.innerText = "Light Status: On";
}
});
coasterMode.on('value', snap => {
rideObject.innerText = "Ride Mode: " + JSON.stringify(snap.val(), null, 3);
});
recordTime.on('value',snap =>{
record.innerText = "The record time is " + JSON.stringify(snap.val()/1000,null,3) + " seconds";
});
timeElapsed.on('value',snap => {
time.innerText = "The previous run was " + JSON.stringify(snap.val()/1000,null,3) + " seconds";
});
totalLoops.on('value',snap => {
allTimeLoops.innerHTML = "The coaster has completed " + JSON.stringify(snap.val(),null,3) + " trips in all";
})
}());
// create references to the database
const lightData = firebase.database().ref().child('lightStatus');
const coasterMode = firebase.database().ref().child('coasterMode');
const coasterEstop = firebase.database().ref().child('coasterEstop');
const loopCount = firebase.database().ref().child('loopCount');
const coasterAlert = firebase.database().ref().child('coasterAlert');
const motorOn = firebase.database().ref().child('motorOn');
const recordTime = firebase.database().ref().child('recordTime');
const timeElapsed = firebase.database().ref().child('timeElapsed');
const totalLoops = firebase.database().ref().child('totalLoops');
// time for time keeping purposes
const timeDeparted = firebase.database().ref().child('timeDeparted');
const timeReturned = firebase.database().ref().child('timeReturned');
var lightInfo;
lightData.once('value',function(snapshot){
lightInfo = snapshot.val();
})
function lightSwitch(){
if(lightInfo){
lightData.set(true);
}
else{
lightData.set(false);
}
lightInfo = !lightInfo;
}//end lightSwitch()
function sendSingle(){
coasterEstop.once('value',function(getData){
if(getData.val()===false){
coasterMode.set("Single loop"); //says the mode is single
sendCoaster();
} // run the motor for 4 seconds or adjust as needed
});
}//end of send single);
function emergencyStop(){
coasterEstop.once('value',function(getData){
if(getData.val()===false){
coasterEstop.set(true);
coasterMode.set("EMERGENCY STOP");
}
else{
coasterEstop.set(false);
coasterMode.set("System Ready");
}
});
}//end of emergencyStop()
function contLoop(){
coasterEstop.once('value',function(getData){
if(getData.val()===false){
coasterMode.set("Continuous");
sendCoaster();
};
});
}
function runFor(){
var n = document.getElementById("loopNumber").value-1;
coasterMode.set("Loop for");
loopCount.set(n);
sendCoaster();
};
//this function runs the motor for 4 seconds when a ride is called
function sendCoaster(){
var d = new Date();
var n = d.getTime();
timeDeparted.set(n);//set the time that the car departs
motorOn.set(true);
setTimeout(function(){
motorOn.set(false);
},4000);
}