# -*- coding:utf-8 -*- import os, sys, time import shutil import thread from threading import Lock from TST.CaptureCard import * from ssat_sdk.sat_environment import * from ssat_sdk.core.ibinder_listener import * from ssat_sdk.service.service_config import * from ssat_sdk.utils.AbnormalClient import abnormal_client class CCardService(): Service_Name = "ccard" def __init__(self): self.serviceConfig = ServiceConfig() self.status = 0 self.capLock = Lock() self.picTake = False self.loopPicture = "" self.pause = True self.puaseTime = 0 def initCCard(self): print u"初始化视频采集卡" try: self.ccard = CaptureCard() except Exception,e: abnormal_client.sendAbnormal('ccard', str(e)) print u"视频采集卡启动失败",e self.status = 0 return 0,e.message ibinder = ListenerBinder(port=30000) self.listener, port = ibinder.createListener() self.serviceConfig.setCCardListenerPort(port) print u"视频采集卡启动成功,CCard Port:",port self.status = 1 while (True): try: conn = self.listener.accept() cmdLine = conn.recv() except Exception,e: abnormal_client.sendAbnormal('ccard', str(e)) print u"采集卡接受指令失败",e continue print u"CCard接收指令:", cmdLine # cmdLine:指令+参数 result = self.execCommand(cmdLine) try: conn.send(result) except Exception, e: print e def execCommand(self, cmdLine): command, param = cmdLine.split("::") if ("takepicture" == command): return self.excuteCapPiture(param) elif ("takePictureLoop" == command): thread.start_new_thread(self.takePictureLoop,(param,)) return True elif ("pausePictureLoop" == command): self.puaseTime = int(param) while (self.pause == False): pass return True elif ("stopPictureLoop" == command): self.picTake = False return True def closeCCard(self): self.ccard.close() self.ccard = None def takePictureLoop(self, pic_path): self.loopPicture = pic_path self.picTake = True # count = 0 while(self.picTake): # count += 1 # print u"循环截图",count self.pause = True time.sleep(self.puaseTime) self.pause = False self.capLock.acquire() self.capPicture(self.loopPicture) self.capLock.release() def excuteCapPiture(self, pic_path): if self.picTake == True: #确保循环截图图片存在 while os.path.isfile(self.loopPicture) <> True: print u"循环截图,loopPicture:",os.path.isfile(self.loopPicture) self.capLock.acquire() try: shutil.move(self.loopPicture, pic_path) self.capLock.release() return True except Exception, e: print e abnormal_client.sendAbnormal('ccard', str(e)) self.capLock.release() return False else: return self.capPicture(pic_path) def capPicture(self, pic_path): picname = os.path.split(pic_path)[1] tmpPic = os.path.join(getSATTmpDIR(),picname) self.ccard.takePicture(tmpPic) try: shutil.move(tmpPic, pic_path) return True except Exception,e: print e abnormal_client.sendAbnormal('ccard', str(e)) return False def getStatus(self): print u"CCard状态:", self.status return self.status