#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Websocket part""" # Third-party modules from astropy.time import Time import time import requests import os # Custom modules from utils.url_stuff import api_base_url def req(address, timeout=3, json=True): """Simple GET request managing some exceptions""" try: res = requests.get(address, timeout=timeout) if res.status_code != 200: return False else: return res.json() if json else res except requests.exceptions.ConnectTimeout as e: return None except requests.exceptions.ConnectionError as e: return None except requests.exceptions.ReadTimeout as e: return None def send_timestamp(socketio): while True: now = Time.now() socketio.emit('timestamp', now.unix) time.sleep(1) def send_status(socketio, url, timeout=1): name = url # maybe different? while True: try: res = req(api_base_url + url) socketio.emit(name, res) except Exception as e: print(f"cannot curl {url}") print(e) time.sleep(1) def send_webcam(socketio): url = "/api/webcam/snapshot" name = url while True: try: res = req(api_base_url + url) img = res["response"].encode("ISO-8859-1") socketio.emit(url, bytes(img)) except Exception as e: print(f"cannot curl {url}") print(e) time.sleep(1) def send_webcam(socketio): url = "/api/webcam/snapshot" name = url while True: try: res = req(api_base_url + url) img = res["response"].encode("ISO-8859-1") socketio.emit(url, bytes(img)) except Exception as e: print(f"cannot curl {url}") print(e) time.sleep(2) # Function to continuously check for new lines and broadcast them def check_new_lines(socketio, filename): old_lines = [] while True: print(filename) with open(filename, 'r') as f: lines = f.readlines() new_lines = lines[-1:] # Get the last line if new_lines != old_lines: socketio.emit('new_lines', new_lines) old_lines = new_lines time.sleep(2) # Sleep for 2 seconds # Function to simulate 'tail -f 30' behavior def tail_f(socketio, num_lines=30): today = Time.now().iso.split()[0] filename = f"./data/log/OARPAF.{today}.log" file_size = os.path.getsize(filename) while True: current_size = os.path.getsize(filename) if current_size < file_size: # File has been truncated or deleted, reset file pointer file_size = 0 with open(filename, 'r') as f: lines = f.readlines()[-num_lines:] elif current_size > file_size: # File has been appended with open(filename, 'r') as f: f.seek(file_size) new_lines = f.readlines() file_size = current_size if new_lines: socketio.emit('new_lines', new_lines) else: # File remains unchanged pass socketio.sleep(0.2) # Sleep for 2 seconds before checking again def start_broadcast(socketio): socketio.start_background_task(tail_f, socketio=socketio) socketio.start_background_task(send_timestamp, socketio=socketio) socketio.start_background_task(send_status, socketio=socketio, url="/api/dome/status") socketio.start_background_task(send_status, socketio=socketio, url="/api/telescope/status") socketio.start_background_task(send_status, socketio=socketio, url="/api/camera/status") socketio.start_background_task(send_status, socketio=socketio, url="/api/environment/status", timeout=40) socketio.start_background_task(send_status, socketio=socketio, url="/api/sequencer/run") socketio.start_background_task(send_webcam, socketio=socketio)