forked from Selfnet/plastikonf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator.py
58 lines (51 loc) · 1.61 KB
/
configurator.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
import util
import pymongo
import time
import wifi
import device as devlib
c = pymongo.MongoClient()
db = c.plastikonf
devices_coll = db.devices
wifis_coll = db.wifis
while True :
wifis = tuple(map(lambda z: z["mac"], wifis_coll.find()))
devices = tuple(devices_coll.find({"state":"inserted", "mac":{"$in":wifis}}).sort("tries", pymongo.ASCENDING))
if len(devices) == 0:
print("No configureable devices found, retrying in 1sec")
time.sleep(1)
continue
device = devices[0]
old_essid = util.essid_from_mac(device["mac"])
pin = device["pin"]
print("Selected "+old_essid+", try #"+str(device["tries"]))
time.sleep(1)
print("Trying to connect to "+old_essid)
r=wifi.connect(old_essid, pin)
if r["status"] != "ok" :
print("Couldn't connect because "+r["reason"])
devices_coll.update({"_id":device["_id"]}, {"$inc": {"tries": 1}})
time.sleep(1)
continue
print("Connected, trying to upload config")
dev = devlib.Device("192.168.0.1")
if not dev.ping() :
print("Device not reachable")
devices_coll.update({"_id":device["_id"]}, {"$inc": {"tries": 1}})
time.sleep(1)
continue
if not dev.config_init() :
print("Device not accessible")
devices_coll.update({"_id":device["_id"]}, {"$inc": {"tries": 1}})
time.sleep(1)
continue
dev.set_essid(device["essid"])
dev.set_wpakey(device["psk"])
dev.set_adminpw(device["adminpw"])
if not dev.upload_config() :
print("Config upload failed")
devices_coll.update({"_id":device["_id"]}, {"$inc": {"tries": 1}})
time.sleep(1)
continue
print("All done")
devices_coll.update({"_id":device["_id"]}, {"$set": {"state": "configured"}})
#wifi.disconnect()