From 71d2590f7bc5f1c534b3294e3fbe5282b394bb18 Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Mon, 13 Feb 2023 12:24:08 +0100 Subject: [PATCH] fix some bugss Took 23 minutes --- src/ArduinoProxy.ts | 49 +++++++++++++++++++++++++++------------------ src/Mixer.ts | 9 ++++++--- update.sh | 24 ++++++++++++---------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/ArduinoProxy.ts b/src/ArduinoProxy.ts index 8b7c055..ddf86b0 100644 --- a/src/ArduinoProxy.ts +++ b/src/ArduinoProxy.ts @@ -1,4 +1,4 @@ -import { SerialPort } from 'serialport' +import {SerialPort} from 'serialport' import debug from "debug"; import {ArduinoProxyPayload} from "./ArduinoProxyPayload"; import {Utils} from "./Utils"; @@ -12,30 +12,13 @@ export class ArduinoProxy { private static callbacks: Record = {}; private static encoding: string = "utf-8"; - private static onData(data) { - data = data.toString().trim(); - try { - console.debug("Received ", data); - let json = JSON.parse(data) as ArduinoProxyPayload; - if (this.callbacks[json.id]) { - this.callbacks[json.id].resolve(json); - delete this.callbacks[json.id]; - log("Answered request " + json.id); - } else { - log("ERROR - Got an response from arduino but we are not waiting for it?"); - } - } catch (e) { - log("ERROR - Got an invalid response from arduino?"); - } - } - public static connect() { return new Promise(async (resolve, reject) => { log("Connecting to arduino..."); let list = await SerialPort.list(); let arduino = list.find((ele) => { - if( !ele.manufacturer ) return false; + if (!ele.manufacturer) return false; return ele.manufacturer.startsWith("Arduino"); }); @@ -78,7 +61,16 @@ export class ArduinoProxy { } public static sendRequest(request: ArduinoProxyPayload, timeout: number = 1000) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + // Pre Check if connection lost + + if (!this.serialPort.isOpen) + try { + await this.connect(); + } catch (e) { + } + + let id = Utils.generateRandomString(8); request.id = id; @@ -103,4 +95,21 @@ export class ArduinoProxy { }); } + private static onData(data) { + data = data.toString().trim(); + try { + console.debug("Received ", data); + let json = JSON.parse(data) as ArduinoProxyPayload; + if (this.callbacks[json.id]) { + this.callbacks[json.id].resolve(json); + delete this.callbacks[json.id]; + log("Answered request " + json.id); + } else { + log("ERROR - Got an response from arduino but we are not waiting for it?"); + } + } catch (e) { + log("ERROR - Got an invalid response from arduino?"); + } + } + } \ No newline at end of file diff --git a/src/Mixer.ts b/src/Mixer.ts index 73d089d..84af5b0 100644 --- a/src/Mixer.ts +++ b/src/Mixer.ts @@ -10,6 +10,7 @@ import debug from "debug"; import {ArduinoProxyPayload} from "./ArduinoProxyPayload"; import {ArduinoProxyPayloadType} from "./ArduinoProxyPayloadType"; import {ContainerHelper} from "./ContainerHelper"; +import {ErrorHandler, InternalError} from "./ErrorHandler"; const isPI = require("detect-rpi"); @@ -75,9 +76,9 @@ export class Mixer { console.debug(e); if (isPI()) { log("[ERROR] GPIO I/O Error " + e); - - // Todo error handling to user await this.cancelFill(); + let error = new InternalError(e + "", "Mixer.ts catch on startFill", "GPIO Exception"); + await ErrorHandler.sendError(error); return; } else { log("[WARNING] GPIO I/O Error, but it's normal cause you are not on raspberry"); @@ -109,6 +110,8 @@ export class Mixer { if (isPI()) { log("[ERROR] GPIO I/O Error " + e); await this.cancelFill(); + let error = new InternalError(e + "", "Mixer.ts catch on stopFill", "GPIO Exception"); + await ErrorHandler.sendError(error); return; } else { log("[WARNING] GPIO I/O Error, but it's normal cause you are not on raspberry"); @@ -181,7 +184,7 @@ export class Mixer { } - // ToDo v2 calc + let container: IContainer = jobIngredient.container; let deltaStartStop = (this._currentJob.endAt.getTime() - this._currentJob.startedAt.getTime()) / 1000; diff --git a/update.sh b/update.sh index 29ec6e5..4934e0a 100755 --- a/update.sh +++ b/update.sh @@ -1,17 +1,19 @@ #!/bin/bash cd /home/itender/itender || exit -1 -if [ -z "$1" ] -then - git pull "https://tobiash:!IwedwrimmVeudiweN!@git.gaminggeneration.de/tobiash/itender.git" --quiet - ./update.sh true +if [ -z "$1" ]; then + git pull "https://tobiash:!IwedwrimmVeudiweN!@git.gaminggeneration.de/tobiash/itender.git" --quiet + exit 1 + ./update.sh true else - yarn - yarn run compile - cd ./arduino/itender/ - arduino-cli compile --fqbn arduino:avr:mega itender.ino || true - sudo systemctl stop itender - timeout 40 arduino-cli upload --port /dev/ttyACM0 --fqbn arduino:avr:mega itender.ino || true - sudo systemctl start itender + yarn + yarn run compile + cd ./arduino/itender/ + arduino-cli compile --fqbn arduino:avr:mega itender.ino || true + sudo systemctl stop itender + sleep 1 + timeout 30 arduino-cli upload --port /dev/ttyACM0 --fqbn arduino:avr:mega itender.ino + sleep 1 + sudo systemctl start itender fi exit 0