Skip to content

Commit

Permalink
Refresh equipment config on web interface updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brickbots committed Feb 2, 2025
1 parent 863be7b commit 50c1793
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
14 changes: 10 additions & 4 deletions python/PiFinder/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def __init__(self):
"""
load all settings from config file
"""
# Set up session config items
# These are transient
self._session_config_dict = {}
self.load_config()

def load_config(self):
"""
Loads all config from disk useful if another
process has changed config
"""
cwd = Path.cwd()
self.config_file_path = Path(utils.data_dir, "config.json")

Expand All @@ -30,10 +40,6 @@ def __init__(self):
with open(self.default_file_path, "r") as config_file:
self._default_config_dict = json.load(config_file)

# Set up session config items
# These are transient
self._session_config_dict = {}

# Load the equipment config
eq_config = self.get_option("equipment")
if eq_config is None:
Expand Down
17 changes: 15 additions & 2 deletions python/PiFinder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,21 @@ def main(
)
p.start()

# Web server
console.write(" Webserver")
console.update()

server_process = Process(
name="Webserver",
target=server.run_server,
args=(keyboard_queue, gps_queue, shared_state, server_logqueue, verbose),
args=(
keyboard_queue,
ui_queue,
gps_queue,
shared_state,
server_logqueue,
verbose,
),
)
server_process.start()

Expand Down Expand Up @@ -423,7 +434,7 @@ def main(
integrator_process.start()

# Server
console.write(" Server")
console.write(" POS Server")
console.update()
posserver_process = Process(
name="SkySafariServer",
Expand Down Expand Up @@ -517,6 +528,8 @@ def main(
set_brightness(screen_brightness, cfg)
elif ui_command == "push_object":
menu_manager.jump_to_label("recent")
elif ui_command == "reload_config":
cfg.load_config()

# Keyboard
keycode = None
Expand Down
52 changes: 42 additions & 10 deletions python/PiFinder/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def auth_wrapper(*args, **kwargs):


class Server:
def __init__(self, q, gps_queue, shared_state, is_debug=False):
def __init__(
self, keyboard_queue, ui_queue, gps_queue, shared_state, is_debug=False
):
self.version_txt = f"{utils.pifinder_dir}/version.txt"
self.q = q
self.keyboard_queue = keyboard_queue
self.ui_queue = ui_queue
self.gps_queue = gps_queue
self.shared_state = shared_state
self.ki = KeyboardInterface()
Expand Down Expand Up @@ -306,6 +309,7 @@ def set_active_instrument(instrument_id: int):
cfg = config.Config()
cfg.equipment.set_active_telescope(cfg.equipment.telescopes[instrument_id])
cfg.save_equipment()
self.ui_queue.put("reload_config")
return template(
"equipment",
equipment=cfg.equipment,
Expand All @@ -321,6 +325,7 @@ def set_active_eyepiece(eyepiece_id: int):
cfg = config.Config()
cfg.equipment.set_active_eyepiece(cfg.equipment.eyepieces[eyepiece_id])
cfg.save_equipment()
self.ui_queue.put("reload_config")
return template(
"equipment",
equipment=cfg.equipment,
Expand Down Expand Up @@ -409,7 +414,12 @@ def equipment_import():
cfg.equipment.eyepieces.append(new_eyepiece)

cfg.save_equipment()
return template("equipment", equipment=config.Config().equipment)
self.ui_queue.put("reload_config")
return template(
"equipment",
equipment=config.Config().equipment,
success_message="Equipment Imported, restart your PiFinder to use this new data",
)

@app.route("/equipment/edit_eyepiece/<eyepiece_id:int>")
@auth_required
Expand Down Expand Up @@ -447,18 +457,28 @@ def equipment_add_eyepiece(eyepiece_id: int):
cfg.equipment.eyepieces.append(eyepiece)

cfg.save_equipment()
self.ui_queue.put("reload_config")
except Exception as e:
logger.error(f"Error adding eyepiece: {e}")

return template("equipment", equipment=config.Config().equipment)
return template(
"equipment",
equipment=config.Config().equipment,
success_message="Eyepiece added, restart your PiFinder to use",
)

@app.route("/equipment/delete_eyepiece/<eyepiece_id:int>")
@auth_required
def equipment_delete_eyepiece(eyepiece_id: int):
cfg = config.Config()
cfg.equipment.eyepieces.pop(eyepiece_id)
cfg.save_equipment()
return template("equipment", equipment=config.Config().equipment)
self.ui_queue.put("reload_config")
return template(
"equipment",
equipment=config.Config().equipment,
success_message="Eyepiece Deleted, restart your PiFinder to remove from menu",
)

@app.route("/equipment/edit_instrument/<instrument_id:int>")
@auth_required
Expand Down Expand Up @@ -511,17 +531,27 @@ def equipment_add_instrument(instrument_id: int):
cfg.equipment.telescopes.append(instrument)

cfg.save_equipment()
self.ui_queue.put("reload_config")
except Exception as e:
logger.error(f"Error adding instrument: {e}")
return template("equipment", equipment=config.Config().equipment)
return template(
"equipment",
equipment=config.Config().equipment,
success_message="Instrument Added, restart your PiFinder to use",
)

@app.route("/equipment/delete_instrument/<instrument_id:int>")
@auth_required
def equipment_delete_instrument(instrument_id: int):
cfg = config.Config()
cfg.equipment.telescopes.pop(instrument_id)
cfg.save_equipment()
return template("equipment", equipment=config.Config().equipment)
self.ui_queue.put("reload_config")
return template(
"equipment",
equipment=config.Config().equipment,
success_message="Instrument Deleted, restart your PiFinder to remove from menu",
)

@app.route("/observations")
@auth_required
Expand Down Expand Up @@ -672,7 +702,7 @@ def time_lock(time=datetime.now()):
)

def key_callback(self, key):
self.q.put(key)
self.keyboard_queue.put(key)

def update_gps(self):
location = self.shared_state.location()
Expand All @@ -691,6 +721,8 @@ def update_gps(self):
self.altitude = None


def run_server(q, gps_q, shared_state, log_queue, verbose=False):
def run_server(
keyboard_queue, ui_queue, gps_queue, shared_state, log_queue, verbose=False
):
MultiprocLogging.configurer(log_queue)
Server(q, gps_q, shared_state, verbose)
Server(keyboard_queue, ui_queue, gps_queue, shared_state, verbose)

0 comments on commit 50c1793

Please sign in to comment.