-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_preview.py
More file actions
64 lines (50 loc) · 1.55 KB
/
callback_preview.py
File metadata and controls
64 lines (50 loc) · 1.55 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
import time
# Import synchronous wrapper of EasyHTTP
from easyhttp_python import EasyHTTP
'''
Callback functions
'''
# Callback function for PING
def handle_ping(sender_id):
print(f"PING from device {sender_id}")
# Callback function for PONG
def handle_pong(sender_id):
print(f"PONG from device {sender_id}")
# Callback function for FETCH
def handle_fetch(sender_id, query, timestamp):
print(f"FETCH request from {sender_id} on {timestamp} with {query}")
# Returning data
return {
"message": "Hello EasyHTTP!"
}
# Callback function for DATA
def handle_data(sender_id, data, timestamp):
print(f"DATA from device {sender_id}: {data}")
# Callback function for PUSH
def handle_push(sender_id, data, timestamp):
print(f"PUSH from device {sender_id}: {data}")
# Returning True to confirm
# You can return False to reject
return True
'''Main process'''
def main():
try:
# Initializing
easy = EasyHTTP(debug=True, port=5000)
# Setting up callback functions
easy.on('on_ping', handle_ping) # Handle PING
easy.on('on_pong', handle_pong) # Handle PONG
easy.on('on_fetch', handle_fetch) # Handle FETCH
easy.on('on_data', handle_data) # Handle DATA
easy.on('on_push', handle_push) # Handle PUSH
# Starting API
easy.start()
while True:
time.sleep(5)
except KeyboardInterrupt:
# Gracefully stopping API
easy.stop()
except Exception as e:
pass
if __name__ == '__main__':
main()