diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed37789 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.venv/ +__pycache__/ +.vscode/ \ No newline at end of file diff --git a/devices.json b/devices.json new file mode 100644 index 0000000..7d048b9 --- /dev/null +++ b/devices.json @@ -0,0 +1,181 @@ +{ + "devices": [ + { + "device_id": "1234567890abcdef", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "on", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "abcdef1234567890", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "on", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Kitchen", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "1234567890abcdef", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "off", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "abcdef1234567999", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "on", + "energy_consumption": { + "current_power_w": 20, + "total_energy_kwh": 5.4 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-21 20:49:42" + }, + { + "device_id": "abcdef1234561547", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "off", + "energy_consumption": { + "current_power_w": 0, + "total_energy_kwh": 0.4 + } + }, + "location": "Kitchen", + "connected": true, + "last_updated": "2024-10-19T14:50:00Z" + }, + { + "device_id": "abcdef123456578", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "off", + "energy_consumption": { + "current_power_w": 10, + "total_energy_kwh": 15.4 + } + }, + "location": "Hallway", + "connected": true, + "last_updated": "2024-10-19T14:50:00Z" + }, + { + "device_id": "abcdef1234563298", + "name": "Smart Thermostat", + "type": "thermostat", + "brand": "Tuya", + "model": "TY-TH-01", + "status": { + "power": "on", + "target_temperature_c": 22, + "current_temperature_c": 21, + "humidity": 45 + }, + "location": "Bedroom", + "connected": true, + "last_updated": "2024-10-19T14:55:00Z" + }, + { + "device_id": "curtain12345", + "name": "Automatic Curtain", + "type": "curtain", + "brand": "Tuya", + "model": "TY-AC-01", + "status": { + "power": "on", + "position": 75, + "open_percent": 75 + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:58:00Z" + }, + { + "device_id": "weatherstation56789", + "name": "Smart Weather Station", + "type": "weather_station", + "brand": "Tuya", + "model": "TY-WS-01", + "status": { + "temperature_c": 16.5, + "humidity_percent": 65, + "pressure_hpa": 1013, + "wind_speed_kmh": 12, + "rainfall_mm": 2.3 + }, + "location": "Garden", + "connected": true, + "last_updated": "2024-10-19T14:59:00Z" + }, + { + "device_id": "mower98765", + "name": "Automatic Lawn Mower", + "type": "lawn_mower", + "brand": "Tuya", + "model": "TY-AM-01", + "status": { + "power": "on", + "battery_percent": 85, + "cutting_mode": "auto", + "cutting_height_mm": 30, + "current_area_m2": 500, + "total_cutting_time_minutes": 120 + }, + "location": "Backyard", + "connected": true, + "last_updated": "2024-10-19T15:00:00Z" + } + ] +} \ No newline at end of file diff --git a/devices_template.json b/devices_template.json new file mode 100644 index 0000000..bbeea3c --- /dev/null +++ b/devices_template.json @@ -0,0 +1,181 @@ +{ + "devices": [ + { + "device_id": "1234567890abcdef", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "on", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "abcdef1234567890", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "on", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Kitchen", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "1234567890abcdef", + "name": "Smart Light Bulb", + "type": "light", + "brand": "Tuya", + "model": "TY-SLB-01", + "status": { + "power": "off", + "brightness": 80, + "color_temp": 5000, + "rgb": { + "red": 255, + "green": 100, + "blue": 50 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:52:00Z" + }, + { + "device_id": "abcdef1234567999", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "on", + "energy_consumption": { + "current_power_w": 20, + "total_energy_kwh": 5.4 + } + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:50:00Z" + }, + { + "device_id": "abcdef1234561547", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "off", + "energy_consumption": { + "current_power_w": 0, + "total_energy_kwh": 0.4 + } + }, + "location": "Kitchen", + "connected": true, + "last_updated": "2024-10-19T14:50:00Z" + }, + { + "device_id": "abcdef123456578", + "name": "Smart Plug", + "type": "plug", + "brand": "Tuya", + "model": "TY-SP-01", + "status": { + "power": "off", + "energy_consumption": { + "current_power_w": 10, + "total_energy_kwh": 15.4 + } + }, + "location": "Hallway", + "connected": true, + "last_updated": "2024-10-19T14:50:00Z" + }, + { + "device_id": "abcdef1234563298", + "name": "Smart Thermostat", + "type": "thermostat", + "brand": "Tuya", + "model": "TY-TH-01", + "status": { + "power": "on", + "target_temperature_c": 22, + "current_temperature_c": 21, + "humidity": 45 + }, + "location": "Bedroom", + "connected": true, + "last_updated": "2024-10-19T14:55:00Z" + }, + { + "device_id": "curtain12345", + "name": "Automatic Curtain", + "type": "curtain", + "brand": "Tuya", + "model": "TY-AC-01", + "status": { + "power": "on", + "position": 75, + "open_percent": 75 + }, + "location": "Living Room", + "connected": true, + "last_updated": "2024-10-19T14:58:00Z" + }, + { + "device_id": "weatherstation56789", + "name": "Smart Weather Station", + "type": "weather_station", + "brand": "Tuya", + "model": "TY-WS-01", + "status": { + "temperature_c": 16.5, + "humidity_percent": 65, + "pressure_hpa": 1013, + "wind_speed_kmh": 12, + "rainfall_mm": 2.3 + }, + "location": "Garden", + "connected": true, + "last_updated": "2024-10-19T14:59:00Z" + }, + { + "device_id": "mower98765", + "name": "Automatic Lawn Mower", + "type": "lawn_mower", + "brand": "Tuya", + "model": "TY-AM-01", + "status": { + "power": "on", + "battery_percent": 85, + "cutting_mode": "auto", + "cutting_height_mm": 30, + "current_area_m2": 500, + "total_cutting_time_minutes": 120 + }, + "location": "Backyard", + "connected": true, + "last_updated": "2024-10-19T15:00:00Z" + } + ] +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e13aca9 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +def main(): + print("SmartCode Developers") + + +if __name__ == "__main__": + main() + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1e9068c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +certifi==2024.8.30 +charset-normalizer==3.4.0 +Faker==30.8.1 +idna==3.10 +mpmath==1.3.0 +numpy==2.1.2 +pandas==2.2.3 +python-dateutil==2.9.0.post0 +pytz==2024.2 +requests==2.32.3 +six==1.16.0 +sympy==1.13.3 +typing_extensions==4.12.2 +tzdata==2024.2 +urllib3==2.2.3 +colorama==0.4.6 diff --git a/schedule_timers.py b/schedule_timers.py new file mode 100644 index 0000000..bacf17c --- /dev/null +++ b/schedule_timers.py @@ -0,0 +1,131 @@ +import json +import os +from typing import Dict, Any + +from datetime import timedelta, datetime + +class ScheduleUpdate: + def __init__(self, turn_on_time, turn_off_time, working_duration, current_time = str(datetime.now()), task_list = []): + self.turn_on_time = turn_on_time ("%Y-%m-%d-%H-%M") + self.turn_off_time = turn_off_time ("%Y-%m-%d-%H-%M") + self.working_duration = working_duration("%M") + self.current_time = current_time + self.task_list = task_list + + def load_devices(self) -> Dict[str, Any]: + try: + with open(os.path.join(os.path.dirname(__file__), "devices.json"), "r") as file: + return json.load(file) + except FileNotFoundError: + print("File not found.") + return {"devices": []} + except json.JSONDecodeError: + print("Error reading the file.") + return {"devices": []} + + def print_devices(self, devices_data: Dict[str, Any]) -> None: + if not devices_data["devices"]: + print("No devices in the system.") + return + + 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}") + + # Adding turn-on and turn-off time + def add_turn_on_off_time(self, devices_data: Dict[str, Any]): + self.print_devices(devices_data) + device_number = int(input("Enter the device number to set working time schedule: ")) + if device_number not in devices_data: + return "Device is not available." + else: + # Setting turn-on time + self.turn_on_time = input("Enter time for turning on the device: ") + + # Setting turn-off time + self.turn_off_time = input("Enter time for turning off the device: ") + + # Verification of dates and adding them to the task list + if self.turn_on_time <= self.current_time < self.turn_off_time: + self.task_list.append(self.turn_on_time) + + else: + return "Incorrect device turn-on time entered." + + if self.current_time >= self.turn_off_time: + self.task_list.append(self.turn_off_time) + + else: + return "Incorrect device turn-off time entered." + + return f"Device's working schedule time has been set: {self.turn_on_time} - {self.turn_off_time}." + + # Setting a time period when (starting from now) the device is going to work + def add_working_time(self, index: int, devices_data: Dict[str, Any]): + self.device = devices_data["devices"][index] + self.print_devices(devices_data) + device_number = int(input("Enter the device number to set working time schedule: ")) + + if device_number not in devices_data: + return "Device is not available." + + # Setting the device's operating time + else: + self.working_duration = input(int("Enter for how long the device is to be turned on in minutes: ")) + if self.working_duration > 0: + self.turn_off_time = self.current_time + timedelta(minutes=self.working_duration) + self.task_list.append(self.current_time, self.turn_off_time) + + else: + return "Wrong operating time for the device." + + # Verification if there's any schedule for any device and turning it on/off if conditions are met + def check_schedule_time(self, scheduled_devices: [str, Any]) -> None: + self.device = scheduled_devices["task_list"] + + for self.device in self.task_list: + if self.turn_on_time <= self.current_time < self.turn_off_time: + if self.device["status"]["power"] == "off": + self.device["status"]["power"] = "on" + else: + self.device["status"]["power"] = "on" + + elif self.current_time >= self.turn_off_time: + if self.device["status"]["power"] == "on": + self.device["status"]["power"] = "off" + else: + self.device["status"]["power"] = "off" + + else: + pass + + +""" +Adding to the menu: + +def menu() -> None: + devices_data = load_devices() + + while True: + print("\nChoose an option:") + print("1. Show devices") + print("2. Toggle device") + print("3. Add device") + print("4. Remove device") + print("5. Set working schedule time for a device") + print("6. Save and exit") + + # Checking if there are any time schedules set and activating them if conditions are met + ScheduleUpdate.check_schedule_time(devices_data["devices"]) + + elif option == "5": + print("\nChoose an option:") + print("a. Set time for turning on and turning off the device") + print("b. Set working duration time for the device") + + option_time = input("Choose an option: ") + if option_time == "a": + ScheduleUpdate.add_turn_on_off_time(devices_data) + if option_time == "b": + ScheduleUpdate.duration_time(devices_data, devices_data) +"""