-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
141 lines (114 loc) · 4.03 KB
/
Test.py
File metadata and controls
141 lines (114 loc) · 4.03 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import asyncio
import aioble
import sys
import uerrno
import time
import json
#from testmq import MQTTClient
#from umqttsimple import MQTTClient
def get_time():
uptime_s = int(time.ticks_ms() / 1000)
uptime_h = int(uptime_s / 3600)
uptime_m = int(uptime_s / 60)
uptime_m = uptime_m % 60
uptime_s = uptime_s % 60
return (
'{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*time.localtime()),
'{:02d}h {:02d}:{:02d}'.format(uptime_h, uptime_m, uptime_s),
)
async def serve(writer, filename):
if filename == '/':
filename = 'index.html'
print("Sending", filename)
if filename.startswith("/api/status"):
await writer.awrite(b"HTTP/1.1 200 OK\r\n")
await writer.awrite("Content-Type: application/json\r\n\r\n")
time_str, uptime_str = get_time()
await writer.awrite(json.dumps({
"time": time_str,
"uptime": uptime_str,
'python': '{} {}'.format(
sys.implementation.name,
'.'.join(
str(s) for s in sys.implementation.version
),
),
'platform': str(sys.platform),
}))
await writer.drain()
print("Closing connection", writer)
await writer.aclose()
await writer.wait_closed()
return
if filename.startswith("/tempdata"):
await writer.awrite(b"HTTP/1.1 200 OK\r\n")
await writer.awrite("Content-Type: application/json\r\n\r\n")
UTC_OFFSET = 10 * 60 * 60
current_time = time.localtime(time.time() + UTC_OFFSET)
formatted_time = "{:02d}:{:02d}".format(current_time[3], current_time[4])
ty = f'{{"time": "{formatted_time}", "temperature": "{26.26}"}}'
await writer.awrite(ty)
await writer.drain()
print("Closing connection", writer)
await writer.aclose()
await writer.wait_closed()
return
await writer.awrite(b"HTTP/1.1 200 OK\r\n\r\n")
try:
with open(f"./assets/{filename}", 'rb') as f:
while True:
data = f.read(64)
if not data:
break
await writer.awrite(data)
await writer.drain()
except OSError as e:
if e.args[0] != uerrno.ENOENT:
print("Error:", e.args[0])
else:
print("File Not Found")
print("Sent")
print("Closing connection", writer)
await writer.aclose()
await writer.wait_closed()
async def handle(reader, writer):
print("***********************************************", reader)
try:
while True:
items = await asyncio.wait_for(reader.readline(), timeout=5)
if len(items) == 0:
break
items = items.decode('ascii').split()
if len(items) > 0 and items[0] == 'GET':
filename = items[1]
#print(len(items), items)
if len(items) == 0:
await serve(writer, filename)
break
except asyncio.TimeoutError:
print("Timeout occurred. Closing connection", reader)
await writer.aclose()
await writer.wait_closed()
except Exception as e:
print(f"Error: {e}")
finally:
print("Finished")
async def start_webserver():
await asyncio.start_server(handle, '0.0.0.0', 80)
async def scan_blea():
while True:
async with aioble.scan(
duration_ms=50, # Total duration of the scan in milliseconds was 100
interval_us=30000, # Scan interval in microseconds was 55_000
window_us=30000, # Scan window in microseconds was 25_250
active=True # Active scan mode
) as scanner:
async for result in scanner:
print('@', end='') #result.device.addr)
try:
loop = asyncio.get_event_loop()
loop.create_task(start_webserver())
loop.create_task(scan_blea())
loop.run_forever()
except Exception as e:
print("Exception:", e)