From d7b529b8b9aec44e8e186e8fd5e84cf181e69853 Mon Sep 17 00:00:00 2001 From: Shekar V Date: Tue, 14 Nov 2023 08:09:08 -0500 Subject: [PATCH] Using send_to_server to directly send messages --- gui/control_main.py | 156 +++++++++++++++------------------- gui/dialog/screen_defaults.py | 4 +- gui/dialog/staff_screen.py | 35 +++----- gui/dialog/user_screen.py | 22 +++-- utils/comm.py | 18 ---- 5 files changed, 92 insertions(+), 143 deletions(-) delete mode 100644 utils/comm.py diff --git a/gui/control_main.py b/gui/control_main.py index 7ed62141..3b9c9dd6 100644 --- a/gui/control_main.py +++ b/gui/control_main.py @@ -52,7 +52,8 @@ from gui.raster import RasterCell, RasterGroup from QPeriodicTable import QPeriodicTable from threads import RaddoseThread, ServerCheckThread, VideoThread -from utils.comm import generate_server_message +import json +from typing import Dict, List, Optional logger = logging.getLogger() @@ -1426,9 +1427,7 @@ def annealButtonCB(self): try: ftime = float(self.annealTime_ledit.text()) if ftime >= 0.1 and ftime <= 5.0: - comm_s = generate_server_message("anneal", [ftime]) - logger.info(comm_s) - self.send_to_server(comm_s) + self.send_to_server("anneal", [ftime]) else: self.popupServerMessage( "Anneal time must be between 0.1 and 5.0 seconds." @@ -2498,9 +2497,7 @@ def protoRadioToggledCB(self, text): pass def beamsizeComboActivatedCB(self, text): - comm_s = generate_server_message("set_beamsize", [text[0:2], text[2:4]]) - logger.info(comm_s) - self.send_to_server(comm_s) + self.send_to_server("set_beamsize", [text[0:2], text[2:4]]) def protoComboActivatedCB(self, text): self.showProtParams() @@ -2605,12 +2602,7 @@ def popImportDialogCB(self): ) self.timerSample.start(SAMPLE_TIMER_DELAY) if fname != "": - logger.info(fname) - comm_s = generate_server_message( - "importSpreadsheet", [fname[0], daq_utils.owner] - ) - logger.info(comm_s) - self.send_to_server(comm_s) + self.send_to_server("importSpreadsheet", [fname[0], daq_utils.owner]) def setUserModeCB(self): self.vidActionDefineCenterRadio.setEnabled(False) @@ -2693,12 +2685,10 @@ def dewarViewToggleCheckCB(self): self.dewarTree.refreshTreePriorityView() def moveOmegaCB(self): - comm_s = generate_server_message( + self.send_to_server( "mvaDescriptor", ["omega", float(self.sampleOmegaMoveLedit.getEntry().text())], ) - logger.info(comm_s) - self.send_to_server(comm_s) def moveEnergyCB(self): energyRequest = float(str(self.energy_ledit.text())) @@ -2706,11 +2696,7 @@ def moveEnergyCB(self): self.popupServerMessage("Energy change must be less than 10 ev") return else: - comm_s = generate_server_message( - "mvaDescriptor", ["energy", float(self.energy_ledit.text())] - ) - logger.info(comm_s) - self.send_to_server(comm_s) + self.send_to_server("mvaDescriptor", ["energy", float(self.energy_ledit.text())]) def setLifetimeCB(self, lifetime): if hasattr(self, "sampleLifetimeReadback_ledit"): @@ -2777,26 +2763,18 @@ def setTransCB(self): except ValueError as e: self.popupServerMessage("Please enter a valid number") return - comm_s = generate_server_message( - "setTrans", ["energy", float(self.transmission_ledit.text())] - ) - logger.info(comm_s) - self.send_to_server(comm_s) + self.send_to_server("setTrans", ["energy", float(self.transmission_ledit.text())]) def setDCStartCB(self): currentPos = float(self.sampleOmegaRBVLedit.getEntry().text()) % 360.0 self.setGuiValues({"osc_start": currentPos}) def moveDetDistCB(self): - comm_s = generate_server_message( - "mvaDescriptor", + self.send_to_server("mvaDescriptor", [ "detectorDist", float(self.detDistMotorEntry.getEntry().text()), - ], - ) - logger.info(comm_s) - self.send_to_server(comm_s) + ]) def omegaTweakNegCB(self): tv = float(self.omegaTweakVal_ledit.text()) @@ -2852,20 +2830,14 @@ def omegaTweakCB(self, tv): self.popupServerMessage("You don't have control") def autoCenterLoopCB(self): - logger.info("auto center loop") - comm_s = generate_server_message("loop_center_xrec") - self.send_to_server(comm_s) + self.send_to_server("loop_center_xrec") def autoRasterLoopCB(self): self.selectedSampleID = self.selectedSampleRequest["sample"] - comm_s = generate_server_message("autoRasterLoop", [self.selectedSampleID]) - self.send_to_server(comm_s) + self.send_to_server("autoRasterLoop", [self.selectedSampleID]) def runRastersCB(self): - comm_s = generate_server_message( - "snakeRaster", [self.selectedSampleRequest["uid"]] - ) - self.send_to_server(comm_s) + self.send_to_server("snakeRaster", [self.selectedSampleRequest["uid"]]) def drawInteractiveRasterCB(self): # any polygon for now, interactive or from xrec for i in range(len(self.polyPointItems)): @@ -2941,8 +2913,7 @@ def center3LoopCB(self): logger.info("3-click center loop") self.threeClickCount = 1 self.click3Button.setStyleSheet("background-color: yellow") - comm_s = generate_server_message("mvaDescriptor", ["omega", 0]) - self.send_to_server(comm_s) + self.send_to_server("mvaDescriptor", ["omega", 0]) def fillPolyRaster( self, rasterReq, waitTime=1 @@ -3119,10 +3090,7 @@ def takeRasterSnapshot(self, rasterReq): reqID=rasterReq["uid"], rasterHeatJpeg=jpegImageFilename, ) - comm_s = generate_server_message( - "ispybLib.insertRasterResult", [str(rasterReq["uid"]), str(visitName)] - ) - self.send_to_server(comm_s) + self.send_to_server("ispybLib.insertRasterResult", [str(rasterReq["uid"]), str(visitName)]) def reFillPolyRaster(self): rasterEvalOption = str(self.rasterEvalComboBox.currentText()) @@ -3221,12 +3189,10 @@ def selectAllCenterCB(self): self.centeringMarksList[i]["graphicsItem"].setSelected(True) def lightUpCB(self): - comm_s = generate_server_message("backlightBrighter") - self.send_to_server(comm_s) + self.send_to_server("backlightBrighter") def lightDimCB(self): - comm_s = generate_server_message("backlightDimmer") - self.send_to_server(comm_s) + self.send_to_server("backlightDimmer") def eraseRastersCB(self): if self.rasterList != []: @@ -3582,22 +3548,21 @@ def pixelSelect(self, event): True ) # because it's easy to forget defineCenter is on if self.zoom4Radio.isChecked(): - comm_s = generate_server_message( + self.send_to_server( "changeImageCenterHighMag", [x_click, y_click, 1] ) elif self.zoom3Radio.isChecked(): - comm_s = generate_server_message( + self.send_to_server( "changeImageCenterHighMag", [x_click, y_click, 0] ) if self.zoom2Radio.isChecked(): - comm_s = generate_server_message( + self.send_to_server( "changeImageCenterLowMag", [x_click, y_click, 1] ) elif self.zoom1Radio.isChecked(): - comm_s = generate_server_message( + self.send_to_server( "changeImageCenterLowMag", [x_click, y_click, 0] ) - self.send_to_server(comm_s) return if self.vidActionRasterDefRadio.isChecked(): self.click_positions.append(event.pos()) @@ -3625,7 +3590,7 @@ def pixelSelect(self, event): if self.threeClickCount > 0: # 3-click centering self.threeClickCount = self.threeClickCount + 1 - comm_s = generate_server_message( + comm_s = self.generate_server_message( "center_on_click", [ correctedC2C_x, @@ -3636,7 +3601,7 @@ def pixelSelect(self, event): ], ) else: - comm_s = generate_server_message( + comm_s = self.generate_server_message( "center_on_click", [ correctedC2C_x, @@ -4206,24 +4171,24 @@ def collectQueueCB(self): if currentRequest == {}: self.addRequestsToAllSelectedCB() logger.info("running queue") - comm_s = generate_server_message("runDCQueue") - self.send_to_server(comm_s) + self.send_to_server("runDCQueue") + def warmupGripperCB(self): - comm_s = generate_server_message("warmupGripper") - self.send_to_server(comm_s) + self.send_to_server("warmupGripper") + def dryGripperCB(self): - comm_s = generate_server_message("dryGripper") - self.send_to_server(comm_s) + self.send_to_server("dryGripper") + def enableTScreenGripperCB(self): - comm_s = generate_server_message("enableDewarTscreen") - self.send_to_server(comm_s) + self.send_to_server("enableDewarTscreen") + def parkGripperCB(self): - comm_s = generate_server_message("parkGripper") - self.send_to_server(comm_s) + self.send_to_server("parkGripper") + def restartServerCB(self): if self.controlEnabled(): @@ -4454,17 +4419,15 @@ def puckToDewarCB(self): def stopRunCB(self): logger.info("stopping collection") - comm_s = generate_server_message("stopDCQueue", [1]) - self.aux_send_to_server(comm_s) + self.aux_send_to_server("stopDCQueue", [1]) def stopQueueCB(self): logger.info("stopping queue") if self.pauseQueueButton.text() == "Continue": - comm_s = generate_server_message("continue_data_collection") - self.aux_send_to_server(comm_s) + self.aux_send_to_server("continue_data_collection") else: - comm_s = generate_server_message("stopDCQueue", [2]) - self.aux_send_to_server(comm_s) + self.aux_send_to_server("stopDCQueue", [2]) + def mountSampleCB(self): if getBlConfig("mountEnabled") == 0: @@ -4479,8 +4442,8 @@ def mountSampleCB(self): else: # No sample ID found, do nothing logger.info("No sample selected, cannot mount") return - comm_s = generate_server_message("mountSample", [self.selectedSampleID]) - self.send_to_server(comm_s) + self.send_to_server("mountSample", [self.selectedSampleID]) + self.zoom2Radio.setChecked(True) self.zoomLevelToggledCB("Zoom2") self.protoComboBox.setCurrentIndex(self.protoComboBox.findText(str("standard"))) @@ -4488,8 +4451,8 @@ def mountSampleCB(self): def unmountSampleCB(self): logger.info("unmount sample") - comm_s = generate_server_message("unmountSample") - self.send_to_server(comm_s) + self.send_to_server("unmountSample") + def refreshCollectionParams(self, selectedSampleRequest, validate_hdf5=True): reqObj = selectedSampleRequest["request_obj"] @@ -4605,14 +4568,14 @@ def refreshCollectionParams(self, selectedSampleRequest, validate_hdf5=True): > 5.0 ): - comm_s = generate_server_message( + self.send_to_server( "mvaDescriptor", [ "omega", selectedSampleRequest["request_obj"]["rasterDef"]["omega"], ], ) - self.send_to_server(comm_s) + if str(reqObj["protocol"]) == "eScan": try: self.escan_steps_ledit.setText(str(reqObj["steps"])) @@ -5202,24 +5165,41 @@ def controlEnabled(self): and self.controlMasterCheckBox.isChecked() ) - def send_to_server(self, s): - if s == "lockControl": + def send_to_server(self, function_name: str, args: "Optional[List]" = None, kwargs: "Optional[Dict]" = None): + if function_name == "lockControl": self.controlMaster_pv.put(0 - self.processID) return - if s == "unlockControl": + if function_name == "unlockControl": self.controlMaster_pv.put(self.processID) return if self.controlEnabled(): time.sleep(0.01) - logger.info("send_to_server: %s" % s) - self.comm_pv.put(s) + message = self.generate_server_message(function_name, args, kwargs) + logger.info(f"send_to_server: {message}") + self.comm_pv.put(message) else: self.popupServerMessage("You don't have control") - def aux_send_to_server(self, s): + def generate_server_message( + self, function_name: str, args: "Optional[List]" = None, kwargs: "Optional[Dict]" = None + ) -> str: + if not args: + args = [] + if not kwargs: + kwargs = {} + return json.dumps( + { + "function": function_name, + "args": args, + "kwargs": kwargs, + } + ) + + def aux_send_to_server(self, function_name: str, args: "Optional[List]" = None, kwargs: "Optional[Dict]" = None): if self.controlEnabled(): time.sleep(0.01) - logger.info("aux_send_to_server: %s" % s) - self.immediate_comm_pv.put(s) + message = self.generate_server_message(function_name, args, kwargs) + logger.info(f"aux_send_to_server: {message}") + self.immediate_comm_pv.put(message) else: self.popupServerMessage("You don't have control") diff --git a/gui/dialog/screen_defaults.py b/gui/dialog/screen_defaults.py index 7dcc743a..6a86e881 100644 --- a/gui/dialog/screen_defaults.py +++ b/gui/dialog/screen_defaults.py @@ -14,7 +14,6 @@ VALID_EXP_TIMES, ) from daq_utils import beamline, getBlConfig, setBlConfig -from utils.comm import generate_server_message if typing.TYPE_CHECKING: from lsdcGui import ControlMain @@ -203,8 +202,7 @@ def reprocessRasterRequestCB(self): try: reqID = self.parent.selectedSampleRequest["uid"] self.parent.drawPolyRaster(db_lib.getRequestByID(reqID)) - comm_s = generate_server_message("reprocessRaster", [reqID]) - self.parent.send_to_server(comm_s) + self.parent.send_to_server("reprocessRaster", [reqID]) except: pass diff --git a/gui/dialog/staff_screen.py b/gui/dialog/staff_screen.py index 07a5f7d5..13ce99d1 100644 --- a/gui/dialog/staff_screen.py +++ b/gui/dialog/staff_screen.py @@ -6,7 +6,6 @@ from config_params import BEAM_CHECK, TOP_VIEW_CHECK, UNMOUNT_COLD_CHECK from daq_utils import getBlConfig, setBlConfig -from utils.comm import generate_server_message if typing.TYPE_CHECKING: from lsdcGui import ControlMain @@ -223,15 +222,13 @@ def getFastDPNodeList(self): return nodeList def setFastDPNodesCB(self): - comm_s = generate_server_message( + self.parent.send_to_server( "fastDPNodes", [ int(self.fastDPNodeEntryList[i].text()) for i in range(self.fastDPNodeCount) ], ) - logger.info(comm_s) - self.parent.send_to_server(comm_s) def lockGuiCB(self): self.parent.send_to_server("lockControl") @@ -240,52 +237,46 @@ def unLockGuiCB(self): self.parent.send_to_server("unlockControl") def setSpotNodesCB(self): - comm_s = generate_server_message( + self.parent.send_to_server( "spotNodes", [int(self.spotNodeEntryList[i].text()) for i in range(self.spotNodeCount)], ) - logger.info(comm_s) - self.parent.send_to_server(comm_s) def unmountColdCB(self): - comm_s = generate_server_message("unmountCold") - self.parent.send_to_server(comm_s) + self.parent.send_to_server("unmountCold") def openPort1CB(self): - comm_s = generate_server_message("openPort", [1]) - self.parent.send_to_server(comm_s) + self.parent.send_to_server("openPort", [1]) def setBeamcenterCB(self): - comm_s = generate_server_message( + self.parent.send_to_server( "set_beamcenter", [self.beamcenterX_ledit.text(), self.beamcenterY_ledit.text()], ) - self.parent.send_to_server(comm_s) - def closePortsCB(self): - self.parent.send_to_server(generate_server_message("closePorts")) + self.parent.send_to_server("closePorts") def clearMountedSampleCB(self): - self.parent.send_to_server(generate_server_message("clearMountedSample")) + self.parent.send_to_server("clearMountedSample") def recoverRobotCB(self): - self.parent.aux_send_to_server(generate_server_message("recoverRobot")) + self.parent.aux_send_to_server("recoverRobot") def rebootEMBL_CB(self): - self.parent.aux_send_to_server(generate_server_message("rebootEMBL")) + self.parent.aux_send_to_server("rebootEMBL") def restartEMBL_CB(self): - self.parent.send_to_server(generate_server_message("restartEMBL")) + self.parent.send_to_server("restartEMBL") def openGripper_CB(self): - self.parent.send_to_server(generate_server_message("openGripper")) + self.parent.send_to_server("openGripper") def closeGripper_CB(self): - self.parent.send_to_server(generate_server_message("closeGripper")) + self.parent.send_to_server("closeGripper") def homePinsCB(self): - self.parent.send_to_server(generate_server_message("homePins")) + self.parent.send_to_server("homePins") def robotOnCheckCB(self, state): if state == QtCore.Qt.Checked: diff --git a/gui/dialog/user_screen.py b/gui/dialog/user_screen.py index f8b9b5cf..7b6137c7 100644 --- a/gui/dialog/user_screen.py +++ b/gui/dialog/user_screen.py @@ -198,24 +198,22 @@ def __init__(self, parent: "ControlMain"): self.setLayout(vBoxColParams1) def setSlit1XCB(self): - comm_s = generate_server_message("setSlit1X", [self.slit1XMotor_ledit.text()]) - self.parent.send_to_server(comm_s) + self.parent.send_to_server("setSlit1X", [self.slit1XMotor_ledit.text()]) def setSlit1YCB(self): - comm_s = generate_server_message("setSlit1Y", [self.slit1YMotor_ledit.text()]) - self.parent.send_to_server(comm_s) + self.parent.send_to_server("setSlit1Y", [self.slit1YMotor_ledit.text()]) def unmountColdCB(self): - self.parent.send_to_server(generate_server_message("unmountCold")) + self.parent.send_to_server("unmountCold") def testRobotCB(self): - self.parent.send_to_server(generate_server_message("testRobot")) + self.parent.send_to_server("testRobot") def recoverRobotCB(self): - self.parent.send_to_server(generate_server_message("recoverRobot")) + self.parent.send_to_server("recoverRobot") def dryGripperCB(self): - self.parent.send_to_server(generate_server_message("dryGripper")) + self.parent.send_to_server("dryGripper") def stopDetCB(self): logger.info("stopping detector") @@ -236,16 +234,16 @@ def rebootZebraIOC_CB(self): self.parent.rebootZebraIOC_pv.put(1) def SEgovCB(self): - self.parent.send_to_server(generate_server_message("setGovState", ["SE"])) + self.parent.send_to_server("setGovState", ["SE"]) def SAgovCB(self): - self.parent.send_to_server(generate_server_message("setGovState", ["SA"])) + self.parent.send_to_server("setGovState", ["SA"]) def DAgovCB(self): - self.parent.send_to_server(generate_server_message("setGovState", ["DA"])) + self.parent.send_to_server("setGovState", ["DA"]) def BLgovCB(self): - self.parent.send_to_server(generate_server_message("setGovState", ["BL"])) + self.parent.send_to_server("setGovState", ["BL"]) def userScreenOKCB(self): self.hide() diff --git a/utils/comm.py b/utils/comm.py deleted file mode 100644 index 0cc41898..00000000 --- a/utils/comm.py +++ /dev/null @@ -1,18 +0,0 @@ -import json -from typing import Dict, List, Optional - - -def generate_server_message( - function_name: str, args: Optional[List] = None, kwargs: Optional[Dict] = None -) -> str: - if not args: - args = [] - if not kwargs: - kwargs = {} - return json.dumps( - { - "function": function_name, - "args": args, - "kwargs": kwargs, - } - )