Moved to shared
This commit is contained in:
@@ -4,7 +4,7 @@ import time
|
||||
import asyncio
|
||||
import requests
|
||||
|
||||
from orchestrateur.microwave_state import MicrowaveState
|
||||
from shared.microwave_state import MicrowaveState, MicrowaveStateFields
|
||||
from orchestrateur.sensors import gps
|
||||
from shared import get_lora, get_mqtt_client, deviceTypes, config, payloads, db
|
||||
from shared.logging import log
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
from shared.cookingState import CookingStates
|
||||
import shared.config as config
|
||||
|
||||
class MicrowaveStateFields:
|
||||
STATE = "state"
|
||||
PAUSED = "paused"
|
||||
COOKING_STATE = "cooking_state"
|
||||
COOKING_ESTIMATED_REMAINING_TIME = "cooking_estimated_remaining_time"
|
||||
COOKING_START_TIME = "cooking_start_time"
|
||||
|
||||
class MicrowaveState:
|
||||
state = None
|
||||
paused = False
|
||||
@@ -14,11 +21,11 @@ class MicrowaveState:
|
||||
self.cooking_state = CookingStates.IDLE
|
||||
self.on_change_callback = on_change_callback
|
||||
|
||||
def _notify_change(self):
|
||||
def _notify_change(self, field: MicrowaveStateFields = None):
|
||||
"""Invokes callback if registered on state attribute changes."""
|
||||
if self.on_change_callback:
|
||||
try:
|
||||
self.on_change_callback()
|
||||
self.on_change_callback(self, field)
|
||||
except Exception as e:
|
||||
print(f"[MicrowaveState] Error in change callback: {e}")
|
||||
|
||||
@@ -34,16 +41,15 @@ class MicrowaveState:
|
||||
|
||||
if self.state != new_state:
|
||||
self.state = new_state
|
||||
# self._notify_change()
|
||||
self._notify_change(MicrowaveStateFields.STATE)
|
||||
|
||||
def set_paused(self, is_paused):
|
||||
if self.paused != is_paused:
|
||||
self.paused = is_paused
|
||||
self._notify_change()
|
||||
self._notify_change(MicrowaveStateFields.PAUSED)
|
||||
|
||||
def toggle_pause(self):
|
||||
self.paused = not self.paused
|
||||
self._notify_change()
|
||||
self.set_paused(not self.paused)
|
||||
|
||||
def set_cooking_state(self, new_cooking_state):
|
||||
if config.DEBUG:
|
||||
@@ -57,17 +63,17 @@ class MicrowaveState:
|
||||
|
||||
if self.cooking_state != new_cooking_state:
|
||||
self.cooking_state = new_cooking_state
|
||||
self._notify_change()
|
||||
self._notify_change(MicrowaveStateFields.COOKING_STATE)
|
||||
|
||||
def set_cooking_estimated_remaining_time(self, time_seconds):
|
||||
if self.cooking_estimated_remaining_time != time_seconds:
|
||||
self.cooking_estimated_remaining_time = time_seconds
|
||||
self._notify_change()
|
||||
self._notify_change(MicrowaveStateFields.COOKING_ESTIMATED_REMAINING_TIME)
|
||||
|
||||
def set_cooking_start_time(self, start_time):
|
||||
if self.cooking_start_time != start_time:
|
||||
self.cooking_start_time = start_time
|
||||
self._notify_change()
|
||||
self._notify_change(MicrowaveStateFields.COOKING_START_TIME)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
Reference in New Issue
Block a user