From 59bb0437e17c5f83cd5a419d3978a71a0485107b Mon Sep 17 00:00:00 2001
From: Tobias Hopp <tobi@gaminggeneration.de>
Date: Wed, 1 Feb 2023 14:44:42 +0100
Subject: [PATCH] update

Took 38 minutes
---
 arduino/itender/itender.ino |  3 ++-
 src/ArduinoProxy.ts         | 13 ++++++++++---
 src/Mixer.ts                | 10 +++++++---
 src/SensorHelper.ts         |  2 +-
 src/Utils.ts                |  4 ++--
 src/web/Setup.ts            |  4 +++-
 6 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/arduino/itender/itender.ino b/arduino/itender/itender.ino
index 5ae5d99..bc11aa6 100644
--- a/arduino/itender/itender.ino
+++ b/arduino/itender/itender.ino
@@ -5,7 +5,7 @@
 #include "HX711.h"
 
 // Define the size of the JSON buffer
-#define JSON_BUFFER_SIZE 256
+// #define JSON_BUFFER_SIZE 256
 
 // Create a JSON object for incoming messages
 StaticJsonDocument<JSON_BUFFER_SIZE> incomingJson;
@@ -97,6 +97,7 @@ void loop() {
 
       // Send the outgoing JSON message
       serializeJson(outgoingJson, Serial);
+      Serial.println();
     }
   }
 }
diff --git a/src/ArduinoProxy.ts b/src/ArduinoProxy.ts
index ef4beb5..8b7c055 100644
--- a/src/ArduinoProxy.ts
+++ b/src/ArduinoProxy.ts
@@ -15,7 +15,7 @@ export class ArduinoProxy {
     private static onData(data) {
         data = data.toString().trim();
         try {
-            log("Receiving " + data);
+            console.debug("Received ", data);
             let json = JSON.parse(data) as ArduinoProxyPayload;
             if (this.callbacks[json.id]) {
                 this.callbacks[json.id].resolve(json);
@@ -80,10 +80,17 @@ export class ArduinoProxy {
     public static sendRequest(request: ArduinoProxyPayload, timeout: number = 1000) {
         return new Promise<ArduinoProxyPayload>((resolve, reject) => {
             let id = Utils.generateRandomString(8);
+
             request.id = id;
             this.callbacks[id] = {resolve: resolve, reject: reject};
-            log("Sending " + request);
-            this.serialPort.write(JSON.stringify(request), ArduinoProxy.encoding, (err: Error | null | undefined) => {
+            let formatted = {
+                id: request.id,
+                type: request.type,
+                data: request.data
+            }
+
+            console.debug("Sending...", formatted);
+            this.serialPort.write(JSON.stringify(formatted), ArduinoProxy.encoding, (err: Error | null | undefined) => {
                 if (err) {
                     reject("I/O error on request " + id);
                 }
diff --git a/src/Mixer.ts b/src/Mixer.ts
index f7e8a29..b06402e 100644
--- a/src/Mixer.ts
+++ b/src/Mixer.ts
@@ -61,20 +61,24 @@ export class Mixer {
             // Start pump here
             try {
                 if (x.container.useProxy) {
+                    console.log("Using proxy as filler");
                     let payload = new ArduinoProxyPayload(ArduinoProxyPayloadType.SET_PIN, {
                         pin: x.container.pumpPin,
                         mode: "DIGITAL",
                         value: 255
                     });
-                    await ArduinoProxy.sendRequest(payload);
+                    await payload.send();
                 } else {
+                    console.log("Using normal gpio rpi");
                     await MyGPIO.setup(x.container.pumpPin, GPIO.DIR_OUT)
                     await MyGPIO.write(x.container.pumpPin, true);
                 }
 
             } catch (e) {
+                console.debug(e);
                 if (isPI()) {
                     log("[ERROR] GPIO I/O Error " + e);
+
                     // Todo error handling to user
                     await this.cancelFill();
                     return;
@@ -99,7 +103,7 @@ export class Mixer {
                             mode: "DIGITAL",
                             "value": 0
                         });
-                        await ArduinoProxy.sendRequest(payload);
+                        await payload.send();
                     } else {
                         await MyGPIO.write(x.container.pumpPin, false);
                     }
@@ -172,7 +176,7 @@ export class Mixer {
                         mode: "DIGITAL",
                         "value": 0
                     });
-                    await ArduinoProxy.sendRequest(payload);
+                    await payload.send();
                 } else {
                     await MyGPIO.write(jobIngredient.container.pumpPin, false);
                 }
diff --git a/src/SensorHelper.ts b/src/SensorHelper.ts
index 9f64c9f..c5ebedc 100644
--- a/src/SensorHelper.ts
+++ b/src/SensorHelper.ts
@@ -26,7 +26,7 @@ export class SensorHelper {
                             pin_data: container.sensorPin1,
                             pin_clock: container.sensorPin2
                         });
-                        let val = await ArduinoProxy.sendRequest(payload);
+                        let val = await payload.send();
                         if (!val.data.value)
                             return reject("");
 
diff --git a/src/Utils.ts b/src/Utils.ts
index 57aa222..57d2adf 100644
--- a/src/Utils.ts
+++ b/src/Utils.ts
@@ -85,11 +85,11 @@ export class Utils {
 
     }
 
-    static generateRandomString(number: number) {
+    static generateRandomString(len: number) {
         let result = '';
         const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
         const charactersLength = characters.length;
-        for (let i = 0; i < length; i++) {
+        for (let i = 0; i < len; i++) {
             result += characters.charAt(Math.floor(Math.random() * charactersLength));
         }
         return result;
diff --git a/src/web/Setup.ts b/src/web/Setup.ts
index 1d831ac..428c8b8 100644
--- a/src/web/Setup.ts
+++ b/src/web/Setup.ts
@@ -161,7 +161,7 @@ Dort werden die Behälter definiert, welche in den iTender gestellt werden.<br>D
                 return;
             }
 
-            setupSaveBtn.disabled = true;
+
 
 
             const ledCheckbox = document.getElementById("ledCheckbox") as HTMLInputElement;
@@ -210,6 +210,8 @@ Dort werden die Behälter definiert, welche in den iTender gestellt werden.<br>D
                 return;
             }
 
+            setupSaveBtn.disabled = true;
+
             let saveModal = new Modal("setup", "Setup");
             let txt = document.createElement("p");
             txt.innerHTML = `Die Einstellungen werden gespeichert...<br>