Skip to content

Commit b51bded

Browse files
authored
v0.16.0
version 0.16.0
2 parents add11dc + deefaae commit b51bded

File tree

5 files changed

+150
-82
lines changed

5 files changed

+150
-82
lines changed

pyobs_alpaca/device.py

+40-17
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def _check_connected(self) -> None:
9090
try:
9191
await self._get(self._alive_param)
9292
connected = True
93-
except ValueError:
93+
except ConnectionError:
9494
connected = False
9595

9696
# did it change?
@@ -129,22 +129,32 @@ async def _get(self, name: str) -> Any:
129129
130130
Returns:
131131
Value of variable.
132+
133+
Raises:
134+
ConnectionError: If an error occurred.
132135
"""
133136

134137
# get url
135138
url = self._build_alpaca_url(name)
136139

137140
# request it
138-
async with aiohttp.ClientSession() as session:
139-
async with session.get(url, timeout=5) as response:
140-
if response.status != 200:
141-
raise ValueError("Could not contact server.")
142-
json = await response.json()
143-
resp = ServerGetResponse(**json)
141+
try:
142+
async with aiohttp.ClientSession() as session:
143+
async with session.get(url, timeout=5) as response:
144+
if response.status != 200:
145+
raise ConnectionError(
146+
f"ALPACA server responded with error {response.status}: {await response.text()}."
147+
)
148+
json = await response.json()
149+
resp = ServerGetResponse(**json)
150+
151+
except asyncio.TimeoutError:
152+
# raise a ConnectionError instead
153+
raise ConnectionError("Connection to ALPACA server timed out.")
144154

145155
# check error
146156
if resp.ErrorNumber != 0:
147-
raise ValueError("Server error: %s" % resp.ErrorMessage)
157+
raise ConnectionError("Server error: %s" % resp.ErrorMessage)
148158

149159
# return value
150160
return resp.Value
@@ -157,11 +167,14 @@ async def get(self, name: str) -> Any:
157167
158168
Returns:
159169
Value of variable.
170+
171+
Raises:
172+
ConnectionError: If an error occurred.
160173
"""
161174

162175
# only do it, if connected
163176
if not self._connected:
164-
raise ValueError("Not connected to ASCOM.")
177+
raise ConnectionError("Not connected to ASCOM.")
165178
return await self._get(name)
166179

