-
Notifications
You must be signed in to change notification settings - Fork 0
/
avctrl
executable file
·166 lines (115 loc) · 4.79 KB
/
avctrl
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
#!/usr/bin/env python
from abc import ABC
import argparse
import logging
import random
import avwx.tds as tds
from avmap.control import Api, Config
from avmap.config import read_config
from avmap.map import flight_category_colors
logger = logging.getLogger(__name__)
config = Config()
cfg = read_config()
api = Api(config.host, config.port)
def set_color(args: argparse.Namespace):
red, green, blue = map(int, str(args.color).split(",")[:3])
color = (red, green, blue)
if api.set_color(args.station, color, show=args.show):
logger.info(f"Set color {args.station} to {color}")
else:
raise SystemExit("Failed to set color")
class Mode(ABC):
name: str = None
def run(self):
raise NotImplementedError("run() not implemented in base Mode")
class RandomMode(Mode):
name = "random"
def run(self):
api.set_colors({station["code"]: [random.randint(0, 255) for _ in range(3)] for station in cfg["stations"]}, show=True)
class OffMode(Mode):
name = "off"
def run(self):
colors = {station["code"]: [0, 0, 0] for station in cfg["stations"]}
api.set_colors(colors, show=True)
class FlightCategoryMode(Mode):
name = "flight_category"
def get_color(self, data, default=[255, 255, 255]):
try:
return flight_category_colors[data["flight_category"]]
except KeyError:
return default
def run(self):
data = tds.get_latest_metar([station["code"] for station in cfg["stations"]])
colors = {station: self.get_color(details) for station, details in data.items()}
api.set_colors(colors, show=True)
# MODES is a dict of Mode subclasses in this module keyed by their names
MODES = {cls.name: cls for cls in globals().values() if isinstance(cls, type) and issubclass(cls, Mode) and cls.name}
def mode_factory(name: str):
ModeClass = MODES.get(name)
if ModeClass:
return ModeClass()
raise ValueError(f"Unknown mode {name}")
def activate_mode(args: argparse.Namespace):
if args.list:
print("Available modes:")
for name in sorted(MODES.keys()):
print(f" {name}")
return
mode = mode_factory(args.name)
mode.run()
def get_color(args: argparse.Namespace):
if args.station:
color = api.get_color(args.station)
logger.info(f"{args.station} color is {color}")
else:
colors = api.get_colors()
for station, color in colors.items():
logger.info(f"{station} color is {color}")
def show_config(args: argparse.Namespace):
print(config.json())
def set_config(args):
setattr(config, args.option, config._cast(args.option, args.value))
config.save()
def build_color_parser(parser: argparse.ArgumentParser) -> None:
subparsers = parser.add_subparsers()
set_parser = subparsers.add_parser("set", help="set color")
set_parser.add_argument("station", metavar="S",
type=str, help="The station")
set_parser.add_argument("color", metavar="C", type=str, help="The color")
set_parser.add_argument("--show", action="store_true",
default=False, help="Show the updated color")
set_parser.set_defaults(func=set_color)
get_parser = subparsers.add_parser("get", help="get color")
get_parser.add_argument("station", metavar="S", nargs="?",
type=str, help="The station, if omitted, all stations are listed")
get_parser.set_defaults(func=get_color)
def build_config_parser(parser: argparse.ArgumentParser) -> None:
subparsers = parser.add_subparsers()
show_parser = subparsers.add_parser("show")
show_parser.set_defaults(func=show_config)
set_parser = subparsers.add_parser("set")
set_parser.add_argument("option", help="option")
set_parser.add_argument("value", help="value")
set_parser.set_defaults(func=set_config)
def build_mode_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--name", help="mode name")
parser.add_argument("--list", "-l", action="store_true", help="list modes")
parser.set_defaults(func=activate_mode)
def parse_args(args=None):
parser = argparse.ArgumentParser(description="Aviation Map Controller")
subparsers = parser.add_subparsers()
color_parser = subparsers.add_parser("color", help="color commands")
build_color_parser(color_parser)
config_parser = subparsers.add_parser("config", help="config commands")
build_config_parser(config_parser)
mode_parser = subparsers.add_parser("mode", help="Display a mode")
build_mode_parser(mode_parser)
return parser.parse_args(args)
def main():
logging.basicConfig(level=logging.INFO)
args = parse_args()
if not hasattr(args, "func"):
parse_args(["--help"])
args.func(args)
if __name__ == "__main__":
main()