CPpost.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # This file create two thread, t1 capturing rstp video and save frame every interval, t2 read frame from database and
  2. # post to https server.
  3. # 摄像机图片上传
  4. import configparser
  5. import time
  6. import pymysql
  7. import requests
  8. from requests_toolbelt.multipart.encoder import MultipartEncoder
  9. from mysqlDataBase import MysqldbOperational
  10. import LogOut
  11. import os
  12. def post(cameraList):
  13. headers = {
  14. "Authorization": token,
  15. "app-id": appId}
  16. timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
  17. for x in range(0, len(cameraList)):
  18. try:
  19. picture_name = '%s.jpg' % cameraList[x][0]
  20. last_change_time = os.path.getmtime(picture_name) # 最后一次修改时间
  21. time_diff = (time.time() - last_change_time) / 60 # 计算时间差(分钟)
  22. if time_diff <= 60:
  23. f = open(picture_name, 'rb')
  24. img = f.read()
  25. img = pymysql.Binary(img)
  26. m = MultipartEncoder({'file': (picture_name, img, 'image/jpeg'),
  27. "timestamp": str(timestamp),
  28. "token": token,
  29. "isImage": "1",
  30. "cameraId": cameraList[x][4]})
  31. headers["Content-Type"] = m.content_type
  32. requests.urllib3.disable_warnings()
  33. ret = requests.post('https://management.super-sight.com.cn:8000/system/api/v1/camera/add',
  34. headers=headers,
  35. data=m, verify=False)
  36. if ret.status_code == 200:
  37. print(timestamp, "post SUCCESS:", ret.text, ret.status_code, picture_name)
  38. else:
  39. print(timestamp, "post ERROR:", ret.text, ret.status_code, picture_name)
  40. else:
  41. print(f"{timestamp}-[{picture_name}]: {int(time_diff / 60)}小时未更新")
  42. except Exception as e:
  43. print(timestamp, "post ERROE", e)
  44. def post_extream(cameraList, startDate, endDate):
  45. headers = {
  46. "Authorization": token,
  47. # 0cdf072c-7594-409d-a036-a5716d39b1f8",
  48. "Content-Type": "application/json"}
  49. for x in range(0, len(cameraList)):
  50. data = {
  51. "token": token,
  52. "appId": appId,
  53. "cameraId": cameraList[x][4],
  54. "start": startDate,
  55. "end": endDate
  56. }
  57. print(cameraList[x][4], startDate, endDate)
  58. requests.urllib3.disable_warnings()
  59. ret = requests.post('https://api.super-sight.com.cn:8000/system/v1/videoReceive/receiveVideoEmergency',
  60. headers=headers, json=data, verify=False)
  61. if ret.status_code == 200:
  62. print(ret.text, ret.status_code)
  63. else:
  64. print(ret.text, ret.status_code)
  65. def posterize_strategy():
  66. lastTime = 0
  67. lastTime1 = 0
  68. lastTime2 = 0
  69. while True:
  70. time.sleep(1)
  71. newTime = int(time.time())
  72. timestamp = int(time.strftime('%H'))
  73. timeNow = int(time.time())
  74. if lastTime1 + 10 <= newTime:
  75. camera_list = read_camera_configuration()
  76. strategy_list = read_strategy()
  77. modelKind = strategy_list[0][0]
  78. posterizeKind = strategy_list[0][1]
  79. posterizeStartDate = strategy_list[0][4]
  80. posterizeEndDate = strategy_list[0][5]
  81. posterizeUnit = strategy_list[0][2]
  82. posterizeTime = strategy_list[0][3]
  83. is_read()
  84. lastTime1 = newTime
  85. if int(modelKind) == 1:
  86. if posterizeUnit == 'H':
  87. _posterizeTime = posterizeTime * 60 * 60
  88. if lastTime2 + _posterizeTime <= timeNow:
  89. startTime = timeNow
  90. endTime = timeNow + _posterizeTime
  91. post_extream(camera_list, startTime, endTime)
  92. lastTime2 = timeNow + 1
  93. elif posterizeUnit == 'M':
  94. _posterizeTime = posterizeTime * 60
  95. if lastTime2 + _posterizeTime <= timeNow:
  96. startTime = timeNow
  97. endTime = timeNow + _posterizeTime
  98. post_extream(camera_list, startTime, endTime)
  99. lastTime2 = timeNow + 1
  100. elif posterizeUnit == 'S':
  101. _posterizeTime = posterizeTime
  102. if lastTime2 + _posterizeTime <= timeNow:
  103. startTime = timeNow
  104. endTime = timeNow + _posterizeTime
  105. post_extream(camera_list, startTime, endTime)
  106. lastTime2 = timeNow + 1
  107. elif int(modelKind) == 2:
  108. if int(posterizeKind) == 1:
  109. if posterizeUnit == 'H':
  110. _posterizeTime = posterizeTime * 60 * 60
  111. if lastTime + _posterizeTime <= newTime:
  112. post(camera_list)
  113. lastTime = newTime
  114. elif posterizeUnit == 'M':
  115. _posterizeTime = posterizeTime * 60
  116. if lastTime + _posterizeTime <= newTime:
  117. print('---> post')
  118. post(camera_list)
  119. print('post down----')
  120. lastTime = newTime
  121. elif posterizeUnit == 'S':
  122. _posterizeTime = posterizeTime
  123. if lastTime + _posterizeTime <= newTime:
  124. post(camera_list)
  125. lastTime = newTime
  126. elif int(posterizeKind) == 2:
  127. if timestamp >= int(posterizeStartDate) and timestamp <= int(posterizeEndDate):
  128. if posterizeUnit == 'H':
  129. _posterizeTime = posterizeTime * 60 * 60
  130. if lastTime + _posterizeTime <= newTime:
  131. post(camera_list)
  132. lastTime = newTime
  133. elif posterizeUnit == 'M':
  134. _posterizeTime = posterizeTime * 60
  135. if lastTime + _posterizeTime <= newTime:
  136. post(camera_list)
  137. lastTime = newTime
  138. elif posterizeUnit == 'S':
  139. _posterizeTime = posterizeTime
  140. if lastTime + _posterizeTime <= newTime:
  141. post(camera_list)
  142. lastTime = newTime
  143. elif int(posterizeKind) == 3:
  144. pass
  145. def read_camera_configuration():
  146. list_camera = cameraSql.select('camera_configuration_table',
  147. fields=["camera_name", "camera_ip", "camera_pass", "order_no", "camera_id"])
  148. return list_camera
  149. def read_strategy():
  150. list_strategy = cameraSql.select('camera_strategy_table',
  151. fields=["model_kind", "posterize_kind", "posterize_unit", "posterize_time",
  152. "posterize_start_date", "posterize_end_date"])
  153. return list_strategy
  154. def is_read():
  155. cond_dict = {"conf_type": 'camera_capture'}
  156. params = {"is_read": "1"}
  157. try:
  158. cameraSql.update('camera_strategy_table', params, cond_dict)
  159. return 'Success'
  160. except:
  161. return 'error'
  162. if __name__ == '__main__':
  163. # time.sleep(10)
  164. _logger = LogOut.Log('CPpost')
  165. # 创建读取配置文件对象
  166. config = configparser.ConfigParser()
  167. config.read("config.ini", encoding="utf-8")
  168. # 获取通用配置项
  169. section = "General" # 读取的section标签
  170. mysql_host = config.get(section, 'mysqlHost')
  171. mysql_username = config.get(section, 'mysqlUsername')
  172. mysql_password = config.get(section, 'mysqlPassword')
  173. mysql_port = config.getint(section, 'mysqlPort')
  174. token = config.get(section, 'token')
  175. appId = config.get(section, 'appId')
  176. # 获取特有配置项
  177. section = 'CPpost'
  178. mysql_database = config.get(section, 'mysqlDatabase')
  179. cameraSql = MysqldbOperational(host=mysql_host,
  180. username=mysql_username,
  181. password=mysql_password,
  182. port=mysql_port,
  183. database=mysql_database,
  184. logger=_logger)
  185. posterize_strategy()