-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulation.py
75 lines (69 loc) · 2.33 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import mysql.connector
import random
import time
# Database connection details
db_config = {
'host': 'localhost',
'user': 'root',
'password': 'root',
'database': 'alsayesdb'
}
# Connect to the database
def connect_to_db():
try:
connection = mysql.connector.connect(**db_config)
if connection.is_connected():
print("Connected to the database")
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
# Fetch and store the parking spots in memory
def fetch_parking_spots(connection):
cursor = connection.cursor(dictionary=True)
try:
cursor.execute("SELECT id, lot_id, current_status FROM ParkingSpot")
spots = cursor.fetchall()
return spots
except mysql.connector.Error as err:
print(f"Error: {err}")
return []
finally:
cursor.close()
def update_parking_spots_status(connection, parking_spots, delay=5):
cursor = connection.cursor()
try:
for spot in parking_spots:
current_status = spot['current_status']
new_status = 'OCCUPIED' if current_status == 'AVAILABLE' else 'AVAILABLE'
spot['current_status'] = new_status
print(f"Updated spot {spot['id']} in lot {spot['lot_id']} to {new_status}")
update_query = """
UPDATE ParkingSpot
SET current_status = %s
WHERE id = %s AND lot_id = %s
"""
cursor.execute(update_query, (spot['current_status'], spot['id'], spot['lot_id']))
time.sleep(delay)
connection.commit()
print("Updated parking spot statuses successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
cursor.close()
def simulate_IoT_device(delay1=5, delay2=10):
print("Starting the simulation")
connection = connect_to_db()
if connection:
try:
parking_spots = fetch_parking_spots(connection)
print(parking_spots)
while True:
update_parking_spots_status(connection, parking_spots, delay1)
time.sleep(delay2)
except KeyboardInterrupt:
print("Stopping the simulation")
finally:
connection.close()
if __name__ == "__main__":
simulate_IoT_device()