2020-10-23 23:45:27 +00:00
|
|
|
import json
|
|
|
|
import os
|
2020-10-30 23:59:58 +00:00
|
|
|
import logging
|
2020-11-04 01:19:56 +00:00
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
from copy import copy
|
2020-11-05 18:58:18 +00:00
|
|
|
|
|
|
|
from plan import PlanObject
|
|
|
|
from helpers.logging_manager import LoggingManager
|
2020-10-28 22:04:11 +00:00
|
|
|
from helpers.os_environment import resolve_external_file_path
|
2020-10-23 23:45:27 +00:00
|
|
|
|
2020-10-24 13:44:26 +00:00
|
|
|
|
|
|
|
class StateManager:
|
2020-10-23 23:45:27 +00:00
|
|
|
filepath = None
|
2020-10-30 23:59:58 +00:00
|
|
|
logger = None
|
2020-10-23 23:45:27 +00:00
|
|
|
__state = {}
|
2020-11-04 01:19:56 +00:00
|
|
|
__state_in_file = {}
|
|
|
|
# Dict of times that params can be updated after, if the time is before current time, it can be written immediately.
|
|
|
|
__rate_limit_params_until = {}
|
|
|
|
__rate_limit_period_s = 0
|
2020-10-23 23:45:27 +00:00
|
|
|
|
2020-11-04 01:19:56 +00:00
|
|
|
|
|
|
|
def __init__(self, name, logger: LoggingManager, default_state=None, rate_limit_params=[], rate_limit_period_s = 5):
|
2020-10-30 23:59:58 +00:00
|
|
|
self.logger = logger
|
|
|
|
|
2020-10-28 22:04:11 +00:00
|
|
|
self.filepath = resolve_external_file_path("/state/" + name + ".json")
|
2020-10-30 23:59:58 +00:00
|
|
|
self._log("State file path set to: " + self.filepath)
|
|
|
|
|
2020-10-23 23:45:27 +00:00
|
|
|
if not os.path.isfile(self.filepath):
|
2020-10-30 23:59:58 +00:00
|
|
|
self._log("No existing state file found.")
|
2020-10-23 23:45:27 +00:00
|
|
|
try:
|
|
|
|
# Try creating the file.
|
|
|
|
open(self.filepath, "x")
|
|
|
|
except:
|
2020-10-30 23:59:58 +00:00
|
|
|
self._log("Failed to create state file.", logging.CRITICAL)
|
2020-10-23 23:45:27 +00:00
|
|
|
return
|
|
|
|
|
2020-10-30 23:59:58 +00:00
|
|
|
with open(self.filepath, 'r') as file:
|
|
|
|
file_state = file.read()
|
2020-10-23 23:45:27 +00:00
|
|
|
|
|
|
|
if file_state == "":
|
2020-10-30 23:59:58 +00:00
|
|
|
self._log("State file is empty. Setting default state.")
|
2020-11-05 18:58:18 +00:00
|
|
|
self.state = default_state
|
2020-11-04 01:19:56 +00:00
|
|
|
self.__state_in_file = copy(self.state)
|
2020-10-23 23:45:27 +00:00
|
|
|
else:
|
2020-10-30 23:59:58 +00:00
|
|
|
try:
|
2020-11-05 18:58:18 +00:00
|
|
|
file_state = json.loads(file_state)
|
2020-11-04 20:33:09 +00:00
|
|
|
|
|
|
|
# Turn from JSON -> PlanObject
|
2020-11-05 18:58:18 +00:00
|
|
|
file_state["loaded_item"] = PlanObject(file_state["loaded_item"]) if file_state["loaded_item"] else None
|
|
|
|
|
|
|
|
file_state["show_plan"] = [PlanObject(obj) for obj in file_state["show_plan"]]
|
2020-11-04 20:33:09 +00:00
|
|
|
|
2020-11-05 18:58:18 +00:00
|
|
|
# Now feed the loaded state into the initialised state manager.
|
|
|
|
self.state = file_state
|
2020-10-30 23:59:58 +00:00
|
|
|
except:
|
|
|
|
self._logException("Failed to parse state JSON. Resetting to default state.")
|
2020-11-05 18:58:18 +00:00
|
|
|
self.state = default_state
|
2020-11-04 20:33:09 +00:00
|
|
|
self.__state_in_file = copy(self.state)
|
2020-10-23 23:45:27 +00:00
|
|
|
|
2020-11-04 01:19:56 +00:00
|
|
|
# Now setup the rate limiting
|
|
|
|
# Essentially rate limit all values to "now" to start with, allowing the first update
|
|
|
|
# of all vars to succeed.
|
|
|
|
for param in rate_limit_params:
|
|
|
|
self.__rate_limit_params_until[param] = self._currentTimeS
|
|
|
|
self.__rate_limit_period_s = rate_limit_period_s
|
|
|
|
|
2020-10-23 23:45:27 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2020-11-04 01:19:56 +00:00
|
|
|
return copy(self.__state)
|
2020-10-23 23:45:27 +00:00
|
|
|
|
|
|
|
@state.setter
|
|
|
|
def state(self, state):
|
2020-11-04 01:19:56 +00:00
|
|
|
self.__state = copy(state)
|
|
|
|
|
|
|
|
def write_to_file(self,state):
|
|
|
|
if self.__state_in_file == state:
|
|
|
|
# No change to be updated.
|
|
|
|
return
|
|
|
|
|
|
|
|
self.__state_in_file = state
|
2020-10-23 23:45:27 +00:00
|
|
|
|
2020-11-04 01:19:56 +00:00
|
|
|
# Make sure we're not manipulating state
|
2020-11-04 20:33:09 +00:00
|
|
|
state_to_json = copy(state)
|
2020-11-04 01:19:56 +00:00
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
|
|
|
current_time = now.strftime("%H:%M:%S")
|
2020-11-04 20:33:09 +00:00
|
|
|
state_to_json["last_updated"] = current_time
|
|
|
|
|
|
|
|
# Not the biggest fan of this, but maybe I'll get a better solution for this later
|
|
|
|
state_to_json["loaded_item"] = state_to_json["loaded_item"].__dict__ if state_to_json["loaded_item"] else None
|
|
|
|
state_to_json["show_plan"] = [repr.__dict__ for repr in state_to_json["show_plan"]]
|
2020-10-30 23:59:58 +00:00
|
|
|
try:
|
2020-11-04 20:33:09 +00:00
|
|
|
state_json = json.dumps(state_to_json, indent=2, sort_keys=True)
|
2020-10-30 23:59:58 +00:00
|
|
|
except:
|
|
|
|
self._logException("Failed to dump JSON state.")
|
|
|
|
else:
|
|
|
|
with open(self.filepath, "w") as file:
|
|
|
|
file.write(state_json)
|
2020-10-23 23:45:27 +00:00
|
|
|
|
|
|
|
def update(self, key, value):
|
2020-11-04 01:19:56 +00:00
|
|
|
update_file = True
|
|
|
|
if (key in self.__rate_limit_params_until.keys()):
|
|
|
|
# The key we're trying to update is expected to be updating very often,
|
|
|
|
# We're therefore going to check before saving it.
|
|
|
|
if self.__rate_limit_params_until[key] > self._currentTimeS:
|
|
|
|
update_file = False
|
|
|
|
else:
|
|
|
|
self.__rate_limit_params_until[key] = self._currentTimeS + self.__rate_limit_period_s
|
|
|
|
|
|
|
|
|
|
|
|
state_to_update = self.state
|
|
|
|
|
2020-11-04 20:33:09 +00:00
|
|
|
if key in state_to_update and state_to_update[key] == value:
|
2020-11-04 01:19:56 +00:00
|
|
|
# We're trying to update the state with the same value.
|
|
|
|
# In this case, ignore the update
|
|
|
|
return
|
|
|
|
|
|
|
|
state_to_update[key] = value
|
|
|
|
|
|
|
|
self.state = state_to_update
|
|
|
|
|
|
|
|
if (update_file == True):
|
|
|
|
self.write_to_file(state_to_update)
|
2020-10-23 23:45:27 +00:00
|
|
|
|
2020-10-30 23:59:58 +00:00
|
|
|
def _log(self, text, level=logging.INFO):
|
|
|
|
self.logger.log.log(level, "State Manager: " + text)
|
|
|
|
|
|
|
|
def _logException(self, text):
|
|
|
|
self.logger.log.exception("State Manager: " + text)
|
2020-11-04 01:19:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _currentTimeS(self):
|
|
|
|
return time.time()
|