Beispiel #1
0
    def etc_toll(rsu_client: RsuSocket):
        # TimingJob.start_scheduler(rsu_client)
        while True:
            time.sleep(random.choice(range(1, 4)))
            now = datetime.now()
            # 查询天线的计费状态,charge=1开启计费,charge=0关闭计费
            rsu_charge = DBRsuCharge.query_rsu_charge()

            # 有接收到数据,表明天线还在工作,更新心跳时间
            rsu_client.rsu_heartbeat_time = datetime.now()
            DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库

            # 查询数据库订单
            _, db_session = create_db_session(
                sqlite_dir=CommonConf.SQLITE_DIR,
                sqlite_database='etc_deduct.sqlite')
            query_item: ETCRequestInfoOrm = db_session.query(
                ETCRequestInfoOrm).filter(
                    and_(
                        ETCRequestInfoOrm.lane_num == rsu_client.lane_num,
                        ETCRequestInfoOrm.create_time >
                        (datetime.now() - timedelta(seconds=10)),
                        ETCRequestInfoOrm.flag == 0)).first()
            # TODO  收到etc扣费请求,但是车上没有obu
            logger.info('当前时间:{}'.format(datetime.now()))
            # 找到订单开始扣费
            if query_item:
                logger.info('开始扣费。。。。。。')
                logger.info('{}, {}'.format(query_item.create_time,
                                            query_item.flag))
                etc_result = EtcToll.toll(query_item, rsu_client)

                etc_result['flag'] = False if random.randint(0,
                                                             3) == 0 else True
                query_item.flag = 1

                if etc_result['flag']:
                    logger.info(
                        '...................扣费成功........................')
                    query_item.deduct_status = EtcDeductStatus.SUCCESS
                else:
                    logger.info(
                        '...................扣费失败........................')
                    query_item.deduct_status = EtcDeductStatus.FAIL
                # 数据修改好后提交
                try:
                    db_session.commit()
                except:
                    db_session.rollback()
                    logger.error(traceback.format_exc())

            else:
                # 没有查询到订单,pass
                pass
            db_session.close()
Beispiel #2
0
def single_process_etc_toll(etc_conf):
    # 根据车道选择天线
    lane_num = etc_conf['lane_num']
    park_code = etc_conf['park_code']
    sn = etc_conf['sn']
    status = 1
    # 添加天线的lane_num, park_code, heartbeat_latest,当前进程号pid,天线状态到etc_deduct.sqlite的表rsu_info中
    DBOPeration.rsu_info_to_db(lane_num, park_code, sn, os.getpid(), status)
    # 创建天线对象
    rsu_socket = RsuSocket(lane_num)
    # 进入到扣费监听状态
    EtcToll.etc_toll(rsu_socket)
Beispiel #3
0
def run_etc_toll():
    logger.info('。。。。。。。。。。。。。。。。。。。启动天线。。。。。。。。。。。。。。。。。。。。。。。。')
    # 先清空etc_deduct.sqlite的表rsu_info
    clear_table_rsu_info()
    # 根据车道选择天线
    rsu0_config = CommonConf.ETC_CONF_DICT['etc'][0]
    lane_num = rsu0_config['lane_num']
    park_code = rsu0_config['park_code']
    sn = rsu0_config['sn']
    # 添加天线的lane_num, park_code, heartbeat_latest到etc_deduct.sqlite的表rsu_info中
    DBOPeration.rsu_info_to_db(lane_num, park_code, sn)
    # 创建天线对象
    rsu_socket = RsuSocket(lane_num)
    # 进入到扣费监听状态
    EtcToll.etc_toll(rsu_socket)
Beispiel #4
0
def single_process_etc_toll(etc_conf):
    # 根据车道选择天线
    # rsu0_config = etc_conf[0]
    lane_num = etc_conf['lane_num']
    park_code = etc_conf['park_code']
    sn = etc_conf['sn']
    status = 1
    # 添加天线的lane_num, park_code, heartbeat_latest,当前进程号pid,天线状态到etc_deduct.sqlite的表rsu_info中
    DBOPeration.rsu_info_to_db(lane_num, park_code, sn, os.getpid(), status)
    # 进入到扣费监听状态
    try:
        # 创建天线对象
        rsu_socket = RsuSocket(lane_num)
        EtcToll.etc_toll(rsu_socket)
    except:
        logger.error(traceback.format_exc())
