From 4aa05ba873a8d56fcf34ca2e1e3f603095808cde Mon Sep 17 00:00:00 2001 From: dirac Date: Mon, 8 Aug 2022 12:24:23 -0400 Subject: [PATCH] ask IP at the beginning of the program --- IsegHVController.py | 56 ++++---- IsegLibrary.py | 340 ++++++++++++++++++++++++-------------------- 2 files changed, 218 insertions(+), 178 deletions(-) diff --git a/IsegHVController.py b/IsegHVController.py index aaf0ee9..98284a4 100755 --- a/IsegHVController.py +++ b/IsegHVController.py @@ -12,6 +12,8 @@ host = socket.gethostname() port = 4305 s.bind((host,port)) +IP = input('Mpod IP address to connect : ') + #===================== GUI import PySimpleGUI as sg @@ -19,12 +21,17 @@ import PySimpleGUI as sg header = ["name", "HV [V]", "Current [mA]"] -chList = iseg.GetChList() -hvList = iseg.GetAllHV() # get all V -iList = iseg.GetAllCurrent() # get all current -outVList = iseg.GetAllOutputHV() -outIList = iseg.GetAllLC() -onOffList = iseg.GetAllOnOff() +mpod = iseg.Mpod(IP) + +if mpod.isConnected == False: + exit() + +chList = mpod.GetChList() +hvList = mpod.GetAllHV() # get all V +iList = mpod.GetAllCurrent() # get all current +outVList = mpod.GetAllOutputHV() +outIList = mpod.GetAllLC() +onOffList = mpod.GetAllOnOff() modChList = iseg.SplitChList(chList) @@ -39,7 +46,6 @@ for k in range(0, nMod): layoutTab.append([]) for k in range(0, nMod): - baseI = 0 for kk in range(0, k): baseI += len(modChList[kk]) @@ -56,9 +62,7 @@ for k in range(0, nMod): ]) for j in range(0, len(modChList[k])) : - i = baseI + j - layoutTab[k].append( [ sg.Input(default_text='', size = 8, justification = "left", key=("n%d" % chList[i])), @@ -77,6 +81,10 @@ for k in range(0, nMod): layoutTabGroup[0].append(sg.Tab("Mod-%0d" % k, layoutTab[k])) layout = [ + [ + sg.Text("IP :", size = 27, justification = "right"), + sg.Input(IP, size = 16, justification = "right", readonly = True), + ], [ sg.Text("refresh period [sec] :", size = 27, justification = "right"), sg.Input(updateTime, size = 8, justification = "right", enable_events=True, key=("-Refresh-")) @@ -123,7 +131,7 @@ while True: if event[0:1] == 'c' : ID = event[1:] - iseg.SwitchOnHV(int(ID), int(window[event].get())) + mpod.SwitchOnHV(int(ID), int(window[event].get())) if event == '-Save-' : fileName = values["Save As"] @@ -144,8 +152,8 @@ while True: window[("n%d" % chList[i])].update(row[0]) window[("v%d" % chList[i])].update(row[2]) window[("i%d" % chList[i])].update(row[3]) - iseg.SetHV(chList[i], float(row[2])) - iseg.SetCurrent(chList[i], float(row[3])/1000) + mpod.SetHV(chList[i], float(row[2])) + mpod.SetCurrent(chList[i], float(row[3])/1000) i += 1 if event in ["-VRateCombo-", "-VRateCh-", "-VRate-"]: @@ -153,33 +161,33 @@ while True: ch = window["-VRateCh-"].get() val = window["-VRate-"].get() if item == comboList[0]: - window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) + window["-VRate-"].update("%.3f" % float(mpod.GetHVRiseRate(int(ch)))) if item == comboList[1]: - window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) + window["-VRate-"].update("%.3f" % float(mpod.GetHVFallRate(int(ch)))) if item == comboList[2]: - iseg.SetHVRiseRate(ch, val) - window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) + mpod.SetHVRiseRate(ch, val) + window["-VRate-"].update("%.3f" % float(mpod.GetHVRiseRate(int(ch)))) if item == comboList[3]: - iseg.SetHVFallRate(ch, val) - window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) + mpod.SetHVFallRate(ch, val) + window["-VRate-"].update("%.3f" % float(mpod.GetHVFallRate(int(ch)))) haha = event.find('_Enter') if haha > 0 : ID = event[:haha] ch = int(ID[1:]) if event[0:1] == 'v' : - iseg.SetHV(ch, float(window[ID].get())) - window[ID].update("%.3f" % iseg.GetHV(ch)) + mpod.SetHV(ch, float(window[ID].get())) + window[ID].update("%.3f" % mpod.GetHV(ch)) if event[0:1] == 'i' : - iseg.SetCurrent(ch, float(window[ID].get())/1000.) - window[ID].update("%.3f" % (iseg.GetCurrent(ch)*1000)) + mpod.SetCurrent(ch, float(window[ID].get())/1000.) + window[ID].update("%.3f" % (mpod.GetCurrent(ch)*1000)) if event == "_TIMEOUT_" : #hvList = GetAllHV() # get all V #iList = GetAllCurrent() # get all current - outVList = iseg.GetAllOutputHV() - outIList = iseg.GetAllLC() + outVList = mpod.GetAllOutputHV() + outIList = mpod.GetAllLC() tempFile = open("temp.dat", "w") diff --git a/IsegLibrary.py b/IsegLibrary.py index 3060697..b64a423 100755 --- a/IsegLibrary.py +++ b/IsegLibrary.py @@ -3,162 +3,192 @@ import os import subprocess -#cmd0Str = "-v2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " -cmd0Str = "-v2c -Op .12 -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " - -def SendCmd(option,cmd): - if option == 0 : - cmdStr = "snmpget " + cmd0Str + cmd - elif option == 1: - cmdStr = "snmpset " + cmd0Str + cmd - elif option == 2: - cmdStr = "snmpwalk " + cmd0Str + cmd - else : - cmdStr = "echo option: 0 - get, 1 - set, 2 - walk" - #print(cmdStr) - result = str(subprocess.check_output(cmdStr, shell=True)) - return result.lstrip('b\'').rstrip('\'').rstrip('\n') - -#======== Get settings -def GetHV(ch): - haha = SendCmd(0, "outputVoltage.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetCurrent(ch): - haha = SendCmd(0, "outputCurrent.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetOutputHV(ch): - haha = SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetLI(ch): - haha = SendCmd(0, "outputMeasurementCurrent.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def IsHVOn(ch): - return SendCmd(0, "outputSwitch.u"+str(ch)) - -def GetHVRiseRate(ch): - haha = SendCmd(0, "outputVoltageRiseRate.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-5].strip()) - -def GetHVFallRate(ch): - haha = SendCmd(0, "outputVoltageFallRate.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-5].strip()) - -#======== Get All channel Setting using snmp walk - -def GetChList(): - haha = SendCmd(2, "outputName") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputName.'): - if len(k) > 0 : - aa = k.find("=") - k = k[0:aa].strip().lstrip('u') - kaka.append(int(k)) - return kaka - -def GetAllHV(): - haha = SendCmd(2, "outputVoltage") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputVoltage'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllCurrent(): - haha = SendCmd(2, "outputCurrent") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputCurrent'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllOutputHV(): - haha = SendCmd(2, "outputMeasurementSenseVoltage") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllLC(): - haha = SendCmd(2, "outputMeasurementCurrent") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllOnOff(): - haha = SendCmd(2, "outputSwitch") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputSwitch'): - if len(k) > 0 : - aa = k.find("INTEGER:") - k = k[aa+12:-2].strip('(').strip(')') - kaka.append(int(k)) - return kaka - -#======== Set Settings -def SetHV(ch, val): - try : - int(ch) - float(val) - return SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val)) - except: - print("either ch is not int or val is not float") +class Mpod: + def __init__(self, ip): + self.IP = ip + #cmd0Str = "-v2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " + self.cmd0Str = "-v2c -Op .12 -m +WIENER-CRATE-MIB -c guru %s " % self.IP + self.isConnected = False + print( "testing communication via " + self.IP) + cmdStr = "snmpwalk " + self.cmd0Str + try : + result = str(subprocess.check_output(cmdStr, shell=True)) + print( result ) + self.isConnected = True + except : + print("cannot establish communitation via " + self.IP) -def SetCurrent(ch, val): - try : - int(ch) - float(val) - return SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val)) - except: - print("either ch is not int or val is not float") - -def SwitchOnHV(ch, onOff): - try : - int(ch) - int(onOff) - SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(10)) - return SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff)) - except : - print("either ch or onOff is not int") + def SendCmd(self, option,cmd): + if (self.isConnected == False ) : return 0 + if option == 0 : + cmdStr = "snmpget " + self.cmd0Str + cmd + elif option == 1: + cmdStr = "snmpset " + self.cmd0Str + cmd + elif option == 2: + cmdStr = "snmpwalk " + self.cmd0Str + cmd + else : + cmdStr = "echo option: 0 - get, 1 - set, 2 - walk" + #print(cmdStr) + result = str(subprocess.check_output(cmdStr, shell=True)) + return result.lstrip('b\'').rstrip('\'').rstrip('\n') -def SetHVRiseRate(ch, rate): - try : - int(ch) - int(rate) - return SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate)) - except: - print("either ch is not int or rate is not float") + #======== Get settings + def GetHV(self, ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputVoltage.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + + def GetCurrent(self, ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputCurrent.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + + def GetOutputHV(self,ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + + def GetLI(self, ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputMeasurementCurrent.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + + def IsHVOn(self, ch): + if (self.isConnected == False ) : return 0 + return self.SendCmd(0, "outputSwitch.u"+str(ch)) + + def GetHVRiseRate(self, ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputVoltageRiseRate.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-5].strip()) + + def GetHVFallRate(self, ch): + if (self.isConnected == False ) : return 0 + haha = self.SendCmd(0, "outputVoltageFallRate.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-5].strip()) -def SetHVFallRate(ch, rate): - try : - int(ch) - int(rate) - return SendCmd(1, "outputVoltageFallRate.u" + str(ch) + " F " + str(rate)) - except: - print("either ch is not int or rate is not float") + #======== Get All channel Setting using snmp walk + + def GetChList(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputName") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputName.'): + if len(k) > 0 : + aa = k.find("=") + k = k[0:aa].strip().lstrip('u') + kaka.append(int(k)) + return kaka + + def GetAllHV(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputVoltage") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputVoltage'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + + def GetAllCurrent(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputCurrent") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputCurrent'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + + def GetAllOutputHV(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputMeasurementSenseVoltage") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + + def GetAllLC(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputMeasurementCurrent") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + + def GetAllOnOff(self): + if (self.isConnected == False ) : return [0] + haha = self.SendCmd(2, "outputSwitch") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputSwitch'): + if len(k) > 0 : + aa = k.find("INTEGER:") + k = k[aa+12:-2].strip('(').strip(')') + kaka.append(int(k)) + return kaka + + #======== Set Settings + def SetHV(self, ch, val): + if (self.isConnected == False ) : return 0 + try : + int(ch) + float(val) + return self.SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val)) + except: + print("either ch is not int or val is not float") + + def SetCurrent(self, ch, val): + if (self.isConnected == False ) : return 0 + try : + int(ch) + float(val) + return self.SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val)) + except: + print("either ch is not int or val is not float") + + def SwitchOnHV(self, ch, onOff): + if (self.isConnected == False ) : return 0 + try : + int(ch) + int(onOff) + self.SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(10)) + return self.SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff)) + except : + print("either ch or onOff is not int") + + def SetHVRiseRate(self, ch, rate): + if (self.isConnected == False ) : return 0 + try : + int(ch) + int(rate) + return self.SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate)) + except: + print("either ch is not int or rate is not float") + + def SetHVFallRate(self, ch, rate): + if (self.isConnected == False ) : return 0 + try : + int(ch) + int(rate) + return SendCmd(1, "outputVoltageFallRate.u" + str(ch) + " F " + str(rate)) + except: + print("either ch is not int or rate is not float") #===================== Auxliary function - def SplitChList(chList): sep = list() for i in range(0, len(chList)): @@ -176,8 +206,10 @@ def SplitChList(chList): return newChList #===================== SandBox -#print( GetOutputHV(0) ) -#print( GetLI(0) ) +#mpod = Mpod("128.186.111.101") + +#print( mpod.GetOutputHV(0) ) +#print( mpod.GetLI(0) ) #print( GetHVRiseRate(0) ) #print( GetHVFallRate(0) ) # @@ -188,6 +220,6 @@ def SplitChList(chList): #hvList = GetAllOutputHV() #print(hvList) -#chList = GetChList() +#chList = mpod.GetChList() #print( SplitChList(chList)) #print( len(SplitChList(chList)))