Skip to content

Commit 860501c

Browse files
committed
Theoretically working heat/cool/off and fan on/off controls and temp targeting
1 parent f3e499a commit 860501c

File tree

2 files changed

+128
-25
lines changed

2 files changed

+128
-25
lines changed

modules/controller.js

+86-15
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ var temperature = require("../modules/temperature");
33
var display = require("../modules/display/display");
44

55
const controlModes = {
6-
MANUAL: 'user',
6+
MANUAL: 'manual',
77
SCHEDULE: "schedule"
88
};
99

1010
const climateModes = {
1111
OFF: 'off',
1212
HEAT: 'heat',
1313
COOL: 'cool',
14-
FAN: 'fan',
1514
};
1615

17-
let controlMode = controlModes.SCHEDULE;
18-
let climateMode = climateModes.OFF; // save/load this from a file later
16+
const fanModes = {
17+
OFF: 'off',
18+
ON: 'on',
19+
};
20+
21+
var controlMode = controlModes.SCHEDULE;
22+
var climateMode = climateModes.OFF; // save/load this from a file later
23+
var fanMode = fanModes.OFF; // save/load this from a file later
1924

20-
let targetTemp = -1;
21-
let running = 0;
22-
let overshootRange = 2;
25+
var targetTemp = -1;
26+
var climateRunning = 0;
27+
var overshootRange = 2;
2328