167180
async def put(self, name: str, timeout: float = 5, **values: Any) -> None:
@@ -171,26 +184,36 @@ async def put(self, name: str, timeout: float = 5, **values: Any) -> None:
171184
name: Name of variable.
172185
timeout: Time in sec for request.
173186
values: Values to set.
187+
188+
Raises:
189+
ConnectionError: If an error occurred.
174190
"""
175191

176192
# only do it, if connected
177193
if not self._connected:
178-
raise ValueError("Not connected to ASCOM.")
194+
raise ConnectionError("Not connected to ASCOM.")
179195

180196
# get url
181197
url = self._build_alpaca_url(name)
182198

183199
# request it
184-
async with aiohttp.ClientSession() as session:
185-
async with session.put(url, data=values, timeout=timeout) as response:
186-
if response.status != 200:
187-
raise ValueError("Could not contact server.")
188-
json = await response.json()
189-
resp = ServerPutResponse(**json)
200+
try:
201+
async with aiohttp.ClientSession() as session:
202+
async with session.put(url, data=values, timeout=timeout) as response:
203+
if response.status != 200:
204+
raise ConnectionError(
205+
f"ALPACA server responded with error {response.status}: {await response.text()}."
206+
)
207+
json = await response.json()
208+
resp = ServerPutResponse(**json)
209+
210+
except asyncio.TimeoutError:
211+
# raise a ConnectionError instead
212+
raise ConnectionError("Connection to ALPACA server timed out.")
190213

191214
# check error
192215
if resp.ErrorNumber != 0:
193-
raise ValueError("Server error: %s" % resp.ErrorMessage)
216+
raise ConnectionError("Server error: %s" % resp.ErrorMessage)
194217

195218

196219
__all__ = ["AlpacaDevice"]

pyobs_alpaca/dome.py

+36-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pyobs.utils.enums import MotionStatus
1111
from pyobs.utils.parallel import event_wait
1212
from pyobs.utils.threads import LockWithAbort
13+
from pyobs.utils import exceptions as exc
1314
from .device import AlpacaDevice
1415

1516
log = logging.getLogger("pyobs")
@@ -67,7 +68,7 @@ async def init(self, **kwargs: Any) -> None:
6768
"""Open dome.
6869
6970
Raises:
70-
ValueError: If dome cannot be opened.
71+
InitError: If dome cannot be opened.
7172
"""
7273

7374
# if already opening, ignore
@@ -81,7 +82,11 @@ async def init(self, **kwargs: Any) -> None:
8182
await self._change_motion_status(MotionStatus.INITIALIZING)
8283

8384
# execute command
84-
await self._device.put("OpenShutter")
85+
try:
86+
await self._device.put("OpenShutter")
87+
except ConnectionError:
88+
await self._change_motion_status(MotionStatus.UNKNOWN)
89+
raise exc.InitError("Could not open dome.")
8590

8691
# wait for it
8792
status = None
@@ -94,7 +99,11 @@ async def init(self, **kwargs: Any) -> None:
9499

95100
# wait a little and update
96101
await event_wait(self._abort_shutter, 1)
97-
status = await self._device.get("ShutterStatus")
102+
try:
103+
status = await self._device.get("ShutterStatus")
104+
except ConnectionError:
105+
await self._change_motion_status(MotionStatus.UNKNOWN)
106+
raise exc.InitError("Could not open dome.")
98107

99108
# set new status
100109
log.info("Dome opened.")
@@ -106,7 +115,7 @@ async def park(self, **kwargs: Any) -> None:
106115
"""Close dome.
107116
108117
Raises:
109-
ValueError: If dome cannot be opened.
118+
ParkError: If dome cannot be opened.
110119
"""
111120

112121
# if already closing, ignore
@@ -121,8 +130,12 @@ async def park(self, **kwargs: Any) -> None:
121130
await self.comm.send_event(RoofClosingEvent())
122131

123132
# send command for closing shutter and rotate to South
124-
await self._device.put("CloseShutter")
125-
await self._device.put("SlewToAzimuth", Azimuth=0)
133+
try:
134+
await self._device.put("CloseShutter")
135+
await self._device.put("SlewToAzimuth", Azimuth=0)
136+
except ConnectionError:
137+
await self._change_motion_status(MotionStatus.UNKNOWN)
138+
raise exc.ParkError("Could not close dome.")
126139

127140
# wait for it
128141
status = None
@@ -131,11 +144,15 @@ async def park(self, **kwargs: Any) -> None:
131144
if status == 4:
132145
log.error("Could not close dome.")
133146
await self._change_motion_status(MotionStatus.UNKNOWN)
134-
return
147+
raise exc.ParkError("Could not close dome.")
135148

136149
# wait a little and update
137150
await event_wait(self._abort_shutter, 1)
138-
status = await self._device.get("ShutterStatus")
151+
try:
152+
status = await self._device.get("ShutterStatus")
153+
except ConnectionError:
154+
await self._change_motion_status(MotionStatus.UNKNOWN)
155+
raise exc.ParkError("Could not close dome.")
139156

140157
# set new status
141158
log.info("Dome closed.")
@@ -147,17 +164,24 @@ async def _move(self, az: float, abort: asyncio.Event) -> None:
147164
Args:
148165
az: Azimuth to move to.
149166
abort: Abort event.
167+
168+
Raises:
169+
MoveError: If dome cannot be moved.
150170
"""
151171

152172
# execute command
153-
await self._device.put("SlewToAzimuth", Azimuth=self._adjust_azimuth(az))
173+
try:
174+
await self._device.put("SlewToAzimuth", Azimuth=self._adjust_azimuth(az))
175+
except ConnectionError:
176+
await self._change_motion_status(MotionStatus.UNKNOWN)
177+
raise exc.MoveError("Could not move dome.")
154178

155179
# wait for it
156180
log_timer = 0
157181
while 180 - abs(abs(az - self._azimuth) - 180) > self._tolerance:
158182
# abort?
159183
if abort.is_set():
160-
return
184+
raise InterruptedError("Moving dome aborted.")
161185

