chroma_22293_service.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import sys
  4. import time
  5. import thread
  6. import json
  7. #from TST.SourceGenInput import *
  8. from ssat_sdk.service.Chroma22293 import *
  9. #import Chroma22293
  10. from ssat_sdk.sat_environment import *
  11. from ssat_sdk.core.ibinder_listener import *
  12. from ssat_sdk.service.service_config import *
  13. from ssat_sdk.utils import LoggingUtil
  14. import inspect
  15. pyFileName = os.path.split(__file__)[-1]
  16. def get_current_function_name():
  17. return inspect.stack()[1][3]
  18. class C22293Service():
  19. Service_Name = "c22293"
  20. def __init__(self):
  21. self.serviceConfig = ServiceConfig()
  22. self.status = 0
  23. self.listObj = {}
  24. self.default = ''
  25. self.className = self.__class__.__name__
  26. def initC22293(self):
  27. print u"初始化22293采集卡"
  28. try:
  29. # self.SG = SourceGenInput()
  30. parseResourceCfg()
  31. list = getChroma22293()
  32. self.default = list["default"]
  33. for item in list["devices"]:
  34. obj = Chroma22293(item["port"])
  35. if obj.open():
  36. self.listObj[item["name"]] = obj
  37. else:
  38. LoggingUtil.getDebugLogger().info(
  39. pyFileName,
  40. self.className,
  41. get_current_function_name(),
  42. '22293启动失败'+str(item["name"])+str(item["port"]))
  43. # print u"22293启动失败:%s,%s" % (item["name"], item["port"])
  44. except Exception, e:
  45. # print u"22293启动失败", e
  46. LoggingUtil.getDebugLogger().info(
  47. pyFileName,
  48. self.className,
  49. get_current_function_name(),
  50. '22293启动失败'+str(e))
  51. self.status = 0
  52. return 0, e.message
  53. ibinder = ListenerBinder(port=30200)
  54. self.listener, port = ibinder.createListener()
  55. self.serviceConfig.setC22293ListenerPort(port)
  56. # print u"22293启动成功,22293 Port:", port
  57. LoggingUtil.getDebugLogger().info(
  58. pyFileName,
  59. self.className,
  60. get_current_function_name(),
  61. '22293启动成功,22293 Port:'+str(port))
  62. self.status = 1
  63. while (True):
  64. try:
  65. conn = self.listener.accept()
  66. cmdLine = conn.recv()
  67. except Exception, e:
  68. # print u"22293接受指令失败", e
  69. LoggingUtil.getDebugLogger().info(
  70. pyFileName,
  71. self.className,
  72. get_current_function_name(),
  73. '22293接受指令失败:'+str(e))
  74. continue
  75. print u"22293接收指令:", cmdLine
  76. # cmdLine:指令+参数 json {"command":"setPattern", "param":[]}
  77. result = self.execCommand(cmdLine)
  78. try:
  79. conn.send(result)
  80. except Exception, e:
  81. # print e
  82. LoggingUtil.getDebugLogger().info(
  83. pyFileName,
  84. self.className,
  85. get_current_function_name(),
  86. '22293发送指令失败:'+str(e))
  87. def execCommand(self, cmdLine):
  88. data = json.loads(cmdLine)
  89. command = data["command"]
  90. param = data["param"]
  91. device = data["device"]
  92. # print u'deviceName:', device
  93. if len(device) == 0:
  94. device = self.default
  95. print u'deviceName=>', device
  96. if device in self.listObj.keys():
  97. if ("getDeviceName" == command):
  98. return self.listObj[device].getDeviceName()
  99. elif ("getDeviceSoft" == command):
  100. return self.listObj[device].getDeviceSoft()
  101. elif ("getStatus" == command):
  102. return self.listObj[device].getStatus()
  103. elif ("setPattern" == command):
  104. return self.listObj[device].setPattern(param[0])
  105. elif ("setTiming" == command):
  106. return self.listObj[device].setTiming(param[0])
  107. elif ("setTimingPattern" == command):
  108. return self.listObj[device].setTimingPattern(param[0], param[1])
  109. elif ("setBlueOFF" == command):
  110. return self.listObj[device].setBlueOFF()
  111. elif ("setBuleON" == command):
  112. return self.listObj[device].setBuleON()
  113. elif ("setGreenOFF" == command):
  114. return self.listObj[device].setGreenOFF()
  115. elif ("setGreenON" == command):
  116. return self.listObj[device].setGreenON()
  117. elif (" setRedOFF" == command):
  118. return self.listObj[device].setRedOFF()
  119. elif ("setRedON" == command):
  120. return self.listObj[device].setRedON()
  121. elif ("setKeyBoardLock" == command):
  122. return self.listObj[device].setKeyBoardLock()
  123. elif ("setKeyBoardUnLock" == command):
  124. return self.listObj[device].setKeyBoardUnLock()
  125. else:
  126. # print u'没有这个设备名称=%s,请检查是否书写正确' %device
  127. LoggingUtil.getDebugLogger().info(
  128. pyFileName,
  129. self.className,
  130. get_current_function_name(),
  131. '没有这个设备名称:'+str(device))
  132. return False