kh_policy.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import json
  4. import logging
  5. from typing import TYPE_CHECKING
  6. from apps.web.constant import Const
  7. from apps.web.core.adapter.kehang import KeHangBox
  8. from apps.web.core.device_define.kehang import BillingType
  9. from apps.web.core.adapter.policy_common import PolicyCommon
  10. from apps.web.core.device_define.kehang import KeHangDeviceSettingsValidator
  11. from apps.web.core.exceptions import ServiceException
  12. from apps.web.device.models import Device
  13. from apps.web.user.models import ConsumeRecord
  14. logger = logging.getLogger(__name__)
  15. if TYPE_CHECKING:
  16. pass
  17. class KHPOLICYBox(PolicyCommon,KeHangBox):
  18. def start_device_realiable(self, order):
  19. return super(KHPOLICYBox, self).start_device_realiable(order)
  20. def set_device_function_param(self, request, lastSetConf):
  21. validator = KeHangDeviceSettingsValidator(request.POST)
  22. if not validator.is_valid():
  23. raise ServiceException({"result": 2, "description": json.dumps(validator.str_errors)})
  24. data = validator.suit_data()
  25. if not self._set_card_coin_time(
  26. data["maxPower"], data["cardCost"], data["coin1Time"], data["coin2Time"], data["coin3Time"]
  27. ):
  28. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0001)"})
  29. if not self._set_card_coin_enable(data["isCardEnable"], data["isCoinEnable"]):
  30. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0002)"})
  31. if not self._set_stop_refund(data["isAutoStop"], data["isCardRefund"]):
  32. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0003)"})
  33. if not self._set_power_step(*data["powerStep"]):
  34. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0004)"})
  35. if not self._set_free_voice(data["isFree"], data["voice"]):
  36. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0005)"})
  37. if not self._set_float_and_noload(
  38. data["floatPower"], data["floatTime"], data["noloadPower"], data["noloadTime"], data["minConsume"]
  39. ):
  40. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0006)"})
  41. if not self._set_card_time(data["card1Time"], data["card2Time"], data["card3Time"]):
  42. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0007)"})
  43. if not self._set_consume_type(data["consumeType"], data["billingType"]):
  44. raise ServiceException({"result": 2, "description": u"参数设置失败,请重试(0008)"})
  45. if data["billingType"] == 2:
  46. if self.device.devTypeCode != Const.DEVICE_TYPE_CODE_KE_HANG_POLICY:
  47. raise ServiceException({"result": 2, "description": u"参数设置失败,您设备版本太低(0009)"})
  48. # 保存数据库一次
  49. otherConf = self.device["otherConf"]
  50. otherConf.update(validator.validated_data)
  51. Device.objects.filter(devNo=self.device.devNo).update(otherConf=otherConf)
  52. def get_port_info(self, port):
  53. """ 获取设备端口的使用详情 """
  54. result = self._get_port_charge_info(port)
  55. left = result["left"]
  56. power = result["power"] / 10.0
  57. portCache = Device.get_port_control_cache(self.device.devNo, str(port))
  58. billingType = portCache.get("billingType")
  59. needKind = portCache.get("needKind")
  60. orderNo = portCache.get("orderNo")
  61. openId = portCache.get("openId")
  62. order = ConsumeRecord.objects.filter(orderNo=orderNo, openId=openId).first() # type: ConsumeRecord
  63. if order:
  64. portCache["nickname"] = order.user.nickname
  65. if needKind and billingType is not None:
  66. if billingType == BillingType.TIME:
  67. portCache[needKind] = portCache["needValue"]
  68. portCache["leftTime"] = left
  69. elif billingType == BillingType.ELEC:
  70. portCache[needKind] = portCache["needValue"] / 100.0
  71. portCache["leftElec"] = left / 100.0
  72. portCache["power"] = power
  73. return portCache