forked from cr/hx870
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite-config.py
More file actions
executable file
·77 lines (64 loc) · 2.4 KB
/
write-config.py
File metadata and controls
executable file
·77 lines (64 loc) · 2.4 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse, os, struct, sys
import coloredlogs, logging
import hxtool
def progress_bar(progress):
sys.stdout.write(".")
sys.stdout.flush()
def config_write(args, config):
# Help with device selection: Try to auto-detect the device model for
# the config data read from disk
magic = struct.unpack('>h', config[0:2])[0]
if not args.model:
for model in hxtool.device.models.keys():
if magic == hxtool.device.models[model].config_model.CONFIG_MAGIC:
args.model = model
devices = hxtool.device.enumerate(
force_model = args.model,
force_device = args.tty,
)
if len(devices) != 1:
# Include the model determined from the config data in the error message
device_name = f"{args.model} " if args.model else ""
wrong_device_count = f"Multiple {device_name}devices" if devices else f"No {device_name}device"
print( f"{wrong_device_count} detected. Try --model or --tty." )
sys.exit(1)
h = devices[0]
if not h.comm.cp_mode or not h.comm.hx_hardware:
print( "Could not open connection." )
sys.exit(1)
# Show device identification, to help users with selecting the right one
mmsi = h.config.read_mmsi()[0]
atis = h.config.read_atis()[0]
callsign = hxtool.callsign.determine(atis)
if not mmsi or mmsi == "FFFFFFFFF":
mmsi = "not set"
if callsign:
callsign = ", call sign " + callsign
elif atis and atis != "FFFFFFFFFF" and mmsi == "not set":
callsign = ", ATIS " + atis
print( f"Device MMSI before writing was {mmsi}{callsign}" )
sys.stdout.write( f"Writing to {h.handle} memory " )
sys.stdout.flush()
try:
coloredlogs.set_level(logging.WARNING)
config = h.config.config_write(config, check_region=False, progress=progress_bar)
except Exception as exc:
print( " Error!" )
raise exc
print( " done" )
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model")
parser.add_argument("-t", "--tty")
parser.add_argument("filename")
args = parser.parse_args()
with open(args.filename, "rb") as f:
config = f.read()
try:
config_write(args, config)
except KeyboardInterrupt:
print( "\nAborted. Warning: The device may be in an inconsistent state." )
sys.exit(1)
main()