views.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. from app import log
  2. from app import register
  3. from app import device
  4. from app.app_config import *
  5. from time import sleep
  6. import ctypes
  7. import vlc
  8. import threading
  9. # from app.register import runFunctionThread
  10. # import traceback
  11. """
  12. desc:
  13. 播放指令执行函数
  14. Parameters:
  15. param1 - 播放相关的数据
  16. Returns:
  17. 执行状态
  18. """
  19. @register.action(PLAY)
  20. def action_play(data):
  21. if "data" in data:
  22. if not device.play(data.get(DATA)):
  23. log.logger.error("Play uri:%s on id:%s error" % (data.get(DATA).get("url"), data.get(ID)))
  24. return False
  25. log.logger.info("Play uri:%s on id:%s successfully" % (data.get(DATA).get("url"), data.get(ID)))
  26. return True
  27. else:
  28. log.logger.info("Data not found")
  29. return False
  30. """
  31. desc:
  32. 暂停指令执行函数
  33. Parameters:
  34. param1 - 暂停相关的数据
  35. Returns:
  36. 执行状态
  37. """
  38. @register.action(PAUSE)
  39. def action_pause(data):
  40. if "value" in data.get(DATA):
  41. value = data.get(DATA).get("value")
  42. else:
  43. log.logger.info("Value not found")
  44. return False
  45. if not device.pause(value):
  46. log.logger.error("Player pause on id:%s error" % (data.get(ID)))
  47. return False
  48. else:
  49. log.logger.info("Player pause on id:%s successfully" % (data.get(ID)))
  50. return True
  51. """
  52. desc:
  53. 恢复指令执行函数
  54. Parameters:
  55. param1 - 恢复相关的数据
  56. Returns:
  57. 执行状态
  58. """
  59. @register.action(RESUME)
  60. def action_resume(data):
  61. if "value" in data.get(DATA):
  62. value = data.get(DATA).get("value")
  63. else:
  64. log.logger.info("Value not found")
  65. return False
  66. if not device.resume(value):
  67. log.logger.error("Player resume on id:%s error" % (data.get(ID)))
  68. return False
  69. else:
  70. log.logger.info("Player resume on id:%s successfully" % (data.get(ID)))
  71. return True
  72. """
  73. desc:
  74. 停止播放指令执行函数
  75. Parameters:
  76. param1 - 停止播放相关的数据
  77. Returns:
  78. 执行状态
  79. """
  80. @register.action(STOP)
  81. def action_stop(data):
  82. if not device.stop():
  83. log.logger.error("Player stop on id:%s stop" % (data.get(ID)))
  84. log.logger.info("Player stop on id:%s successfully" % (data.get(ID)))
  85. return True
  86. """
  87. desc:
  88. 设置分机号指令执行函数
  89. Parameters:
  90. param1 - 设置分机号相关的数据
  91. Returns:
  92. 执行状态
  93. """
  94. @register.action(SET_EXTEN)
  95. def action_set_exten(data):
  96. if "data" in data:
  97. if HOST in data.get(DATA):
  98. host = data.get(DATA).get(HOST)
  99. if EXTEN in data.get(DATA):
  100. exten = data.get(DATA).get(EXTEN)
  101. if PASSWORD in data.get(DATA):
  102. password = data.get(DATA).get(PASSWORD)
  103. if not (host and exten and password):
  104. log.logger.warning("data not compelete")
  105. return False
  106. else:
  107. log.logger.info("data not found")
  108. return False
  109. if not device.set_exten(host, exten, password):
  110. log.logger.error("Set exten:%s on id:%s error" % (exten, data.get(ID)))
  111. log.logger.info("Set exten:%s on id:%s successfully" % (exten, data.get(ID)))
  112. return True
  113. """
  114. desc:
  115. 设置播放器音量指令执行函数
  116. Parameters:
  117. param1 - 音量相关的数据
  118. Returns:
  119. 执行状态
  120. """
  121. @register.action(SET_SOLF_VOLUME)
  122. def action_set_soft_volume(data):
  123. if "volume" in data.get(DATA):
  124. volume = int(data.get(DATA).get(VOLUME))
  125. else:
  126. log.logger.warning("volume not found!")
  127. return False
  128. if not device.set_soft_volume(volume):
  129. log.logger.error("Set volume:%d on id:%s error" % (volume, data.get(ID)))
  130. log.logger.info("Set soft volume:%d on id:%s successfully" % (volume, data.get(ID)))
  131. return True
  132. """
  133. desc:
  134. 设置硬件音量指令执行函数
  135. Parameters:
  136. param1 - 硬件音量相关的数据
  137. Returns:
  138. 执行状态
  139. """
  140. @register.action(SET_HARD_VOLUME)
  141. def action_set_hard_volume(data):
  142. if "volume" in data.get(DATA):
  143. volume = int(data.get(DATA).get(VOLUME))
  144. else:
  145. log.logger.warning("volume not found!")
  146. return False
  147. if not device.set_hard_volume(volume):
  148. log.logger.error("Set hard volume:%d on id:%s error" % (volume, data.get(ID)))
  149. log.logger.info("Set hard volume:%d on id:%s successfully" % (volume, data.get(ID)))
  150. return True
  151. """
  152. desc:
  153. 设置强控指令执行函数
  154. Parameters:
  155. param1 - 设置强控相关的数据
  156. Returns:
  157. 执行状态
  158. """
  159. @register.action(SET_HARD_VOLUME_CONTROL)
  160. def action_set_hard_volume_control(data):
  161. if "value" in data.get(DATA):
  162. value = data.get(DATA).get(VALUE)
  163. else:
  164. log.logger.warning("value not found!")
  165. return False
  166. if not device.set_hard_volume_control(value):
  167. log.logger.error("Set hard volume control:%s on id:%s error" % (value, data.get(ID)))
  168. log.logger.info("Set hard volume control:%s on id:%s successfully" % (value, data.get(ID)))
  169. return True
  170. """
  171. desc:
  172. 设置相关数据通用指令执行函数
  173. Parameters:
  174. param1 - 相关的数据
  175. Returns:
  176. 执行状态
  177. """
  178. @register.action(SET_SERVICE_SETTINGS)
  179. def action_set_service_settings(data):
  180. if "data" in data:
  181. data_obj = data.get(DATA)
  182. if not device.set_service_settings(data_obj):
  183. log.logger.error("Set service settings:%s on id:%s error" % (data_obj, data.get(ID)))
  184. return False
  185. log.logger.info("Set service settings:%s on id:%s successfully" % (data_obj, data.get(ID)))
  186. return True
  187. """
  188. desc:
  189. 开始推送rtsp指令执行函数
  190. Parameters:
  191. param1 - rtsp相关的数据
  192. Returns:
  193. 执行状态
  194. """
  195. @register.action("start-pull-rtsp")
  196. def action_start_pull_rtsp(data):
  197. if "src" in data.get(DATA):
  198. src = data.get(DATA).get("src")
  199. else:
  200. log.logger.warning("src not found!")
  201. return False
  202. if "dst" in data.get(DATA):
  203. dst = data.get(DATA).get("dst")
  204. else:
  205. log.logger.warning("dst not found!")
  206. return False
  207. if not device.start_pull_rtsp(src, dst):
  208. log.logger.error("Start pull rtsp src:%s on id:%s error" % (src, data.get(ID)))
  209. else:
  210. log.logger.info("Start pull rtsp src:%s on id:%s successfully" % (src, data.get(ID)))
  211. return True
  212. """
  213. desc:
  214. 停止推送rtsp指令执行函数
  215. Parameters:
  216. param1 - rtsp相关的数据
  217. Returns:
  218. 执行状态
  219. """
  220. @register.action("stop-pull-rtsp")
  221. def action_stop_pull_rtsp(data):
  222. if not device.stop_pull_rtsp():
  223. log.logger.error("Stop pull rtsp on id:%s error" % (data.get(ID)))
  224. else:
  225. log.logger.info("Stop pull rtsp on id:%s successfully" % (data.get(ID)))
  226. return True
  227. vlc_debug = {
  228. 'DEBUG': 0,
  229. 'NOTICE': 2,
  230. 'WARNING': 3,
  231. 'ERROR': 4
  232. }
  233. """
  234. desc:
  235. 捕获libvlc库的日志
  236. """
  237. vsnprintf = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')).vsnprintf
  238. vsnprintf.restype = ctypes.c_int
  239. vsnprintf.argtypes = (
  240. ctypes.c_char_p,
  241. ctypes.c_size_t,
  242. ctypes.c_char_p,
  243. ctypes.c_void_p,
  244. )
  245. @vlc.CallbackDecorators.LogCb
  246. def log_callback(data, level, ctx, fmt, args):
  247. if level < vlc_debug[VLC_DEBUG_LEVEL]:
  248. return
  249. outBuf = ctypes.create_string_buffer(VLC_BUF_LEN)
  250. vsnprintf(outBuf, VLC_BUF_LEN, fmt, args)
  251. try:
  252. log_msg = str(outBuf.value, 'utf-8')
  253. except Exception as e:
  254. log.logger.error(e)
  255. if level == vlc_debug['DEBUG']:
  256. log.logger.debug(log_msg)
  257. elif level == vlc_debug['NOTICE']:
  258. log.logger.info(log_msg)
  259. elif level == vlc_debug['WARNING']:
  260. log.logger.warning(log_msg)
  261. if "buffer too late" in log_msg:
  262. device.set_vlc_warning_count(device.get_vlc_warning_count() + 1)
  263. if device.get_vlc_warning_count() > VLC_WARNING_COUNT:
  264. log.logger.info("Replay uri:%s" % (device.current_uri))
  265. t = threading.Thread(target = device.player.play, args = (device.current_uri,))
  266. t.setDaemon(True)
  267. t.start()
  268. log.logger.info("Replay finished!")
  269. device.vlc_error_count = 0
  270. else:
  271. pass
  272. else:
  273. log.logger.error(log_msg)
  274. if "Timestamp conversion failed" in log_msg:
  275. device.set_vlc_error_count(device.get_vlc_error_count() + 1)
  276. if device.get_vlc_error_count() > VLC_ERROR_COUNT:
  277. log.logger.info("Replay uri:%s" % (device.current_uri))
  278. t = threading.Thread(target = device.player.play, args = (device.current_uri,))
  279. t.setDaemon(True)
  280. t.start()
  281. log.logger.info("Replay finished!")
  282. device.vlc_error_count = 0
  283. else:
  284. pass
  285. def vlc_log_start():
  286. log.logger.info("Start vlc log")
  287. device.player.instance.log_set(log_callback, None)
  288. return True