device.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. os.system("/etc/scripts/pa_mute.sh 0 > /dev/null 2>&1 &")
  214. self.player.play(self.current_uri)
  215. log.logger.info("Player reconnect finished!")
  216. sleep(1)
  217. else:
  218. log.logger.info("Not need reconnect!")
  219. return True
  220. """
  221. desc:
  222. 组装mqtt连接到的服务器时需上报的初始化数据
  223. """
  224. def _get_init_data(self):
  225. init_data = {}
  226. data = {}
  227. data.setdefault(SOLF_VOLUME, self.player_current_volume)
  228. data.setdefault(HARD_VOLUME, self.hard_volume)
  229. data.setdefault("hard-volume-control", self.hard_volume_control)
  230. data.setdefault(EXTEN, self.exten)
  231. data.setdefault(DEVICE_MODEL, self.model)
  232. status = self.player.get_state()
  233. if status == 1:
  234. data.setdefault(STATUS, PLAYER_PLAYING)
  235. data.setdefault("url", self.current_uri)
  236. elif status == 0:
  237. data.setdefault(STATUS, PLAYER_PAUSE)
  238. elif status == -1:
  239. data.setdefault(STATUS, PLAYER_IDLE)
  240. else:
  241. data.setdefault(STATUS, PLAYER_ERROR)
  242. init_data.setdefault(ACTION_NAME, "init")
  243. init_data.setdefault(DATA, data)
  244. return init_data
  245. """
  246. desc:
  247. 设置播放器音量
  248. Parameters:
  249. param1 - 音量
  250. param2 - 类型
  251. """
  252. def set_soft_volume(self, volume, type = "default"):
  253. old_volume = self.player_current_volume
  254. self.player_current_volume = volume
  255. project_cf.read(PROJECT_CONFIG)
  256. project_cf.set("general", "player_init_volume", str(volume))
  257. with open(PROJECT_CONFIG, "w+") as f:
  258. project_cf.write(f, space_around_delimiters=False)
  259. os.system("sync")
  260. if self.player.get_state() == 1:
  261. if old_volume > volume:
  262. self.FadeOut(volume, old_volume)
  263. elif old_volume < volume:
  264. self.FadeIn(old_volume, volume)
  265. else:
  266. pass
  267. else:
  268. self.player.set_volume(volume)
  269. return True
  270. """
  271. desc:
  272. 设置系统相关数据
  273. Parameters:
  274. param1 - 相关数据
  275. """
  276. def set_service_settings(self, data):
  277. if "soft-volume" in data:
  278. self.player_current_volume = int(data.get("soft-volume"))
  279. if "hard-volume" in data:
  280. self.set_hard_volume(int(data.get("hard-volume")))
  281. if "hard-volume-control" in data:
  282. self.set_hard_volume_control(data.get("hard-volume-control"))
  283. return True
  284. """
  285. desc:
  286. 开始拉取本地摄像头的rtsp流并推送到服务器
  287. Parameters:
  288. param1 - 本地摄像头rtsp地址
  289. param2 - 服务器的rtsp地址
  290. """
  291. def start_pull_rtsp(self, src, dst):
  292. if not src:
  293. log.Logger.error("src not found")
  294. return False
  295. if not dst:
  296. log.Logger.error("dst not found")
  297. return False
  298. tmp_pull_rtsp = self.pull_rtsp
  299. self.stop_pull_rtsp()
  300. self.pull_rtsp = tmp_pull_rtsp
  301. sleep(1)
  302. os.system("%s %s %s > /dev/null 2>&1 &" % (RTSP_PUSHER, src, dst))
  303. log.logger.info("%s %s %s > /dev/null 2>&1 &" % (RTSP_PUSHER, src, dst))
  304. self.pull_rtsp = self.pull_rtsp + 1
  305. log.logger.info("self.pull_rtsp:%d" % (self.pull_rtsp))
  306. return True
  307. """
  308. desc:
  309. 停止拉取本地摄像头的rtsp流
  310. """
  311. def stop_pull_rtsp(self):
  312. os.system("/usr/bin/killall rtsp_pull_push > /dev/null 2>&1 &")
  313. self.pull_rtsp = 0
  314. return True
  315. """
  316. desc:
  317. 设置系统的音量
  318. Parameters:
  319. param1 - 系统音量值
  320. """
  321. def set_hard_volume(self, volume):
  322. if volume and volume >= 0 and volume <= 10:
  323. real_hard_volume = volume * 10 + 9
  324. try:
  325. os.system("/usr/bin/amixer -q sset 'Master',0 %d > /dev/null 2>&1 &" % (real_hard_volume))
  326. speaker_cf.read(SPEAKER_CONFIG_FILE)
  327. speaker_cf.set("system", "volume_out", str(volume))
  328. with open(SPEAKER_CONFIG_FILE, "w+") as f:
  329. speaker_cf.write(f, space_around_delimiters=False)
  330. os.system("sync")
  331. self.hard_volume = volume
  332. except ERROR:
  333. log.logger.error("Set hard volume error:\n%s" % (ERROR))
  334. return False
  335. else:
  336. log.logger.error("Parameter volume not exist")
  337. return False
  338. return True
  339. """
  340. desc:
  341. 设置强控,不允许通过硬件按键修改系统音量
  342. Parameters:
  343. param1 - 强控开关
  344. """
  345. def set_hard_volume_control(self, value):
  346. if value:
  347. try:
  348. speaker_cf.read(SPEAKER_CONFIG_FILE)
  349. speaker_cf.set("volume", "hard_volume_control", value)
  350. with open(SPEAKER_CONFIG_FILE, "w+") as f:
  351. speaker_cf.write(f, space_around_delimiters=False)
  352. os.system("sync")
  353. self.hard_volume_control = value
  354. except ERROR:
  355. log.logger.error("Set hard volume control value:\n%s" % (ERROR))
  356. return False
  357. else:
  358. log.logger.error("Set hard volume control value not exist")
  359. return False
  360. return True
  361. """
  362. desc:
  363. 设置系统分机信息
  364. Parameters:
  365. param1 - 服务器地址
  366. param2 - 用户名(分机号)
  367. param3 - 分机密码
  368. """
  369. def set_exten(self, host, exten, password):
  370. if host and exten and password:
  371. try:
  372. linphone_cf.read(LINPHONE_CONFIG_FILE)
  373. linphone_cf.set("auth_info_0", "username", exten)
  374. linphone_cf.set("auth_info_0", "passwd", password)
  375. reg_proxy = "<sip:%s>" % (host)
  376. reg_identity = "sip:%s@%s" % (exten, host)
  377. linphone_cf.set("proxy_0", "reg_proxy", reg_proxy)
  378. linphone_cf.set("proxy_0", "reg_identity", reg_identity)
  379. with open(LINPHONE_CONFIG_FILE, "w+") as f:
  380. linphone_cf.write(f, space_around_delimiters=False)
  381. os.system("sync")
  382. except ERROR:
  383. log.logger.error("Set exten error:\n%s" % (ERROR))
  384. return False
  385. else:
  386. log.logger.error("Set exten value not full")
  387. return False
  388. return True
  389. """
  390. desc:
  391. vlc播放指定数据
  392. Parameters:
  393. param1 - 播放数据
  394. """
  395. def play(self, data):
  396. if "url" in data:
  397. url = data.get("url")
  398. else:
  399. log.logger.warning("Url not found!")
  400. return False
  401. if "soft-volume" in data:
  402. self.set_soft_volume(int(data.get("soft-volume")))
  403. if "hard-volume" in data:
  404. self.set_hard_volume(int(data.get("hard-volume")))
  405. if "hard-volume-control" in data:
  406. self.set_hard_volume_control(data.get("hard-volume-control"))
  407. os.system("/etc/scripts/pa_mute.sh 0 > /dev/null 2>&1 &")
  408. try:
  409. self.init_player_state = 1
  410. self.current_uri = url
  411. self.play_action_flag = 0
  412. try:
  413. project_cf.read(PROJECT_CONFIG)
  414. project_cf.set("general", "init_player_state", str(self.init_player_state))
  415. project_cf.set("general", "current_uri", str(self.current_uri))
  416. with open(PROJECT_CONFIG, "w+") as f:
  417. project_cf.write(f, space_around_delimiters=False)
  418. os.system("sync")
  419. except ERROR:
  420. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  421. return False
  422. self.player.stop()
  423. self.player.play(url)
  424. except:
  425. log.logger.error("Player play uri:%s is error" % (url))
  426. return False
  427. return True
  428. """
  429. desc:
  430. 播放器暂停
  431. Parameters:
  432. param1 - 暂停模式
  433. """
  434. def pause(self, value):
  435. self.init_player_state = 0
  436. if value == "stop":
  437. try:
  438. self.player.stop()
  439. except:
  440. log.logger.error("Player pause(stop) is error")
  441. else:
  442. try:
  443. self.player.pause()
  444. except:
  445. log.logger.error("Player pause is error")
  446. try:
  447. project_cf.read(PROJECT_CONFIG)
  448. project_cf.set("general", "init_player_state", str(self.init_player_state))
  449. with open(PROJECT_CONFIG, "w+") as f:
  450. project_cf.write(f, space_around_delimiters=False)
  451. os.system("sync")
  452. except ERROR:
  453. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  454. return True
  455. """
  456. desc:
  457. 播放器恢复播放
  458. Parameters:
  459. param1 - 恢复数据
  460. """
  461. def resume(self, value):
  462. self.init_player_state = 1
  463. try:
  464. project_cf.read(PROJECT_CONFIG)
  465. project_cf.set("general", "init_player_state", str(self.init_player_state))
  466. with open(PROJECT_CONFIG, "w+") as f:
  467. project_cf.write(f, space_around_delimiters=False)
  468. os.system("sync")
  469. except ERROR:
  470. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  471. if self.player.get_state() == 1:
  472. return True
  473. if value == "play":
  474. data = {}
  475. data.setdefault("url", self.current_uri)
  476. try:
  477. self.play(data)
  478. except:
  479. log.logger.error("Player resume(play) is error")
  480. else:
  481. try:
  482. self.player.resume()
  483. except:
  484. log.logger.error("Player resume is error")
  485. return True
  486. """
  487. desc:
  488. 播放器停止播放
  489. """
  490. def stop(self):
  491. self.init_player_state = -1
  492. self.current_uri = ""
  493. self.play_action_flag = 0
  494. try:
  495. self.FadeOut(0, self.player_current_volume)
  496. self.player.stop()
  497. try:
  498. project_cf.read(PROJECT_CONFIG)
  499. project_cf.set("general", "init_player_state", str(self.init_player_state))
  500. project_cf.set("general", "current_uri", "")
  501. with open(PROJECT_CONFIG, "w+") as f:
  502. project_cf.write(f, space_around_delimiters=False)
  503. os.system("sync")
  504. except ERROR:
  505. log.logger.error("Save init player state or current uri error:\n%s" % (ERROR))
  506. return False
  507. except:
  508. log.logger.error("Player stop is error")
  509. return False
  510. os.system("/etc/scripts/pa_mute.sh 1 > /dev/null 2>&1 &")
  511. return True
  512. def on_subcribe(self, client, userdata, mid, granted_qos):
  513. pass
  514. def on_publish(self, client, userdata, mid):
  515. pass
  516. def on_disconnect(self, client, userdata, rc):
  517. log.logger.info("Mqtt disconnect!")
  518. """
  519. desc:
  520. 检测系统的必要配置信息
  521. """
  522. def _check_config(self):
  523. if not self.model:
  524. log.logger.error("Model not found!")
  525. return False
  526. elif not self.mac:
  527. log.logger.error("Mac not found!")
  528. return False
  529. elif not self.server_ipaddr:
  530. log.logger.error("Server Ipaddr not found!")
  531. return False
  532. elif not self.exten:
  533. log.logger.error("Extension not found!")
  534. return False
  535. elif self.hard_volume < 0:
  536. log.logger.error("Hard Volume error!")
  537. return False
  538. else:
  539. return True
  540. """
  541. desc:
  542. 监测系统状态变化,如音量,播放状态等
  543. """
  544. def _check_variable(self):
  545. old_soft_volume = self.player_current_volume
  546. old_hard_volume = self.hard_volume
  547. old_exten = self.exten
  548. old_player_state = self.player.get_state()
  549. old_hard_volume_control = self.hard_volume_control
  550. old_pull_rtsp = self.pull_rtsp
  551. while True:
  552. sleep(CHECK_INTERVAL)
  553. data = {}
  554. event = {}
  555. event.setdefault(ACTION_NAME, TOPIC_UPDATE)
  556. event.setdefault(DATA, data)
  557. new_soft_volume = self.player_current_volume
  558. if old_soft_volume != new_soft_volume:
  559. old_soft_volume = new_soft_volume
  560. data.setdefault(SOLF_VOLUME, new_soft_volume)
  561. project_cf.read(PROJECT_CONFIG)
  562. new_hard_volume = int(speaker_cf.get("system", "volume_out"))
  563. if old_hard_volume != new_hard_volume:
  564. old_hard_volume = new_hard_volume
  565. data.setdefault(HARD_VOLUME, new_hard_volume)
  566. linphone_cf.read(LINPHONE_CONFIG_FILE)
  567. new_exten = linphone_cf.get("auth_info_0", "username")
  568. if old_exten != new_exten:
  569. old_exten = new_exten
  570. data.setdefault(EXTEN, new_exten)
  571. if self.init_player_state == 1 and self.player.get_state() != 1:
  572. if self.current_uri != "":
  573. self._player_reconnect()
  574. new_player_state = self.player.get_state()
  575. if old_player_state != new_player_state and self.play_action_flag == 0:
  576. old_player_state = new_player_state
  577. if new_player_state == -1:
  578. player_status = PLAYER_IDLE
  579. elif new_player_state == 0:
  580. player_status = PLAYER_PAUSE
  581. elif new_player_state == 1:
  582. player_status = PLAYER_PLAYING
  583. else:
  584. player_status = PLAYER_ERROR
  585. data.setdefault(STATUS, player_status)
  586. speaker_cf.read(SPEAKER_CONFIG_FILE)
  587. new_hard_volume_control = speaker_cf.get("volume", "hard_volume_control")
  588. if old_hard_volume_control != new_hard_volume_control:
  589. old_hard_volume_control = new_hard_volume_control
  590. data.setdefault(HARD_VOLUME_CONTROL, new_hard_volume_control)
  591. new_pull_rtsp = self.pull_rtsp
  592. if old_pull_rtsp != new_pull_rtsp:
  593. old_pull_rtsp = new_pull_rtsp
  594. if new_pull_rtsp != 0:
  595. data.setdefault("pull-rtsp", 1)
  596. else:
  597. data.setdefault("pull-rtsp", 0)
  598. if event.get(DATA) != {}:
  599. try:
  600. self.mqttClient.publish(TOPIC_EVENT, json.dumps(event).encode('utf-8'), MQTT_QOS)
  601. log.logger.info("Publish event:%s" % (event))
  602. except ERROR:
  603. log.logger.error("Publish event:\n%s" % (ERROR))
  604. """
  605. desc:
  606. 监测fifo
  607. """
  608. def _fifo(self):
  609. if not os.path.exists(FIFO_PATH):
  610. os.mkfifo(FIFO_PATH)
  611. rf = os.open(FIFO_PATH, os.O_RDWR)
  612. while True:
  613. data = os.read(rf, 64)
  614. data = data.decode("utf-8")
  615. log.logger.info("received msg from fifo:%s" % (data))
  616. if data == "0":
  617. if self.player.get_state() == 1:
  618. self.play_action_flag = 1
  619. self.tmp_init_player_state = self.init_player_state
  620. self.init_player_state = 0
  621. self.player.stop()
  622. elif data == "1":
  623. self.play_action_flag = 0
  624. self.init_player_state = self.tmp_init_player_state
  625. else:
  626. log.logger.error("No support this action!")
  627. os.close(rf)
  628. """
  629. desc:
  630. 启动监测线程
  631. """
  632. def check_start(self):
  633. try:
  634. t = threading.Thread(target = self._check_variable, args = ())
  635. t.setDaemon(True)
  636. t.start()
  637. return True
  638. except ERROR:
  639. log.logger.error(ERROR)
  640. """
  641. desc:
  642. 启动fifo监测
  643. """
  644. def check_fifo_start(self):
  645. try:
  646. t = threading.Thread(target = self._fifo, args = ())
  647. t.setDaemon(True)
  648. t.start()
  649. return True
  650. except ERROR:
  651. log.logger.error(ERROR)
  652. """
  653. desc:
  654. socket断线回调
  655. """
  656. def on_socket_close(self, client, userdata, sock):
  657. log.logger.error("Socket error(disconnect) occur!")