def initVar(self):
     self.count = 0
     self.image_image = np.zeros((800, 800, 3), np.uint8)
     self.image_map = np.zeros((800, 800, 3), np.uint8)
     self.image_info = np.zeros((800, 800, 3), np.uint8)
     self.keep = np.zeros((800, 800, 3), np.uint8)
     # self.image = np.zeros((800,800,3),np.uint8)
     self.img_fireman = []
     self.offset_x = 1
     self.offset_y = 1
     self.image_image_flag = False
     self.image_map_flag = True
     self.image_info_flag = False
     self.ok_flag = False
     self.start_point = (0, 0)  ##### for draw rectangle
     self.end_point = (0, 0)
     self.release_mouse = False
     self.choose_flag = False
     self.remove_flag = False
     self.choose_fireman = -1
     self.middle_x = 1170  ##### middle of map image
     self.middle_y = 700
     self.keep_fire = []
     #self.host = '172.20.10.2'
     self.host = '192.168.43.9'
     #self.host = '127.0.0.1'
     self.port = 8888
     self.time_press = 0
     self.info_flag = 0
     self.client_list = [client(0), client(1), client(2), client(3)]
     self.connection_num = np.zeros(4)
     self.subplot_count = [0, 1, 2, 3]
     self.client_dict = {"client": 1}
     self.refresh_map = False
     self.refresh_img = False
     self.click_to_cancel = False
     self.name_space_height = 50
     self.resize_height = 480 + 200
     self.resize_weight = 640 + 600
     self.height = 480
     self.weight = 640
     self.click_client = 0  ##### the client you click in window
     self.encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
     self.hot_mask = []
     self.explosion_mask = []
     self.map_width = 1174
     self.map_height = 705
     self.max_x = self.map_width * 2
     self.max_y = self.map_height * 2
     #---------------------------------------------------------#
     self.inti_flag = -1
     self.init_time = 0
     self.time_to_come_out = 2880
Exemple #2
0
    def accept_wrapper(self, sock):
        conn, addr = sock.accept()  # Should be ready to read
        print('accepted connection from', addr)

        conn.setblocking(False)
        data = types.SimpleNamespace(addr=addr, inb=b'', outb=b'')
        events = selectors.EVENT_READ | selectors.EVENT_WRITE
        self.sel.register(conn, events, data=data)
        ###### create an client object an put into dictionary with it's address ######
        min_num = min(self.subplot_count)
        ###### create an white image with client name ######
        self.client_list[min_num] = client(min_num)
        self.client_dict[str(addr[1])] = min_num
        ###### number remove from list subplot_count ######
        self.subplot_count.remove(min_num)

        #--------------------------------------------------------------#
        i = 0
        while (i < 4):
            if (self.connection_num[i] == 0):
                self.client_list[i].set_info(i, addr)
                self.connection_num[i] = 1
                inti_flag = i
                self.client_list[i].time_in = time.time()
                break
            i = i + 1
            # add new connection
            # 創造一個新的Object給Device
        #--------------------------------------------------------------------#

        print("Client: ")
        print("\tnum: ", self.client_list[i].id_num)
        print("\tip_addr: ", self.client_list[i].ip_addr)
Exemple #3
0
    def service_connection(self, key, mask):
        sock = key.fileobj
        data = key.data
        if mask & selectors.EVENT_READ:
            recv_data = None
            client_host = self.client_dict[str(data.addr[1])]
            if (self.client_list[client_host].first_time_recv()):
                print("Getting Name...")
                recv_data = sock.recv(16)
                name = recv_data.decode()
                #name = (str)(client_list[client_host].get_num()) + "." + name
                self.client_list[client_host].set_name(name)
                ###### Default : white background black font ######
                self.set_namespace_color(client_host, (255, 255, 255),
                                         (0, 0, 0))
                print("The Name is: ", name)
                self.init_time = time.time()
            else:
                #print("get_package_size: ",self.client_list[client_host].get_package_size() )
                if (self.client_list[client_host].get_package_size() <= 0):
                    try:
                        ###### recv the image size ######
                        recv_data = sock.recv(16)
                        recv_data_msg = recv_data.decode().strip()
                        #print("msg = ", recv_data_msg)
                        if ("FLIR" in recv_data_msg):
                            #print("flir image size msg")
                            #print("IR-FLIR=",time.time() - self.client_list[client_host].t)
                            self.client_list[client_host].set_package(
                                int(recv_data_msg[4:len(recv_data_msg)]), 2)
                        elif ("IR" in recv_data_msg):
                            #print("ir image size msg")
                            self.client_list[client_host].t = time.time()
                            self.client_list[client_host].set_package(
                                int(recv_data_msg[2:len(recv_data_msg)]), 1)
                            #print("recv IR size = ",time.time() - self.client_list[client_host].t)
                        elif ("TH70" in recv_data_msg):
                            #print("TH70 msg")
                            self.client_list[client_host].set_threshold(
                                1, float(recv_data_msg[4:len(recv_data_msg)]))
                        elif ("TH100" in recv_data_msg):
                            #print("TH100 msg")
                            self.client_list[client_host].set_threshold(
                                2, float(recv_data_msg[5:len(recv_data_msg)]))
                        elif (len(recv_data_msg) == 0):
                            pass
                        else:
                            #------------------------------------------------------------------#
                            for i in self.client_list:
                                if (i.ip_addr == data.addr):
                                    i.time_pass = time.time() - self.init_time
                                    #print(i.time_pass)
                                    if ("HELP2" in recv_data_msg):
                                        #print("HELP2")
                                        self.helpConditionExec(
                                            "HELP2", i.id_num)
                                        self.client_list[
                                            client_host].set_sos_flag(True)
                                    elif ("HELP" in recv_data_msg):
                                        #print("HELP")
                                        self.helpConditionExec(
                                            "HELP", i.id_num)
                                    elif ("num" in recv_data_msg):
                                        i.fire_num = recv_data_msg[
                                            3:len(recv_data_msg)]
                                        #print(i.fire_num)
                                    elif ("DRAW" in recv_data_msg):
                                        #print("id: ",i.id_num)
                                        #print(recv_data_msg)
                                        self.drawNewSpot(
                                            recv_data_msg[4:len(recv_data_msg
                                                                )], i.id_num)
                                        if (self.client_list[client_host].
                                                sos_flag):
                                            self.set_namespace_color(
                                                client_host, (255, 255, 255),
                                                (0, 0, 0))
                                            self.client_list[
                                                client_host].set_sos_flag(
                                                    False)
                                    else:
                                        break
                                # Device 傳輸資料時, call 對應function
                            #--------------------------------------------------------------------#

                    except Exception as e:
                        print("error in get msg: ", e.args)
                        #pass

                else:
                    ###### recv the img ######
                    #print("image msg")
                    #try:
                    t1 = time.time()
                    recv_data = sock.recv(
                        self.client_list[client_host].get_package_size())
                    #print("recv img = ",time.time() - t1)
                    ###### concatenate recv msg to image ######
                    #print(type(recv_data))
                    self.client_list[client_host].combine_recv_img(recv_data)
                    self.client_list[client_host].decrease_package_size(
                        len(recv_data))
                    if (self.client_list[client_host].get_package_size() <= 0):
                        ###### image recv complete ######
                        t = time.time()
                        send_flag = self.client_list[client_host].decode_img()
                        #print("decode_img time = ",time.time() - t)
                        if (send_flag):
                            self.refresh_img = True
                            send_flag = False
                            #        try:
                            combine = self.client_list[
                                client_host].read_combine_img()
                            _, encode = cv2.imencode('.jpg', combine,
                                                     self.encode_param)
                            data_combine = np.array(encode)
                            stringData = data_combine.tostring()
                            sock.send(str(len(stringData)).ljust(16).encode())
                            sock.send(stringData)
                            #print("time = ",time.time() - self.client_list[client_host].t)
                            #           except Exception as e:
                            #               print("error in send image to client : ",e.args)
                            ###### decide which background color to brush ######
                            brush_background_ornot = self.client_list[
                                client_host].brush_namespace_background()
                            if (brush_background_ornot == 1):
                                ###### Red background with white font ######
                                self.set_namespace_color(
                                    client_host, (0, 0, 255), (255, 255, 255))
                            elif (brush_background_ornot == 2):
                                ###### White background with black font ######
                                self.set_namespace_color(
                                    client_host, (255, 255, 255), (0, 0, 0))
                            self.client_list[client_host].set_package(-1, 0)
                # except Exception as e:
                #    print ("error in get image msg: ",e.args)
                #   self.client_list[client_host].except_for_img()

            if not recv_data:
                print('closing connection to', data.addr)
                #-------------------------------------------------------------------#
                print(str(data.addr[0]))
                print(self.client_list[0].ip_addr)
                for i in self.client_list:
                    if (i.ip_addr == data.addr):
                        self.connection_num[i.id_num] = 0
                        self.client_list[i.id_num] = client(i.id_num)
                self.image_map = self.keep_fire.copy()
                self.refresh_map = True
                # Close Connection 的時候取消 Object
                #--------------------------------------------------------------------#
                self.client_list[self.client_dict[str(
                    data.addr[1])]].set_visible(False)
                self.refresh_img = True
                self.subplot_count.append(self.client_dict[str(data.addr[1])])
                del self.client_dict[str(data.addr[1])]
                self.sel.unregister(sock)
                sock.close()