Skip to content

Commit 17adbe6

Browse files
committed
[ctrl] added shutdown and startup geocom commands
1 parent 83e5cc3 commit 17adbe6

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

src/instrumentman/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from . import jobs
2727
from . import datatransfer
2828
from . import settings
29+
from . import control
2930

3031

3132
@extra_group(
@@ -151,6 +152,22 @@ def cli_upload() -> None:
151152
"""Upload data to the instrument."""
152153

153154

155+
@cli.group(
156+
"shutdown",
157+
aliases=["sh", "exit", "deactivate", "turnoff", "switchoff"]
158+
) # type: ignore[misc]
159+
def cli_shutdown() -> None:
160+
"""Deactivate various instrument functions."""
161+
162+
163+
@cli.group(
164+
"startup",
165+
aliases=["st", "enter", "activate", "turnon", "switchon"]
166+
) # type: ignore[misc]
167+
def cli_startup() -> None:
168+
"""Activate various instrument functions."""
169+
170+
154171
cli.add_command(morse.cli)
155172
cli.add_command(terminal.cli)
156173
cli_measure.add_command(setmeasurement.cli_measure)
@@ -178,3 +195,5 @@ def cli_upload() -> None:
178195
cli_convert.add_command(setup.cli_convert_gsi_to_targets)
179196
cli_convert.add_command(setup.cli_convert_targets_to_gsi)
180197
cli_convert.add_command(setmeasurement.cli_convert_set_to_gsi)
198+
cli_shutdown.add_command(control.cli_shutdown_geocom)
199+
cli_startup.add_command(control.cli_startup_geocom)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from typing import Any
2+
3+
from click_extra import (
4+
extra_command,
5+
argument,
6+
Choice
7+
)
8+
9+
from ..utils import (
10+
com_port_argument,
11+
com_option_group
12+
)
13+
14+
15+
@extra_command(
16+
"geocom",
17+
params=None,
18+
context_settings={"auto_envvar_prefix": None}
19+
) # type: ignore[misc]
20+
@argument(
21+
"component",
22+
help="Instrument component to shut down",
23+
type=Choice(
24+
(
25+
"protocol",
26+
"instrument",
27+
"edm",
28+
"pointer",
29+
"telescopic-camera",
30+
"overview-camera"
31+
),
32+
case_sensitive=False
33+
)
34+
)
35+
@com_port_argument()
36+
@com_option_group()
37+
def cli_shutdown_geocom(**kwargs: Any) -> None:
38+
from .app import main_shutdown_geocom
39+
40+
main_shutdown_geocom(**kwargs)
41+
42+
43+
@extra_command(
44+
"geocom",
45+
params=None,
46+
context_settings={"auto_envvar_prefix": None}
47+
) # type: ignore[misc]
48+
@argument(
49+
"component",
50+
help="Instrument component to start up",
51+
type=Choice(
52+
(
53+
"instrument",
54+
"edm",
55+
"pointer",
56+
"telescopic-camera",
57+
"overview-camera"
58+
),
59+
case_sensitive=False
60+
)
61+
)
62+
@com_port_argument()
63+
@com_option_group()
64+
def cli_startup_geocom(**kwargs: Any) -> None:
65+
from .app import main_startup_geocom
66+
67+
main_startup_geocom(**kwargs)

src/instrumentman/control/app.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from logging import getLogger
2+
3+
from geocompy.communication import open_serial
4+
from geocompy.geo import GeoCom
5+
from geocompy.geo.gctypes import GeoComCode
6+
7+
from ..utils import print_error
8+
9+
10+
def main_shutdown_geocom(
11+
component: str,
12+
port: str,
13+
timeout: int = 15,
14+
retry: int = 1,
15+
baud: int = 9600,
16+
sync_after_timeout: bool = False
17+
) -> None:
18+
logger = getLogger("iman.files.list")
19+
with open_serial(
20+
port,
21+
speed=baud,
22+
timeout=timeout,
23+
retry=retry,
24+
sync_after_timeout=sync_after_timeout,
25+
logger=logger.getChild("com")
26+
) as com:
27+
instrument = GeoCom(com, logger.getChild("instrument"))
28+
29+
match component:
30+
case "protocol":
31+
resp = instrument.com.switch_to_local()
32+
case "edm":
33+
resp = instrument.edm.switch_edm(False)
34+
case "pointer":
35+
resp = instrument.edm.switch_laserpointer(False)
36+
case "telescopic-camera":
37+
resp = instrument.cam.switch_camera_power(False, "TELESCOPIC")
38+
case "overview-camera":
39+
resp = instrument.cam.switch_camera_power(False, "OVERVIEW")
40+
case "instrument":
41+
resp = instrument.com.switch_off()
42+
case _:
43+
raise ValueError(
44+
f"Unknown component '{component}'"
45+
)
46+
47+
if resp.error != GeoComCode.OK:
48+
print_error(
49+
f"Could not deactivate '{component}' ({resp.error.name})"
50+
)
51+
exit(1)
52+
53+
54+
def main_startup_geocom(
55+
component: str,
56+
port: str,
57+
timeout: int = 15,
58+
retry: int = 1,
59+
baud: int = 9600,
60+
sync_after_timeout: bool = False
61+
) -> None:
62+
logger = getLogger("iman.files.list")
63+
with open_serial(
64+
port,
65+
speed=baud,
66+
timeout=timeout,
67+
retry=retry,
68+
sync_after_timeout=sync_after_timeout,
69+
logger=logger.getChild("com")
70+
) as com:
71+
instrument = GeoCom(com, logger.getChild("instrument"))
72+
73+
match component:
74+
case "instrument":
75+
resp = instrument.com.switch_on()
76+
case "edm":
77+
resp = instrument.edm.switch_edm(True)
78+
case "pointer":
79+
resp = instrument.edm.switch_laserpointer(True)
80+
case "telescopic-camera":
81+
resp = instrument.cam.switch_camera_power(True, "TELESCOPIC")
82+
case "overview-camera":
83+
resp = instrument.cam.switch_camera_power(True, "OVERVIEW")
84+
case _:
85+
raise ValueError(
86+
f"Unknown component '{component}'"
87+
)
88+
89+
if resp.error != GeoComCode.OK:
90+
print_error(
91+
f"Could not activate '{component}' ({resp.error.name})"
92+
)
93+
exit(1)

0 commit comments

Comments
 (0)