Beispiel #5
0
def etc_fee_deduction(body: OBUModel):
    """
    etc扣费
    :param body:
    :return:
    """

    body.recv_time = time.time()
    try:
        logger.info(
            '=====================lane_num: {}  接收到扣费请求====================='.
            format(body.lane_num))
        DBOPeration.etc_request_info_to_db(body)
        result = dict(flag=True, errorCode='', errorMessage='', data=None)

    except:
        logger.error(traceback.format_exc())
        result = dict(flag=False,
                      errorCode='01',
                      errorMessage='etc扣费失败',
                      data=None)
    return result
Beispiel #6
0
    def etc_toll(rsu_client: RsuSocket):
        cache_time = datetime.now()
        while True:
            now = datetime.now()
            # 查询天线的计费状态,charge=1开启计费,charge=0关闭计费
            rsu_charge = DBRsuCharge.query_rsu_charge()
            if rsu_charge == 0:
                rsu_client.rsu_heartbeat_time = now  # 更新心跳
                DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                time.sleep(30)
                continue

            # socket监听,接受数据
            try:
                msg_str = rsu_client.recv_msg_max_wait_time()
            except:
                err_msg = traceback.format_exc()
                logger.error(err_msg)
                time.sleep(10)
                DBOPeration.update_rsu_pid_status(rsu_client, 0)
                rsu_client.close_socket()
                rsu_client.init_rsu()
                continue
            # 有接收到数据,表明天线还在工作,更新心跳时间
            rsu_client.rsu_heartbeat_time = datetime.now()
            if CommonConf.ETC_MAKER == 'soulin':
                if msg_str[6:16] == 'b2ffffffff':  # 心跳指令
                    logger.info('lane_num:{}  心跳指令:{}, 天线时间:{}, 当前时间:{}'.format(rsu_client.lane_num, msg_str,
                                                                                rsu_client.rsu_heartbeat_time,
                                                                                datetime.now()))
                    DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                    continue
                # # 检测到obu, 检测到obu时,会狂发b2指令,频繁的更新数据库,所以此种情况下不要更新天线心跳时间
                # if msg_str[6: 8] == 'b2':
                #     logger.info('lane_num:{}  检测到obu: {}'.format(rsu_client.lane_num, msg_str))
                # else:
                #     logger.info('lane_num:{}  接收到指令: {}'.format(rsu_client.lane_num, msg_str))
            elif CommonConf.ETC_MAKER == 'jinyi':
                if msg_str[8: 12] == 'b200':  # 心跳指令
                    logger.info('lane_num:{}  心跳指令:{}, 天线时间:{}, 当前时间:{}'.format(rsu_client.lane_num, msg_str,
                                                                                rsu_client.rsu_heartbeat_time,
                                                                                datetime.now()))
                    DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                    continue
                elif msg_str[8: 12] == 'b201':
                    logger.error('射频初始化异常: {}'.format(msg_str))
                    break
                elif msg_str[8: 12] == 'b202':
                    logger.error('PSAM卡初始化异常或无卡: {}'.format(msg_str))
                    break
            elif CommonConf.ETC_MAKER == 'wanji':
                if msg_str[16: 20] == 'b100':
                    logger.info('lane_num:{}  心跳指令:{}, 天线时间:{}, 当前时间:{}'.format(rsu_client.lane_num, msg_str,
                                                                                rsu_client.rsu_heartbeat_time,
                                                                                datetime.now()))
                    DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                    continue
                elif msg_str[16: 18] == 'b1':
                    logger.error('心跳出错:{}'.format(msg_str))
                    continue
            # 检测到obu会狂发指令,通过添加if语句可以控制每隔一秒打印日志
            if (datetime.now() - cache_time).seconds >= 1:
                logger.info('lane_num:{}  检测到obu: {}'.format(rsu_client.lane_num, msg_str))
                cache_time = datetime.now()

            # 查询数据库订单
            _, db_session = create_db_session(sqlite_dir=CommonConf.SQLITE_DIR,
                                              sqlite_database='etc_deduct.sqlite')
            query_item: ETCRequestInfoOrm = db_session.query(ETCRequestInfoOrm).filter(
                and_(ETCRequestInfoOrm.lane_num == rsu_client.lane_num,
                     ETCRequestInfoOrm.create_time > (datetime.now() - timedelta(seconds=10)),
                     ETCRequestInfoOrm.flag == 0)).first()
            # 找到订单开始扣费
            if query_item:
                logger.info('开始扣费。。。。。。')
                logger.info('{}, {}'.format(query_item.create_time, query_item.flag))
                logger.info('{}'.format(query_item.park_code))
                try:
                    EtcToll.toll(query_item, rsu_client, db_session)
                except:
                    logger.error(traceback.format_exc())

            else:
                db_session.close()
