thread_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import getopt
  4. import os
  5. import sys
  6. import threading
  7. import time
  8. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  9. sys.path.insert(0, PROJECT_ROOT)
  10. try:
  11. options, args = getopt.getopt(sys.argv[1:], 'e:i:c:', ['env=', 'imei=', 'code='])
  12. except getopt.GetoptError as e:
  13. print(str(e))
  14. sys.exit()
  15. system_env = 'testing'
  16. imei = ''
  17. for name, value in options:
  18. if name in ('-e', '--env'):
  19. system_env = value
  20. if name in ('-i', '--imei'):
  21. imei = value
  22. if name in ('-c', '--code'):
  23. code = value
  24. if not imei:
  25. print 'imei is null.'
  26. sys.exit(2)
  27. import os
  28. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.{env}'.format(env = system_env))
  29. from script.base import init_env
  30. init_env(interactive = False)
  31. from apps.web.device.models import Device
  32. from apps.web.core.helpers import ActionDeviceBuilder
  33. from apps.web.core.exceptions import ServiceException
  34. class Tester(threading.Thread):
  35. def __init__(self, sleepTime, runTimes, imei, func, **args):
  36. super(Tester, self).__init__()
  37. dev = Device.get_dev(imei)
  38. self._smartBox = ActionDeviceBuilder.create_action_device(dev, typeCode = code)
  39. self._func = func
  40. self._args = args
  41. self._runTimes = runTimes
  42. self._sleepTime = sleepTime
  43. def run(self):
  44. count = 0
  45. while count < self._runTimes:
  46. count += 1
  47. try:
  48. result = eval('self._smartBox.%s(**self._args)' % self._func)
  49. # result = self._smartBox.get_port_status_from_dev()
  50. print result
  51. if self._sleepTime:
  52. time.sleep(self._sleepTime)
  53. except ServiceException, e:
  54. if u'充电桩正在忙' in e.result.get('description'):
  55. print('the device is busuy. error = %s' % e)
  56. except Exception, e:
  57. print(str(e))
  58. # ii = 0
  59. # for ii in range(0, 10):
  60. # Tester(0, 1, imei, 'get_port_info', line = ii % 10).start()
  61. # ii = 0
  62. # for ii in range(2):
  63. # Tester(0,10000000000,logicalCode,'get_IC_coin_power_config').start()
  64. # ii = 0
  65. # for ii in range(2):
  66. # Tester(0,10000000000,logicalCode,'get_freemode_volume_andsoon_config').start()
  67. # ii = 0
  68. # for ii in range(2):
  69. # Tester(0,10000000000,logicalCode,'get_fullstop_cardrefund').start()
  70. # Tester(0,10000000000,logicalCode,'set_IC_coin_power_config',infoDict={'maxPower':1000,'icMoney':1,'time1':5,'time2':10,'time3':16}).start()
  71. # Tester(0,10000000000,logicalCode,'set_card_time_config',infoDict={'card1Time':5,'card2Time':10,'card3Time':16}).start()
  72. # Tester(0,10000000000,logicalCode,'set_coin_card_enable',infoDict={'putCoins':True,'icCard':True}).start()
  73. # Tester(0,10000000000,logicalCode,'set_freemode_volume_config',infoDict={'chargeFree':False,'volume':5}).start()
  74. # Tester(0,10000000000,logicalCode,'set_fuchong_config',infoDict={'fuchongPower':200,'fuchongTime':1}).start()
  75. # Tester(0,10000000000,logicalCode,'set_fullstop_cardrefund',infoDict={'autoStop':True,'cardRefund':True}).start()
  76. # Tester(0,10000000000,logicalCode,'set_gear_conf',infoDict={'power1':100,'power1ratio':800,'power2':200,'power2ratio':900,'power3':300,'power3ratio':1000,'power4':400,'power4ratio':1200,'power5':500,'power5ratio':1500}).start()
  77. # Tester(30, 10, imei, 'get_port_status_from_dev').start()
  78. # time.sleep(1)
  79. # Tester(200, 1, imei, 'get_dev_consume_count').start()
  80. # time.sleep(1)
  81. #
  82. # Tester(200, 1, imei, 'get_dev_setting').start()
  83. # time.sleep(1)
  84. #
  85. for ii in range(0, 10):
  86. Tester(1, 1, imei, 'start_device', package = {'time': 1, 'unit': u'分钟', 'coins': 1, 'price': 1},
  87. openId = 'aaaaaaaaaaaaaa', attachParas = {'chargeIndex': ii + 1}).start()
  88. #
  89. # for ii in range(10):
  90. # Tester(100, 1, imei, 'stop_charging_port', port = 10 - ii).start()
  91. # time.sleep(5)
  92. #
  93. # time.sleep(10)
  94. # dev = Device.get_dev_by_l(logicalCode)
  95. # box = ActionDeviceBuilder.create_action_device(dev)
  96. # box.get_port_status_from_dev()
  97. print 'finished'