Ejemplo n.º 1
0
def notification_charge_order_info_for_bonus():
    """
    推送充电订单信息(运营考核奖励)
    使用要求:自充电桩停止充电并生成订单后,订单须在150秒内上报到市级平台e充网,如上报失败
    须按照以下频率推送订单信息(150/300/…./1800/3600/….,单位秒)
    """
    start_date = date(2020, 4, 7)
    orders = Order.objects.filter(Q(report_result__isnull=True) | Q(report_result__gt=0),  charg_pile__is_subsidy=1, status=2, begin_time__date__gte=start_date, consum_money__gt=0)
    result ={}
    for order in orders:
        if order.begin_time is None or order.end_time is None:
            continue

        if order.report_time is not None:
            if (datetime.now() - order.report_time).seconds < 145:
                continue

        try:
            gun = ChargingGun.objects.get(charg_pile=order.charg_pile, gun_num=order.gun_num)
        except ChargingGun.DoesNotExist as ex:
            gun = None

        ConnectorID = '{}{}'.format(order.charg_pile.pile_sn, order.gun_num)
        result["StartChargeSeq"] = order.out_trade_no
        result["ConnectorID"] = ConnectorID
        result["StartTime"] = order.begin_time.strftime("%Y-%m-%d %H:%M:%S")
        result["EndTime"] = order.end_time.strftime("%Y-%m-%d %H:%M:%S")
        if order.start_charge_seq:
            result["ChargeModel"] = 1
        else:
            result["ChargeModel"] = 3

        result["TotalPower"] = float(order.get_total_reading())
        result["TotalMoney"] = float(order.consum_money.quantize(decimal.Decimal("0.01")))
        result["UserName"] = order.name
        result["StationID"] = str(order.charg_pile.station.id)
        result["EquipmentID"] = str(order.charg_pile.id)
        result["ConnectorPower"] = float(gun.power) if gun is not None else 0
        result["ChargeLast"] = int(order.total_seconds()) if order.total_seconds() > 0 else 0
        result["MeterValueStart"] = float(order.begin_reading.quantize(decimal.Decimal("0.01")))
        result["MeterValueEnd"] = float(order.end_reading.quantize(decimal.Decimal("0.01")))
        if order.charg_status_id == 91:
            result["StopReason"] = 0    # 用户手动停止充电
        elif order.charg_status_id in [95, 96]:
            result["StopReason"] = 3    # 充电机设备故障
        elif order.charg_status_id in [98, 92]:
            result["StopReason"] = 4    # 连接器断开
        elif order.charg_status_id in [93, 94, 97]:
            result["StopReason"] = 1    # 客户归属地运营商平台停止充
        else:
            result["StopReason"] = 3  # 充电机设备故障

        echarge = EchargeNet(settings.MQTT_REDIS_URL, settings.MQTT_REDIS_PORT)
        print('EchargeNet: ', id(echarge))
        ret_data = echarge.notification_charge_order_info_for_bonus(**result)

        if "Ret" in ret_data and ret_data["Ret"] == 0:
            # 解密
            ret_crypt_data = ret_data["Data"]
            ret_decrypt_data = data_decode(ret_crypt_data)
            # 获取到code值
            print(ret_decrypt_data["StartChargeSeq"])
            ConfirmResult = ret_decrypt_data["ConfirmResult"]
        else:
            ConfirmResult = 99

        order.report_result = ConfirmResult
        order.report_time = datetime.now()
        order.save()
        if ConfirmResult > 0:
            print("notification_charge_order_info_for_bonus failure:", ConfirmResult)
        else:
            print("notification_charge_order_info_for_bonus success:", ConfirmResult)
Ejemplo n.º 2
0
def notification_charge_order_info_for_bonus():
    """
    推送充电订单信息(运营考核奖励)
    使用要求:自充电桩停止充电并生成订单后,订单须在150秒内上报到市级平台e充网,如上报失败
    须按照以下频率推送订单信息(150/300/…./1800/3600/….,单位秒)
    """
    orders = Order.objects.filter(Q(report_result__isnull=True)
                                  | Q(report_result__gt=0),
                                  start_charge_seq__isnull=False,
                                  status=2)
    result = {}
    # Ret = 0
    # Msg = ""
    for order in orders:
        if order.begin_time is None or order.end_time is None:
            continue

        if order.report_time is not None:
            if (datetime.now() - order.report_time).seconds < 145:
                continue

        try:
            gun = ChargingGun.objects.get(charg_pile=order.charg_pile,
                                          gun_num=order.gun_num)
        except ChargingGun.DoesNotExist as ex:
            gun = None

        ConnectorID = '{}{}'.format(order.charg_pile.pile_sn, order.gun_num)
        result["StartChargeSeq"] = order.start_charge_seq
        result["ConnectorID"] = ConnectorID
        result["StartTime"] = order.begin_time.strftime("%Y-%m-%d %H:%M:%S")
        result["EndTime"] = order.end_time.strftime("%Y-%m-%d %H:%M:%S")
        result["ChargeModel"] = 0
        result["TotalPower"] = str(order.get_total_reading())
        result["TotalMoney"] = str(order.consum_money)
        result["UserName"] = order.name
        result["StationID"] = str(order.charg_pile.station.id)
        result["EquipmentID"] = order.charg_pile.pile_sn
        result["ConnectorPower"] = gun.power if gun is not None else 0
        result["ChargeLast"] = order.total_seconds()
        result["MeterValueStart"] = str(order.begin_reading)
        result["MeterValueEnd"] = str(order.end_reading)
        if order.charg_status_id == 91:
            result["StopReason"] = 0  # 用户手动停止充电
        elif order.charg_status_id in [95, 96]:
            result["StopReason"] = 3  # 充电机设备故障
        elif order.charg_status_id in [98, 92]:
            result["StopReason"] = 4  # 连接器断开
        elif order.charg_status_id in [93, 94, 97]:
            result["StopReason"] = 1  # 客户归属地运营商平台停止充

        # encrypt_data = data_encode(**result)  # 数据加密
        # # 数据签名, 用Ret+Msg+Data生成返回签名
        # sig_data = "{0}{1}{2}".format(str(Ret), Msg, encrypt_data)
        # ret_sig = get_hmac_md5(settings.SIGSECRET, sig_data)
        # result = {
        #     "Ret": Ret,
        #     "Msg": Msg,
        #     "Data": encrypt_data,
        #     "Sig": ret_sig,
        # }
        echarge = EchargeNet(settings.MQTT_REDIS_URL, settings.MQTT_REDIS_PORT)
        ret_data = echarge.notification_charge_order_info_for_bonus(**result)

        if "Ret" in ret_data and ret_data["Ret"] == 0:
            # 解密
            ret_crypt_data = ret_data["Data"]
            ret_decrypt_data = data_decode(ret_crypt_data)
            # 获取到code值
            dict_decrpt_data = json.loads(ret_decrypt_data)
            print(dict_decrpt_data["StartChargeSeq"])
            ConfirmResult = dict_decrpt_data["ConfirmResult"]
        else:
            ConfirmResult = 99

        order.report_result = ConfirmResult
        order.report_time = datetime.now()
        order.save()
        if ConfirmResult > 0:
            print("推送充电订单信息失败!", ConfirmResult)
        else:
            print("推送充电订单信息成功!", ConfirmResult)