-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkotol.js
221 lines (189 loc) · 6.32 KB
/
kotol.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
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
var mongoose = require('mongoose');
// MongoDB database connection either localhost or remote, provide link to your own database
// var urlLink = 'mongodb://localhost:27017/kotol';
var urlLink = 'mongodb://heroku:[email protected]:27017,boilertimeconfigs-shard-00-01-ssig8.mongodb.net:27017,boilertimeconfigs-shard-00-02-ssig8.mongodb.net:27017/kotol?ssl=true&replicaSet=boilerTimeConfigs-shard-0&authSource=admin';
mongoose.connect(urlLink);
var TimeConfig = require('./models/timeConfig');
var CurrentTimeConfig = require('./models/currentTimeConfig');
var Boost = require('./models/boost');
var BoostConfig = require('./models/boostConfig');
// wiringpi start
var wiringpi = require('wiringpi-node');
function setUpPWMOut() {
// # use 'GPIO naming'
wiringpi.setup('gpio');
// # set #18 to be a PWM output
wiringpi.pinMode(18, wiringpi.PWM_OUTPUT);
// # set the PWM mode to milliseconds stype
wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS);
// # divide down clock
wiringpi.pwmSetClock(192);
wiringpi.pwmSetRange(2000);
}
// change pin mode to input - turn off servo
function turnOffPWM() {
wiringpi.pinMode(18, wiringpi.INPUT);
}
var min = 80;
var max = 260;
var currentAngle = min;
var delayPeriod = 20;
function calculateAngle(temperature) {
return min + temperature * 20;
}
function calculateTemperature(angle) {
return (angle - min) / 20;
}
function writeNumber(angle) {
wiringpi.pwmWrite(18, angle);
currentAngle = angle;
console.log(angle);
}
function delayedWrite(currentAngle, desiredAngle, positive) {
// break if desiredAngle has been reached
if (positive && (currentAngle >= desiredAngle)) return;
if (!positive && (currentAngle <= desiredAngle)) return;
setTimeout(function () {
if (positive)
currentAngle++;
else
currentAngle--;
// console.log(currentAngle);
writeNumber(currentAngle);
// call next() recursively
delayedWrite(currentAngle, desiredAngle, positive);
}, delayPeriod);
}
function writeNumberSlow(angle) {
setUpPWMOut();
if (angle > currentAngle) {
delayedWrite(currentAngle, angle, true, wiringpi);
} else {
delayedWrite(currentAngle, angle, false, wiringpi);
}
setTimeout(turnOffPWM, delayPeriod * 200 + 1000);
}
// wiringpi end
var currentTime;
var previousTime = '00:00';
var currentTemp = 0;
var currentBoost = null;
var lastTimeOfDay;
var lastTempOfDay;
function setTemperature(temp) {
console.log();
console.log('Temperature set to: ' + temp);
writeNumberSlow(calculateAngle(temp));
}
function printInfo() {
console.log();
console.log('current temperature: \t' + currentTemp);
console.log('last temperature of day: \t' + lastTempOfDay + ' (updated only when needed)');
console.log('previous time: \t\t' + previousTime);
console.log('current time: \t\t' + currentTime);
console.log('last time of day: \t\t' + lastTimeOfDay + ' (updated only when needed)');
}
function writeCurrentTempToDB() {
CurrentTimeConfig.updateOne({}, {
$set: {
time: new Date().getTime(),
temperature: calculateTemperature(currentAngle)
}
}, function (err, timeConfig) {
if (err) throw err;
console.log('Wrote current temperature to db.')
});
}
function updateLastConfigOfDay() {
TimeConfig.find().sort({time: -1}).exec(function (err, timeConfigs) {
if (err) throw err;
console.log('Updating last config');
if (timeConfigs.length > 0) {
lastTimeOfDay = timeConfigs[0].time;
lastTempOfDay = timeConfigs[0].temperature;
} else {
lastTimeOfDay = '00:00';
lastTempOfDay = 0;
}
});
}
function updatePreviousConfig() {
if (currentBoost) {
return;
}
TimeConfig.find({time: {$lt: currentTime}}).sort({time: -1}).exec(function (err, timeConfigs) {
if (err) throw err;
// if config exists use it
if (timeConfigs.length > 0) {
if (previousTime !== timeConfigs[0].time || currentTemp !== timeConfigs[0].temperature) {
previousTime = timeConfigs[0].time;
currentTemp = timeConfigs[0].temperature;
console.log('Change found');
setTemperature(currentTemp);
}
// if not - use lastConfigOfDay
} else {
if (previousTime !== lastTimeOfDay || currentTemp !== lastTempOfDay) {
previousTime = lastTimeOfDay;
currentTemp = lastTempOfDay;
setTemperature(currentTemp);
}
updateLastConfigOfDay();
}
});
}
function updateTime() {
var date = new Date();
var hours = date.getHours();
if (hours <= 9) {
hours = '0' + hours;
}
var minutes = date.getMinutes();
if (minutes <= 9) {
minutes = '0' + minutes;
}
currentTime = hours + ':' + minutes;
}
function updateBoost() {
BoostConfig.findOne().exec(function (err, result) {
if (err) throw err;
var durationInMin = result.duration;
var duration = Date.now() - durationInMin * 60 * 1000;
Boost.findOne({time: {$gt: duration}})
.sort({time: 'desc'})
.exec(function (err, boost) {
if (err) throw err;
currentBoost = boost;
});
});
if (currentBoost && currentTemp !== currentBoost.temperature) {
currentTemp = currentBoost.temperature;
console.log('Boost override temperature');
setTemperature(currentTemp);
}
}
function everyMinute() {
updateTime();
// update of last configuration at end of the day
if (currentTime === '23:59')
updateLastConfigOfDay();
// setting proper temperature to servo motor
if (/^[0-9][0-9]:[0-9]5$/.test(currentTime))
everyTenMinutesCheck();
updateBoost();
updatePreviousConfig();
setTimeout(printInfo, 3000);
setTimeout(writeCurrentTempToDB, 4000);
}
function everyTenMinutesCheck() {
console.log('Executing every-ten-minutes check.');
setTemperature(currentTemp);
}
function main() {
setUpPWMOut();
writeNumber(calculateAngle(0));
setTimeout(updateLastConfigOfDay, 2000);
setTimeout(everyMinute, 7000);
setInterval(everyMinute, 60000);
}
main();