Skip to content

Commit

Permalink
Update smart_home.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 27, 2024
1 parent 345dd0f commit 1b214fb
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions iot-integration/iot_devices/smart_home.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
import requests

class SmartHomeDevice:
def __init__(self, device_id, device_token):
self.device_id = device_id
self.device_token = device_token

def get_utility_bill(self):
# Make a request to the smart home API to retrieve the utility bill
response = requests.get(f'https://smart-home-api.com/bills/{self.device_id}', headers={'Authorization': f'Bearer {self.device_token}'})
return response.json()

def make_payment(self, amount):
# Make a request to the smart home API to make a payment
response = requests.post(f'https://smart-home-api.com/payments/{self.device_id}', json={'amount': amount}, headers={'Authorization': f'Bearer {self.device_token}'})
return response.json()
import os
import json
import socket
import threading
from cryptography.fernet import Fernet

class SmartHome:
def __init__(self, home_id, home_secret, devices=None):
self.home_id = home_id
self.home_secret = home_secret
self.devices = devices if devices else []
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind(('localhost', 8080))
self.socket.listen(5)
self.threads = []

def add_device(self, device):
self.devices.append(device)

def remove_device(self, device):
self.devices.remove(device)

def send_command(self, device_id, command):
for device in self.devices:
if device['id'] == device_id:
device['socket'].send(command.encode())
break

def receive_data(self, device_id):
for device in self.devices:
if device['id'] == device_id:
data = device['socket'].recv(1024)
return data.decode()

def start_listening(self):
while True:
client_socket, address = self.socket.accept()
device_id = client_socket.recv(1024).decode()
device_secret = client_socket.recv(1024).decode()
if self.authenticate_device(device_id, device_secret):
self.devices.append({'id': device_id, 'socket': client_socket})
thread = threading.Thread(target=self.handle_device, args=(client_socket,))
thread.start()
self.threads.append(thread)

def handle_device(self, client_socket):
while True:
data = client_socket.recv(1024)
if data:
print(f'Received data from device {client_socket.getpeername()}: {data.decode()}')
else:
break

def authenticate_device(self, device_id, device_secret):
cipher_suite = Fernet(self.home_secret)
encrypted_device_secret = cipher_suite.encrypt(device_secret.encode())
return encrypted_device_secret == device_secret.encode()

def start(self):
thread = threading.Thread(target=self.start_listening)
thread.start()
self.threads.append(thread)

def stop(self):
for thread in self.threads:
thread.join()

if __name__ == '__main__':
home_id = 'home123'
home_secret = 'secret123'
smart_home = SmartHome(home_id, home_secret)
smart_home.start()

0 comments on commit 1b214fb

Please sign in to comment.