def testCase(mode): print("Start UDP client-server test. test mode: %s \n" % str(mode)) tCount, tPass = 0, True # test fail count and test pass flag. if mode == 1: print("Start the UDP Server.") servThread = testThread(None, 0, "server thread") servThread.start() print("Start the UDP Client.") client = udpCom.udpClient(('127.0.0.1', UDP_PORT)) # test case 0 print("0. HearBeat message test:\n----") tPass = True for i in range(3): msg = "Test data %s" % str(i) result = client.sendMsg(msg, resp=True) tPass = tPass and (msg.encode('utf-8') == result) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 1 print("1. Client disconnect test:\n----") tPass = True client.disconnect() client = None try: client.sendMsg('Testdata', resp=True) tPass = False except: tPass = True if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 2 print("2. Server stop test:\n----") servThread.stop() time.sleep(1) # wait 1 second for all the sorcket close. tPass = (servThread.threadName is None) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 3 print("3. Client timeout test:\n----") client = udpCom.udpClient(('127.0.0.1', UDP_PORT)) resp = client.sendMsg('Testdata', resp=True) tPass = (resp is None) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) print(" => All test finished: %s/4" % str(tCount)) client = servThread = None else: print("Add more other exception test here.")
def __init__(self, parent, id, title): """ Init the UI and parameters """ wx.Frame.__init__(self, parent, id, title, size=(800, 340)) self.SetBackgroundColour(wx.Colour(200, 210, 200)) self.SetIcon(wx.Icon(gv.ICO_PATH)) # build the UI. self.SetSizer(self._buildUISizer()) # Set the raspberry pi UDP connector. self.connector = udpCom.udpClient((RSP_IP, UDP_PORT)) # Set the periodic call back self.lastPeriodicTime = {'UI': time.time(), 'Data': time.time()} self.timer = wx.Timer(self) self.updateLock = False self.Bind(wx.EVT_TIMER, self.periodic) self.timer.Start(PERIODIC) # every 500 ms # login to the raspberry PI and fetch the running state. # Fetch the connection state from raspberry PI. self.connectRsp('Login') # Fetch the load state from the raspberry PI. self.connectRsp('Load') # Fetch the generator state from the raspberry PI. self.connectRsp('Gen') # Added the state bar. self.statusbar = self.CreateStatusBar() self.Bind(wx.EVT_CLOSE, self.onClose) self.Refresh(False) print("AppFrame init finished.")
def __init__(self, parent, threadID, name, paramDict): threading.Thread.__init__(self) self.threadName = name self.paramDict = paramDict self.client = udpCom.udpClient((self.paramDict['IPADD'], UDP_PORT)) self.termiate = False self.data = None self.dataIn = None self.now = time.time()
def __init__(self, parent): """ Init the image fetch UDP client and motion detection handler. Init example : cam = camServer(None) """ self.paramDict = self.loadConfig() self.client = udpCom.udpClient((self.paramDict['IPADD'], UDP_PORT)) self.staticBack = None self.termiate = False # program terminate flag self.frameRate = 10
def __init__(self, parent): """ Create a UDP server and feed back the checked file's PATT value when the client connect to it. Init example: checker = pattServer(None) """ self.paramDict = self.loadConfig() # Init the PATT calculator. self.verifier = patt.pattChecker(self.paramDict['BLKNU'], self.paramDict['FMPAT']) # Init the communicate UDP client. self.client = udpCom.udpClient((self.paramDict['IPADD'], UDP_PORT))
def __init__(self, clientIP=None): """ Create a UDP server and feed back the checked file's PATT value when the client connect to it. Init example: checker = pattServer(clientIP='127.0.0.1') """ blockNum = 4 # Init the PATT calculator. self.verifier = patt.pattChecker(blockNum, FM_PATH) # Init the communicate UDP client. if clientIP is None: clientIP = '127.0.0.1' self.client = udpCom.udpClient((clientIP, UDP_PORT))
def __init__(self, camIP): """ Init the image fetch UDP client and motion detection handler. Init example : cam = camServer(camIP='172.27.142.65') """ self.client = udpCom.udpClient((camIP, UDP_PORT)) self.staticBack = None self.diffLvl = 30 # Motion changed level which will be detected.(smaller->sensitive) self.contourIgnRng = ( 400, 10000 ) # contour ingore range. target not in range will be ingnored. self.termiate = False # program terminate flag self.showDiff = False # flag to whether to show the difference frame. self.frameRate = 10
def __init__(self, parent, videoSrc=0): """ Init the image fetch UDP client and motion detection handler. Init example : cam = camServer(None) """ self.paramDict = self.loadConfig() self.client = udpCom.udpClient((self.paramDict['IPADD'], UDP_PORT)) self.termiate = False # program terminate flag # self.staticBack = None # parameter used for # Video capture section: self.videoSrc = videoSrc print("Capture video from src: %s" % str(self.videoSrc)) self.cam = cv2.VideoCapture(self.videoSrc) self.encodeParam = [int(cv2.IMWRITE_JPEG_QUALITY), 90] # image encode parameter # Image resolution setup: self.imageW = self.cam.get(3) if TEST_MD else 1600 self.imageH = self.cam.get(4) if TEST_MD else 900 print("Video resolution is: %s" %str((self.imageW, self.imageH))) if not TEST_MD: self.setResolution(self.imageW, self.imageH) # Data parameters: self.data = None # image data (Byte data). self.now = time.time() # time tag used to calculate the latency.
def __init__(self, parent, id, title): """ Init the UI and parameters """ wx.Frame.__init__(self, parent, id, title, size=(600, 240)) self.SetBackgroundColour(wx.Colour(200, 210, 200)) self.SetIcon(wx.Icon(gv.ICO_PATH)) self.loadCbList = [] self.ledList = [] self.SetSizer(self._buidUISizer()) gv.iGnMgr = gm.pwrGenMgr(self, 0, "Gen mgr") gv.iGnMgr.start() gv.iGnMgr.setLoad([], []) self.client = udpCom.udpClient(('127.0.0.1', UDP_PORT)) self.parmList = [ 50, 22, 50, 50, gv.iGnMgr.getMotorSp(), gv.iGnMgr.getPumpSp(), 0, 1 ] self.setLEDVal(0, self.parmList[0] * 100) self.setLEDVal(1, self.parmList[1] * 10) self.setLEDVal(2, gv.iGnMgr.getMotorSp()) #self.setLEDVal(3, gv.iGnMgr.getPumpSp()) # Set the periodic call back self.lastPeriodicTime = time.time() self.timer = wx.Timer(self) self.updateLock = False self.Bind(wx.EVT_TIMER, self.periodic) self.timer.Start(PERIODIC) # every 500 ms self.statusbar = self.CreateStatusBar() self.statusbar.SetStatusText('COM Msg to Arduino: %s ' % str(self.parmList)) self.Bind(wx.EVT_CLOSE, self.onClose) print("Program init finished.")
def __init__(self, parent): """ Create a client to login to the server and submit the gateway log's data. init example: client = gwWebClient(None) """ self.showConstant() self.gwClient = udpCom.udpClient(SEV_IP) self.latency = 0.0001 self.terminate = False self.lineCounts = [0, 0] print("load the configure file and login.") self.dataDist = None with open(GW_CONFIG, "r") as fh: self.dataDist = json.loads(fh.readlines()[-1].rstrip()) # load the last line of the json. lat, lon = self.dataDist['GPS'].split(',') loginStr = ";".join(('L', self.dataDist['gatewayID'], lat, lon)) print("send the login message: <%s>" %str(loginStr)) resp = self.gwClient.sendMsg(loginStr, resp=True) if resp.decode('utf-8') == 'R;L': print('Login the server as a new gateway.') else: print('Server replay: %s' %str(resp)) latThread = threading.Thread(target=self.checkLatency) # Not work: threading.Thread(target=self.checkLatency()) latThread.daemon = True # here need to use method referring instead of invoking. latThread.start() # https://stackoverflow.com/questions/30701983/new-thread-blocks-main-thread
def stop(self): """ Stop the udp server. Create a endclient to bypass the revFrom() block.""" self.server.serverStop() endClient = udpCom.udpClient(('127.0.0.1', UDP_PORT)) endClient.disconnect() endClient = None
def testCase(mode): print("Start UDP client-server test. test mode: %s \n" % str(mode)) tCount, tPass = 0, True # test fail count and test pass flag. if mode == '0': print("Start the UDP Server.") servThread = testThread(None, 0, "server thread") servThread.start() print("Start the UDP Client.") client = udpCom.udpClient(('127.0.0.1', UDP_PORT)) # test case 0 print("0. HearBeat message test:\n----") tPass = True for i in range(3): msg = "Test data %s" % str(i) result = client.sendMsg(msg, resp=True) tPass = tPass and (msg.encode('utf-8') == result) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 1 print("1. Client disconnect test:\n----") tPass = True client.disconnect() client = None try: client.sendMsg('Testdata', resp=True) tPass = False except: tPass = True if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 2 print("2. Server stop test:\n----") servThread.stop() time.sleep(1) # wait 1 second for all the sorcket close. tPass = (servThread.threadName is None) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) # test case 3 print("3. Client timeout test:\n----") client = udpCom.udpClient(('127.0.0.1', UDP_PORT)) resp = client.sendMsg('Testdata', resp=True) tPass = (resp is None) if tPass: tCount += 1 print("Test passed: %s \n----\n" % str(tPass)) print(" => All test finished: %s/4" % str(tCount)) client = servThread = None elif mode == '1': print(" - Please input the UDP port: ") udpPort = int(str(input())) server = udpCom.udpServer(None, udpPort) server.serverStart(handler=msgHandler) print("Start the UDP echo server licening port [%s]" % udpPort) elif mode == '2': print(" - Please input the IP address: ") ipAddr = str(input()) print(" - Please input the UDP port: ") udpPort = int(str(input())) client = udpCom.udpClient((ipAddr, udpPort)) while True: print(" - Please input the message: ") msg = str(input()) resp = client.sendMsg(msg, resp=True) print(" - Server resp: %s" % str(resp)) else: print("Input %s is not valid, program terminate." % str(uInput))