def ntp_service2(pipe: int, src_ia: int, req: bytearray) -> (bytearray, int): """校时服务.返回值是应答和错误码.错误码为0表示回调成功,否则是错误码""" ip, port = dcom.pipe_to_addr(pipe) if len(req) == 0: time_zone = 8 elif len(req) == 1: time_zone = req[0] if time_zone >= 0x80: time_zone = -(0x100 - time_zone) else: lagan.warn(TAG, "ip:%s port:%d ia:0x%x ntp failed.len is wrong:%d", ip, port, src_ia, len(req)) return None, ERROR_CODE_RX_FORMAT now = datetime.utcnow() + timedelta(hours=time_zone) t = AckRidGetTime2() t.time_zone = time_zone t.year = now.year t.month = now.month t.day = now.day t.hour = now.hour t.minute = now.minute t.second = now.second t.weekday = now.isoweekday() lagan.info( TAG, 'ip:%s port:%d ntp time:%04d-%02d-%02d %02d:%02d:%02d +%02d00 MST', ip, port, t.year, t.month, t.day, t.hour, t.minute, t.second, t.time_zone) return t.struct_to_bytearray(), 0
def ntp_service1(pipe: int, src_ia: int, req: bytearray) -> (bytearray, int): """校时服务.返回值是应答和错误码.错误码为0表示回调成功,否则是错误码""" ip, port = dcom.pipe_to_addr(pipe) if len(req) == 0: time_zone = 8 elif len(req) == 1: time_zone = req[0] if time_zone >= 0x80: time_zone = -(0x100 - time_zone) else: lagan.warn(TAG, "ip:%s port:%d ia:0x%x ntp failed.len is wrong:%d", ip, port, src_ia, len(req)) return None, ERROR_CODE_RX_FORMAT now = datetime.utcnow() + timedelta(hours=time_zone) if time_zone >= 0: s = '%04d-%02d-%02d %02d:%02d:%02d +%02d00 MST' % ( now.year, now.month, now.day, now.hour, now.minute, now.second, time_zone) else: s = '%04d-%02d-%02d %02d:%02d:%02d -%02d00 MST' % ( now.year, now.month, now.day, now.hour, now.minute, now.second, -time_zone) lagan.info(TAG, 'ip:%s port:%d ntp time:%s', ip, port, s) return tziot.str_to_bytearray(s), 0
def case4(): # 日志模块载入.全局载入一次,参数是分割文件大小,默认是10M lagan.load() # 默认输出界别是info,本行不会打印 lagan.debug("case4", "debug test print") lagan.info("case4", "info test print") lagan.warn("case4", "warn test print") lagan.error("case4", "error test print")
def _deal_rx(data: bytearray, standard_header: utz.StandardHeader, pipe: int): """处理标准层回调函数""" if standard_header.dst_ia != config.local_ia or standard_header.next_head != utz.HEADER_FLP: return body = utz.flp_frame_to_bytes(data) if body is None: lagan.warn(config.TAG, "flp frame to bytes failed!src ia:0x%x", standard_header.src_ia) return dcom.receive(config.PROTOCOL_NUM, pipe, standard_header.src_ia, body)
def _deal_rx(data: bytearray, standard_header: utz.StandardHeader, pipe: int): if standard_header.dst_ia != config.local_ia or standard_header.next_head != utz.HEADER_CMP: return payload = utz.flp_frame_to_bytes(data) if payload is None: lagan.warn(config.TAG, "parse cmp failed.flp frame to bytes failed") return if len(payload) == 0: lagan.warn(config.TAG, "parse cmp failed.payload len is wrong:%d", len(payload)) return knock.call(utz.HEADER_CMP, payload[0], payload[1:])
def pipe_bind_net(ia: int, pwd: str, ip: str, port: int) -> int: """ 绑定网络管道.绑定成功后返回管道号""" global _socket if _socket is not None: lagan.warn(config.TAG, "already bind pipe net") return PIPE_NET config.local_pwd = pwd _socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) _socket.bind((ip, port)) _bind(PIPE_NET, ia, _socket_tx, _socket_is_allow_send) threading.Thread(target=_socket_rx).start() return PIPE_NET
def _socket_rx(): global _socket, _item while True: data, address = _socket.recvfrom(config.FRAME_MAX_LEN) if len(data) == 0: continue lagan.info(config.TAG, 'udp rx:%r len:%d', address, len(data)) lagan.print_hex(config.TAG, lagan.LEVEL_DEBUG, bytearray(data)) if _item.is_rx: lagan.warn(config.TAG, 'udp rx:%r len:%d.deal is too slow!!!', address, len(data)) continue _item.pipe = dcom.addr_to_pipe(address[0], address[1]) _item.data = data _item.is_rx = True
def deal_assign_slave_router(req: bytearray, *args) -> (bytearray, bool): """处理分配从机帧.返回值是应答数据和应答标志.应答标志为false表示不需要应答""" if len(req) == 0: lagan.warn(config.TAG, "deal apply failed.payload len is wrong:%d", len(req)) return None, False j = 0 if req[j] != 0: lagan.warn(config.TAG, "deal apply failed.error code:%d", req[j]) return None, False j += 1 if len(req) != 16: lagan.warn(config.TAG, "deal apply failed.payload len is wrong:%d", len(req)) return None, False param.parent.ia = utz.bytes_to_ia(req[j:j + utz.IA_LEN]) j += utz.IA_LEN ip = socket.inet_ntoa(req[j:j + 4]) j += 4 port = (req[j] << 8) + req[j + 1] j += 2 param.parent.pipe = dcom.addr_to_pipe(ip, port) lagan.info(config.TAG, "apply success.parent ia:0x%x ip:%s port:%d cost:%d", param.parent.ia, ip, port, req[j]) return None, False
def deal_ack_connect_parent_router(req: bytearray, *args) -> (bytearray, bool): global _conn_num """dealAckConnectParentRouter 处理应答连接帧.返回值是应答数据和应答标志.应答标志为false表示不需要应答""" if len(req) == 0: lagan.warn(config.TAG, "deal conn failed.payload len is wrong:%d", len(req)) return None, False j = 0 if req[j] != 0: lagan.warn(config.TAG, "deal conn failed.error code:%d", req[j]) return None, False j += 1 if len(req) != 2: lagan.warn(config.TAG, "deal conn failed.payload len is wrong:%d", len(req)) return None, False _conn_num = 0 param.parent.is_conn = True param.parent.cost = req[j] param.parent.timestamp = int(time.time()) lagan.info(config.TAG, "conn success.parent ia:0x%x cost:%d", param.parent.ia, param.parent.cost) return None, False
async def _conn_thread(): global _conn_num while True: # 如果网络通道不开启则无需连接 if not fpipe.pipe_is_allow_send(fpipe.PIPE_NET): await asyncio.sleep(1) continue if param.parent.ia != utz.IA_INVALID: _conn_num += 1 if _conn_num > _CONN_NUM_MAX: _conn_num = 0 param.parent.ia = utz.IA_INVALID lagan.warn(config.TAG, "conn num is too many!") continue lagan.info(config.TAG, "send conn frame") _send_conn_frame() if param.parent.ia == utz.IA_INVALID: await asyncio.sleep(1) else: await asyncio.sleep(config.CONN_INTERVAL)
def case3(): lagan.load() lagan.debug('case3', 'print:%d,a=%d', 101, 102) lagan.warn('case3', 'print:%d,b=%d', 101, 102)
def warn(msg: str, *args, **kwarg): """打印warn信息""" if _filter_level == lagan.LEVEL_OFF or lagan.LEVEL_WARN < _filter_level: return lagan.warn(_TAG, msg, *args, **kwarg)