2429
const displayCycles = {
2530
CURRENT: "current",
@@ -32,22 +37,49 @@ let displayCycle = displayCycles.CURRENT;
3237
function watchTemperature() {
3338
temperature.getTemp().then(function (temp) {
3439

35-
if(running) { // If heat or cool is running, check if it's time to shut off
40+
temp = parseFloat(temp);
41+
42+
console.log();
43+
console.log("Current temp: " + temp);
44+
console.log("Target temp: " + targetTemp);
45+
console.log("Control Mode: " + controlMode);
46+
console.log("Climate Mode: " + climateMode);
47+
console.log("Running: " + climateRunning);
48+
49+
if(climateRunning) { // If heat or cool is running, check if it's time to shut off
3650
if(climateMode === climateModes.HEAT) {
51+
console.log(targetTemp + overshootRange);
52+
console.log(temp > targetTemp + overshootRange);
3753
if(temp > targetTemp + overshootRange) {
3854
// Shut off heat
55+
console.log("Heat off");
3956
thermostat.heatOff();
57+
climateRunning = 0;
4058
}
4159
} else if(climateMode === climateModes.COOL) {
4260
if(temp < targetTemp - overshootRange) {
4361
// Shut off AC
62+
console.log("AC off");
4463
thermostat.acOff();
64+
climateRunning = 0;
4565
}
4666
}
4767

4868
} else { // If not, check if it should be
49-
if(temp < targetTemp - overshootRange) {
50-
69+
if(climateMode === climateModes.HEAT) {
70+
if(temp < targetTemp - overshootRange) {
71+
// Turn on heat
72+
console.log("Heat on");
73+
thermostat.heatOn();
74+
climateRunning = 1;
75+
}
76+
} else if(climateMode === climateModes.COOL) {
77+
if(temp > targetTemp + overshootRange) {
78+
// Turn off AC
79+
console.log("AC on");
80+
thermostat.acOn();
81+
climateRunning = 1;
82+
}
5183
}
5284
}
5385

@@ -101,30 +133,69 @@ module.exports = {
101133
});
102134
},
103135

104-
setMode: function(m) {
136+
setControlMode: function(m) {
137+
switch (m) {
138+
case("manual"):
139+
controlMode = controlModes.MANUAL;
140+
break;
141+
case("schedule"):
142+
controlMode = controlModes.SCHEDULE;
143+
break;
144+
}
145+
watchTemperature();
146+
return true;
147+
148+
},
149+
150+
setClimateMode: function(m) {
105151
if(controlMode !== controlModes.MANUAL) {
106152
return false;
107153
} else {
154+
climateRunning = 0;
155+
thermostat.heatOff();
156+
thermostat.acOff();
108157
switch (m) {
109158
case("heat"):
110159
climateMode = climateModes.HEAT;
111160
break;
112161
case("cool"):
113162
climateMode = climateModes.COOL;
114163
break;
115-
case("fan"):
116-
climateMode = climateModes.FAN;
117-
break;
118164
case("off"):
119165
climateMode = climateModes.OFF;
120166
break;
121167
}
168+
watchTemperature();
169+
return true;
170+
}
171+
},
172+
173+
setFanMode: function(m) {
174+
if(controlMode !== controlModes.MANUAL) {
175+
return false;
176+
} else {
177+
switch (m) {
178+
case("on"):
179+
fanMode = fanModes.ON;
180+
thermostat.fanOn();
181+
break;
182+
case("off"):
183+
fanMode = fanModes.OFF;
184+
thermostat.fanOff();
185+
break;
186+
}
122187
return true;
123188
}
124189
},
125190

126191
setTemp: function(temp) {
127-
targetTemp = temp;
192+
if(controlMode !== controlModes.MANUAL) {
193+
return false;
194+
} else {
195+
targetTemp = parseInt(temp);
196+
watchTemperature();
197+
return true;
198+
}
128199
}
129200

130201
};

server.js

+42-10
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,59 @@ app.get('*.js', function(req, res) {
2020
res.sendFile(path.join(__dirname, 'www', req.url));
2121
});
2222

23-
app.get('/api/setmode/:mode', function(req, res) {
24-
if(req.param.mode !== "heat" || req.param.mode !== "cool" || req.param.mode !== "fan" || req.param.mode !== "off") {
23+
app.get('/api/setcontrolmode/:mode', function(req, res) {
24+
if(req.params.mode !== "manual" && req.params.mode !== "schedule") {
2525
res.status(400).send('Invalid mode');
2626

27-
} else if(controller.setMode(req.param.mode)) {
28-
res.send(req.param.mode + " mode active");
27+
} else {
28+
controller.setControlMode(req.params.mode);
29+
res.send(req.params.mode + " mode active");
30+
}
31+
});
32+
33+
app.get('/api/setclimatemode/:mode', function(req, res) {
34+
if(req.params.mode !== "heat" && req.params.mode !== "cool" && req.params.mode !== "off") {
35+
res.status(400).send('Invalid mode');
36+
37+
} else {
38+
if(controller.setClimateMode(req.params.mode)) {
39+
if(req.params.mode === "off") {
40+
res.send("No mode active");
41+
} else {
42+
res.send(req.params.mode + " mode active");
43+
}
44+
45+
} else {
46+
res.status(403).send('Manual mode not active');
47+
}
48+
}
49+
});
50+
51+
app.get('/api/setfanmode/:mode', function(req, res) {
52+
if(req.params.mode !== "on" && req.params.mode !== "off") {
53+
res.status(400).send('Invalid mode');
2954

3055
} else {
31-
res.status(403).send('Manual mode not active');
56+
if(controller.setFanMode(req.params.mode)) {
57+
res.send("Fan set to req.params.mode");
58+
59+
} else {
60+
res.status(403).send('Manual mode not active');
61+
}
3262
}
3363
});
3464

3565
app.get('/api/settemp/:temp', function(req, res) {
36-
if(!Number.isInteger(req.param.temp)) {
66+
if(!Number.isInteger(parseInt(req.params.temp))) {
3767
res.status(400).send('Invalid temp');
3868

39-
} else if(controller.setTemp(req.param.temp)) {
40-
res.send("Temperature Set");
41-
4269
} else {
43-
res.status(403).send('Manual mode not active');
70+
if(controller.setTemp(req.params.temp)) {
71+
res.send("Temperature Set");
72+
73+
} else {
74+
res.status(403).send('Manual mode not active');
75+
}
4476
}
4577
});
4678

0 commit comments

Comments
 (0)