forked from ch2i/LoraGW-Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled.py
executable file
·317 lines (267 loc) · 8.95 KB
/
oled.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
import os
import psutil
import platform
import thread
import socket
import SocketServer
import json
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from luma.oled.device import sh1106
from PIL import ImageFont
import time
from datetime import datetime
width=128
height=64
font_pixel=12
line1=0
line2=line1+12
line3=line2+12
line4=line3+12
line5=line4+12
col1=0
lora_rssi = None
lora_chan = None
lora_freq = None
lora_datr = None
lora_mote = None
class LeaseEntry:
def __init__(self, leasetime, macAddress, ipAddress, name):
if (leasetime == '0'):
self.staticIP = True
else:
self.staticIP = False
self.leasetime = datetime.fromtimestamp(int(leasetime)).strftime('%Y-%m-%d %H:%M:%S')
self.macAddress = macAddress.upper()
self.ipAddress = ipAddress
self.name = name
def serialize(self):
return {
'staticIP': self.staticIP,
'leasetime': self.leasetime,
'macAddress': self.macAddress,
'ipAddress': self.ipAddress,
'name': self.name
}
class MyUDPHandler(SocketServer.BaseRequestHandler):
def is_json(self, myjson):
try:
json_object = json.loads(myjson)
except ValueError, e:
return False
return True
def handle(self):
json_data
global lora_rssi
global lora_chan
global lora_freq
global lora_datr
global lora_mote
data = self.request[0]
# for poly_pkt_fwd remove 12 first bytes
# if mp_pkt_fwd then packet are already fine
if data.find("{\"rxpk\":[") > 0:
data = data[12::]
if self.is_json(data):
#print("\r\n")
#print(data)
js_data = json.loads(data)
# for mp_pkt_fwd
if js_data.get('type')=='uplink':
lora_mote = js_data["mote"]
lora_rssi = js_data["rssi"]
lora_chan = js_data["chan"]
lora_freq = js_data["freq"]
lora_datr = js_data["datr"]
# for poly_pkt_fwd
elif js_data.get('rxpk'):
lora_mote = "legacy_fwd"
lora_rssi = js_data["rxpk"][0]["rssi"]
lora_chan = js_data["rxpk"][0]["chan"]
lora_freq = js_data["rxpk"][0]["freq"]
lora_datr = js_data["rxpk"][0]["datr"]
def leaseSort(arg):
# Fixed IPs first
if arg.staticIP == True:
return 0
else:
return arg.name.lower()
def getLeases():
leases = list()
try:
with open("/var/lib/misc/dnsmasq.leases") as f:
for line in f:
elements = line.split()
if len(elements) == 5:
entry = LeaseEntry(elements[0],elements[1],elements[2],elements[3])
leases.append(entry)
leases.sort(key = leaseSort)
return [lease.serialize() for lease in leases]
except Exception as e:
print "error in getLeases"
print(e)
print(type(e))
return None
def do_nothing(obj):
pass
def make_font(name, size):
font_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fonts', name))
return ImageFont.truetype(font_path, size)
# rev.1 users set port=0
# substitute spi(device=0, port=0) below if using that interface
serial = i2c(port=1, address=0x3c)
device = sh1106(serial)
#device = ssd1306(serial)
device.cleanup = do_nothing
font10 = make_font("/usr/share/fonts/truetype/luma/ProggyTiny.ttf", 16)
byteunits = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
def udp_receive(delay):
server = SocketServer.UDPServer(("127.0.0.1", 1688), MyUDPHandler)
server.serve_forever()
#server.handle_request()
def filesizeformat(value):
exponent = int(log(value, 1024))
return "%.1f %s" % (float(value) / pow(1024, exponent), byteunits[exponent])
def bytes2human(n):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = int(float(n) / prefix[s])
return '%s%s' % (value, s)
return "%sB" % n
def if_stat(iface):
try:
stat = psutil.net_io_counters(pernic=True)[iface]
return "Tx %s Rx %s" % (bytes2human(stat.bytes_sent), bytes2human(stat.bytes_recv))
except KeyError as e:
return "Tx %s Rx %s" % (bytes2human(0), bytes2human(0))
def lan_info(iface):
ip = "No Interface"
stat = ""
try:
for snic in psutil.net_if_addrs()[iface]:
if snic.family == socket.AF_INET:
ip = snic.address
stat = if_stat(iface)
break
else:
ip ="No IP"
return "%-5s: %s" % (iface, ip), stat
except KeyError as e:
return ip, stat
def uptime():
try:
f = open( "/proc/uptime" )
contents = f.read().split()
f.close()
except:
return "no /proc/uptime"
total_seconds = float(contents[0])
# Helper vars:
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24
# Get the days, hours, etc:
days = int( total_seconds / DAY )
hours = int( ( total_seconds % DAY ) / HOUR )
minutes = int( ( total_seconds % HOUR ) / MINUTE )
seconds = int( total_seconds % MINUTE )
# Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
string = ""
if days > 0:
string += str(days) + "d "
if len(string) > 0 or hours > 0:
string += str(hours) + "h "
if len(string) > 0 or minutes > 0:
string += str(minutes) + "m "
if hours == 0 and seconds >0:
string += str(seconds) + "s"
return string;
def ip_client(thislease):
ips = thislease["ipAddress"].split('.')
return "%s.%s: %s" % (ips[2],ips[3], thislease["name"])
def stats():
global looper
lease = None
with canvas(device) as draw:
#draw.rectangle((0,0,127,63), outline="white", fill="black")
if looper==0:
looper=1
if lora_mote != None:
draw.text((col1, line1),"Mote %s" % lora_mote, font=font10, fill=255)
draw.text((col1, line2),"RSSI %sdBi" % lora_rssi, font=font10, fill=255)
draw.text((col1, line3),"Chan %s" % lora_chan, font=font10, fill=255)
draw.text((col1, line4),"Freq %.2f MHz" % lora_freq, font=font10, fill=255)
draw.text((col1, line5),"Rate %s" % lora_datr, font=font10, fill=255)
else:
draw.text((col1, line1),"No LoRaWAN Data yet", font=font10, fill=255)
elif looper==1:
looper = 2
draw.text((col1, line1),"Host :%s" % socket.gethostname(), font=font10, fill=255)
# Try to get wlan0 if not then eth0
ip, stats = lan_info("eth0")
if ip == "No Interface":
ip, stats = lan_info("wlan0")
draw.text((col1, line2), ip, font=font10, fill=255)
draw.text((col1, line3), stats, font=font10, fill=255)
ip, stats = lan_info("ap0")
if ip != "No IP" and ip != "No Interface":
draw.text((col1, line4), ip, font=font10, fill=255)
draw.text((col1, line5), stats, font=font10, fill=255)
lease = getLeases()
else:
draw.text((col1, line4), "ap0", font=font10, fill=255)
draw.text((col1, line5), "No Access Point", font=font10, fill=255)
if lease == None:
looper = 3
elif looper==2:
looper = 3
lease = getLeases()
lg = len(lease)
draw.text((col1, line1), "Wifi Clients => %d" % lg, font=font10, fill=255)
if lg>=1: draw.text((col1, line2), ip_client(lease[0]), font=font10, fill=255)
if lg>=2: draw.text((col1, line3), ip_client(lease[1]), font=font10, fill=255)
if lg>=3: draw.text((col1, line4), ip_client(lease[2]), font=font10, fill=255)
if lg>=4: draw.text((col1, line5), ip_client(lease[3]), font=font10, fill=255)
elif looper==3:
looper = 0
tempC = int(open('/sys/class/thermal/thermal_zone0/temp').read())
av1, av2, av3 = os.getloadavg()
mem = psutil.virtual_memory()
dsk = psutil.disk_usage("/")
draw.text((col1, line1), "CPU LOAD: %.1f %.1f" % (av1, av3), font=font10, fill=255)
draw.text((col1, line2), "MEM FREE: %s/%s" % (bytes2human(mem.available), bytes2human(mem.total)), font=font10, fill=255)
draw.text((col1, line3), "DSK FREE: %s/%s" % (bytes2human(dsk.total-dsk.used), bytes2human(dsk.total)),font=font10, fill=255)
draw.text((col1, line4), "CPU TEMP: %sc" % (str(tempC/1000)), font=font10, fill=255)
draw.text((col1, line5), "UP : %s" % uptime(), font=font10, fill=255)
else:
#draw.text((col1, line1),"%s %s" % (platform.system(),platform.release()), font=font10, fill=255)
#uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
#draw.text((col1, line2),str(datetime.now().strftime('%a %b %d %H:%M:%S')), font=font10, fill=255)
#draw.text((col1, line3),"Uptime %s" % str(uptime).split('.')[0], font=font10, fill=255)
looper=0
def main():
global looper
global json_data
looper = 1
json_data = None
try:
thread.start_new_thread( udp_receive, (5, ) )
except:
print "Error: unable to start thread"
while True:
stats()
if looper==0:
time.sleep(2)
else:
time.sleep(5)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass