Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
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
Esempio n. 5
0
def _socket_rx():
    global _socket
    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))
        pipe_receive(dcom.addr_to_pipe(address[0], address[1]), data)
Esempio n. 6
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")
Esempio n. 7
0
def _apply_thread():
    while True:
        # 如果网络通道不开启则无需申请
        if not fpipe.pipe_is_allow_send(fpipe.PIPE_NET):
            time.sleep(1)
            continue

        if fdcom.is_dcom_init and param.parent.ia == utz.IA_INVALID:
            lagan.info(config.TAG, "send apply frame")
            _send_apply_frame()

        if fdcom.is_dcom_init:
            time.sleep(10)
        else:
            time.sleep(1)
Esempio n. 8
0
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
Esempio n. 9
0
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)
Esempio n. 10
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)
Esempio n. 11
0
def info(msg: str, *args, **kwarg):
    """打印info信息"""
    if _filter_level == lagan.LEVEL_OFF or lagan.LEVEL_INFO < _filter_level:
        return
    lagan.info(_TAG, msg, *args, **kwarg)