From db5597d702f14418918a673b4cf5135c0e82daf0 Mon Sep 17 00:00:00 2001
From: Karl Hudgell <karl@k-world.me.uk>
Date: Mon, 14 Sep 2020 12:11:37 +0100
Subject: [PATCH] working time, screen, organize a bit

---
 app.js                               | 58 +++++++++++++++-------
 libs/clock.js                        | 12 +++++
 libs/common.js                       |  3 ++
 libs/globalVars.js                   |  1 +
 libs/lcd.js                          | 48 +++++++++++++++----
 libs/led.js                          | 35 ++++++++++++++
 modules/hotwater.js                  | 72 ++++++++++++++++++++++++++++
 package-lock.json                    |  5 ++
 package.json                         |  1 +
 poll.js                              | 63 ------------------------
 lcd.js => samples/lcd.js             |  0
 rgb.js => samples/rgb.js             |  0
 webserver.js => samples/webserver.js |  0
 test.js                              |  5 ++
 14 files changed, 215 insertions(+), 88 deletions(-)
 create mode 100644 libs/clock.js
 create mode 100644 libs/led.js
 create mode 100644 modules/hotwater.js
 delete mode 100644 poll.js
 rename lcd.js => samples/lcd.js (100%)
 rename rgb.js => samples/rgb.js (100%)
 rename webserver.js => samples/webserver.js (100%)
 create mode 100644 test.js

diff --git a/app.js b/app.js
index 986ee4b..91557c8 100644
--- a/app.js
+++ b/app.js
@@ -1,24 +1,50 @@
 const { Gpio } = require('onoff');
-const poll = require('./poll')
+const hotwater = require('./modules/hotwater')
 const common = require('./libs/common')
 const globalVars = require('./libs/globalVars')
+const led = require('./libs/led')
 const lcd = require('./libs/lcd')
 
-lcd.heading()
-// lcd.time()
-poll.setInterval
 
-const switchIn = new Gpio('17', 'in', 'both');
+async function main() {
+  lcd.clearScreen()
+  await lcd.intro()
+  lcd.time()
+  hotwater.setInterval
 
-switchIn.watch( async (err, value) => {
-  if (err) {
-    console.log('Error', err);
-  }
-  if (value === 1) {
-    if (globalVars.waterOn === 'true') {
-      common.request(0)
-    } else {
-      common.request(1)
+  const heatingSwitch = new Gpio('17', 'in', 'both');
+  const appSwitch = new Gpio('23', 'in', 'both');
+
+  heatingSwitch.watch(async (err, value) => {
+    if (err) {
+      console.log('Error', err);
     }
-  }
-})
\ No newline at end of file
+    if (value === 1) {
+      if (globalVars.waterOn === 'true') {
+        common.request(0)
+      } else {
+        common.request(1)
+      }
+    }
+  })
+
+  appSwitch.watch(async (err, value) => {
+    if (err) {
+      console.log('Error', err);
+    }
+    if (value === 1) {
+      console.log('pressed')
+    }
+  })
+
+}
+
+process.stdin.resume();
+
+process.on('SIGINT', function () {
+  lcd.clearScreen()
+  led.set('off')
+  process.exit(1)
+});
+
+main()
\ No newline at end of file
diff --git a/libs/clock.js b/libs/clock.js
new file mode 100644
index 0000000..eae05dc
--- /dev/null
+++ b/libs/clock.js
@@ -0,0 +1,12 @@
+const moment = require('moment'); // require
+
+module.exports = {
+    time: () => {
+        try {
+            t = moment().format('HH:MM');
+            return t
+        } catch (error) {
+            console.log(error)
+        }
+    }
+}
diff --git a/libs/common.js b/libs/common.js
index 85e8909..e77186f 100644
--- a/libs/common.js
+++ b/libs/common.js
@@ -15,4 +15,7 @@ module.exports = {
             });
 
     },
+    pause: async (time) => {
+        await new Promise(resolve => setTimeout(resolve, time));
+    },
 }
\ No newline at end of file
diff --git a/libs/globalVars.js b/libs/globalVars.js
index a9dee08..65f019d 100644
--- a/libs/globalVars.js
+++ b/libs/globalVars.js
@@ -1,5 +1,6 @@
 class globalVariables {
     waterOn = "";
+    initialWater = "";
  }
 
 module.exports = new globalVariables();
\ No newline at end of file
diff --git a/libs/lcd.js b/libs/lcd.js
index 8bd57cc..d7f5c16 100644
--- a/libs/lcd.js
+++ b/libs/lcd.js
@@ -1,22 +1,52 @@
 const LCD = require('raspberrypi-liquid-crystal');
