device.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. from app import config, log, register, project_cf, speaker_cf, linphone_cf, player
  2. import paho.mqtt.client as mqtt
  3. import json
  4. from app.app_config import *
  5. import sys
  6. from time import sleep
  7. from threading import Lock
  8. import os
  9. import vlc
  10. import threading
  11. """
  12. desc:
  13. 项目主要功能类
  14. """
  15. class Device(object):
  16. def __init__(self, config = config, player = player):
  17. self.model = config.model
  18. self.hostname = config.hostname
  19. self.ipaddr = config.ipaddr
  20. self.hard_volume = config.hard_volume
  21. self.hard_volume_control = config.hard_volume_control
  22. self.exten = config.exten
  23. self.exten_password = config.exten_password
  24. self.server_ipaddr = config.server_ipaddr
  25. self.mac = config.mac
  26. self.connect_flags = 1
  27. self.player_current_volume = config.player_current_volume
  28. self.init_player_state = config.init_player_state
  29. self.current_uri = config.current_uri
  30. self.pull_rtsp = 0
  31. self.warning_lock = Lock()
  32. self.error_lock = Lock()
  33. self.source_error_lock = Lock()
  34. #添加服务器播放还是本地播放flag 0:服务器播放 1:本地播放
  35. self.play_action_flag = 0
  36. self.tmp_init_player_state = 0
  37. if not self._check_config():
  38. sys.exit(1)
  39. self.player = player
  40. self.player.event_manager = self.player.media.event_manager()
  41. """
  42. desc:
  43. 播放器事件回调注册
  44. Parameters:
  45. param1 - 播放器事件名
  46. param2 - 回调函数名
  47. Returns:
  48. """
  49. self.player.event_manager.event_attach(vlc.EventType.MediaPlayerEncounteredError, self.MediaPlayerEncounteredError_cb)
  50. self.player.event_manager.event_attach(vlc.EventType.VlmMediaInstanceStatusError, self.VlmMediaInstanceStatusError_cb)
  51. self.player.event_manager.event_attach(vlc.EventType.MediaPlayerPlaying, self.MediaPlayerPlaying_cb)
  52. self.player.event_manager.event_attach(vlc.EventType.MediaPlayerPaused, self.MediaPlayerPaused_cb)
  53. self.player.event_manager.event_attach(vlc.EventType.MediaPlayerStopped, self.MediaPlayerStopped_cb)
  54. self.vlc_error_count = 0
  55. self.vlc_source_error_count = 0
  56. self.vlc_warning_count = 0
  57. self.mqttClient = mqtt.Client(client_id = self.mac, clean_session = True)
  58. self.mqttClient.username_pw_set(username=self.exten, password=self.exten_password)
  59. self.mqttClient.on_connect = self.on_connect
  60. # 注册监听器
  61. def add_callback(self, event_type, callback):
  62. pass
  63. # 移除监听器
  64. def remove_callback(self, event_type, callback):
  65. pass
  66. def MediaPlayerEncounteredError_cb(self, event):
  67. # self.set_vlc_source_error_count(self.get_vlc_source_error_count() + 1)
  68. # if self.get_vlc_source_error_count() > VLC_SOURCE_ERROR_COUNT:
  69. # self.set_vlc_source_error_count(0)
  70. # return
  71. # else:
  72. # log.logger.error("MediaPlayerEncounteredError_cb")
  73. # sleep(2)
  74. # log.logger.info("Replay uri:%s" % (self.current_uri))
  75. # t = threading.Thread(target = self.player.play, args = (self.current_uri,))
  76. # t.setDaemon(True)
  77. # t.start()
  78. # log.logger.info("Replay finished!")
  79. log.logger.error("Player play error")
  80. def VlmMediaInstanceStatusError_cb(self, event):
  81. log.logger.error("VlmMediaInstanceStatusError_cb")
  82. """
  83. desc:
  84. 开始播放事件回调,控制声音渐入
  85. """
  86. def MediaPlayerPlaying_cb(self, event):
  87. log.logger.error("MediaPlayerPlaying_cb")
  88. t = threading.Thread(target = self.FadeIn, args = (0, self.player_current_volume,))
  89. t.setDaemon(True)
  90. t.start()
  91. log.logger.info("Set volume FadeIn finished!")
  92. def MediaPlayerPaused_cb(self, event):
  93. log.logger.error("MediaPlayerPaused_cb")
  94. def MediaPlayerStopped_cb(self, event):
  95. log.logger.error("MediaPlayerStopped_cb")
  96. """
  97. desc:
  98. 播放器声音渐入实现函数,从low_volume音量过渡到high_volume
  99. Parameters:
  100. param1 - 低音量
  101. param2 - 高音量
  102. """
  103. def FadeIn(self, low_volume, high_volume):
  104. self.player.set_volume(low_volume)
  105. per_volume = (high_volume - low_volume)//10
  106. tmp_volume = low_volume + per_volume
  107. i = 0
  108. while i < 10:
  109. sleep(0.1)
  110. if i == 9:
  111. self.player.set_volume(high_volume)
  112. else:
  113. self.player.set_volume(tmp_volume)
  114. tmp_volume = tmp_volume + per_volume
  115. i = i + 1
  116. return True
  117. """
  118. desc:
  119. 播放器声音渐出实现函数,从high_volume音量过渡到low_volume
  120. Parameters:
  121. param1 - 高音量
  122. param2 - 低音量
  123. """
  124. def FadeOut(self, low_volume, high_volume):
  125. per_volume = (high_volume - low_volume)//10
  126. tmp_volume = high_volume - per_volume
  127. i = 0
  128. while i < 9:
  129. sleep(0.1)
  130. if i == 8:
  131. self.player.set_volume(low_volume)
  132. else:
  133. self.player.set_volume(tmp_volume)
  134. tmp_volume = tmp_volume - per_volume
  135. i = i + 1
  136. return True
  137. """
  138. desc:
  139. 系统信号处理函数,主要处理键盘输入,如ctrl+c等
  140. Parameters:
  141. param1 - 信号数字
  142. param2 - 处理方法
  143. """
  144. def singal_handler(self, singal_num, handler):
  145. self.mqttClient.disconnect()
  146. self.connect_flags = 0
  147. self.stop_pull_rtsp()
  148. log.logger.info("Mqtt disconnect now!")
  149. sys.exit(0)
  150. """
  151. desc:
  152. 设置播放器警告日志数量
  153. Parameters:
  154. param1 - 数量
  155. """
  156. def set_vlc_warning_count(self, value):
  157. self.warning_lock.acquire()
  158. self.vlc_warning_count = value
  159. self.warning_lock.release()
  160. def get_vlc_warning_count(self):
  161. return self.vlc_warning_count
  162. def set_vlc_error_count(self, value):
  163. self.error_lock.acquire()
  164. self.vlc_error_count = value
  165. self.error_lock.release()
  166. def get_vlc_error_count(self):
  167. return self.vlc_error_count
  168. def set_vlc_source_error_count(self, value):
  169. self.source_error_lock.acquire()
  170. self.vlc_source_error_count = value
  171. self.source_error_lock.release()
  172. def get_vlc_source_error_count(self):
  173. return self.vlc_source_error_count
  174. """
  175. desc:
  176. 连接mqtt服务器
  177. """
  178. def run(self):
  179. while self.connect_flags:
  180. try:
  181. log.logger.info("Mqtt to %s:%d, connectting..." % (self.server_ipaddr, SERVER_MQTT_PORT))
  182. self.mqttClient.connect(self.server_ipaddr, SERVER_MQTT_PORT, MQTT_CONNECT_TIMEOUT)
  183. self.connect_flags = 0
  184. except:
  185. log.logger.error("Mqtt connect to %s:%d error" % (self.server_ipaddr, SERVER_MQTT_PORT))
  186. log.logger.error("Mqtt reconnect...")
  187. sleep(RETRY_SEC)
  188. self.mqttClient.loop_start()
  189. self.mqttClient.on_message = register.recieve_run
  190. self.mqttClient.on_subcribe = self.on_subcribe
  191. self.mqttClient.on_disconnect = self.on_disconnect
  192. self.mqttClient.on_publish = self.on_publish
  193. self.mqttClient.on_socket_close = self.on_socket_close
  194. self.mqttClient.subscribe(TOPIC_COMMAND, MQTT_QOS)
  195. return True
  196. """
  197. desc:
  198. mqtt连接事件回调函数
  199. """
  200. def on_connect(self, client, userdata, flags, rc):
  201. log.logger.info(mqtt.connack_string(rc))
  202. self._player_reconnect()
  203. init_data = self._get_init_data()
  204. self.mqttClient.publish(TOPIC_EVENT, json.dumps(init_data).encode("utf-8"), MQTT_QOS)
  205. log.logger.info("Publish data: %s" % (init_data))
  206. return True
  207. """
  208. desc:
  209. 播放器重新连接
  210. """
  211. def _player_reconnect(self):
  212. if self.current_uri != "" and self.init_player_state == 1:
  213. self.player.play(self.current_uri)
  214. log.logger.info("Player reconnect finished!")
  215. sleep(1)
  216. else:
  217. log.logger.info("Not need reconnect!")
  218. return True
  219. """
  220. desc:
  221. 组装mqtt连接到的服务器时需上报的初始化数据
  222. """
  223. def _get_init_data(self):
  224. init_data = {}
  225. data = {}
  226. data.setdefault(SOLF_VOLUME, self.player_current_volume)
  227. data.setdefault(HARD_VOLUME, self.hard_volume)
  228. data.setdefault("hard-volume-control", self.hard_volume_control)
  229. data.setdefault(EXTEN, self.exten)
  230. data.setdefault(DEVICE_MODEL, self.model)
  231. status = self.player.get_state()
  232. if status == 1:
  233. data.setdefault(STATUS, PLAYER_PLAYING)
  234. data.setdefault("url", self.current_uri)
  235. elif status == 0:
  236. data.setdefault(STATUS, PLAYER_PAUSE)
  237. elif status == -1:
  238. data.setdefault(STATUS, PLAYER_IDLE)
  239. else:
  240. data.setdefault(STATUS, PLAYER_ERROR)
  241. init_data.setdefault(ACTION_NAME, "init")
  242. init_data.setdefault(DATA, data)
  243. return init_data
  244. """
  245. desc:
  246. 设置播放器音量
  247. Parameters:
  248. param1 - 音量
  249. param2 - 类型
  250. """
  251. def set_soft_volume(self, volume, type = "default"):
  252. old_volume = self.player_current_volume
  253. self.player_current_volume = volume
  254. project_cf.read(PROJECT_CONFIG)
  255. project_cf.set("general", "player_init_volume", str(volume))
  256. with open(PROJECT_CONFIG, "w+") as f:
  257. project_cf.write(f, space_around_delimiters=False)
  258. os.system("sync")
  259. if self.player.get_state() == 1:
  260. if old_volume > volume:
  261. self.FadeOut(volume, old_volume)
  262. elif old_volume < volume:
  263. self.FadeIn(old_volume, volume)
  264. else:
  265. pass
  266. else:
  267. self.player.set_volume(volume)
  268. return True
  269. """
  270. desc:
  271. 设置系统相关数据
  272. Parameters:
  273. param1 - 相关数据
  274. """
  275. def set_service_settings(self, data):
  276. if "soft-volume" in data:
  277. self.player_current_volume = int(data.get("soft-volume"))
  278. if "hard-volume" in data:
  279. self.set_hard_volume(int(data.get("hard-volume")))
  280. if "hard-volume-control" in data:
  281. self.set_hard_volume_control(data.get("hard-volume-control"))
  282. return True
  283. """
  284. desc:
  285. 开始拉取本地摄像头的rtsp流并推送到服务器
  286. Parameters:
  287. param1 - 本地摄像头rtsp地址
  288. param2 - 服务器的rtsp地址
  289. """
  290. def start_pull_rtsp(self, src, dst):
  291. if not src:
  292. log.Logger.error("src not found")
  293. return False
  294. if not dst:
  295. log.Logger.error("dst not found")
  296. return False
  297. tmp_pull_rtsp = self.pull_rtsp
  298. self.stop_pull_rtsp()
  299. self.pull_rtsp = tmp_pull_rtsp
  300. sleep(1)
  301. os.system("%s %s %s > /dev/null 2>&1 &" % (RTSP_PUSHER, src, dst))
  302. log.logger.info("%s %s %s > /dev/null 2>&1 &" % (RTSP_PUSHER, src, dst))
  303. self.pull_rtsp = self.pull_rtsp + 1
  304. log.logger.info("self.pull_rtsp:%d" % (self.pull_rtsp))
  305. return True
  306. """
  307. desc:
  308. 停止拉取本地摄像头的rtsp流
  309. """
  310. def stop_pull_rtsp(self):
  311. os.system("/usr/bin/killall rtsp_pull_push > /dev/null 2>&1 &")
  312. self.pull_rtsp = 0
  313. return True
  314. """
  315. desc:
  316. 设置系统的音量
  317. Parameters:
  318. param1 - 系统音量值
  319. """
  320. def set_hard_volume(self, volume):
  321. if volume and volume >= 0 and volume <= 10:
  322. real_hard_volume = volume * 5 + 75
  323. try:
  324. os.system("/usr/bin/amixer sset 'Headphone',0 %d > /dev/null 2>&1 &" % (real_hard_volume))
  325. speaker_cf.read(SPEAKER_CONFIG_FILE)
  326. speaker_cf.set("system", "volume_out", str(volume))
  327. with open(SPEAKER_CONFIG_FILE, "w+") as f:
  328. speaker_cf.write(f, space_around_delimiters=False)
  329. os.system("sync")
  330. self.hard_volume = volume
  331. except ERROR:
  332. log.logger.error("Set hard volume error:\n%s" % (ERROR))
  333. return False
  334. else:
  335. log.logger.error("Parameter volume not exist")
  336. return False
  337. return True
  338. """
  339. desc:
  340. 设置强控,不允许通过硬件按键修改系统音量
  341. Parameters:
  342. param1 - 强控开关
  343. """
  344. def set_hard_volume_control(self, value):
  345. if value:
  346. try:
  347. speaker_cf.read(SPEAKER_CONFIG_FILE)
  348. speaker_cf.set("volume", "hard_volume_control", value)
  349. with open(SPEAKER_CONFIG_FILE, "w+") as f:
  350. speaker_cf.write(f, space_around_delimiters=False)
  351. os.system("sync")
  352. self.hard_volume_control = value
  353. except ERROR:
  354. log.logger.error("Set hard volume control value:\n%s" % (ERROR))
  355. return False
  356. else:
  357. log.logger.error("Set hard volume control value not exist")
  358. return False
  359. return True
  360. """
  361. desc:
  362. 设置系统分机信息
  363. Parameters:
  364. param1 - 服务器地址
  365. param2 - 用户名(分机号)
  366. param3 - 分机密码
  367. """
  368. def set_exten(self, host, exten, password):
  369. if host and exten and password:
  370. try:
  371. linphone_cf.read(LINPHONE_CONFIG_FILE)
  372. linphone_cf.set("auth_info_0", "username", exten)
  373. linphone_cf.set("auth_info_0", "passwd", password)
  374. reg_proxy = "<sip:%s>" % (host)
  375. reg_identity = "sip:%s@%s" % (exten, host)
  376. linphone_cf.set("proxy_0", "reg_proxy", reg_proxy)
  377. linphone_cf.set("proxy_0", "reg_identity", reg_identity)
  378. with open(LINPHONE_CONFIG_FILE, "w+") as f:
  379. linphone_cf.write(f, space_around_delimiters=False)
  380. os.system("sync")
  381. except ERROR:
  382. log.logger.error("Set exten error:\n%s" % (ERROR))
  383. return False
  384. else:
  385. log.logger.error("Set exten value not full")
  386. return False
  387. return True
  388. """
  389. desc:
  390. vlc播放指定数据
  391. Parameters:
  392. param1 - 播放数据
  393. """
  394. def play(self, data):
  395. if "url" in data:
  396. url = data.get("url")
  397. else:
  398. log.logger.warning("Url not found!")
  399. return False
  400. if "soft-volume" in data:
  401. self.set_soft_volume(int(data.get("soft-volume")))
  402. if "hard-volume" in data:
  403. self.set_hard_volume(int(data.get("hard-volume")))
  404. if "hard-volume-control" in data:
  405. self.set_hard_volume_control(data.get("hard-volume-control"))
  406. try:
  407. self.init_player_state = 1
  408. self.current_uri = url
  409. self.play_action_flag = 0
  410. try:
  411. project_cf.read(PROJECT_CONFIG)
  412. project_cf.set("general", "init_player_state", str(self.init_player_state))
  413. project_cf.set("general", "current_uri", str(self.current_uri))
  414. with open(PROJECT_CONFIG, "w+") as f:
  415. project_cf.write(f, space_around_delimiters=False)
  416. os.system("sync")
  417. except ERROR:
  418. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  419. return False
  420. self.player.stop()
  421. self.player.play(url)
  422. except:
  423. log.logger.error("Player play uri:%s is error" % (url))
  424. return False
  425. return True
  426. """
  427. desc:
  428. 播放器暂停
  429. Parameters:
  430. param1 - 暂停模式
  431. """
  432. def pause(self, value):
  433. self.init_player_state = 0
  434. if value == "stop":
  435. try:
  436. self.player.stop()
  437. except:
  438. log.logger.error("Player pause(stop) is error")
  439. else:
  440. try:
  441. self.player.pause()
  442. except:
  443. log.logger.error("Player pause is error")
  444. try:
  445. project_cf.read(PROJECT_CONFIG)
  446. project_cf.set("general", "init_player_state", str(self.init_player_state))
  447. with open(PROJECT_CONFIG, "w+") as f:
  448. project_cf.write(f, space_around_delimiters=False)
  449. os.system("sync")
  450. except ERROR:
  451. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  452. return True
  453. """
  454. desc:
  455. 播放器恢复播放
  456. Parameters:
  457. param1 - 恢复数据
  458. """
  459. def resume(self, value):
  460. self.init_player_state = 1
  461. try:
  462. project_cf.read(PROJECT_CONFIG)
  463. project_cf.set("general", "init_player_state", str(self.init_player_state))
  464. with open(PROJECT_CONFIG, "w+") as f:
  465. project_cf.write(f, space_around_delimiters=False)
  466. os.system("sync")
  467. except ERROR:
  468. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  469. if self.player.get_state() == 1:
  470. return True
  471. if value == "play":
  472. data = {}
  473. data.setdefault("url", self.current_uri)
  474. try:
  475. self.play(data)
  476. except:
  477. log.logger.error("Player resume(play) is error")
  478. else:
  479. try:
  480. self.player.resume()
  481. except:
  482. log.logger.error("Player resume is error")
  483. return True
  484. """
  485. desc:
  486. 播放器停止播放
  487. """
  488. def stop(self):
  489. self.init_player_state = -1
  490. self.current_uri = ""
  491. self.play_action_flag = 0
  492. try:
  493. self.FadeOut(0, self.player_current_volume)
  494. self.player.stop()
  495. try:
  496. project_cf.read(PROJECT_CONFIG)
  497. project_cf.set("general", "init_player_state", str(self.init_player_state))
  498. project_cf.set("general", "current_uri", "")
  499. with open(PROJECT_CONFIG, "w+") as f:
  500. project_cf.write(f, space_around_delimiters=False)
  501. os.system("sync")
  502. except ERROR:
  503. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  504. return False
  505. except:
  506. log.logger.error("Player stop is error")
  507. return False
  508. return True
  509. def on_subcribe(self, client, userdata, mid, granted_qos):
  510. pass
  511. def on_publish(self, client, userdata, mid):
  512. pass
  513. def on_disconnect(self, client, userdata, rc):
  514. log.logger.info("Mqtt disconnect!")
  515. """
  516. desc:
  517. 检测系统的必要配置信息
  518. """
  519. def _check_config(self):
  520. if not self.model:
  521. log.logger.error("Model not found!")
  522. return False
  523. elif not self.mac:
  524. log.logger.error("Mac not found!")
  525. return False
  526. elif not self.server_ipaddr:
  527. log.logger.error("Server Ipaddr not found!")
  528. return False
  529. elif not self.exten:
  530. log.logger.error("Extension not found!")
  531. return False
  532. elif self.hard_volume < 0:
  533. log.logger.error("Hard Volume error!")
  534. return False
  535. else:
  536. return True
  537. """
  538. desc:
  539. 监测系统状态变化,如音量,播放状态等
  540. """
  541. def _check_variable(self):
  542. old_soft_volume = self.player_current_volume
  543. old_hard_volume = self.hard_volume
  544. old_exten = self.exten
  545. old_player_state = self.player.get_state()
  546. old_hard_volume_control = self.hard_volume_control
  547. old_pull_rtsp = self.pull_rtsp
  548. while True:
  549. sleep(CHECK_INTERVAL)
  550. data = {}
  551. event = {}
  552. event.setdefault(ACTION_NAME, TOPIC_UPDATE)
  553. event.setdefault(DATA, data)
  554. new_soft_volume = self.player_current_volume
  555. if old_soft_volume != new_soft_volume:
  556. old_soft_volume = new_soft_volume
  557. data.setdefault(SOLF_VOLUME, new_soft_volume)
  558. project_cf.read(PROJECT_CONFIG)
  559. new_hard_volume = int(speaker_cf.get("system", "volume_out"))
  560. if old_hard_volume != new_hard_volume:
  561. old_hard_volume = new_hard_volume
  562. data.setdefault(HARD_VOLUME, new_hard_volume)
  563. linphone_cf.read(LINPHONE_CONFIG_FILE)
  564. new_exten = linphone_cf.get("auth_info_0", "username")
  565. if old_exten != new_exten:
  566. old_exten = new_exten
  567. data.setdefault(EXTEN, new_exten)
  568. if self.init_player_state == 1 and self.player.get_state() != 1:
  569. if self.current_uri != "":
  570. self._player_reconnect()
  571. new_player_state = self.player.get_state()
  572. if old_player_state != new_player_state and self.play_action_flag == 0:
  573. old_player_state = new_player_state
  574. if new_player_state == -1:
  575. player_status = PLAYER_IDLE
  576. elif new_player_state == 0:
  577. player_status = PLAYER_PAUSE
  578. elif new_player_state == 1:
  579. player_status = PLAYER_PLAYING
  580. else:
  581. player_status = PLAYER_ERROR
  582. data.setdefault(STATUS, player_status)
  583. speaker_cf.read(SPEAKER_CONFIG_FILE)
  584. new_hard_volume_control = speaker_cf.get("volume", "hard_volume_control")
  585. if old_hard_volume_control != new_hard_volume_control:
  586. old_hard_volume_control = new_hard_volume_control
  587. data.setdefault(HARD_VOLUME_CONTROL, new_hard_volume_control)
  588. new_pull_rtsp = self.pull_rtsp
  589. if old_pull_rtsp != new_pull_rtsp:
  590. old_pull_rtsp = new_pull_rtsp
  591. if new_pull_rtsp != 0:
  592. data.setdefault("pull-rtsp", 1)
  593. else:
  594. data.setdefault("pull-rtsp", 0)
  595. if event.get(DATA) != {}:
  596. try:
  597. self.mqttClient.publish(TOPIC_EVENT, json.dumps(event).encode('utf-8'), MQTT_QOS)
  598. log.logger.info("Publish event:%s" % (event))
  599. except ERROR:
  600. log.logger.error("Publish event:\n%s" % (ERROR))
  601. """
  602. desc:
  603. 监测fifo
  604. """
  605. def _fifo(self):
  606. if not os.path.exists(FIFO_PATH):
  607. os.mkfifo(FIFO_PATH)
  608. rf = os.open(FIFO_PATH, os.O_RDWR)
  609. while True:
  610. data = os.read(rf, 64)
  611. data = data.decode("utf-8")
  612. log.logger.info("received msg from fifo:%s" % (data))
  613. if data == "0":
  614. if self.player.get_state() == 1:
  615. self.play_action_flag = 1
  616. self.tmp_init_player_state = self.init_player_state
  617. self.init_player_state = 0
  618. self.player.stop()
  619. elif data == "1":
  620. self.play_action_flag = 0
  621. self.init_player_state = self.tmp_init_player_state
  622. else:
  623. log.logger.error("No support this action!")
  624. os.close(rf)
  625. """
  626. desc:
  627. 启动监测线程
  628. """
  629. def check_start(self):
  630. try:
  631. t = threading.Thread(target = self._check_variable, args = ())
  632. t.setDaemon(True)
  633. t.start()
  634. return True
  635. except ERROR:
  636. log.logger.error(ERROR)
  637. """
  638. desc:
  639. 启动fifo监测
  640. """
  641. def check_fifo_start(self):
  642. try:
  643. t = threading.Thread(target = self._fifo, args = ())
  644. t.setDaemon(True)
  645. t.start()
  646. return True
  647. except ERROR:
  648. log.logger.error(ERROR)
  649. """
  650. desc:
  651. socket断线回调
  652. """
  653. def on_socket_close(self, client, userdata, sock):
  654. log.logger.error("Socket error(disconnect) occur!")