diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/4005CMD_Labs.iml b/.idea/4005CMD_Labs.iml new file mode 100644 index 0000000..3d18b39 --- /dev/null +++ b/.idea/4005CMD_Labs.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..263d684 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8ff1f6a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..eb0167a --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Conflict.txt b/Conflict.txt index d93d495..f6b781a 100644 --- a/Conflict.txt +++ b/Conflict.txt @@ -1,4 +1,2 @@ hieee -hyyyy -hie Im a conflict \ No newline at end of file diff --git a/README.md b/README.md index 1f87f85..9a8a7cf 100644 Binary files a/README.md and b/README.md differ diff --git a/Week4_Lab1/binary_lab.py b/Week4_Lab1/binary_lab.py new file mode 100644 index 0000000..53196dc --- /dev/null +++ b/Week4_Lab1/binary_lab.py @@ -0,0 +1,33 @@ +import base64 +import json +def main(): + # 1. Simulate an image file (Bytes) + # In Python, b"..." means bytes, not string. + fake_image_data = b"This is not really an image, but it is binary data." + print(f"Original Binary: {fake_image_data}") + + # 2. Encode to Base64 (still bytes) + b64_bytes = base64.b64encode(fake_image_data) + # 3. Decode to UTF-8 String (Critical Step for JSON) + # JSON cannot hold bytes, so we turn the base64 bytes into a normal string. + # This is exactly what you do in the Week 7 Tutorial. + image_string = b64_bytes.decode('utf-8') + print(f"\nBase64 String (Safe for JSON): {image_string}") + # 4. Pack into JSON + payload = { + "filename": "camera_capture.jpg", + "image_data": image_string + } + json_payload = json.dumps(payload) + print(f"\nJSON Payload ready for MQTT: {json_payload}") + # --- SIMULATE RECEIVING THE MESSAGE --- + # 5. Unpack JSON + received = json.loads(json_payload) + received_str = received["image_data"] + # 6. Decode back to Binary + # We ignore the filename for now, just get the data back. + original_data = base64.b64decode(received_str) + print(f"\nRecovered Binary: {original_data}") + +if __name__ == "__main__": + main() diff --git a/Week4_Lab1/config.json b/Week4_Lab1/config.json new file mode 100644 index 0000000..7c7d3c8 --- /dev/null +++ b/Week4_Lab1/config.json @@ -0,0 +1,5 @@ +{ + "broker": "broker.hivemq.com", + "port": 1883, + "topic": "home/livingroom" +} \ No newline at end of file diff --git a/Week4_Lab1/file_handler.py b/Week4_Lab1/file_handler.py new file mode 100644 index 0000000..9f15a62 --- /dev/null +++ b/Week4_Lab1/file_handler.py @@ -0,0 +1,42 @@ +import json +import os + +FILENAME = "config.json" + + +def save_config(data: dict): + """Writes a dictionary to a file as JSON.""" + with open(FILENAME, "w") as f: + # json.dump (no 's') writes directly to file + json.dump(data, f, indent=4) + print("Config saved.") + + +def load_config() -> dict: + """Reads JSON from a file and returns a dictionary.""" + if not os.path.exists(FILENAME): + print("No config file found. Returning empty dict.") + return {} + + with open(FILENAME, "r") as f: + return json.load(f) + + +def main(): + # Create some dummy settings + my_settings = { + "broker": "broker.hivemq.com", + "port": 1883, + "topic": "home/livingroom" + } + + # Save them + save_config(my_settings) + + # Load them back + loaded_settings = load_config() + print(f"Loaded broker: {loaded_settings['broker']}") + + +if __name__ == "__main__": + main() diff --git a/Week4_Lab1/json_basic.py b/Week4_Lab1/json_basic.py new file mode 100644 index 0000000..3e0de57 --- /dev/null +++ b/Week4_Lab1/json_basic.py @@ -0,0 +1,32 @@ +import json + + +def main(): + + # 1. A Python Dictionary (exists in memory) + + sensor_data = { + "id": "sensor_01", + "temp": 22.5, + "active": True + } + + print(f"Original Type: {type(sensor_data)}") + + # 2. Serialize (Freeze it into a string) + # indent=4 makes it pretty to read + json_string = json.dumps(sensor_data, indent=4) + + print(f"Serialized Type: {type(json_string)}") + print("The String looks like this:") + print(json_string) + + # 3. Deserialize (Thaw it back into a dictionary) + # This is what you do when you receive an MQTT message + received_data = json.loads(json_string) + print(f"Back to Type: {type(received_data)}") + print(f"Temperature is: {received_data['temp']}") + + +if __name__ == "__main__": + main() diff --git a/Week4_Lab1/safe parsing.py b/Week4_Lab1/safe parsing.py new file mode 100644 index 0000000..019e3ed --- /dev/null +++ b/Week4_Lab1/safe parsing.py @@ -0,0 +1,33 @@ +import json + + +def process_message(json_msg: str): + data = json.loads(json_msg) + + # SAFE WAY: .get() allows us to provide a default value (e.g., 0.0) + # If "temp" is missing, it uses 0.0 instead of crashing. + temperature = data.get("temp", 0.0) + + # UNSAFE WAY: Direct access + # Uncommenting the next line would crash the script + # humidity = data["humidity"] + + print(f"Processing... Temp: {temperature}") + +def main(): + # A perfect message + good_msg = '{"id": "s1", "temp": 23.5, "humidity": 60}' + print("--- Test 1 (Good Message) ---") + process_message(good_msg) + + # A broken message (missing temp) + bad_msg = '{"id": "s1", "error": "battery_low"}' + print("\n--- Test 2 (Bad Message) ---") + process_message(bad_msg) + + +if __name__ == "__main__": + main() + + + diff --git a/Week5_lab1/create_table.py b/Week5_lab1/create_table.py new file mode 100644 index 0000000..39ab3d3 --- /dev/null +++ b/Week5_lab1/create_table.py @@ -0,0 +1,21 @@ +import sqlite3 +def create_table(): + conn = sqlite3.connect('smart_home.db') + cursor = conn.cursor() + # SQL Command to create a table + # We use triple quotes """ for multi-line strings + sql_command = """ + CREATE TABLE IF NOT EXISTS sensors ( + sensor_id INTEGER PRIMARY KEY, + location TEXT NOT NULL, + type TEXT NOT NULL, + install_date TEXT + ); + """ + cursor.execute(sql_command) + print("Table 'sensors' created successfully.") + # Commit saves the changes (Crucial!) + conn.commit() + conn.close() +if __name__ == "__main__": + create_table() \ No newline at end of file diff --git a/Week5_lab1/db_setup.py b/Week5_lab1/db_setup.py new file mode 100644 index 0000000..736156d --- /dev/null +++ b/Week5_lab1/db_setup.py @@ -0,0 +1,12 @@ +import sqlite3 +def create_database(): + print("Connecting to database...") +# This creates the file 'smart_home.db' if it doesn't exist +conn = sqlite3.connect('smart_home.db') +# Create the cursor +cursor = conn.cursor() +print("Database connection successful.") +# Always close the connection when done +conn.close() +if __name__ == "__main__": + create_database() \ No newline at end of file diff --git a/Week5_lab1/insert_data.py b/Week5_lab1/insert_data.py new file mode 100644 index 0000000..d130f39 --- /dev/null +++ b/Week5_lab1/insert_data.py @@ -0,0 +1,19 @@ +import sqlite3 +def add_sensor(location, sensor_type, date): + conn = sqlite3.connect('smart_home.db') + cursor = conn.cursor() + + # The '?' are placeholders. The library safely inserts the variables later. + sql = "INSERT INTO sensors (location, type, install_date) VALUES (?, ?, ?)" + # The data must be a tuple + data = (location, sensor_type, date) + cursor.execute(sql, data) + conn.commit() + print(f"Added sensor: {sensor_type} in {location}") + conn.close() + +if __name__ == "__main__": + add_sensor("Living Room", "Temperature", "2023-01-15") + add_sensor("Kitchen", "Motion", "2023-02-20") + add_sensor("Bedroom", "Light", "2023-03-05") + diff --git a/Week5_lab1/modify_data.py b/Week5_lab1/modify_data.py new file mode 100644 index 0000000..517a707 --- /dev/null +++ b/Week5_lab1/modify_data.py @@ -0,0 +1,30 @@ +import sqlite3 +def move_sensor(sensor_type, new_location): + conn = sqlite3.connect('smart_home.db') + cursor = conn.cursor() + + # UPDATE syntax + sql = "UPDATE sensors SET location = ? WHERE type = ?" + cursor.execute(sql, (new_location, sensor_type)) + conn.commit() + print(f"Moved all {sensor_type} sensors to {new_location}.") + conn.close() + + +def remove_sensor(sensor_id): + conn = sqlite3.connect('smart_home.db') + cursor = conn.cursor() + + # DELETE syntax + sql = "DELETE FROM sensors WHERE sensor_id = ?" + cursor.execute(sql, (sensor_id,)) # Note the comma for a single-item tuple! + conn.commit() + print(f"Deleted sensor ID {sensor_id}.") + conn.close() + + +if __name__ == "__main__": + move_sensor("Motion", "Hallway") + remove_sensor(1) # Deletes the Living Room Temperature sensor + + diff --git a/Week5_lab1/read_data.py b/Week5_lab1/read_data.py new file mode 100644 index 0000000..f293ff0 --- /dev/null +++ b/Week5_lab1/read_data.py @@ -0,0 +1,18 @@ +import sqlite3 +def get_all_sensors(): + conn = sqlite3.connect('smart_home.db') + cursor = conn.cursor() + # SELECT * means "Select All Columns" + cursor.execute("SELECT * FROM sensors") + + # fetchall() grabs every row found by the query + rows = cursor.fetchall() + print("--- Current Sensors ---") + for row in rows: + # 'row' is a tuple: (1, 'Living Room', 'Temperature', '2023-01-15') + print(f"ID: {row[0]} | Loc: {row[1]} | Type: {row[2]}") + + conn.close() + +if __name__ == "__main__": + get_all_sensors() \ No newline at end of file diff --git a/Week5_lab1/smart_home.db b/Week5_lab1/smart_home.db new file mode 100644 index 0000000..68578f6 Binary files /dev/null and b/Week5_lab1/smart_home.db differ diff --git a/Lab1/converter.py b/Week_3Lab1/converter.py similarity index 100% rename from Lab1/converter.py rename to Week_3Lab1/converter.py diff --git a/Lab1/demo.py b/Week_3Lab1/demo.py similarity index 100% rename from Lab1/demo.py rename to Week_3Lab1/demo.py diff --git a/Lab1/temp convert 2.py b/Week_3Lab1/temp convert 2.py similarity index 100% rename from Lab1/temp convert 2.py rename to Week_3Lab1/temp convert 2.py diff --git a/Lab1/utils.py b/Week_3Lab1/utils.py similarity index 100% rename from Lab1/utils.py rename to Week_3Lab1/utils.py diff --git a/config.json b/config.json new file mode 100644 index 0000000..7c7d3c8 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "broker": "broker.hivemq.com", + "port": 1883, + "topic": "home/livingroom" +} \ No newline at end of file diff --git a/import sys.bib b/import sys.bib new file mode 100644 index 0000000..f167da5 --- /dev/null +++ b/import sys.bib @@ -0,0 +1,3 @@ +import sys +print(sys.argv) + \ No newline at end of file