def join(message): iplat_print("Join client") device_id = message['device_id'] sensor_id = message['sensor_id'] server_id = device_id + "_" + sensor_id print("Server ID : ", server_id) print("Server info : ", self.server_config[server_id]) self.socket_io.emit('server_info', self.server_config[server_id]) return ""
def run(self, sensor_node): # Data Send & Receive Part # (4) ... while True: recv_data = self.recv() # ################################################################################################# # data format # # 1. Fast mode # - [data] # # 2. Byte(Single Sensor) - SID(4byte) + DATA(1byte) # - [{"sid": sid, "data": data}] # # 3. Byte(Multi Sensor) - SID1(4B) + DATA1(1B) + SID2(4B) + DATA2(1B) + SID3(4B) + DATA3(1B) # - [{"sid": sid, "data": data}, {"sid": sid, "data": data}, {"sid": sid, "data": data}, ...] # # 4. Block Mode - SID + DATA1 + DATA2 + DATA3 + ... + DATAn # - [{"sid": sid, "data": [data1, data2, data3, ..., datan]}] # ################################################################################################# iip = IPlatPacket.decode(packet=recv_data) # For debugging iplat_print(iip) # Send data to df handler self.server.queue_data.put( ENCODE_DATA_FORMAT(status=True, isn=sensor_node, iip=iip)) did = sensor_node.get_did() for sid in sensor_node.get_sids(): key = f"{did}-{sid}" if self.server.sd_handler_manager[key]["command"].empty(): continue self.server.sd_handler_manager[key]["command"].get() self.server.sd_handler_manager[key]["data"].put({ "data": iip.decode(sid), "timestamp": datetime.now().strftime('%Y%m%d%H%M%S%f') })
def connect(): iplat_print("Connect client to cmd server") return ""
def handle(self): # Passive Mode - Control Method : Reset # SN DC # | | # | Req Connection | # | ---------------------> | (1) - Connection Request # | | # | | (2) - Checking Passive / Active Mode (up to v1.4) # | ACK | # | <--------------------- | (3) - Connection Response # | | (Check a did, sid, sps, resolution) # | | 1. This information isn't existed in db # | | send nack packet and raise error # | | 2. else # | | send ack packet # | | and set ip, port and alive in database # | | # | Data(Auto Start) | # | ---------------------> | (4 ...) # | Data | # | ---------------------> | # | ... | # | | # | Data | # | ---------------------> | # Active Mode (timeout - 3s) # SN(3s) DC(3s) # | | # | Req Connection | # | ---------------------> | (1) # | | (2) - Checking Passive / Active Mode (up to v1.4) # | ACK | # | <--------------------- | (3) # | | # Wait | ... | (3) - Go to Liveness Check # | | # | Start Collecting | # | <--------------------- | (4) # | Data | # | ---------------------> | (5) # | ACK | # | <--------------------- | (6) # | | # | ... | # | | # | Data | # | ---------------------> | # | ACK | # | <--------------------- | # | End Collecting | # | <--------------------- | (n) # | ACK | # | ---------------------> | (n + 1) # Active Mode - Liveness Checking # | Is Alive | # | ---------------------> | # | ACK | # | <--------------------- | iplat_print("Success to connect client : " + self.client_address[0]) # Timeout self.request.settimeout(3) sensor_node = IPlatSensorNode() try: # (1) - Request Connection Part recv_data = self.recv() device_info = IPlatPacket.decode(packet=recv_data) # Register sensor node information at sensor node # return : status, error code res, err_code = sensor_node.register(self.server.database, device_info["did"], device_info["sensors"]) if res: # (2) - Send ACK self.send(IPlatPacket.encode(IPC.CONTROL, "ACK", err_code)) for sid in sensor_node.get_sids(): # (2) - Set ip and port in database and create data streaming server key, queue_command, queue_data = self.set_stream_server_config( did=sensor_node.get_did(), sid=sid, ip=self.get_ip(), port=self.server.stream_server_port) if not key in self.server.sd_handler_manager: self.server.sd_handler_manager.setdefault( key, { "data": None, "command": None }) self.server.sd_handler_manager[key][ "data"] = queue_data self.server.sd_handler_manager[key][ "command"] = queue_command # (2) - Set sensor node is alived self.set_alive_sensor_node(did=sensor_node.get_did(), sid=sid, is_alive=True) else: # (2) - Send NACK self.send(IPlatPacket.encode(IPC.CONTROL, "NACK", err_code)) # Raise Error raise Exception("Can not device and sensor certification") self.run(sensor_node) except TimeoutError as e: iplat_print(e) except Exception as e: iplat_print(e) # Finish communication try: self.server.queue_data.put( ENCODE_DATA_FORMAT(status=False, isn=sensor_node, iip=None)) self.clear_sensor_node(sensor_node) except Exception as e: iplat_print(e)
def connect(): iplat_print("Connect client") return "hello"
def serve_forever(self): while True: try: if self.queue_data.empty(): continue data = self.queue_data.get() status, isn, iip = DECODE_DATA_FORMAT(data) did = isn.get_did() # Status is False if not status: for sid in isn.get_sids(): key = self.get_sensing_key(did=did, sid=sid) if key in self.manager: self.insert_et(st=self.manager[key]["st"], did=did, sid=sid, et=datetime.now().strftime( '%Y%m%d%H%M%S%f')[:-4]) del self.manager[key] continue # Status is True # Check the support of only single sensor or multi sensor # - Supported only single sensor if iip.mode == IPC.DATF or iip.mode == IPC.DATK: sid = isn.get_sids()[0] key = self.get_sensing_key(did=did, sid=sid) if not key in self.manager: sps, bps = isn.get_sensor_attr(sid) self.update_manager(key=key, did=did, sid=sid, sps=sps, bps=bps) self.write_data_to_file(self.manager[key]['path'], self.manager[key]['st'] + ".dat", iip.buffer[0][sid], self.manager[key]["idx"]) self.manager[key]["idx"] += 1 # - Supported multi sensor else: for sid in isn.get_sids(): key = self.get_sensing_key(did=did, sid=sid) if not key in self.manager: sps, bps = isn.get_sensor_attr(sid) self.update_manager(key=key, did=did, sid=sid, sps=sps, bps=bps) for buffer in iip.buffer: sid = list(buffer.keys())[0] key = self.get_sensing_key(did=did, sid=sid) if not sid in buffer: # TODO : Error Check continue self.write_data_to_file( self.manager[key]['path'], self.manager[key]['st'] + ".dat", buffer[sid], self.manager[key]["idx"]) self.manager[key]["idx"] += 1 except Exception as e: iplat_print("DB : " + str(e))