Beispiel #7
0
    def etc_toll(rsu_client: RsuSocket):
        # TimingJob.start_scheduler(rsu_client)
        while True:
            now = datetime.now()
            # 查询天线的计费状态,charge=1开启计费,charge=0关闭计费
            rsu_charge = DBRsuCharge.query_rsu_charge()
            if rsu_charge == 0:
                rsu_client.rsu_heartbeat_time = now  # 更新心跳
                DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                time.sleep(30)
                continue

            # if 0 <= now.hour <= 4:  # 0:00-5:00和22:00-24:00关闭天线
            #     if rsu_client.rsu_on_or_off == StatusFlagConfig.RSU_ON:
            #         logger.info('-------------关闭天线---------------')
            #         rsu_client.close_socket()
            #         rsu_client.rsu_on_or_off = StatusFlagConfig.RSU_OFF
            #
            #     rsu_client.rsu_heartbeat_time = now  # 更新心跳
            #     DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
            #     time.sleep(60)
            #     logger.info('。。。当前天线处于休眠状态。。。')
            #     continue
            # elif rsu_client.rsu_on_or_off == StatusFlagConfig.RSU_OFF:   # 其它时间段打开天线
            #     logger.info('-------------打开天线---------------')
            #     rsu_client.init_rsu()
            #     rsu_client.rsu_on_or_off = StatusFlagConfig.RSU_ON

            # socket监听,接受数据
            try:
                msg_str = rsu_client.recv_msg_max_wait_time()
            except:
                err_msg = traceback.format_exc()
                logger.error(err_msg)
                if err_msg.find('远程主机强迫关闭了一个现有的连接') != -1 or err_msg.find(
                        '你的主机中的软件中止了一个已建立的连接') != -1:
                    time.sleep(30)
                else:
                    time.sleep(10)
                DBOPeration.update_rsu_pid_status(rsu_client, 0)
                rsu_client.close_socket()
                rsu_client.init_rsu()
                continue
            # 有接收到数据,表明天线还在工作,更新心跳时间
            rsu_client.rsu_heartbeat_time = datetime.now()
            if msg_str[6:16] == 'b2ffffffff':  # 心跳指令
                logger.info('lane_num:{}  心跳指令:{}, 天线时间:{}, 当前时间:{}'.format(
                    rsu_client.lane_num, msg_str,
                    rsu_client.rsu_heartbeat_time, datetime.now()))
                DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                continue
            # 检测到obu, 检测到obu时,会狂发b2指令,频繁的更新数据库,所以此种情况下不要更新天线心跳时间
            if msg_str[6:8] == 'b2':
                logger.info('lane_num:{}  检测到obu: {}'.format(
                    rsu_client.lane_num, msg_str))
            else:
                logger.info('lane_num:{}  接收到指令: {}'.format(
                    rsu_client.lane_num, msg_str))
            # 查询数据库订单
            _, db_session = create_db_session(
                sqlite_dir=CommonConf.SQLITE_DIR,
                sqlite_database='etc_deduct.sqlite')
            query_item: ETCRequestInfoOrm = db_session.query(
                ETCRequestInfoOrm).filter(
                    and_(
                        ETCRequestInfoOrm.lane_num == rsu_client.lane_num,
                        ETCRequestInfoOrm.create_time >
                        (datetime.now() - timedelta(seconds=10)),
                        ETCRequestInfoOrm.flag == 0)).first()
            # TODO  收到etc扣费请求,但是车上没有obu
            # 找到订单开始扣费
            if query_item:
                logger.info('开始扣费。。。。。。')
                logger.info('{}, {}'.format(query_item.create_time,
                                            query_item.flag))
                etc_result = EtcToll.toll(query_item, rsu_client)
                query_item.flag = 1

                if etc_result['flag']:
                    logger.info(
                        '...................扣费成功........................')
                    query_item.deduct_status = EtcDeductStatus.SUCCESS
                else:
                    logger.info(
                        '...................扣费失败........................')
                    query_item.deduct_status = EtcDeductStatus.FAIL
                # 数据修改好后提交
                try:
                    db_session.commit()
                except:
                    db_session.rollback()
                    logger.error(traceback.format_exc())

            else:
                # 没有查询到订单,pass
                pass
            db_session.close()
