diff --git a/Scripts/Common.py b/Scripts/Common.py index ebbfe99..6a44e87 100644 --- a/Scripts/Common.py +++ b/Scripts/Common.py @@ -49,7 +49,7 @@ MAX_WATER_LEVEL = TANK_HEIGHT - TOP_EMPTY_DISTANCE TWO_THIRD_LEVEL = MAX_WATER_LEVEL // 3 * 2 # Two-thirds full level -CHECK_INTERVAL_SECONDS = 5 # Check distance every 5 seconds +CHECK_INTERVAL_SECONDS = 0.5 # Check distance every 0.5 seconds MAX_PRE_NIGHT_FILL_TIME = 600 NIGHT_12AM = 0 diff --git a/Scripts/Main.py b/Scripts/Main.py index 12d9c5c..b004d3f 100644 --- a/Scripts/Main.py +++ b/Scripts/Main.py @@ -171,7 +171,7 @@ def main(): morning8RunDone = True # Afternoon valve operation - if now.hour == AFTERNOON_5PM and not aftrn5RunDone: + if now.hour == NOON_12PM and not aftrn5RunDone: print("Afternoon run: Activating valves.") gpio.output(VALVE1_PIN, True) gpio.output(VALVE2_PIN, True) @@ -182,7 +182,7 @@ def main(): aftrn5RunDone = True # Evening valve operation - if now.hour == NIGHT_9PM and not evening9RunDone: + if now.hour == AFTERNOON_5PM and not evening9RunDone: print("Evening run: Activating valves.") gpio.output(VALVE1_PIN, True) gpio.output(VALVE2_PIN, True) @@ -217,16 +217,17 @@ def main(): waterLevel = TANK_HEIGHT - distance motorStatus = rtDb.get("motorStatus", "OFF") + print(f"!!!! Motor Status {motorStatus}; Config Update Available: {configUpdateAvailable} !!!!") if configUpdateAvailable: # Apply config from rtDb.json both for Manual and Auto modes if motorStatus == "ON": gpio.output(MOTOR_PIN, True) - print(f"{currentMode} mode - Config update: Motor ON") + print(f">>> {currentMode} mode - Config update: Motor ON") else: gpio.output(MOTOR_PIN, False) - print(f"{currentMode} mode - Config update: Motor OFF") + print(f">>> {currentMode} mode - Config update: Motor OFF") if currentMode == "Manual": pass @@ -269,10 +270,19 @@ def main(): lastCheckTime = currentTime - if configUpdateAvailable or motorStatus != lastMotorStatus or waterLevel != lastWaterLevel: - writeRtDb(motorStatus = motorStatus, tankLevel = waterLevel, configUpdateAvailable = False) + # Update database if there are changes + dbUpdates = {} + if motorStatus != lastMotorStatus: + dbUpdates["motorStatus"] = motorStatus lastMotorStatus = motorStatus - lastWaterLevel = waterLevel + + if waterLevel != lastWaterLevel: + dbUpdates["tankLevel"] = waterLevel + lastWaterLevel = waterLevel + + if dbUpdates: + print(f"Updating DB with: {dbUpdates}") + writeRtDb(**dbUpdates) # Smart sleep: shorter sleep when config updates are expected if configUpdateAvailable: diff --git a/Scripts/Utility.py b/Scripts/Utility.py index ec66978..80e6e01 100644 --- a/Scripts/Utility.py +++ b/Scripts/Utility.py @@ -10,16 +10,11 @@ def readRtDb(): return {} -def writeRtDb(motorStatus=None, tankLevel=None, configUpdateAvailable=None, mode=None): +def writeRtDb(**kwargs): db = readRtDb() - if motorStatus is not None: - db["motorStatus"] = motorStatus - if tankLevel is not None: - db["tankLevel"] = tankLevel - if configUpdateAvailable is not None: - db["configUpdateAvailable"] = configUpdateAvailable - if mode is not None: - db["mode"] = mode + if not kwargs: + return # No changes to write + db.update(kwargs) try: with open(RT_DB_FILE, 'w') as f: json.dump(db, f) diff --git a/config.yaml b/config.yaml index 1b27551..9fc0c4f 100644 --- a/config.yaml +++ b/config.yaml @@ -1,2 +1,2 @@ -appVersion: 1.3.2.1009 +appVersion: 1.3.3.1010