-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
228 lines (200 loc) · 8.38 KB
/
javascript.js
File metadata and controls
228 lines (200 loc) · 8.38 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* AUTHOR: Andre Basson
* COURSE: Compsci 345
* ASSIGNMENT: 3, Task 2 (Design Prototype Implementation)
* YEAR: 2016 s1
*/
/*
* creating some arbitrary rooms for the purpose of the prototype scenario as per assignment documentation
*/
function createRooms(){
rooms.push(createRoom('Lounge'));
rooms.push(createRoom('Kitchen'));
rooms.push(createRoom('Bathroom 1'));
rooms.push(createRoom('Bathroom 2'));
rooms.push(createRoom('Bedroom 1'));
rooms.push(createRoom('Bedroom 2'));
}
function createRoom(roomName){
return new Room(roomName);
}
/*
* setting some arbitrary room states for the purpose of the prototype scenario as per assignment documentation
*/
function setSomeRoomStates(){
rooms[0].setLightState(true); // switch heating on in the Lounge
rooms[2].setLightState(true); // switch the lights on in bathroom 1
}
/*
* populateHomeScreen() populates the home screen content (4 buttons) and header
*/
function populateHomeScreen() {
var outputContent = "<input class='homeScreenButtons' type='button' name='rsc'"
+ "value='Room Specific Controls' onclick='populateRoomSpecificControls()'></input>"
+ "<input class='homeScreenButtons' type='button' name='hwc'"
+ "value='Housewide Controls'></input><br>"
+ "<input class='homeScreenButtons' type='button' name='cctv'"
+ "value='CCTV'></input>"
+ "<input class='homeScreenButtons' type='button' name='settings'"
+ "value='Settings'></input>";
document.getElementById("output").innerHTML = outputContent;
document.getElementById("heading").innerHTML = "Home";
}
/*
* populateRoomSpecificControlsScreen() populates the 'rooms' screen content (4 buttons) and header
*/
function populateRoomSpecificControls(){
var outputContent = "";
// create HTML buttons for each of the rooms
for (var i=0; i<rooms.length; i++){
var lineBreak = "";
if (i%3==0 && i!=0){lineBreak="<br>"};
var roomName = rooms[i].getName();
outputContent += lineBreak+"<input class='roomsScreenButtons' type='button' name='"+roomName+"'"
+"value='"+roomName+"' onclick='populateRoomControls("+i+")'></input>";
}
document.getElementById("output").innerHTML = outputContent;
document.getElementById("heading").innerHTML = "Rooms";
}
function populateRoomControls(roomIndex){
var tempString = "";
var room = rooms[roomIndex];
//debug
//console.log("room = "+ room.getName());
var outputContent = "";
outputContent +="<div id='roomControlItems'><span style='color:#3B5998'><h3>Room Items</h3></span>"
+ "<table id='roomControlStates'>"
+ "<tr><th></th><th></th></tr>"
+ "<tr><td><span style='color: #8C8C8C'>Lights</span></td><td id='lightState'></td></tr>"
//+ other possible controls to go here
+ "</table></div>";
// update HTML content
document.getElementById("output").innerHTML = outputContent;
document.getElementById("heading").innerHTML = ""+room.getName();
// update HTML toggle switches
//...light switch
tempString += "<label class='switch'><input id='lightSwitch' type='checkbox' onclick='toggleState("+roomIndex+",0)'><div class='slider round'></div></lable>"
document.getElementById("lightState").innerHTML= tempString;
if(room.getLightState()){
document.getElementById('lightSwitch').checked = true;
} else {document.getElementById('lightSwitch').checked = false;
}
}
function toggleState(rmIndex,rmItemNo){
var toggleSwitchID = "";
switch(rmItemNo) {
case 0:
//debug
console.log("toggleState() called");
toggleSwitchID = "lightSwitch";
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
if(document.getElementById(toggleSwitchID).checked){
//then the toggle switch has just been set to on, so switch on the light
rooms[rmIndex].setLightState(true);
} else if(document.getElementById(toggleSwitchID).checked == false) {
//then the toggle switch has just been set to off, so switch off the light
rooms[rmIndex].setLightState(false);
}
updateSideBar();
}
/*
* updateSideBar() populates or updates the Current Status / Active Rooms sidebar content
*/
function updateSideBar(){
var currentStatus = getCurrentStatus();
var currentStatusDiv = document.getElementById("currentStatusContent");
currentStatusDiv.innerHTML = "<span style='color:#3B5998'>Alarms</span>       "+currentStatus[0]+"<br>"
+"<span style='color:#3B5998'>Doors</span>         "+currentStatus[1]+"<br>"
+"<span style='color:#3B5998'>Energy</span>       "+currentStatus[2]+"<br>"
+"<span style='color:#3B5998'>Temp</span>          "+currentStatus[3];
var activeRoomsByIndexArray = getActiveRooms(rooms);
var activeRoomsOutput = "";
for (var i=0; i<activeRoomsByIndexArray.length; i++){
//console.log("Active Room: "+rooms[activeRoomsByIndexArray[i]].getName());
var activeRoom = rooms[activeRoomsByIndexArray[i]];
activeRoomsOutput += "<span style='color:#3B5998'>"+activeRoom.getName()+"</span>"+getActivities(activeRoom)+"<br>";
}
document.getElementById("activeRoomContent").innerHTML=activeRoomsOutput;
}
/*
* getCurrentStatus() returns the current status (stored) for [Alarms, Doors, Energy, Temp]
*/
function getCurrentStatus(){
var statusArray = JSON.parse(sessionStorage.getItem("currentStatusStore"));
//console.log("statusArray is "+statusArray);
return statusArray;
}
function appliance(name){
// properties
this.name = setName(name);
this.stateOn = setState(false); // boolean, default
// methods
this.getName = function(){return this.name};
this.setName = function(name){this.name = name};
this.getState = function(){return this.stateOn};
this.setState = function(state){this.StateOn = state};
}
function Room(roomName){
// properties
this.name = roomName; //setName(roomName);
this.lightOn = false; //setLightState(false); // boolean, false = off
this.appliances = new Array();
this.heatingOn = false; //setHeatingState(false); // boolean, false = off
this.doorUnLocked = false; //setDoorLockState(false); // boolean, false = locked
this.windowBlindDrawn = false; //setWindowBlindState(false); // boolean, false = open
// methods
this.getName = function(){return this.name;}
this.setName = function(name){this.name = roomName;}
this.getLightState = function(){return this.lightOn;}
this.setLightState = function(bLightOn){this.lightOn = bLightOn;}
this.getHeatingState = function(){return this.heatingOn;}
this.setHeatingState = function(bHeatingOn){this.heatingOn = bHeatingOn;}
this.getDoorLockState = function(){return this.doorUnLocked;}
this.setDoorLockState = function(bDoorUnlocked){this.doorUnLocked = bDoorUnlocked;}
this.getWindowBlindState = function(){return this.windowBlindDrawn;}
this.setWindowBlindState = function(bWindowBlindDrawn){this.windowBlindDrawn = bWindowBlindDrawn;}
this.addAppliance = function(applianceName){
this.appliances = appliances.push(applianceName);
}
this.removeAppliance = function(applianceName){
var index = this.appliances[applianceName]
this.appliance.splice(index,1); // deletes only the element at that index
}
this.getApplianceState = function(applianceName){this.appliances[applianceName].getState();}
this.setApplianceState = function(applianceName, applianceStateOn){this.appliances[applianceName].setState(applianceStateOn);}
}
function getActiveRooms(rooms){
var activeRoomsByIndexArray = new Array(); // create an empty array to store room index numbers
for (var i=0; i<rooms.length; i++){
if ( (rooms[i].getLightState()) || (rooms[i].getHeatingState())
|| (rooms[i].getDoorLockState()) || (rooms[i].getWindowBlindState()) ){
activeRoomsByIndexArray.push(i);
}
}
return activeRoomsByIndexArray;
}
/*
* returns the activities currently active (in 'on' state) in a room, as HTML
*/
function getActivities(room){
var outputStringHTML = "";
if (room.getLightState() == true){
outputStringHTML += "<img class='imgLight' src='images/light-on2.png' alt='light img'> "
} else if (room.getHeatingState()){
outputStringHTML += "<img class='imgThermometre' src='images/thermometre-up.png' alt='light img'> "
} else if (room.getDoorLockState()){
outputStringHTML += "<img class='imgLock' src='images/lock-open.png' alt='light img'> "
} else if (getWindowBlindState()){
outputStringHTML += "<img class='imgBlinds' src='images/blinds-closed.png' alt='light img'> "
}
return outputStringHTML;
}