Beispiel #8
0
    def etc_toll(rsu_client: RsuSocket):
        """
        根据检测到的obu信息判断是否扣费
        @param rsu_client:
        @return:
        """

        while True:
            # socket监听,接受数据
            try:
                msg_str = rsu_client.recv_msg_max_wait_time()
            except:
                DBOPeration.update_rsu_pid_status(rsu_client, 0)
                err_msg = traceback.format_exc()
                logger.error(err_msg)
                if err_msg.find('远程主机强迫关闭了一个现有的连接') != -1 or err_msg.find('你的主机中的软件中止了一个已建立的连接') != -1:
                    time.sleep(30)
                else:
                    time.sleep(10)
                continue
            # 有接收到数据,表明天线还在工作,更新心跳时间
            rsu_client.rsu_heartbeat_time = datetime.now()
            if msg_str[8: 12] == 'b200':  # 心跳指令
                logger.info('lane_num:{}  心跳指令:{}, 天线时间:{}, 当前时间:{}'.format(rsu_client.lane_num, msg_str,
                                                                            rsu_client.rsu_heartbeat_time,
                                                                            datetime.now()))
                DBOPeration.update_rsu_heartbeat(rsu_client)  # 心跳更新入库
                continue
            elif msg_str[8: 12] == 'b201':
                logger.error('射频初始化异常: {}'.format(msg_str))
                break
            elif msg_str[8: 12] == 'b202':
                logger.error('PSAM卡初始化异常或无卡: {}'.format(msg_str))
                break
            elif msg_str[8: 10] == 'b4':
                logger.info('检测到车辆信息')
                logger.info(msg=msg_str)
                b4_info = rsu_client.command_recv_set.parse_b4(msg_str)
                plate_no = b4_info['VehicleLicencePlateNumber']
                plate_color = b4_info['VehicleLicencePlateColor']

                # TODO 待删
                # if (plate_no == 'ffffffffffffffffffffffff' and plate_color == 'ffff') or \
                #         (plate_no == '000000000000000000000000' and plate_color == '0000'):  # 测试卡
                #     logger.info('测试卡:{}'.format(msg_str))
                #     logger.info('b4信息: {}'.format(json.dumps(b4_info)))
                #     plate_no, plate_color = 'd4c141313131313100000000', '0000'

                plate_no = CommonUtil.parse_plate_code(plate_no)
                logger.info('车牌号: {}, 车颜色:{}'.format(plate_no, plate_color))
                park_code = rsu_client.rsu_conf['park_code']  # 停车场
                if plate_no:
                    # 上传车辆信息
                    threading.Thread(target=ThirdEtcApi.upload_vehicle_plate_no,
                                     args=(park_code, plate_no, plate_color)).start()
                else:
                    logger.error('读到的车牌号为空。。。。。。。')
                # TODO 该处为测试,待删,扣所有obu的车费
                if plate_no:
                    plate_no_test = '鲁L12345'
                    query_vehicle_owe: VehicleOweOrm = DBClient.query_vehicle_owe('鲁L12345', '0000')  # 查询欠费车辆信息
                    query_vehicle_owe.plate_no = plate_no
                else:
                    query_vehicle_owe = None

                # TODO 待恢复
                # query_vehicle_owe: VehicleOweOrm = DBClient.query_vehicle_owe(plate_no, plate_color)  # 查询欠费车辆信息


                if query_vehicle_owe:  # 如果车辆欠费,开启扣费
                    logger.info('-------------车牌号:{}, 车颜色:{} 欠费,开始扣费----------'.format(plate_no, plate_color))
                    # etc_result = rsu_client.fee_deduction(body)
                    etc_result = EtcToll.toll(query_vehicle_owe, rsu_client)
                    if etc_result['flag'] is True:
                        logger.info('------------------------扣费成功--------------------------')
                        # TODO 后续成功扣费后需要删除数据库中的数据
                    else:
                        logger.error('------------------------扣费失败--------------------------')

                else:
                    logger.info('车牌号:{}, 车颜色:{} 没有欠费'.format(plate_no, plate_color))

            else:
                logger.info('lane_num:{}  接收到指令: {}'.format(rsu_client.lane_num, msg_str))
                if not msg_str:
                    time.sleep(3)