-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·178 lines (148 loc) · 4.89 KB
/
cli.py
File metadata and controls
executable file
·178 lines (148 loc) · 4.89 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
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
#!/usr/bin/env python
import json
import subprocess
import click
def mkdir_cmd(port, dir_path):
return ['ampy', '--port', port, '-d', '0.5', 'mkdir', dir_path]
def put_cmd(port, src_path, dest_path=None):
cmd_list = ['ampy', '--port', port, '-d', '0.5', 'put', src_path]
if dest_path:
cmd_list.append(dest_path)
return cmd_list
@click.group()
def cli():
pass
@cli.command()
@click.option(
'--chip',
required=True,
type=click.Choice(['esp8266', 'esp32'], case_sensitive=False),
help='The chip type you want to flash'
)
@click.option(
'--port',
required=True,
type=str,
help='The usb port the device is connect to'
)
@click.option('--bin-file', required=True, type=str, help='The path of the bin file')
def flash(chip, port, bin_file):
"""
Erases the chip's flash and writes it to the chip again.
"""
# Erase the flash
click.echo('Erasing flash')
subprocess.run([
'esptool.py',
'--chip',
chip,
'--port',
port,
'erase-flash'
])
# Flash the chip
click.echo(f'\n\nFlashing device with `{bin_file.split("/")[-1]}`')
subprocess.run([
'esptool.py',
'--chip',
chip,
'--port',
port,
'--baud',
'460800',
'write-flash',
'-z',
'0x1000',
bin_file,
])
@cli.command()
@click.option(
'--port',
required=True,
type=str,
help='The usb port the device is connect to'
)
@click.option('--init-config', is_flag=True, help='Reinitialise the config file')
@click.option(
'--config-file',
type=str,
required=False,
help='Reinitialise config from a file'
)
def install(port, init_config, config_file):
"""
Installs the firmware to the chip
"""
if init_config:
with open('iotdevice/config/config.example.json', 'r') as _file:
config = json.loads(_file.read())
if config_file:
with open(config_file, 'r') as _file:
init_config = dict(
[item.strip().split(': ') for item in _file.readlines()]
)
config['wifi']['essid'] = init_config['wifi_essid']
config['wifi']['password'] = init_config['wifi_password']
config['mqtt']['host'] = init_config['mqtt_host']
iot_server_host = init_config['iot_server_host']
config['health']['url'] = (
f'http://{iot_server_host}:8000/health/' + '{identifier}/'
)
config['main']['webrepl_password'] = init_config['webrepl_password']
drivers = init_config.get('drivers', None)
if drivers:
config['drivers'] = [d.strip() for d in drivers.split(',')]
else:
config['wifi']['essid'] = click.prompt('WiFi SSID', type=click.STRING)
config['wifi']['password'] = click.prompt(
'WiFi Password',
type=click.STRING,
hide_input=True,
confirmation_prompt=True
)
config['mqtt']['host'] = click.prompt('MQTT Host', type=click.STRING)
iot_server_host = click.prompt('Iot Server Host', type=click.STRING)
config['health']['url'] = (
f'http://{iot_server_host}:8000/health/' + '{identifier}/'
)
config['main']['webrepl_password'] = click.prompt(
'Web REPL Password',
type=click.STRING,
hide_input=True,
confirmation_prompt=True
)
drivers = click.prompt('List of drivers to import', type=click.STRING, default='')
if drivers:
config['drivers'] = [d.strip() for d in drivers.split(',')]
with open('iotdevice/config/config.json', 'w') as _file:
_file.write(json.dumps(config, indent=4))
# Write the firmware to the device
click.echo(f'Writing firmware to `{port}`')
subprocess.run(mkdir_cmd(port, 'config'))
subprocess.run(
put_cmd(port, 'iotdevice/config/config.json', 'config/config.json')
)
if config.get('drivers'):
subprocess.run(mkdir_cmd(port, 'drivers'))
subprocess.run(
put_cmd(
port,
'iotdevice/drivers/__init__.py',
'drivers/__init__.py'
)
)
for driver_name in config['drivers']:
subprocess.run(
put_cmd(
port,
f'iotdevice/drivers/{driver_name}.py',
f'drivers/{driver_name}.py'
)
)
subprocess.run(put_cmd(port, 'iotdevice/__init__.py'))
subprocess.run(put_cmd(port, 'iotdevice/utils.py'))
subprocess.run(put_cmd(port, 'iotdevice/rules.py'))
subprocess.run(put_cmd(port, 'iotdevice/boot.py'))
subprocess.run(put_cmd(port, 'iotdevice/main.py'))
if __name__ == '__main__':
cli()