wu_xinjun
2022-06-16 14cc8f11a79fabec83f3c5754af45583fb4edcd0
src/microwave/deviceTools.py
@@ -1,4 +1,5 @@
import datetime
import queue
from socketserver import BaseRequestHandler, UDPServer
import time
import os, sys
@@ -7,42 +8,17 @@
import numpy as np
from ..utils import file_fliter, write_ply
from .servers import MicroWaveReceiver
from .servers import MicroWaveReceiver,MicroWaveUDPThread
from .parsers import MicroWaveParser
# # add python path of src to sys.path
# src_path = os.path.join(__file__, *(['..'] * 2))
# src_path = os.path.abspath(src_path)
# sys.path.insert(1, src_path)
# udp_get
class PointCloudUDPRequestHandler(BaseRequestHandler):
    def handle(self):
        packet_threshold = self.server.packet_threshold
        packed_count = self.server.packed_count
        path = self.server.path
        print(time.asctime(),' Got connection from {}, {}'.format(self.client_address, packed_count))
        # Get message and client socket
        msg, sock = self.request
        time_tag = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
        f = open('{}/{}_packet{}.udp'.format(path,time_tag,packed_count), 'wb')
        f.write(msg)
        f.close()
        self.server.packed_count += 1
        self.server.udp_list.append(msg)
        if packed_count >= packet_threshold:
            UDPServer.server_close(self.server)
def udp_get_tool(microwaveHost,packetThreshold,deviceName,saveFolder,subFolder):
    # host = ''
    # host = '192.168.2.65' # use this host to get the udp data from microwave device
    port = 9911
    server =UDPServer((microwaveHost, port), PointCloudUDPRequestHandler)
    udp_queue = queue.Queue()
    udp_get_stop = {"value": False}
    if subFolder is not None:
        path = os.path.join(saveFolder,subFolder)
@@ -52,23 +28,42 @@
    if not os.path.exists(path):
        os.makedirs(path)
    server.path = os.path.abspath(path)
    server.packet_threshold = packetThreshold
    server.packed_count = 0
    server.udp_list = []
    udp_thread = MicroWaveUDPThread(MicrowaveHost=microwaveHost,
                                MicrowavePort=port,
                                udp_queue=udp_queue,
                                udp_get_stop=udp_get_stop,
                                packet_threshold=packetThreshold,
                                timeusage_threshold=None)
    udp_thread.setDaemon(True)
    udp_thread.setName("UDPGet_Thread")
    print(time.asctime(),'Server started on port', port)
    print('....')
    print('ctrl-c to quit server.')
    try:
        server.serve_forever()
        udp_thread.start()
        run = True
        while run:
            packed_count = 0
            while run or not udp_queue.empty():
                value = udp_queue.get()
                while value is not False:
                    time_tag = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
                    with ('{}/{}_packet{}.udp'.format(path,time_tag,packed_count), 'wb') as f:
                        f.write(udp_queue.get())
                        packed_count += 1
                else:
                    run = False
    except KeyboardInterrupt:
        server.server_close()
        udp_get_stop = {"value": True}
        print(time.asctime(),"Server Stopped")
    except:
        server.server_close()
        udp_get_stop = {"value": True}
        print(time.asctime(),"Server Stopped")
    return server.udp_list
# udp2bin
def udp2bin_tool(udp_folder_Or_list):