-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpndSRC.py
290 lines (253 loc) · 9.02 KB
/
pndSRC.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
import network
import pndConfig as cfg
import time
import ntptime
import machine
import socket
import gc
import sys
import ujson
import json
from machine import Pin, I2C, ADC
class storage():
def __init__(self):
self.file = "data.pnd"
self.str = ""
self.obj = {}
def read(self):
r = open(self.file)
self.obj = json.loads(r.read())
return self.obj
def write(self, obj):
self.str = json.dumps(obj, separators=None)
f = open(self.file, "w" )
f.write(self.str)
f.close()
def cx(self, cx):
mystr = json.dumps(cx, separators=None)
f = open("cx.pnd", "w" )
f.write(mystr)
f.close()
class dtime():
def __init__(self):
year, mon, day, h, m, s, nope, nope2 = time.localtime()
h = h + 2
year = year - 2000
if(h == 24 ):
h = 0
elif(h == 25):
h = 1
self.date = str(day) + "." + str(mon) + "." + str(year)
if(m < 10):
sM = "0" + str(m)
else:
sM = str(m)
if(s < 10):
sS = "0" + str(s)
else:
sS = str(s)
if(h < 10):
sH = "0" + str(h)
else:
sH = str(h)
self.time = sH + ":" + sM+ ":" + sS
class wifi():
def __init__(self):
self.wlan = network.WLAN(network.STA_IF)
self.lastTry = 0
self.e = "wifi struggles"
def connect(self):
conntimeout = 0
wlan = self.wlan
wlan.active(False)
wlan.active(True)
wlan.connect(cfg.wifi.gc_ssid, cfg.wifi.gc_secret)
print('connecting to: ' + cfg.wifi.gc_ssid)
while not wlan.isconnected():
print('connecting...')
time.sleep(1)
conntimeout = conntimeout + 1
if(conntimeout > 20): break
if(not wlan.isconnected()): ecx.handle(ecx(),self,self.e)
print(wlan.ifconfig())
print('connected!')
def disconnect(self):
wlan = self.wlan
wlan.disconnect()
wlan.active(False)
print('disconnected!')
def reconnect(self):
wlan = self.wlan
wlan.disconnect()
wlan.active(False)
print('disconnected!')
wlan.active(False)
del wlan
time.sleep(1)
self.wlan = network.WLAN(network.STA_IF)
self.wlan.active(True)
conntimeout = 1
self.wlan.connect(cfg.wifi.gc_ssid, cfg.wifi.gc_secret)
print('connecting to: ' + cfg.wifi.gc_ssid)
while not self.wlan.isconnected():
print('connecting...')
time.sleep(1)
conntimeout = conntimeout + 1
self.lastTry = 1
if(conntimeout > 20): break
if(not self.wlan.isconnected()): ecx.handle(ecx(),self, self.e)
def getMac(self):
import ubinascii
mac = ubinascii.hexlify(self.wlan.config('mac'),':').decode()
return mac
def status(self):
wlan = self.wlan
status = wlan.status()
if(status == 3):
ip, netmask, gateway, dns = wlan.ifconfig()
status = "\r\nWLan connected to: " + cfg.wifi.gc_ssid
status += "\r\nWLan IP: " + ip
status += "\r\nWLan NetMask: " + netmask
status += "\r\nWLan Gateway: " + gateway
status += "\r\nWLan DNS: " + dns
return status
else:
return "\r\nnot connected"
def getIp(self):
wlan = self.wlan
ip, netmask, gateway, dns = wlan.ifconfig()
return ip
class system():
def __init__(self):
self.timeInit = time.time()
self.obj = None
def setCallback(self, obj):
self.cb = obj
def status(self):
cpufq = machine.freq() / 1000000
memfree = gc.mem_free() / 1024
memusage = gc.mem_alloc() / 1024
mem = memfree + memusage
system = "System:\nDevice: "+ cfg.gc_name + "\nSystem: " + sys.platform + "\nFirmware: " + sys.version
system += "\n\nRessources:\nCPU Speed: " + "{:.0f}".format(cpufq) + "MHz"
system += "\nMEM All: " + "{:.2f}".format(mem) + "Kb"
system += "\nMEM Free: " + "{:.2f}".format(memfree) + "Kb"
system += "\nMEM Used: " + "{:.2f}".format(memusage) + "Kb"
return system
def statusJSON(self):
pass
def getUptimeM(self):
timeDiff = time.time()-self.timeInit
(minutes, seconds) = divmod(timeDiff, 60)
(hours, minutes) = divmod(minutes, 60)
(days,hours) = divmod(hours, 24)
return minutes
class ecx():
def handle(self,obj, e):
try:
if(hasattr(obj, 'lastTry')):
if(obj.lastTry == 1):
print("FATAL ERROR - Restart device. Error: " + str(e))
time.sleep(5)
machine.reset()
print("FATAL ERROR - Trying recovery from: " + str(e))
time.sleep(2)
del obj
import pnd as recoverPND
fresh_runtime = recoverPND.rt()
fresh_runtime.init(1)
time.sleep(1)
fresh_runtime.run()
except:
import pnd as lastPND
last_runtime = lastPND.rt()
last_runtime.init(1)
time.sleep(1)
last_runtime.run()
class webserver():
sockl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockl.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def showStatusPage(self):
s = self.sockl
s.bind(('', 80))
s.listen(5)
print('WebServices up and running! Waiting for connections...')
while True:
try:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
self.handler(request, conn)
gc.collect()
except Exception as e:
pass
def handler(self, request, conn):
#print('Request = %s' % request)
json = request.find('/?json=')
if(json == 6):
self.JSONHandler(request, conn)
exit()
get = request.find('/?get=system')
restart = request.find('/?fun=reset')
print(get)
response = ""
if(get == 6):
s = system()
stat = system.status()
final = stat.replace("\n", "<br>")
response += '<html><head><title>Pundo Web Server</title></head><body><h1>Pundo Web Server</h1><p><strong>' + final + '</strong></p></body></html>'
print("bin 2")
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
elif(restart == 6):
print('time to restart')
response += '<html><head><title>Pundo Web Server</title></head><body><h1>Pundo Web Server</h1><p><strong>Rebooting</strong></p></body></html>'
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
time.sleep(3)
machine.reset()
else:
response += '<html><head><title>Pundo Web Server</title></head><body><h1>Pundo Web Server</h1><p><strong>Wrong Command</strong></p></body></html>'
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
def JSONHandler(self, request, conn):
pass
class pndIFBase:
def pndObj(self, id:str) -> Object:
"""Test pls hang on"""
pass
def pndTest(self) ->str:
return "Still in development please hang on"
class httpRQ(pndIFBase):
def pndObj(self):
return self
def post(self, url, data):
post_data = data ##ujson.dumps(data)
request_url = url
res = requests.post(request_url, headers = {'content-type': 'application/json'}, data = post_data)
return res.text
class pndIFGame:
Game = object()
def gameRuntime(self) -> Object:
"""Must return an object wtih an run method"""
pass
def setUp(self) ->str:
self.Game = self.gameRuntime()
self.Game.setup()
return "setup Done"
def run(self) ->str:
self.Game.run()
return "Game started"
def stop(self) ->str:
self.Game.stop()
return "Game Stopped"