-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (73 loc) · 2.96 KB
/
main.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
import configparser
import sys
import shutil
import csv
import os
import subprocess
if __name__ == "__main__":
# Parse command line arguments
_, projectPath, ssid, wifiPassword, hostAddress, hostSecret = sys.argv
# Copy project to working directory, making a backup.
try:
os.mkdir("./PIO")
except FileExistsError:
pass
shutil.rmtree("./PIO", ignore_errors=True)
try:
shutil.copytree(projectPath,"./PIO", dirs_exist_ok=True)
except shutil.Error:
# Most likely it couldn't copy read only git files.
pass
# CSV layout:
# DeviceName, DeviceIP, CurrentOTAPasswod, NewOTAPassword, DOOR/INTERLOCK, RF125PS_READER/OLD_READER, RGBW/GRBW, N_LEDS, SKELETON_CARD
# Parse csv file
csvEntries = []
with open("devices.csv", "r") as csvFile:
reader = csv.reader(csvFile, delimiter=",")
for row in reader:
csvEntries.append(row)
# Strip spaces
csvEntries = [[entry.strip() for entry in row] for row in csvEntries]
# Parse Platformio.ini
platformIOConfig = configparser.ConfigParser()
platformIOConfig.read("./PIO/platformio.ini")
originalBuildFlags = platformIOConfig["env:ota"]["build_flags"]
originalBuildFlags = originalBuildFlags.rstrip("\n") + "\n" # Ensure proper newline
# Compile and upload firmware using PIO
resultsSummary = []
for device in csvEntries:
print(f"Working on {device[0]}")
print(device)
# Make build flags
buildFlags= originalBuildFlags + \
f"-DWIFI_PASSWORD=\"{wifiPassword}\"\n" + \
f"-DWIFI_SSID=\"{ssid}\"\n" + \
f"-DHOST_SECRET=\"{hostSecret}\"\n" + \
f"-DHOST_ADDRESS=\"{hostAddress}\"\n" + \
f"-DDEVICE_NAME=\"{device[0]}\"\n" + \
f"-DOTA_PASSWORD=\"{device[3]}\"\n" + \
f"-DSKELETON_CARD={device[8]}\n" + \
f"-DN_LEDS={device[7]}\n" + \
f"-D{device[4]}\n" + \
f"-D{device[5]}\n" + \
f"-D{device[6]}\n"
print(buildFlags)
# Edit platformio.ini
platformIOConfig["env:ota"]["upload_port"] = device[1]
platformIOConfig["env:ota"]["upload_flags"] = f"\n--port=8266\n--auth={str(device[2])}"
platformIOConfig["env:ota"]["build_flags"] = buildFlags
with open("./PIO/platformio.ini", "w") as platformIOConfigFile:
platformIOConfig.write(platformIOConfigFile)
# PIO run
exitCode = subprocess.call(["pio", "run", "-t", "upload", "-d", "./PIO", "-e", "ota"])
endc = '\033[0m'
if exitCode == 0:
color = '\033[92m'
resultsSummary.append(f"{device[0]}{color} SUCCESS{endc}")
else:
color = '\033[91m'
resultsSummary.append(f"{device[0]}{color} FAIL{endc}")
# Display summary.
print("\nSummary:\n")
for result in resultsSummary:
print(result)