99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
import json
|
|
import os
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
def _years_ago(dt, years):
|
|
try:
|
|
return dt.replace(year=dt.year - years)
|
|
except ValueError:
|
|
return dt.replace(year=dt.year - years, month=2, day=28)
|
|
|
|
|
|
class GunObject:
|
|
|
|
def __init__(self, gun_id="", gun_mac=""):
|
|
self.gun_id = gun_id
|
|
self.gun_mac = gun_mac
|
|
self.count = 0
|
|
self.fired = 0
|
|
self.fired_overall = 0
|
|
self.battery = 0
|
|
self.battery_overall = 0
|
|
self.settings_battery_type = 0
|
|
self.settings_battery_changes = 0
|
|
self.settings_battery_life = 0
|
|
self.settings_fired_max = 0
|
|
self.history_battery_changes = 0
|
|
self.history_battery_usage = 0
|
|
self.history_fired = 0
|
|
self.change_history = []
|
|
self._init_dates()
|
|
|
|
def _init_dates(self):
|
|
self.last_seen = _years_ago(datetime.now() - timedelta(minutes=3957), 23)
|
|
self.last_count = self.last_seen
|
|
self.battery_last_changed = datetime.min
|
|
self.fired_last = datetime.min
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"gun_id": self.gun_id,
|
|
"gun_mac": self.gun_mac,
|
|
"last_seen": self.last_seen.isoformat(),
|
|
"last_count": self.last_count.isoformat(),
|
|
"count": self.count,
|
|
"fired": self.fired,
|
|
"fired_overall": self.fired_overall,
|
|
"battery": self.battery,
|
|
"battery_overall": self.battery_overall,
|
|
"battery_last_changed": self.battery_last_changed.isoformat(),
|
|
"settings_battery_type": self.settings_battery_type,
|
|
"settings_battery_changes": self.settings_battery_changes,
|
|
"settings_battery_life": self.settings_battery_life,
|
|
"settings_fired_max": self.settings_fired_max,
|
|
"history_battery_changes": self.history_battery_changes,
|
|
"history_battery_usage": self.history_battery_usage,
|
|
"history_fired": self.history_fired,
|
|
"fired_last": self.fired_last.isoformat(),
|
|
"change_history": list(self.change_history),
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data):
|
|
gun = cls()
|
|
gun.gun_id = data.get("gun_id", "")
|
|
gun.gun_mac = data.get("gun_mac", "")
|
|
gun.count = data.get("count", 0)
|
|
gun.fired = data.get("fired", 0)
|
|
gun.fired_overall = data.get("fired_overall", 0)
|
|
gun.battery = data.get("battery", 0)
|
|
gun.battery_overall = data.get("battery_overall", 0)
|
|
gun.settings_battery_type = data.get("settings_battery_type", 0)
|
|
gun.settings_battery_changes = data.get("settings_battery_changes", 0)
|
|
gun.settings_battery_life = data.get("settings_battery_life", 0)
|
|
gun.settings_fired_max = data.get("settings_fired_max", 0)
|
|
gun.history_battery_changes = data.get("history_battery_changes", 0)
|
|
gun.history_battery_usage = data.get("history_battery_usage", 0)
|
|
gun.history_fired = data.get("history_fired", 0)
|
|
gun.change_history = list(data.get("change_history", []))
|
|
for name in ("last_seen", "last_count", "battery_last_changed", "fired_last"):
|
|
raw = data.get(name)
|
|
setattr(gun, name, datetime.fromisoformat(raw) if raw else datetime.min)
|
|
return gun
|
|
|
|
def store_gun_info(self):
|
|
try:
|
|
os.makedirs("./items", exist_ok=True)
|
|
path = "./items/{}.cfg".format(self.gun_id)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
json.dump(self.to_dict(), f, indent=1)
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
@classmethod
|
|
def load_gun_info(cls, path):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return cls.from_dict(json.load(f))
|