async def simulate( command: Command, nodes: Dict[str, Node], glob: str, delay=5.0, random: bool = False, ): """Simulate images being written to disk.""" # We make GLOB not required so that --stop doesn't need to specify a path, # but in this callback we want it defined. if glob is None: return command.fail( error="A valid path pattern is required to start the simulation.") images = globlib.glob(glob) if len(images) == 0: return command.fail(error="No images found.") sample = globlib.glob(glob) if random: randomlib.shuffle(sample) images_cycle = itertools.cycle(sample) while True: for node in nodes: command.info(filename=[node, node, next(images_cycle)]) await asyncio.sleep(delay)
async def status( command: Command, controllers: dict[str, ArchonController], controller_name: str, ): """Reads the frame status.""" if controller_name not in controllers: return command.fail(f"Controller {controller_name!r} does not exist.") controller = controllers[controller_name] if not check_controller(command, controller): return command.fail() try: frame = await controller.get_frame() except ArchonError as err: return command.fail(text={ "controller": controller.name, "text": err, }) return command.finish(frame={ "controller": controller.name, **frame, })
async def read( command: Command, controllers: dict[str, ArchonController], controller_name: str, save: bool, ): """Reads the configuration from the controller.""" if controller_name not in controllers: return command.fail(f"Controller {controller_name!r} does not exist.") controller = controllers[controller_name] if not check_controller(command, controller): return command.fail() if save: path: str | bool = os.path.expanduser( f"~/archon_{controller.name}.acf") else: path: str | bool = False try: config = await controller.read_config(save=path) except ArchonError as err: return command.fail(text={ "controller": controller.name, "text": str(err), }) if save is False: return command.finish(config={ "controller": controller.name, "config": config, }) return command.finish(text=f"Config written to {path!r}")
async def status(command: Command, controllers: dict[str, IebController]): """Returns the status of wago sensor.""" #loop = asyncio.get_running_loop() for wago in controllers: if controllers[wago].name == 'wago': try: wago_status1 = await controllers[wago].getWAGOEnv() if wago_status1: command.info(text="Temperature & Humidity is:",status={ "rhtT1(40001)":controllers[wago].sensors['rhtT1(40001)'], "rhtRH1(40002)":controllers[wago].sensors['rhtRH1(40002)'], "rhtT2(40003)":controllers[wago].sensors['rhtT2(40003)'], "rhtRH2(40004)":controllers[wago].sensors['rhtRH2(40004)'], "rtd1(40009)":controllers[wago].sensors['rtd1(40009)'], "rtd2(40010)":controllers[wago].sensors['rtd2(40010)'], "rtd3(40011)":controllers[wago].sensors['rtd3(40011)'], "rtd4(40012)":controllers[wago].sensors['rtd4(40012)'] }) else: return command.fail(text=f"ERROR: Did not read sensors/powers") except LvmIebError as err: return command.fail(error=str(err)) return command.finish()
async def status(command: Command, controllers: dict[str, IebController]): #return the status of shutter. print(controllers) command.info(text="Checking all shutters") tasks = [] for shutter in controllers: if controllers[shutter].name == 'shutter': try: tasks.append(controllers[shutter].send_command("status")) except LvmIebError as err: return command.fail(error=str(err)) result_shutter = await asyncio.gather(*tasks) for n in result_shutter: try: if n == "opened": return command.info(status={ "opened/closed:": n, }) elif n == "closed": return command.info(status={ "opened/closed:": n, }) else: return command.fail(test='shutter is in a bad state') except LvmIebError as err: return command.fail(error=str(err)) return command.finish()
async def fetch( command: Command, controllers: dict[str, ArchonController], controller_name: str, buffer: str, file, ): """Low-level command to fetch a buffer and write it to disk.""" if controller_name not in controllers: return command.fail(f"Controller {controller_name!r} does not exist.") controller = controllers[controller_name] if not check_controller(command, controller): return command.fail() buffer_no = int(buffer) if file is None: # Save to ~/archon_<controller_name>_NNNN.fits. Find the highest file with that # format and increase the counter. pattern = os.path.expanduser( f"~/archon_{controller.name}_[0-9][0-9][0-9][0-9].fits") existing = glob(pattern) if len(existing) == 0: nfile = 1 else: last = sorted(existing)[-1] nfile = int(re.search(r"([0-9]{4})\.fits$", last)[1]) + 1 path = os.path.expanduser( f"~/archon_{controller.name}_{nfile:04d}.fits") else: path: str = os.path.relpath(str(file)) dirname = os.path.dirname(path) if not os.path.exists(dirname): return command.fail( error= "Parent of file does not exists or file is badly formatted.") def notifier(msg): command.info(text={ "controller": controller.name, "text": msg, }) try: array = await controller.fetch(buffer_no, notifier=notifier) except BaseException as err: return command.fail( text={ "controller": controller.name, "text": f"Failed fetching data: {str(err)}", }) # Create a simple HDU list with the data. hdu = astropy.io.fits.PrimaryHDU(data=array) hdulist = astropy.io.fits.HDUList([hdu]) hdulist.writeto(path, overwrite=True) return command.finish(f"File saved to {path}")
async def init(command: Command, controllers: dict[str, IebController]): #return the status of hartmann. command.info(text="Checking all hartmanns") tasks = [] for h in controllers: if controllers[h].name == 'hartmann_right': try: tasks.append(controllers[h].initialize()) except LvmIebError as err: return command.fail(error=str(err)) if controllers[h].name == 'hartmann_left': try: tasks.append(controllers[h].initialize()) except LvmIebError as err: return command.fail(error=str(err)) await asyncio.gather(*tasks) return command.finish()
async def setpower(command: Command, controllers: dict[str, IebController]): """Returns the status of wago sensor.""" #loop = asyncio.get_running_loop() for wago in controllers: if controllers[wago].name == 'wago': try: wago_status1 = await controllers[wago].setWAGOPower("hartmann_right_power", 'ON') if wago_status1: command.info(text="Power state of the components are:",status={ "shutter_power":controllers[wago].power_status["shutter_power"], "hartmann_right_power":controllers[wago].power_status["hartmann_right_power"], "hartmann_left_power":controllers[wago].power_status["hartmann_left_power"] }) else: return command.fail(text=f"ERROR: Did not read sensors/powers") except LvmIebError as err: return command.fail(error=str(err)) return command.finish()
async def write( command: Command, controllers: dict[str, ArchonController], controller_name: str, config_path: click.Path, applyall: bool, poweron: bool, ): """Writes a configuration file to the controller.""" if controller_name not in controllers: return command.fail(f"Controller {controller_name!r} does not exist.") controller = controllers[controller_name] if not check_controller(command, controller): return command.fail() path = str(config_path) def notifier(msg): command.info(text={ "controller": controller.name, "text": msg, }) try: await controller.write_config( path, applyall=applyall, poweron=poweron, notifier=notifier, ) except ArchonError as err: return command.fail(text={ "controller": controller.name, "text": str(err), }) return command.finish(text=f"Config file {path!r} successfully loaded.")
async def status(command: Command, controllers: dict[str, IebController]): #return the status of hartmann. command.info(text="Checking all hartmanns") tasks = [] print(controllers) for h in controllers: print(controllers[h].name) if controllers[h].name == 'hartmann_right': print(controllers[h].name, controllers[h].host, controllers[h].port) try: tasks.append(controllers[h].get_status()) except LvmIebError as err: return command.fail(error=str(err)) if controllers[h].name == 'hartmann_left': print(controllers[h].name, controllers[h].host, controllers[h].port) try: tasks.append(controllers[h].get_status()) except LvmIebError as err: return command.fail(error=str(err)) result_hartmann = await asyncio.gather(*tasks) print(result_hartmann) try: command.info( status={ "hartmann_left opened/closed:": result_hartmann[0], "hartmann_right opened/closed:": result_hartmann[1], }) except LvmIebError as err: return command.fail(error=str(err)) return command.finish()
async def close(command: Command, controllers: dict[str, IebController], side: str): """close the hartmann""" #side = "all" tasks = [] for hartmann in controllers: if side == "all" or side == "right": if controllers[hartmann].name == 'hartmann_right': try: tasks.append(controllers[hartmann].send_command("close")) except LvmIebError as err: return command.fail(error=str(err)) if side == "all" or side == "left": if controllers[hartmann].name == 'hartmann_left': try: tasks.append(controllers[hartmann].send_command("close")) except LvmIebError as err: return command.fail(error=str(err)) command.info(text="Closing all hartmanns") await asyncio.gather(*tasks) return command.finish(hartmann="closed")
async def status(command: Command, switches: [], name: str, portnum: int): """print the status of the NPS.""" status = {} for switch in switches: try: # status |= await switch.statusAsJson(name, portnum) works only with python 3.9 status = dict( list(status.items()) + list((await switch.statusAsJson(name, portnum)).items())) except PowerException as ex: return command.fail(error=str(ex)) command.info(STATUS=status) return command.finish("done")
async def close(command: Command, controllers: dict[str, IebController]): """close the shutter""" tasks = [] for shutter in controllers: if controllers[shutter].name == 'shutter': try: tasks.append(controllers[shutter].send_command("close")) except LvmIebError as err: return command.fail(error=str(err)) command.info(text="Closing all shutters") print("----close----") current_time = datetime.datetime.now() print('before command gathered : %s', current_time) await asyncio.gather(*tasks) current_time = datetime.datetime.now() print('after command gathered : %s', current_time) return command.finish(shutter="closed")