123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- # This file create two thread, t1 capturing rstp video and save frame every interval, t2 read frame from database and
- # post to https server.
- # 摄像机图片上传
- import configparser
- import time
- import pymysql
- import requests
- from requests_toolbelt.multipart.encoder import MultipartEncoder
- from mysqlDataBase import MysqldbOperational
- import LogOut
- import os
- def post(cameraList):
- headers = {
- "Authorization": token,
- "app-id": appId}
- timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
- for x in range(0, len(cameraList)):
- try:
- picture_name = '%s.jpg' % cameraList[x][0]
- last_change_time = os.path.getmtime(picture_name) # 最后一次修改时间
- time_diff = (time.time() - last_change_time) / 60 # 计算时间差(分钟)
- if time_diff <= 60:
- f = open(picture_name, 'rb')
- img = f.read()
- img = pymysql.Binary(img)
- m = MultipartEncoder({'file': (picture_name, img, 'image/jpeg'),
- "timestamp": str(timestamp),
- "token": token,
- "isImage": "1",
- "cameraId": cameraList[x][4]})
- headers["Content-Type"] = m.content_type
- requests.urllib3.disable_warnings()
- ret = requests.post('https://management.super-sight.com.cn:8000/system/api/v1/camera/add',
- headers=headers,
- data=m, verify=False)
- if ret.status_code == 200:
- print(timestamp, "post SUCCESS:", ret.text, ret.status_code, picture_name)
- else:
- print(timestamp, "post ERROR:", ret.text, ret.status_code, picture_name)
- else:
- print(f"{timestamp}-[{picture_name}]: {int(time_diff / 60)}小时未更新")
- except Exception as e:
- print(timestamp, "post ERROE", e)
- def post_extream(cameraList, startDate, endDate):
- headers = {
- "Authorization": token,
- # 0cdf072c-7594-409d-a036-a5716d39b1f8",
- "Content-Type": "application/json"}
- for x in range(0, len(cameraList)):
- data = {
- "token": token,
- "appId": appId,
- "cameraId": cameraList[x][4],
- "start": startDate,
- "end": endDate
- }
- print(cameraList[x][4], startDate, endDate)
- requests.urllib3.disable_warnings()
- ret = requests.post('https://api.super-sight.com.cn:8000/system/v1/videoReceive/receiveVideoEmergency',
- headers=headers, json=data, verify=False)
- if ret.status_code == 200:
- print(ret.text, ret.status_code)
- else:
- print(ret.text, ret.status_code)
- def posterize_strategy():
- lastTime = 0
- lastTime1 = 0
- lastTime2 = 0
- while True:
- time.sleep(1)
- newTime = int(time.time())
- timestamp = int(time.strftime('%H'))
- timeNow = int(time.time())
- if lastTime1 + 10 <= newTime:
- camera_list = read_camera_configuration()
- strategy_list = read_strategy()
- modelKind = strategy_list[0][0]
- posterizeKind = strategy_list[0][1]
- posterizeStartDate = strategy_list[0][4]
- posterizeEndDate = strategy_list[0][5]
- posterizeUnit = strategy_list[0][2]
- posterizeTime = strategy_list[0][3]
- is_read()
- lastTime1 = newTime
- if int(modelKind) == 1:
- if posterizeUnit == 'H':
- _posterizeTime = posterizeTime * 60 * 60
- if lastTime2 + _posterizeTime <= timeNow:
- startTime = timeNow
- endTime = timeNow + _posterizeTime
- post_extream(camera_list, startTime, endTime)
- lastTime2 = timeNow + 1
- elif posterizeUnit == 'M':
- _posterizeTime = posterizeTime * 60
- if lastTime2 + _posterizeTime <= timeNow:
- startTime = timeNow
- endTime = timeNow + _posterizeTime
- post_extream(camera_list, startTime, endTime)
- lastTime2 = timeNow + 1
- elif posterizeUnit == 'S':
- _posterizeTime = posterizeTime
- if lastTime2 + _posterizeTime <= timeNow:
- startTime = timeNow
- endTime = timeNow + _posterizeTime
- post_extream(camera_list, startTime, endTime)
- lastTime2 = timeNow + 1
- elif int(modelKind) == 2:
- if int(posterizeKind) == 1:
- if posterizeUnit == 'H':
- _posterizeTime = posterizeTime * 60 * 60
- if lastTime + _posterizeTime <= newTime:
- post(camera_list)
- lastTime = newTime
- elif posterizeUnit == 'M':
- _posterizeTime = posterizeTime * 60
- if lastTime + _posterizeTime <= newTime:
- print('---> post')
- post(camera_list)
- print('post down----')
- lastTime = newTime
- elif posterizeUnit == 'S':
- _posterizeTime = posterizeTime
- if lastTime + _posterizeTime <= newTime:
- post(camera_list)
- lastTime = newTime
- elif int(posterizeKind) == 2:
- if timestamp >= int(posterizeStartDate) and timestamp <= int(posterizeEndDate):
- if posterizeUnit == 'H':
- _posterizeTime = posterizeTime * 60 * 60
- if lastTime + _posterizeTime <= newTime:
- post(camera_list)
- lastTime = newTime
- elif posterizeUnit == 'M':
- _posterizeTime = posterizeTime * 60
- if lastTime + _posterizeTime <= newTime:
- post(camera_list)
- lastTime = newTime
- elif posterizeUnit == 'S':
- _posterizeTime = posterizeTime
- if lastTime + _posterizeTime <= newTime:
- post(camera_list)
- lastTime = newTime
- elif int(posterizeKind) == 3:
- pass
- def read_camera_configuration():
- list_camera = cameraSql.select('camera_configuration_table',
- fields=["camera_name", "camera_ip", "camera_pass", "order_no", "camera_id"])
- return list_camera
- def read_strategy():
- list_strategy = cameraSql.select('camera_strategy_table',
- fields=["model_kind", "posterize_kind", "posterize_unit", "posterize_time",
- "posterize_start_date", "posterize_end_date"])
- return list_strategy
- def is_read():
- cond_dict = {"conf_type": 'camera_capture'}
- params = {"is_read": "1"}
- try:
- cameraSql.update('camera_strategy_table', params, cond_dict)
- return 'Success'
- except:
- return 'error'
- if __name__ == '__main__':
- # time.sleep(10)
- _logger = LogOut.Log('CPpost')
- # 创建读取配置文件对象
- config = configparser.ConfigParser()
- config.read("config.ini", encoding="utf-8")
- # 获取通用配置项
- section = "General" # 读取的section标签
- mysql_host = config.get(section, 'mysqlHost')
- mysql_username = config.get(section, 'mysqlUsername')
- mysql_password = config.get(section, 'mysqlPassword')
- mysql_port = config.getint(section, 'mysqlPort')
- token = config.get(section, 'token')
- appId = config.get(section, 'appId')
- # 获取特有配置项
- section = 'CPpost'
- mysql_database = config.get(section, 'mysqlDatabase')
- cameraSql = MysqldbOperational(host=mysql_host,
- username=mysql_username,
- password=mysql_password,
- port=mysql_port,
- database=mysql_database,
- logger=_logger)
- posterize_strategy()
|