From 07b3aeab94223f521bc833859b35165ecc71515d Mon Sep 17 00:00:00 2001 From: mkruszyna Date: Sat, 14 Dec 2024 14:07:44 +0100 Subject: [PATCH 1/3] feat: demo version for customer --- Devices/LawnMower.py | 38 +------------- Devices/WeatherStation.py | 42 ++------------- Devices/bulb.py | 29 +--------- Devices/curtain.py | 2 +- Devices/device.py | 3 +- Devices/plug.py | 2 +- Devices/thermostat.py | 2 +- devices.json | 30 +++++------ ui_terminal.py | 108 ++++++++++++++++++++++++++++++++++++-- 9 files changed, 130 insertions(+), 126 deletions(-) diff --git a/Devices/LawnMower.py b/Devices/LawnMower.py index d7ff59e..9690173 100644 --- a/Devices/LawnMower.py +++ b/Devices/LawnMower.py @@ -1,28 +1,10 @@ -from device import NotCompatibleDevice, Device, load_json, save_json +from .device import NotCompatibleDevice, Device, load_json, save_json class LawnMower(Device): def __init__(self, device_id: str, device_type: str): super().__init__(device_id, device_type) - def get_name(self) -> str | None: - """ - Get the name of the lawn mower. - """ - return self.load_device_info().get('name', None) - - def get_brand(self) -> str | None: - """ - Get the brand of the lawn mower. - """ - return self.load_device_info().get('brand', None) - - def get_model(self) -> str | None: - """ - Get the model of the lawn mower. - """ - return self.load_device_info().get('model', None) - def get_power(self) -> str | None: """ Get the current power status of the lawn mower (on or off). @@ -59,33 +41,15 @@ def get_total_cutting_time(self) -> int | None: """ return self.load_device_info().get('status', {}).get('total_cutting_time_minutes', None) - def get_location(self) -> str | None: - """ - Get the location of the lawn mower. - """ - return self.load_device_info().get('location', None) - - def get_last_updated(self) -> str | None: - """ - Get the last updated time of the lawn mower. - """ - return self.load_device_info().get('last_updated', None) - if __name__ == "__main__": lawn_mower_device = LawnMower("mower98765", "LawnMower") lawn_mower_device.connect_to_device() - - print("Name:", lawn_mower_device.get_name()) - print("Brand:", lawn_mower_device.get_brand()) - print("Model:", lawn_mower_device.get_model()) print("Power:", lawn_mower_device.get_power()) print("Battery Percentage:", lawn_mower_device.get_battery_percent()) print("Cutting Mode:", lawn_mower_device.get_cutting_mode()) print("Cutting Height (mm):", lawn_mower_device.get_cutting_height()) print("Current Area (m²):", lawn_mower_device.get_current_area()) print("Total Cutting Time (minutes):", lawn_mower_device.get_total_cutting_time()) - print("Location:", lawn_mower_device.get_location()) - print("Last Updated:", lawn_mower_device.get_last_updated()) lawn_mower_device.turn_on_off("off") diff --git a/Devices/WeatherStation.py b/Devices/WeatherStation.py index 8dc2594..07ec425 100644 --- a/Devices/WeatherStation.py +++ b/Devices/WeatherStation.py @@ -1,27 +1,9 @@ -from device import NotCompatibleDevice, Device, load_json, save_json +from .device import NotCompatibleDevice, Device, load_json, save_json class WeatherStation(Device): def __init__(self, device_id: str, device_type: str): - super().__init__(device_id, device_type) - - def get_name(self) -> str: - """ - Get the name of the weather station. - """ - return self.load_device_info().get('name', None) - - def get_brand(self) -> str: - """ - Get the brand of the weather station. - """ - return self.load_device_info().get('brand', None) - - def get_model(self) -> str: - """ - Get the model of the weather station. - """ - return self.load_device_info().get('model', None) + super().__init__(device_id, device_type) def get_temperature(self) -> float: """ @@ -51,34 +33,18 @@ def get_rainfall(self) -> float: """ Get the rainfall measurement in millimeters. """ - return self.load_device_info().get('status', {}).get('rainfall_mm', None) - - def get_location(self) -> str: - """ - Get the location of the weather station. - """ - return self.load_device_info().get('location', None) - - def get_last_updated(self) -> str: - """ - Get the last updated timestamp of the weather station. - """ - return self.load_device_info().get('last_updated', None) + return self.load_device_info().get('status', {}).get('rainfall_mm', None) if __name__ == "__main__": weather_station_device = WeatherStation("weatherstation56789", "WeatherStation") weather_station_device.connect_to_device() - print("Name:", weather_station_device.get_name()) - print("Brand:", weather_station_device.get_brand()) - print("Model:", weather_station_device.get_model()) + print("Temperature (C):", weather_station_device.get_temperature()) print("Humidity (%):", weather_station_device.get_humidity()) print("Pressure (hPa):", weather_station_device.get_pressure()) print("Wind Speed (km/h):", weather_station_device.get_wind_speed()) print("Rainfall (mm):", weather_station_device.get_rainfall()) - print("Location:", weather_station_device.get_location()) - print("Last Updated:", weather_station_device.get_last_updated()) weather_station_device.turn_on_off("off") diff --git a/Devices/bulb.py b/Devices/bulb.py index 1f56a68..1e3ce2e 100644 --- a/Devices/bulb.py +++ b/Devices/bulb.py @@ -1,34 +1,10 @@ -from device import NotCompatibleDevice, Device, load_json, save_json +from .device import NotCompatibleDevice, Device, load_json, save_json class Bulb(Device): def __init__(self, device_id: str, device_type: str): super().__init__(device_id, device_type) - def get_name(self) -> str | None: - """ - Get the name of the bulb. - - The name of the bulb as a string, or None if not found. - """ - return self.load_device_info().get('name', None) - - def get_brand(self) -> str | None: - """ - Get the brand of the bulb. - - The brand of the bulb as a string, or None if not found. - """ - return self.load_device_info().get('brand', None) - - def get_model(self) -> str | None: - """ - Get the model of the bulb. - - The model of the bulb as a string, or None if not found. - """ - return self.load_device_info().get('model', None) - def get_power(self) -> str | None: """ Get the current power status of the bulb (on or off). @@ -64,9 +40,6 @@ def get_rgb(self) -> dict | None: if __name__ == '__main__': bulb_device = Bulb("1234567890abcdef", "Bulb") bulb_device.connect_to_device() - print("Name:", bulb_device.get_name()) - print("Brand:", bulb_device.get_brand()) - print("Model:", bulb_device.get_model()) print("Power:", bulb_device.get_power()) print("Brightness:", bulb_device.get_brightness()) print("Color Temperature:", bulb_device.get_color_temp()) diff --git a/Devices/curtain.py b/Devices/curtain.py index 9ee45b9..dd10106 100644 --- a/Devices/curtain.py +++ b/Devices/curtain.py @@ -1,4 +1,4 @@ -from device import NotCompatibleDevice, Device, load_json +from .device import NotCompatibleDevice, Device, load_json class Curtain(Device): def __init__(self, device_id: str, device_type: str): diff --git a/Devices/device.py b/Devices/device.py index bf31f9a..31410b3 100644 --- a/Devices/device.py +++ b/Devices/device.py @@ -2,13 +2,14 @@ import json import sys import os +sys.path.append(os.path.dirname(os.path.abspath(__file__))) from datetime import datetime from colorama import Fore, init sys.path.append(os.path.abspath('..')) from logging_config import get_logger -FILE_PATH = "../devices.json" +FILE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..\\devices.json") MAX_ATTEMPTS = 3 # Initialize Colorama (necessary for Windows compatibility) init(autoreset=True) diff --git a/Devices/plug.py b/Devices/plug.py index 1b6aec6..dbf6dd1 100644 --- a/Devices/plug.py +++ b/Devices/plug.py @@ -1,4 +1,4 @@ -from device import NotCompatibleDevice, Device, load_json +from .device import NotCompatibleDevice, Device, load_json class Plug(Device): def __init__(self, device_id: str, device_type: str): diff --git a/Devices/thermostat.py b/Devices/thermostat.py index 622cb27..dfe855c 100644 --- a/Devices/thermostat.py +++ b/Devices/thermostat.py @@ -1,4 +1,4 @@ -from device import NotCompatibleDevice, Device, load_json +from .device import NotCompatibleDevice, Device, load_json class Thermostat(Device): def __init__(self, device_id: str, device_type: str): diff --git a/devices.json b/devices.json index 51c081e..34aa48e 100644 --- a/devices.json +++ b/devices.json @@ -1,14 +1,14 @@ { "devices": [ { - "device_id": "1234567890abcdef", + "device_id": "bulb1", "device_secret_key": "xxx", "name": "Smart Light Bulb", "type": "bulb", "brand": "Tuya", "model": "TY-SLB-01", "status": { - "power": "OFF", + "power": "on", "brightness": 80, "color_temp": 5000, "rgb": { @@ -19,10 +19,10 @@ }, "location": "Living Room", "connected": true, - "last_updated": "2024-12-01 20:12:21" + "last_updated": "2024-12-14 13:43:52" }, { - "device_id": "abcdef1234567890", + "device_id": "bulb2", "device_secret_key": "xxx", "name": "Smart Light Bulb", "type": "bulb", @@ -43,7 +43,7 @@ "last_updated": "2024-10-19T14:52:00Z" }, { - "device_id": "1234567890abcdef", + "device_id": "bulb3", "device_secret_key": "xxx", "name": "Smart Light Bulb", "type": "bulb", @@ -64,7 +64,7 @@ "last_updated": "2024-10-19T14:52:00Z" }, { - "device_id": "abcdef1234567999", + "device_id": "plug1", "device_secret_key": "xxx", "name": "new_plug_name", "type": "plug", @@ -82,7 +82,7 @@ "last_updated": "2024-12-01 20:04:41" }, { - "device_id": "abcdef1234561547", + "device_id": "plug2", "device_secret_key": "xxx", "name": "Smart Plug", "type": "plug", @@ -100,7 +100,7 @@ "last_updated": "2024-10-19T14:50:00Z" }, { - "device_id": "abcdef123456578", + "device_id": "plug3", "device_secret_key": "xxx", "name": "Smart Plug", "type": "plug", @@ -118,7 +118,7 @@ "last_updated": "2024-10-19T14:50:00Z" }, { - "device_id": "abcdef1234563298", + "device_id": "thermostat1", "device_secret_key": "xxx", "name": "Smart Thermostat", "type": "thermostat", @@ -135,7 +135,7 @@ "last_updated": "2024-10-19T14:55:00Z" }, { - "device_id": "curtain12345", + "device_id": "curtain1", "device_secret_key": "xxx", "name": "Automatic Curtain", "type": "curtain", @@ -151,7 +151,7 @@ "last_updated": "2024-10-19T14:58:00Z" }, { - "device_id": "weatherstation56789", + "device_id": "station1", "device_secret_key": "xxx", "name": "Smart Weather Station", "type": "weather_station", @@ -170,14 +170,14 @@ "last_updated": "2024-12-01 20:26:09" }, { - "device_id": "mower98765", + "device_id": "mower1", "device_secret_key": "xxx", "name": "Automatic Lawn Mower", - "type": "lawn_mower", + "type": "lawnmower", "brand": "Tuya", "model": "TY-AM-01", "status": { - "power": "off", + "power": "on", "battery_percent": 85, "cutting_mode": "auto", "cutting_height_mm": 30, @@ -186,7 +186,7 @@ }, "location": "Backyard", "connected": true, - "last_updated": "2024-12-01 20:20:42" + "last_updated": "2024-12-14 14:02:46" } ] } \ No newline at end of file diff --git a/ui_terminal.py b/ui_terminal.py index 0afe741..cfad516 100644 --- a/ui_terminal.py +++ b/ui_terminal.py @@ -1,6 +1,18 @@ import json import os from typing import Dict, Any +import itertools +import sys +sys.path.append(os.path.dirname(os.path.abspath(__file__))) +from types import FunctionType +from Devices.device import Device +from Devices.bulb import Bulb +from Devices.curtain import Curtain +from Devices.LawnMower import LawnMower +from Devices.plug import Plug +from Devices.thermostat import Thermostat +from Devices.WeatherStation import WeatherStation + def save_devices(devices: Dict[str, Any]) -> None: @@ -38,10 +50,10 @@ def print_devices(devices_data: Dict[str, Any]) -> None: if not devices_data["devices"]: print("No devices in the system.") return - + print("\n") for index, device in enumerate(devices_data["devices"]): power_status = device['status'].get('power', 'unknown') - print(f"{index + 1}. {device['name']} ({device['location']}) - Power: {power_status}") + print(f"{index + 1}. Name :{device['name']} ({device['location']}) - Device id: {device['device_id']}, device type: {device['type']}, power: {power_status}") def add_device(devices_data: Dict[str, Any]) -> None: @@ -67,16 +79,101 @@ def remove_device(devices_data: Dict[str, Any]) -> None: print("Enter a valid number.") +def create_device_object(device_id: str, device_type: str): + match device_type: + case "plug": + return Plug(device_id, device_type) + case "bulb": + return Bulb(device_id, device_type) + case "curtain": + return Curtain(device_id, device_type) + case "lawnmower": + return LawnMower(device_id, device_type) + case "thermostat": + return Thermostat(device_id, device_type) + case "weather_station": + return WeatherStation(device_id, device_type) + case _: + print("Unsupported device type") + return None + +def get_specific_operations(device): + return [ + x for x, y in type(device).__dict__.items() + if isinstance(y, (FunctionType, classmethod, staticmethod)) and x != "__init__" + ] + +def loop_specific_operations(device, specific_operations): + specific_operations.append("Return") + while True: + for index, element in enumerate(specific_operations): + print(f"{index}: {element}") + + specific_operation = int(input("\nChoose an option number: ")) + if hasattr(device, specific_operations[specific_operation]): + method = getattr(device, specific_operations[specific_operation]) + print(f"\n{method()}\n") + elif specific_operations[specific_operation] == "Return": + return + else: + print(f"Method does not exist.") + +def interact_with_device(): + device_id = input("Enter device id: ") + device_type = input("Enter device type: ") + device = create_device_object(device_id, device_type) + if device is None: + return + device.connect_to_device() + while True: + print("\nChoose an option:") + print("1. Get device Status") + print("2. Display all device info") + print("3. Turn on") + print("4. Turn off") + print("5. Reboot") + print("6. Change device name") + print("7. Change device password") + print("8. Specific operations") + print("9. Return to menu") + option = input("Choose an option number: ") + match option: + case "1": + print(device.get_status()) + case "2": + device.display_device_info() + case "3": + device.turn_on_off("on") + case "4": + device.turn_on_off("off") + case "5": + device.reboot() + case "6": + new_name = input("Enter new device name ") + device.change_device_name(new_name) + case "7": + device.change_device_password() + case "8": + specific_operations = get_specific_operations(device) + loop_specific_operations(device, specific_operations) + case "9": + device.disconnect_from_device() + return + case _: + print("Unsupported operation") + def menu() -> None: - devices_data = load_devices() + while True: + devices_data = load_devices() print("\nChoose an option:") print("1. Show devices") print("2. Toggle device") print("3. Add device") print("4. Remove device") - print("5. Save and exit") + print("5. Interact with device") + print("6. Save and exit") option = input("Choose an option number: ") @@ -102,6 +199,9 @@ def menu() -> None: remove_device(devices_data) elif option == "5": + interact_with_device() + + elif option == "6": save_devices(devices_data) print("Data has been saved.") break From 675e9939a6244f01608dab955812576bf2e2aaca Mon Sep 17 00:00:00 2001 From: mkruszyna Date: Sat, 14 Dec 2024 14:13:35 +0100 Subject: [PATCH 2/3] feat: add logs print + some logs for demo --- logs/app.log | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ ui_terminal.py | 14 +++++++-- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 logs/app.log diff --git a/logs/app.log b/logs/app.log new file mode 100644 index 0000000..4fa0525 --- /dev/null +++ b/logs/app.log @@ -0,0 +1,79 @@ +2024-12-14 13:11:45,886 - INFO - Connecting to device bulb1... +2024-12-14 13:12:41,405 - INFO - Connecting to device bulb1... +2024-12-14 13:13:18,973 - INFO - Connecting to device bulb1... +2024-12-14 13:14:47,437 - INFO - Connecting to device bulb1... +2024-12-14 13:20:08,842 - INFO - Connecting to device bulb1... +2024-12-14 13:20:11,097 - INFO - Successfully connected to device bulb1. +2024-12-14 13:20:17,081 - INFO - Getting status for device bulb1 +2024-12-14 13:21:47,305 - INFO - Connecting to device bulb1... +2024-12-14 13:21:49,328 - INFO - Successfully connected to device bulb1. +2024-12-14 13:22:00,448 - INFO - Getting status for device bulb1 +2024-12-14 13:22:05,932 - INFO - Device bulb1 power set to on. +2024-12-14 13:22:07,016 - INFO - Getting status for device bulb1 +2024-12-14 13:22:13,704 - INFO - Rebooting device bulb1... +2024-12-14 13:22:13,707 - INFO - Device bulb1 power set to off. +2024-12-14 13:22:13,711 - INFO - Device bulb1 power set to on. +2024-12-14 13:22:13,711 - INFO - Device bulb1 is back online. +2024-12-14 13:22:19,784 - INFO - Getting status for device bulb1 +2024-12-14 13:24:17,864 - INFO - Connecting to device bulb1... +2024-12-14 13:24:20,016 - INFO - Successfully connected to device bulb1. +2024-12-14 13:24:27,727 - INFO - Getting status for device bulb1 +2024-12-14 13:24:38,095 - INFO - Rebooting device bulb1... +2024-12-14 13:24:38,099 - INFO - Device bulb1 power set to off. +2024-12-14 13:24:38,102 - INFO - Device bulb1 power set to on. +2024-12-14 13:24:38,102 - INFO - Device bulb1 is back online. +2024-12-14 13:24:50,924 - INFO - Device name changed successfully. New name: Bulba +2024-12-14 13:26:01,583 - INFO - Connecting to device bulb1... +2024-12-14 13:26:03,655 - INFO - Successfully connected to device bulb1. +2024-12-14 13:26:47,335 - INFO - Connecting to device bulb1... +2024-12-14 13:26:49,839 - INFO - Successfully connected to device bulb1. +2024-12-14 13:27:56,991 - INFO - Connecting to device bulb1... +2024-12-14 13:27:59,862 - INFO - Successfully connected to device bulb1. +2024-12-14 13:36:40,076 - INFO - Connecting to device bulb1... +2024-12-14 13:36:41,260 - INFO - Successfully connected to device bulb1. +2024-12-14 13:37:11,027 - INFO - Connecting to device bulb1... +2024-12-14 13:37:12,874 - INFO - Successfully connected to device bulb1. +2024-12-14 13:37:44,267 - INFO - Connecting to device bulb1... +2024-12-14 13:37:45,826 - INFO - Successfully connected to device bulb1. +2024-12-14 13:39:11,394 - INFO - Connecting to device bulb1... +2024-12-14 13:39:13,074 - INFO - Successfully connected to device bulb1. +2024-12-14 13:40:34,242 - INFO - Connecting to device bulb... +2024-12-14 13:40:35,913 - ERROR - Wrong password, failed to connect to device bulb. +2024-12-14 13:41:06,393 - INFO - Connecting to device bulb1... +2024-12-14 13:41:07,849 - INFO - Successfully connected to device bulb1. +2024-12-14 13:42:00,193 - INFO - Connecting to device bulb1... +2024-12-14 13:42:01,161 - INFO - Successfully connected to device bulb1. +2024-12-14 13:43:20,232 - INFO - Connecting to device bulb1... +2024-12-14 13:43:21,088 - INFO - Successfully connected to device bulb1. +2024-12-14 13:43:27,944 - INFO - Getting status for device bulb1 +2024-12-14 13:43:41,931 - INFO - Device bulb1 power set to on. +2024-12-14 13:43:42,808 - INFO - Getting status for device bulb1 +2024-12-14 13:43:46,698 - INFO - Device bulb1 power set to off. +2024-12-14 13:43:48,359 - INFO - Getting status for device bulb1 +2024-12-14 13:43:52,671 - INFO - Rebooting device bulb1... +2024-12-14 13:43:52,675 - INFO - Device bulb1 power set to off. +2024-12-14 13:43:52,678 - INFO - Device bulb1 power set to on. +2024-12-14 13:43:52,678 - INFO - Device bulb1 is back online. +2024-12-14 13:45:16,744 - INFO - Connecting to device bulb1... +2024-12-14 13:45:18,319 - INFO - Successfully connected to device bulb1. +2024-12-14 13:46:14,528 - INFO - Connecting to device bulb1... +2024-12-14 13:46:15,952 - INFO - Successfully connected to device bulb1. +2024-12-14 13:46:34,926 - INFO - disconnected from device bulb1. +2024-12-14 13:49:05,102 - INFO - Connecting to device therm1... +2024-12-14 13:49:06,902 - ERROR - Wrong password, failed to connect to device therm1. +2024-12-14 13:53:38,133 - INFO - Connecting to device mower1... +2024-12-14 13:53:38,133 - ERROR - Connection error: Device mower1 is not compatible. +2024-12-14 13:56:50,083 - INFO - Connecting to device bulb1... +2024-12-14 13:56:52,315 - INFO - Successfully connected to device bulb1. +2024-12-14 13:56:54,115 - INFO - disconnected from device bulb1. +2024-12-14 13:57:05,643 - INFO - Connecting to device curtain1... +2024-12-14 13:57:07,307 - INFO - Successfully connected to device curtain1. +2024-12-14 13:57:16,586 - INFO - disconnected from device curtain1. +2024-12-14 13:57:32,890 - INFO - Connecting to device mower1... +2024-12-14 13:57:35,378 - INFO - Successfully connected to device mower1. +2024-12-14 14:02:21,100 - INFO - Device mower1 power set to off. +2024-12-14 14:02:27,912 - INFO - Getting status for device mower1 +2024-12-14 14:02:46,192 - INFO - Rebooting device mower1... +2024-12-14 14:02:46,195 - INFO - Device mower1 power set to off. +2024-12-14 14:02:46,198 - INFO - Device mower1 power set to on. +2024-12-14 14:02:46,199 - INFO - Device mower1 is back online. diff --git a/ui_terminal.py b/ui_terminal.py index cfad516..0dcddb7 100644 --- a/ui_terminal.py +++ b/ui_terminal.py @@ -118,6 +118,12 @@ def loop_specific_operations(device, specific_operations): else: print(f"Method does not exist.") +def show_logs(): + file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs\\app.log") + with open(file_path, "r") as file: + for line in file: + print(line.strip()) + def interact_with_device(): device_id = input("Enter device id: ") device_type = input("Enter device type: ") @@ -173,7 +179,8 @@ def menu() -> None: print("3. Add device") print("4. Remove device") print("5. Interact with device") - print("6. Save and exit") + print("6. Show logs") + print("7. Save and exit") option = input("Choose an option number: ") @@ -199,9 +206,12 @@ def menu() -> None: remove_device(devices_data) elif option == "5": - interact_with_device() + interact_with_device() elif option == "6": + show_logs() + + elif option == "7": save_devices(devices_data) print("Data has been saved.") break From 4781c41a3c41179e6b991d2976b29b688525346f Mon Sep 17 00:00:00 2001 From: mkruszyna Date: Sat, 14 Dec 2024 15:01:36 +0100 Subject: [PATCH 3/3] feat: fix parameters --- Devices/device.py | 2 +- devices.json | 10 +++++----- ui_terminal.py | 14 ++++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Devices/device.py b/Devices/device.py index 31410b3..5307ad5 100644 --- a/Devices/device.py +++ b/Devices/device.py @@ -9,7 +9,7 @@ sys.path.append(os.path.abspath('..')) from logging_config import get_logger -FILE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..\\devices.json") +FILE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../devices.json") MAX_ATTEMPTS = 3 # Initialize Colorama (necessary for Windows compatibility) init(autoreset=True) diff --git a/devices.json b/devices.json index 34aa48e..30baf5b 100644 --- a/devices.json +++ b/devices.json @@ -3,7 +3,7 @@ { "device_id": "bulb1", "device_secret_key": "xxx", - "name": "Smart Light Bulb", + "name": "bulb111111", "type": "bulb", "brand": "Tuya", "model": "TY-SLB-01", @@ -19,7 +19,7 @@ }, "location": "Living Room", "connected": true, - "last_updated": "2024-12-14 13:43:52" + "last_updated": "2024-12-14 14:43:20" }, { "device_id": "bulb2", @@ -50,7 +50,7 @@ "brand": "Tuya", "model": "TY-SLB-01", "status": { - "power": "off", + "power": "on", "brightness": 80, "color_temp": 5000, "rgb": { @@ -154,7 +154,7 @@ "device_id": "station1", "device_secret_key": "xxx", "name": "Smart Weather Station", - "type": "weather_station", + "type": "weatherstation", "brand": "Tuya", "model": "TY-WS-01", "status": { @@ -186,7 +186,7 @@ }, "location": "Backyard", "connected": true, - "last_updated": "2024-12-14 14:02:46" + "last_updated": "2024-12-14 14:47:52" } ] } \ No newline at end of file diff --git a/ui_terminal.py b/ui_terminal.py index 0dcddb7..febeb2c 100644 --- a/ui_terminal.py +++ b/ui_terminal.py @@ -59,7 +59,9 @@ def print_devices(devices_data: Dict[str, Any]) -> None: def add_device(devices_data: Dict[str, Any]) -> None: name = input("Enter device name: ") location = input("Enter device location: ") - new_device = {"name": name, "location": location, "status": {"power": "off"}} + device_id = input("Enter device id: ") + device_type = input("Enter device type: ") + new_device = {"name": name, "location": location, "device_id":device_id,"type":device_type, "status": {"power": "off"}} devices_data["devices"].append(new_device) save_devices(devices_data) print(f"Device '{name}' added successfully.") @@ -91,7 +93,7 @@ def create_device_object(device_id: str, device_type: str): return LawnMower(device_id, device_type) case "thermostat": return Thermostat(device_id, device_type) - case "weather_station": + case "weatherstation": return WeatherStation(device_id, device_type) case _: print("Unsupported device type") @@ -107,9 +109,9 @@ def loop_specific_operations(device, specific_operations): specific_operations.append("Return") while True: for index, element in enumerate(specific_operations): - print(f"{index}: {element}") + print(f"{index+1}: {element}") - specific_operation = int(input("\nChoose an option number: ")) + specific_operation = int(input("\nChoose an option number: "))-1 if hasattr(device, specific_operations[specific_operation]): method = getattr(device, specific_operations[specific_operation]) print(f"\n{method()}\n") @@ -119,7 +121,7 @@ def loop_specific_operations(device, specific_operations): print(f"Method does not exist.") def show_logs(): - file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs\\app.log") + file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs/app.log") with open(file_path, "r") as file: for line in file: print(line.strip()) @@ -209,7 +211,7 @@ def menu() -> None: interact_with_device() elif option == "6": - show_logs() + show_logs() elif option == "7": save_devices(devices_data)