162186
# log?
163187
if log_timer == 0:
@@ -186,7 +210,7 @@ async def move_altaz(self, alt: float, az: float, **kwargs: Any) -> None:
186210
az: Az in deg to move to.
187211
188212
Raises:
189-
ValueError: If device could not move.
213+
MoveError: If device could not be moved.
190214
"""
191215

192216
# do nothing, if not ready
@@ -261,7 +285,7 @@ async def _update_status(self) -> None:
261285
# get azimuth
262286
try:
263287
self._azimuth = self._adjust_azimuth(await self._device.get("Azimuth"))
264-
except ValueError:
288+
except ConnectionError:
265289
# ignore it
266290
pass
267291

pyobs_alpaca/focuser.py

+38-27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pyobs.modules import timeout
99
from pyobs.utils.enums import MotionStatus
1010
from pyobs.utils.threads import LockWithAbort
11+
from pyobs.utils import exceptions as exc
1112
from .device import AlpacaDevice
1213

1314
log = logging.getLogger(__name__)
@@ -32,6 +33,9 @@ def __init__(self, **kwargs: Any):
3233
# init mixins
3334
MotionStatusMixin.__init__(self, motion_status_interfaces=["IFocuser"])
3435

36+
# register exception
37+
exc.register_exception(exc.MotionError, 3, timespan=600, callback=self._default_remote_error_callback)
38+
3539
async def open(self) -> None:
3640
"""Open module."""
3741
await Module.open(self)
@@ -46,15 +50,15 @@ async def init(self, **kwargs: Any) -> None:
4650
"""Initialize device.
4751
4852
Raises:
49-
ValueError: If device could not be initialized.
53+
InitError: If device could not be initialized.
5054
"""
5155
pass
5256

5357
async def park(self, **kwargs: Any) -> None:
5458
"""Park device.
5559
5660
Raises:
57-
ValueError: If device could not be parked.
61+
ParkError: If device could not be parked.
5862
"""
5963
pass
6064

@@ -79,19 +83,20 @@ async def get_fits_header_before(
7983
# return header
8084
return {"TEL-FOCU": (pos / step, "Focus of telescope [mm]")}
8185

82-
except ValueError as e:
86+
except ConnectionError as e:
8387
log.warning("Could not determine focus position: %s", e)
8488
return {}
8589

8690
@timeout(60000)
8791
async def set_focus(self, focus: float, **kwargs: Any) -> None:
8892
"""Sets new focus.
8993
90-
Args:
91-
focus: New focus value.
94+
Raises:
95+
MoveError: If telescope cannot be moved.
9296
"""
9397

9498
# set focus + offset
99+
95100
await self._set_focus(focus + self._focus_offset)
96101

97102
async def set_focus_offset(self, offset: float, **kwargs: Any) -> None:
@@ -122,28 +127,34 @@ async def _set_focus(self, focus: float) -> None:
122127

123128
# acquire lock
124129
async with LockWithAbort(self._lock_motion, self._abort_motion):
125-
# get step size
126-
step = await self._device.get("StepSize")
127-
128-
# calculating new focus and move it
129-
log.info("Moving focus to %.2fmm...", focus)
130-
await self._change_motion_status(MotionStatus.SLEWING, interface="IFocuser")
131-
foc = int(focus * step * 1000.0)
132-
await self._device.put("Move", Position=foc)
133-
134-
# wait for it
135-
while abs(await self._device.get("Position") - foc) > 10:
136-
# abort?
137-
if self._abort_motion.is_set():
138-
log.warning("Setting focus aborted.")
139-
return
140-
141-
# sleep a little
142-
await asyncio.sleep(0.1)
143-
144-
# finished
145-
log.info("Reached new focus of %.2fmm.", await self._device.get("Position") / step / 1000.0)
146-
await self._change_motion_status(MotionStatus.POSITIONED, interface="IFocuser")
130+
try:
131+
# get step size
132+
step = await self._device.get("StepSize")
133+
134+
# calculating new focus and move it
135+
log.info("Moving focus to %.2fmm...", focus)
136+
await self._change_motion_status(MotionStatus.SLEWING, interface="IFocuser")
137+
foc = int(focus * step * 1000.0)
138+
await self._device.put("Move", Position=foc)
139+
140+
# wait for it
141+
while abs(await self._device.get("Position") - foc) > 10:
142+
# abort?
143+
if self._abort_motion.is_set():
144+
await self._device.put("Halt")
145+
await self._change_motion_status(MotionStatus.POSITIONED, interface="IFocuser")
146+
raise InterruptedError("Setting focus aborted.")
147+
148+
# sleep a little
149+
await asyncio.sleep(0.1)
150+
151+
# finished
152+
log.info("Reached new focus of %.2fmm.", await self._device.get("Position") / step / 1000.0)
153+
await self._change_motion_status(MotionStatus.POSITIONED, interface="IFocuser")
154+
155+
except ConnectionError:
156+
await self._change_motion_status(MotionStatus.ERROR, interface="IFocuser")
157+
raise exc.MoveError("Could not move focus.")
147158

148159
async def get_focus(self, **kwargs: Any) -> float:
149160
"""Return current focus.

0 commit comments

Comments
 (0)