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 service1(pipe: int, src_ia: int, req: bytearray) -> (bytearray, int): print(dcom.pipe_to_addr(pipe), '0x%x' % src_ia, req) return bytearray(b'jdh99'), 0
def _socket_tx(pipe: int, data: bytearray): ip, port = dcom.pipe_to_addr(pipe) _socket.sendto(data, (ip, port)) lagan.info(config.TAG, "udp send:ip:%s port:%d len:%d", ip, port, len(data)) lagan.print_hex(config.TAG, lagan.LEVEL_DEBUG, data)