ccard_service.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding:utf-8 -*-
  2. import os, sys, time
  3. import shutil
  4. import thread
  5. from threading import Lock
  6. from TST.CaptureCard import *
  7. from ssat_sdk.sat_environment import *
  8. from ssat_sdk.core.ibinder_listener import *
  9. from ssat_sdk.service.service_config import *
  10. from ssat_sdk.utils.AbnormalClient import abnormal_client
  11. class CCardService():
  12. Service_Name = "ccard"
  13. def __init__(self):
  14. self.serviceConfig = ServiceConfig()
  15. self.status = 0
  16. self.capLock = Lock()
  17. self.picTake = False
  18. self.loopPicture = ""
  19. self.pause = True
  20. self.puaseTime = 0
  21. def initCCard(self):
  22. print u"初始化视频采集卡"
  23. try:
  24. self.ccard = CaptureCard()
  25. except Exception,e:
  26. abnormal_client.sendAbnormal('ccard', str(e))
  27. print u"视频采集卡启动失败",e
  28. self.status = 0
  29. return 0,e.message
  30. ibinder = ListenerBinder(port=30000)
  31. self.listener, port = ibinder.createListener()
  32. self.serviceConfig.setCCardListenerPort(port)
  33. print u"视频采集卡启动成功,CCard Port:",port
  34. self.status = 1
  35. while (True):
  36. try:
  37. conn = self.listener.accept()
  38. cmdLine = conn.recv()
  39. except Exception,e:
  40. abnormal_client.sendAbnormal('ccard', str(e))
  41. print u"采集卡接受指令失败",e
  42. continue
  43. print u"CCard接收指令:", cmdLine
  44. # cmdLine:指令+参数
  45. result = self.execCommand(cmdLine)
  46. try:
  47. conn.send(result)
  48. except Exception, e:
  49. print e
  50. def execCommand(self, cmdLine):
  51. command, param = cmdLine.split("::")
  52. if ("takepicture" == command):
  53. return self.excuteCapPiture(param)
  54. elif ("takePictureLoop" == command):
  55. thread.start_new_thread(self.takePictureLoop,(param,))
  56. return True
  57. elif ("pausePictureLoop" == command):
  58. self.puaseTime = int(param)
  59. while (self.pause == False):
  60. pass
  61. return True
  62. elif ("stopPictureLoop" == command):
  63. self.picTake = False
  64. return True
  65. def closeCCard(self):
  66. self.ccard.close()
  67. self.ccard = None
  68. def takePictureLoop(self, pic_path):
  69. self.loopPicture = pic_path
  70. self.picTake = True
  71. # count = 0
  72. while(self.picTake):
  73. # count += 1
  74. # print u"循环截图",count
  75. self.pause = True
  76. time.sleep(self.puaseTime)
  77. self.pause = False
  78. self.capLock.acquire()
  79. self.capPicture(self.loopPicture)
  80. self.capLock.release()
  81. def excuteCapPiture(self, pic_path):
  82. if self.picTake == True:
  83. #确保循环截图图片存在
  84. while os.path.isfile(self.loopPicture) <> True:
  85. print u"循环截图,loopPicture:",os.path.isfile(self.loopPicture)
  86. self.capLock.acquire()
  87. try:
  88. shutil.move(self.loopPicture, pic_path)
  89. self.capLock.release()
  90. return True
  91. except Exception, e:
  92. print e
  93. abnormal_client.sendAbnormal('ccard', str(e))
  94. self.capLock.release()
  95. return False
  96. else:
  97. return self.capPicture(pic_path)
  98. def capPicture(self, pic_path):
  99. picname = os.path.split(pic_path)[1]
  100. tmpPic = os.path.join(getSATTmpDIR(),picname)
  101. self.ccard.takePicture(tmpPic)
  102. try:
  103. shutil.move(tmpPic, pic_path)
  104. return True
  105. except Exception,e:
  106. print e
  107. abnormal_client.sendAbnormal('ccard', str(e))
  108. return False
  109. def getStatus(self):
  110. print u"CCard状态:", self.status
  111. return self.status