working time cleanup

This commit is contained in:
Karl Hudgell 2020-09-14 12:27:18 +01:00
parent db5597d702
commit cc06dfb78c
6 changed files with 58 additions and 68 deletions

4
app.js
View File

@ -9,8 +9,8 @@ const lcd = require('./libs/lcd')
async function main() {
lcd.clearScreen()
await lcd.intro()
lcd.time()
hotwater.setInterval
setInterval(lcd.time, 55000)
setInterval(hotwater.getStatus, 5000)
const heatingSwitch = new Gpio('17', 'in', 'both');
const appSwitch = new Gpio('23', 'in', 'both');

View File

@ -1,10 +1,10 @@
const moment = require('moment'); // require
module.exports = {
time: () => {
try {
t = moment().format('HH:MM');
return t
var time = new Date();
currentTime = ("0" + time.getHours()).slice(-2) + ":" +
("0" + time.getMinutes()).slice(-2)
return currentTime
} catch (error) {
console.log(error)
}

View File

@ -29,19 +29,10 @@ module.exports = {
lcd.printLineSync(2, title)
},
time: async () => {
// set interval
var tid = setInterval(mycode, 60000);
async function mycode() {
lcd.setCursorSync(15, 0);
let f
let f
f = await clock.time()
lcd.printSync(f)
}
function abortTimer() { // to be called when you want to stop the timer
clearInterval(tid);
}
},
heatingStatus: async (input) => {
lcd.setCursorSync(0, 3);

View File

@ -1,47 +1,32 @@
const http = require('http')
const { Gpio } = require('onoff');
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const redLED = new Gpio('4', 'out');
const blueLED = new Gpio('27', 'out')
const greenLED = new Gpio('22', 'out')
const lcd = require('../libs/lcd')
setInterval(function () {
module.exports = {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
getStatus: () => {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
var request = http.request(rest_options, function (response) {
var content = "";
var request = http.request(rest_options, function (response) {
var content = "";
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
// Once we're done streaming the response, parse it as json.
response.on('end', function () {
var data = JSON.parse(content);
let waterOn = data.waterOn
if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
} else {
if (waterOn != globalVars.waterOn) {
// Once we're done streaming the response, parse it as json.
response.on('end', function () {
var data = JSON.parse(content);
let waterOn = data.waterOn
if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
@ -51,22 +36,30 @@ setInterval(function () {
led.set('blue')
globalVars.waterOn = waterOn
}
} else {
if (waterOn != globalVars.waterOn) {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
}
}
}
});
});
});
// Report errors
request.on('error', function (error) {
lcd.heatingStatus('Error')
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.waterOn = 'error'
});
// Report errors
request.on('error', function (error) {
lcd.heatingStatus('Error')
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.waterOn = 'error'
});
request.end();
}, 5000);
module.exports = {
setInterval
request.end();
}
}

View File

@ -12,7 +12,6 @@
"fs": "0.0.1-security",
"got": "^11.6.2",
"http": "0.0.1-security",
"moment": "^2.28.0",
"onoff": "^6.0.0",
"pigpio": "^3.2.3",
"raspberrypi-liquid-crystal": "^1.15.0",

13
test.js
View File

@ -1,5 +1,12 @@
const moment = require('moment'); // require
// const moment = require('moment'); // require
t = moment().format('HH:MM:ss');
console.log(t)
// t = moment().format('HH:MM:ss');
// console.log(t)
var time = new Date();
currentTime = ("0" + time.getHours()).slice(-2) + ":" +
("0" + time.getMinutes()).slice(-2)
console.log(currentTime)