diff --git a/app.js b/app.js index 91557c8..589844c 100644 --- a/app.js +++ b/app.js @@ -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'); diff --git a/libs/clock.js b/libs/clock.js index eae05dc..9384074 100644 --- a/libs/clock.js +++ b/libs/clock.js @@ -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) } diff --git a/libs/lcd.js b/libs/lcd.js index d7f5c16..5370f3e 100644 --- a/libs/lcd.js +++ b/libs/lcd.js @@ -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); diff --git a/modules/hotwater.js b/modules/hotwater.js index 0ea8755..5bb5ebd 100644 --- a/modules/hotwater.js +++ b/modules/hotwater.js @@ -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(); + } } \ No newline at end of file diff --git a/package.json b/package.json index c56a79a..f58df55 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js index 547f570..ca4c2b0 100644 --- a/test.js +++ b/test.js @@ -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)