-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_control.py
More file actions
65 lines (55 loc) · 1.79 KB
/
device_control.py
File metadata and controls
65 lines (55 loc) · 1.79 KB
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
import time
from easyhttp_python import EasyHTTP
# PUSH request callback
def handle_push(sender_id, data, timestamp):
print(f"PUSH command from {sender_id}: {data}")
# Validation and command processing
if data and data.get("command") == "led":
state = data.get("state")
print(f"Turning LED {state}")
# Here you can add real GPIO control
return True # Command was successfully executed
elif data and data.get("command") == "reboot":
print("Scheduling reboot...")
# You can schedule a reboot here
return True
else:
print(f"Unknown command: {data}")
return False # Unknown command
def main():
# Initializing
easy = EasyHTTP(debug=True, port=5000)
# Setting up callback functions
easy.on("on_push", handle_push)
# Starting API
easy.start()
# An example of sending test commands (if you need to test two-way communication)
# Uncomment if you want this device to also send commands
"""
try:
while True:
# Example of sending a command to turn on an LED
success = easy.push("ABC123", {
"command": "led",
"state": "on",
"brightness": 80
})
if success:
print("Command executed successfully!")
else:
print("Device rejected the command")
time.sleep(5)
except KeyboardInterrupt:
print("\nStopping...")
easy.stop()
"""
# Just waiting for incoming commands
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping device...")
# Gracefully stopping API
easy.stop()
if __name__ == '__main__':
main()