+const common = require('./common')
+const globalVars = require('./globalVars')
+const clock = require('./clock');
+const { time } = require('./clock');
 
 const lcd = new LCD(1, 0x27, 20, 4);
 
 lcd.beginSync();
 
 module.exports = {
-    heading: async () => {
+    clearScreen: async () => {
         lcd.clearSync();
-        lcd.printLineSync(0, 'MiGenie Hot Water')
-        lcd.printLineSync(1, '             By Karl')
+    },
+    clearLine: async (line) => {
+        line = line - 1
+        lcd.setCursorSync(0, line);
+        lcd.printSync('                    ')
+    },
+    intro: async () => {
+        lcd.clearSync();
+        lcd.printLineSync(1, 'MiGenie Status')
+        lcd.printLineSync(2, '             By Karl')
+        await common.pause(3000)
+        module.exports.heading('Hot Water')
+    },
+    heading: async (title) => {
+        lcd.clearSync();
+        lcd.printLineSync(2, title)
     },
     time: async () => {
-        lcd.setCursorSync(10,2);
-        lcd.printSync('time')
+        // set interval
+        var tid = setInterval(mycode, 60000);
+        async function mycode() {
+            
+        lcd.setCursorSync(15, 0);
+        let f 
+        f = await clock.time()
+        lcd.printSync(f)
+            
+        }
+        function abortTimer() { // to be called when you want to stop the timer
+            clearInterval(tid);
+        }
     },
-    updateScreen: async (input) => {
-        lcd.setCursorSync(3, 0);
-        lcd.printLineSync(3, input)
+    heatingStatus: async (input) => {
+        lcd.setCursorSync(0, 3);
+        lcd.printSync('                    ')
+        lcd.setCursorSync(0, 3);
+        lcd.printSync('    ' + input)
     },
-
 }
\ No newline at end of file
diff --git a/libs/led.js b/libs/led.js
new file mode 100644
index 0000000..ac6f017
--- /dev/null
+++ b/libs/led.js
@@ -0,0 +1,35 @@
+const { Gpio } = require('onoff');
+
+const redLED = new Gpio('4', 'out');
+const blueLED = new Gpio('27', 'out')
+const greenLED = new Gpio('22', 'out')
+
+
+module.exports = {
+    set: async (colour) => {
+        switch (colour) {
+            case 'red':
+                redLED.writeSync(1)
+                blueLED.writeSync(0)
+                greenLED.writeSync(0)
+                break;
+            case 'blue':
+                blueLED.writeSync(1)
+                redLED.writeSync(0)
+                greenLED.writeSync(0)
+                break;
+            case 'green':
+                blueLED.writeSync(0)
+                redLED.writeSync(0)
+                greenLED.writeSync(1)
+                break;
+            case 'off':
+                blueLED.writeSync(0)
+                redLED.writeSync(0)
+                greenLED.writeSync(0)
+                break;
+            default:
+                break;
+        }
+    },
+}
\ No newline at end of file
diff --git a/modules/hotwater.js b/modules/hotwater.js
new file mode 100644
index 0000000..0ea8755
--- /dev/null
+++ b/modules/hotwater.js
@@ -0,0 +1,72 @@
+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 () {
+
+    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 = "";
+
+        // 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) {
+                    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'
+    });
+
+    request.end();
+}, 5000);
+
+module.exports = {
+    setInterval
+}
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index 154aaa1..d0d718e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -397,6 +397,11 @@
       "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz",
       "integrity": "sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ=="
     },
+    "moment": {
+      "version": "2.28.0",
+      "resolved": "https://registry.npmjs.org/moment/-/moment-2.28.0.tgz",
+      "integrity": "sha512-Z5KOjYmnHyd/ukynmFd/WwyXHd7L4J9vTI/nn5Ap9AVUgaAE15VvQ9MOGmJJygEUklupqIrFnor/tjTwRU+tQw=="
+    },
     "ms": {
       "version": "2.1.2",
       "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
diff --git a/package.json b/package.json
index f58df55..c56a79a 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
     "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/poll.js b/poll.js
deleted file mode 100644
index ea7d768..0000000
--- a/poll.js
+++ /dev/null
@@ -1,63 +0,0 @@
-const http = require('http')
-const { Gpio } = require('onoff');
-const globalVars = require('./libs/globalVars')
-
-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 () {
-
-    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 = "";
-
-        // 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
-            globalVars.waterOn = waterOn
-            if (waterOn === 'true') {
-                lcd.updateScreen('Heating')
-                redLED.writeSync(1)
-                blueLED.writeSync(0)
-                greenLED.writeSync(0)
-
-            } else {
-                lcd.updateScreen('Not Heating')
-                blueLED.writeSync(1)
-                redLED.writeSync(0)
-                greenLED.writeSync(0)
-
-            }
-        });
-    });
-
-    // Report errors
-    request.on('error', function (error) {
-        lcd.updateScreen('Error')
-        redLED.writeSync(0)
-        blueLED.writeSync(0)
-        greenLED.writeSync(1)
-        console.log("Error while calling endpoint.", error);
-    });
-
-    request.end();
-}, 5000);
-
-module.exports = {
-    setInterval
-}
\ No newline at end of file
diff --git a/lcd.js b/samples/lcd.js
similarity index 100%
rename from lcd.js
rename to samples/lcd.js
diff --git a/rgb.js b/samples/rgb.js
similarity index 100%
rename from rgb.js
rename to samples/rgb.js
diff --git a/webserver.js b/samples/webserver.js
similarity index 100%
rename from webserver.js
rename to samples/webserver.js
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..547f570
--- /dev/null
+++ b/test.js
@@ -0,0 +1,5 @@
+const moment = require('moment'); // require
+
+
+            t = moment().format('HH:MM:ss');
+            console.log(t)