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