Skip to content

Commit e996755

Browse files
authored
Rebuild UI on reconnect and allow startup without server (#33)
Not perfect, in particular would like it better if at least the active plan stayed loaded, if not all of the various plans Closes #32 Closes #31
1 parent 35589bc commit e996755

File tree

2 files changed

+166
-130
lines changed

2 files changed

+166
-130
lines changed

bluesky_cmds/somatic/plan_ui.py

Lines changed: 143 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
import toolz
66
from qtpy import QtWidgets
77
from .comms import RM
8+
from .signals import plans_allowed_updated, devices_allowed_updated
89
from bluesky_hwproxy import zmq_single_request as hwproxy_request
910

1011
import WrightTools as wt
1112
from bluesky_cmds.project import widgets as pw
1213
from bluesky_cmds.project import classes as pc
1314

14-
# TODO: rebuild uis on change to devices_allowed
15-
devices_all_json = RM.devices_allowed()["devices_allowed"]
16-
devices_all = {}
17-
1815

1916
def get_all_components(k, v):
2017
out = {k: v}
@@ -28,17 +25,30 @@ def get_units(device):
2825
key_name = device.replace(".", "_")
2926
else:
3027
base_name = key_name = device
31-
return hwproxy_request("describe", {"device": base_name})[0]["return"].get(key_name, {}).get(
28+
return hwproxy_request("describe", {"device": base_name})[0].get("return", {}).get(key_name, {}).get(
3229
"units", None
3330
)
3431

35-
for k, v in devices_all_json.items():
36-
devices_all.update(get_all_components(k, v))
3732

33+
devices_all = {}
34+
devices_all_json = {}
35+
devices_movable = []
36+
devices_not_movable = []
37+
devices_with_deps = []
38+
39+
def update_devices():
40+
global devices_all, devices_all_json, devices_movable, devices_not_movable, devices_with_deps
41+
devices_all_json = RM.devices_allowed()["devices_allowed"]
42+
devices_all = {}
43+
44+
for k, v in devices_all_json.items():
45+
devices_all.update(get_all_components(k, v))
46+
47+
devices_movable = list(filter(lambda x: devices_all[x]["is_movable"], devices_all))
48+
devices_not_movable = list(filter(lambda x: not devices_all[x]["is_movable"], devices_all))
49+
devices_with_deps = list(filter(lambda x: "components" in devices_all[x], devices_all))
50+
update_plan_ui()
3851

39-
devices_movable = list(filter(lambda x: devices_all[x]["is_movable"], devices_all))
40-
devices_not_movable = list(filter(lambda x: not devices_all[x]["is_movable"], devices_all))
41-
devices_with_deps = list(filter(lambda x: "components" in devices_all[x], devices_all))
4252

4353

4454
class PlanUI:
@@ -831,145 +841,151 @@ def on_method_selected(self):
831841

832842

833843
plan_ui_lookup = defaultdict(PlanUI)
834-
plan_ui_lookup["sleep"] = PlanUI(
835-
[
836-
FloatWidget("time", "time", 1.0),
837-
]
838-
)
839-
plan_ui_lookup["mv"] = PlanUI(
840-
[
841-
MvArgsWidget(),
842-
]
843-
)
844-
plan_ui_lookup["grid_scan_wp"] = PlanUI(
845-
[
846-
MetadataWidget(),
847-
DeviceListWidget(),
848-
GridscanArgsWidget(),
849-
ConstantWidget(),
850-
]
851-
)
852-
plan_ui_lookup["rel_grid_scan_wp"] = PlanUI(
853-
[
854-
MetadataWidget(),
855-
DeviceListWidget(),
856-
GridscanArgsWidget(),
857-
ConstantWidget(),
858-
]
859-
)
860-
plan_ui_lookup["scan_wp"] = PlanUI(
861-
[
862-
MetadataWidget(),
863-
DeviceListWidget(),
864-
ScanArgsWidget(),
865-
IntWidget("Npts", "num", 11),
866-
ConstantWidget(),
867-
]
868-
)
869-
plan_ui_lookup["rel_scan_wp"] = PlanUI(
870-
[
871-
MetadataWidget(),
872-
DeviceListWidget(),
873-
ScanArgsWidget(),
874-
IntWidget("Npts", "num", 11),
875-
ConstantWidget(),
876-
]
877-
)
878-
plan_ui_lookup["list_scan_wp"] = PlanUI(
879-
[
880-
MetadataWidget(),
881-
DeviceListWidget(),
882-
ListscanArgsWidget(),
883-
ConstantWidget(),
884-
]
885-
)
886-
plan_ui_lookup["rel_list_scan_wp"] = PlanUI(
887-
[
888-
MetadataWidget(),
889-
DeviceListWidget(),
890-
ListscanArgsWidget(),
891-
ConstantWidget(),
892-
]
893-
)
894-
plan_ui_lookup["list_grid_scan_wp"] = PlanUI(
895-
[
896-
MetadataWidget(),
897-
DeviceListWidget(),
898-
ListscanArgsWidget(),
899-
ConstantWidget(),
900-
]
901-
)
902-
plan_ui_lookup["rel_list_grid_scan_wp"] = PlanUI(
903-
[
904-
MetadataWidget(),
905-
DeviceListWidget(),
906-
ListscanArgsWidget(),
907-
ConstantWidget(),
908-
]
909-
)
910-
plan_ui_lookup["count"] = PlanUI(
911-
[
912-
MetadataWidget(),
913-
DeviceListWidget(),
914-
IntWidget("Npts", "num", 1),
915-
FloatWidget("Delay", "delay", 0),
916-
]
917-
)
918-
919-
if devices_with_deps:
920-
plan_ui_lookup["run_tune_test"] = PlanUI(
844+
845+
def update_plan_ui():
846+
plan_ui_lookup["sleep"] = PlanUI(
847+
[
848+
FloatWidget("time", "time", 1.0),
849+
]
850+
)
851+
plan_ui_lookup["mv"] = PlanUI(
852+
[
853+
MvArgsWidget(),
854+
]
855+
)
856+
plan_ui_lookup["grid_scan_wp"] = PlanUI(
857+
[
858+
MetadataWidget(),
859+
DeviceListWidget(),
860+
GridscanArgsWidget(),
861+
ConstantWidget(),
862+
]
863+
)
864+
plan_ui_lookup["rel_grid_scan_wp"] = PlanUI(
865+
[
866+
MetadataWidget(),
867+
DeviceListWidget(),
868+
GridscanArgsWidget(),
869+
ConstantWidget(),
870+
]
871+
)
872+
plan_ui_lookup["scan_wp"] = PlanUI(
873+
[
874+
MetadataWidget(),
875+
DeviceListWidget(),
876+
ScanArgsWidget(),
877+
IntWidget("Npts", "num", 11),
878+
ConstantWidget(),
879+
]
880+
)
881+
plan_ui_lookup["rel_scan_wp"] = PlanUI(
921882
[
922883
MetadataWidget(),
923884
DeviceListWidget(),
924-
OpaSelectorWidget(),
925-
SpectrometerWidget(include_center=False),
885+
ScanArgsWidget(),
886+
IntWidget("Npts", "num", 11),
887+
ConstantWidget(),
926888
]
927889
)
928-
opa=OpaSelectorWidget()
929-
plan_ui_lookup["run_setpoint"] = PlanUI(
890+
plan_ui_lookup["list_scan_wp"] = PlanUI(
930891
[
931892
MetadataWidget(),
932893
DeviceListWidget(),
933-
opa,
934-
OpaMotorSelectorWidget(opa_selector=opa),
935-
FloatWidget("Width", "width", 1),
936-
IntWidget("Npts", "npts", 11),
937-
SpectrometerWidget(include_center=False),
894+
ListscanArgsWidget(),
895+
ConstantWidget(),
938896
]
939897
)
940-
opa=OpaSelectorWidget()
941-
plan_ui_lookup["run_intensity"] = PlanUI(
898+
plan_ui_lookup["rel_list_scan_wp"] = PlanUI(
942899
[
943900
MetadataWidget(),
944901
DeviceListWidget(),
945-
opa,
946-
OpaMotorSelectorWidget(opa_selector=opa),
947-
FloatWidget("Width", "width", 1),
948-
IntWidget("Npts", "npts", 11),
949-
SpectrometerWidget(include_center=False),
902+
ListscanArgsWidget(),
903+
ConstantWidget(),
950904
]
951905
)
952-
opa=OpaSelectorWidget()
953-
plan_ui_lookup["run_holistic"] = PlanUI(
906+
plan_ui_lookup["list_grid_scan_wp"] = PlanUI(
954907
[
955908
MetadataWidget(),
956909
DeviceListWidget(),
957-
opa,
958-
OpaMotorSelectorWidget(opa_selector=opa),
959-
OpaMotorSelectorWidget(opa_selector=opa),
960-
FloatWidget("Width", "width", 1),
961-
IntWidget("Npts", "npts", 11),
962-
SpectrometerWidget(include_center=False),
910+
ListscanArgsWidget(),
911+
ConstantWidget(),
963912
]
964913
)
965-
opa=OpaSelectorWidget()
966-
plan_ui_lookup["motortune"] = PlanUI(
914+
plan_ui_lookup["rel_list_grid_scan_wp"] = PlanUI(
967915
[
968916
MetadataWidget(),
969917
DeviceListWidget(),
970-
opa,
971-
BoolWidget("Use Tune Points", "use_tune_points"),
972-
OpaMotorFullWidget(opa_selector=opa),
973-
SpectrometerWidget(),
918+
ListscanArgsWidget(),
919+
ConstantWidget(),
974920
]
975921
)
922+
plan_ui_lookup["count"] = PlanUI(
923+
[
924+
MetadataWidget(),
925+
DeviceListWidget(),
926+
IntWidget("Npts", "num", 1),
927+
FloatWidget("Delay", "delay", 0),
928+
]
929+
)
930+
931+
if devices_with_deps:
932+
plan_ui_lookup["run_tune_test"] = PlanUI(
933+
[
934+
MetadataWidget(),
935+
DeviceListWidget(),
936+
OpaSelectorWidget(),
937+
SpectrometerWidget(include_center=False),
938+
]
939+
)
940+
opa=OpaSelectorWidget()
941+
plan_ui_lookup["run_setpoint"] = PlanUI(
942+
[
943+
MetadataWidget(),
944+
DeviceListWidget(),
945+
opa,
946+
OpaMotorSelectorWidget(opa_selector=opa),
947+
FloatWidget("Width", "width", 1),
948+
IntWidget("Npts", "npts", 11),
949+
SpectrometerWidget(include_center=False),
950+
]
951+
)
952+
opa=OpaSelectorWidget()
953+
plan_ui_lookup["run_intensity"] = PlanUI(
954+
[
955+
MetadataWidget(),
956+
DeviceListWidget(),
957+
opa,
958+
OpaMotorSelectorWidget(opa_selector=opa),
959+
FloatWidget("Width", "width", 1),
960+
IntWidget("Npts", "npts", 11),
961+
SpectrometerWidget(include_center=False),
962+
]
963+
)
964+
opa=OpaSelectorWidget()
965+
plan_ui_lookup["run_holistic"] = PlanUI(
966+
[
967+
MetadataWidget(),
968+
DeviceListWidget(),
969+
opa,
970+
OpaMotorSelectorWidget(opa_selector=opa),
971+
OpaMotorSelectorWidget(opa_selector=opa),
972+
FloatWidget("Width", "width", 1),
973+
IntWidget("Npts", "npts", 11),
974+
SpectrometerWidget(include_center=False),
975+
]
976+
)
977+
opa=OpaSelectorWidget()
978+
plan_ui_lookup["motortune"] = PlanUI(
979+
[
980+
MetadataWidget(),
981+
DeviceListWidget(),
982+
opa,
983+
BoolWidget("Use Tune Points", "use_tune_points"),
984+
OpaMotorFullWidget(opa_selector=opa),
985+
SpectrometerWidget(),
986+
]
987+
)
988+
989+
# TODO: Reload current UI state
990+
plans_allowed_updated.connect(update_plan_ui)
991+
devices_allowed_updated.connect(update_devices)

bluesky_cmds/somatic/queue.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(self, parent_widget, message_widget):
4646
self.update_ui()
4747
somatic.signals.queue_updated.connect(self.update_queue)
4848
somatic.signals.history_updated.connect(self.update_history)
49+
somatic.signals.plans_allowed_updated.connect(self.rebuild_plan_ui)
50+
somatic.signals.devices_allowed_updated.connect(self.rebuild_plan_ui)
4951

5052
def add_button_to_table(self, i, j, text, color):
5153
button = pw.SetButton(text, color=color)
@@ -217,14 +219,16 @@ def show_preset_dialog(self, item):
217219
self.update_presets()
218220

219221

220-
221222
def create_plan_frame(self):
222223
frame = QtWidgets.QWidget()
223224
frame.setLayout(QtWidgets.QVBoxLayout())
224225
layout = frame.layout()
225226
layout.setContentsMargins(0, 0, 0, 0)
226227
input_table = pw.InputTable()
227-
allowed_plans = RM.plans_allowed()
228+
try:
229+
allowed_plans = RM.plans_allowed()
230+
except:
231+
allowed_plans = {"plans_allowed": {"count": None}}
228232
allowed_values = allowed_plans["plans_allowed"].keys()
229233
self.plan_combo = pc.Combo(allowed_values=allowed_values)
230234
self.plan_combo.updated.connect(self.on_plan_selected)
@@ -239,13 +243,29 @@ def create_plan_frame(self):
239243
layout.addWidget(append_button)
240244
return frame
241245

242-
def on_append_to_queue(self):
246+
def rebuild_plan_ui(self):
247+
for frame in self.type_frames.values():
248+
self.settings_layout.removeWidget(frame)
249+
frame.hide()
250+
frame.close()
251+
self.type_frames["plan"] = self.create_plan_frame()
252+
for frame in self.type_frames.values():
253+
self.settings_layout.insertWidget(8, frame)
254+
frame.hide()
255+
self.on_load_item(self.get_plan().to_dict())
256+
self.update_type()
257+
258+
def get_plan(self):
243259
plan_name = self.plan_combo.read()
244260
widget = self.plan_widgets[plan_name]
245261
kwargs = widget.kwargs
246262
meta = kwargs.pop("md", {})
247263
plan = BPlan(plan_name, *widget.args, **kwargs)
248264
plan.meta = meta
265+
return plan
266+
267+
def on_append_to_queue(self):
268+
plan = self.get_plan()
249269
RM.item_add(plan)
250270

251271
def on_queue_start_clicked(self):

0 commit comments

Comments
 (0)