加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
device_info.py 64.70 KB
一键复制 编辑 原始数据 按行查看 历史
jczhang02 提交于 2024-09-08 15:22 . feat(version2)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
import pymysql.cursors
import logging
import uuid
from datetime import datetime
import json
from flask import request, jsonify
import system_setup
import requests
from configparser import ConfigParser
from camera_discovery import CameraDiscovery
import binascii
# 读取配置文件
conf = ConfigParser()
conf.read("setting.ini", encoding='utf-8')
# log日志
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 数据库连接
def mysql():
mydb = pymysql.connect(
host=conf.get("mysql_settings", "host"),
port=int(conf.get("mysql_settings", "port")),
user=conf.get("mysql_settings", "user"),
password=conf.get("mysql_settings", "password"),
database='device_info',
cursorclass=pymysql.cursors.DictCursor
)
return mydb
# 设备新增
def deviceAdd():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
number = jsons['number']
installTime = jsons['installTime']
position = jsons['position']
alarmPhone = ''
if jsons.__contains__('alarmPhone'): alarmPhone = jsons['alarmPhone']
img = ''
if jsons.__contains__('img'): img = jsons['img']
install_person = ''
if jsons.__contains__('installPerson'): install_person = jsons['installPerson']
install_person_phone = ''
if jsons.__contains__('installPersonPhone'): install_person_phone = jsons['installPersonPhone']
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
camera_account = ''
if jsons.__contains__('cameraAccount'): camera_account = jsons['cameraAccount']
camera_password = ''
if jsons.__contains__('cameraPassword'): camera_password = jsons['cameraPassword']
camera_ip = ''
if jsons.__contains__('cameraIp'): camera_ip = jsons['cameraIp']
camera_port = ''
if jsons.__contains__('cameraPort'): camera_port = jsons['cameraPort']
device_type = jsons['deviceType']
device_classify = jsons['deviceClassify']
product_id = jsons['productId']
device_class = jsons['deviceClass']
trans_device_code = ''
if jsons.__contains__('trans_device_code'): trans_device_code = jsons['trans_device_code']
is_children = jsons['is_children']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = str(uuid.uuid4())
sql = "SELECT number FROM `device_info` WHERE number = '" + jsons['number'] + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="设备已存在")
sql = "insert into device_info (id,name,number,install_time,position,alarm_phone,install_person,install_person_phone,remark,current_state,img,create_time,normal_update_time,device_type,device_classify,product_id,device_class,camera_account,camera_password,camera_ip,camera_port,trans_device_code,is_children) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%s)" \
% (id, name, number, installTime, position, alarmPhone, install_person, install_person_phone, remark, '1',
img, creation_date, creation_date, device_type, device_classify, product_id, device_class,
camera_account, camera_password, camera_ip, camera_port,trans_device_code,is_children)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 设备更新
def deviceUpdate():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
cursor = mydb.cursor()
jsons = request.json
name = jsons['name']
number = str(jsons['number'])
installTime = jsons['installTime']
position = jsons['position']
#alarmPhone = jsons['alarmPhone']
alarmPhone = ''
if jsons.__contains__('alarmPhone'): alarmPhone = jsons['alarmPhone']
img = ''
if jsons.__contains__('img'): img = jsons['img']
install_person = ''
if jsons.__contains__('installPerson'): install_person = jsons['installPerson']
install_person_phone = ''
if jsons.__contains__('installPersonPhone'): install_person_phone = jsons['installPersonPhone']
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
device_type = jsons['deviceType']
device_classify = jsons['deviceClassify']
product_id = jsons['productId']
device_class = jsons['deviceClass']
id = jsons['id']
camera_account = ''
if jsons.__contains__('cameraAccount'): camera_account = jsons['cameraAccount']
camera_password = ''
if jsons.__contains__('cameraPassword'): camera_password = jsons['cameraPassword']
camera_ip = ''
if jsons.__contains__('cameraIp'): camera_ip = jsons['cameraIp']
camera_port = ''
if jsons.__contains__('cameraPort'): camera_port = jsons['cameraPort']
trans_device_code = ''
if jsons.__contains__('trans_device_code'): trans_device_code = jsons['trans_device_code']
is_children = jsons['is_children']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sql = "SELECT number FROM `device_info` WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
oldNumber = cursor.fetchone()
if number != oldNumber['number']:
sql = "SELECT number FROM `device_info` WHERE number = '" + jsons['number'] + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="设备已存在")
sql = "DELETE FROM device_info WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
sql = "insert into device_info (id,name,number,install_time,position,alarm_phone,install_person,install_person_phone,remark,current_state,img,create_time,normal_update_time,device_type,device_classify,product_id,device_class,camera_account,camera_password,camera_ip,camera_port,trans_device_code,is_children) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%s)" \
% (id, name, number, installTime, position, alarmPhone, install_person, install_person_phone, remark, '1',
img, creation_date, creation_date, device_type, device_classify, product_id, device_class,
camera_account, camera_password, camera_ip, camera_port,trans_device_code,is_children)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0
)
# 设备删除
def deviceDel():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('id'):
sql = "select * from device_info where id='" + jsons.get("id") + "'"
cursor.execute(sql)
device_info = cursor.fetchone()
if device_info['device_type'] == '1111':
sql = "SELECT number FROM `device_info` WHERE trans_device_code = '" + device_info['number'] + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="存在子设备无法删除!")
sql = "DELETE FROM device_info WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 再关闭数据库连接
return jsonify(
msg="success",
code=0
)
# 设备详情
def deviceDetails():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('id'):
sql = "select * from device_info where id='" + jsons.get("id") + "'"
cursor.execute(sql)
device_info = cursor.fetchone()
else:
return jsonify(code=1, msg="缺少必要参数id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 再关闭数据库连接
return jsonify(
msg="success",
code=0,
data=device_info
)
# 设备分页列表
def deviceListPage():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('page') and jsons.__contains__('limit'):
page = jsons.get("page")
limit = jsons.get("limit")
name = "AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
number = "AND number like '%" + jsons.get("number") + "%'" if jsons.get("number") is not None else ""
currentState = " AND current_state = '" + jsons.get("currentState") + "'" if jsons.get("currentState") is not None else ""
deviceClassify = " AND device_classify = '" + jsons.get("deviceClassify") + "'" if jsons.get("deviceClassify") is not None else ""
deviceClass = " AND device_class = '" + jsons.get("deviceClass") + "'" if jsons.get("deviceClass") is not None else ""
is_children = " AND is_children = '" + jsons.get("is_children") + "'" if jsons.get("is_children") is not None else ""
trans_device_code = " AND trans_device_code = '" + jsons.get("trans_device_code") + "'" if jsons.get("trans_device_code") is not None else ""
sql = "SELECT *,(select `name` from device_type where device_type.id =device_info.device_type) as device_type_name,(select `name` from product where product.id =device_info.product_id) as product_name from device_info where 1=1 " + name + number + currentState + deviceClassify + deviceClass + is_children +trans_device_code+ " order by create_time desc"
cursor.execute(sql)
cur_data = cursor.fetchall()
items = []
for item in cur_data[int(limit) * (int(page) - 1):int(limit) * int(page)]:
temp = {}
temp['id'] = item['id']
temp['name'] = item['name']
temp['number'] = item['number']
temp['installTime'] = item['install_time']
temp['position'] = item['position']
temp['alarm_phone'] = item['alarm_phone']
temp['install_person'] = item['install_person']
temp['install_person_phone'] = item['install_person_phone']
temp['remark'] = item['remark']
temp['current_state'] = item['current_state']
temp['img'] = item['img']
temp['create_time'] = item['create_time']
temp['normal_update_time'] = item['normal_update_time']
temp['device_type'] = item['device_type']
temp['device_type_name'] = item['device_type_name']
temp['trans_device_code'] = item['trans_device_code']
temp['camera_account'] = item['camera_account']
temp['camera_password'] = item['camera_password']
temp['camera_ip'] = item['camera_ip']
temp['camera_port'] = item['camera_port']
temp['device_classify'] = item['device_classify']
temp['product_id'] = item['product_id']
temp['product_name'] = item['product_name']
temp['device_class'] = item['device_class']
temp['is_children'] = item['is_children']
temp['trans_device_code'] = item['trans_device_code']
items.append(temp)
rlt_data = {}
num = len(cur_data)
rlt_data["totalSize"] = num
rlt_data["pages"] = num // int(limit) + 1
rlt_data["pageSize"] = limit
rlt_data["Items"] = items
rlt_data["currentPage"] = page
rlt_data["currentSize"] = limit
if int(page) == rlt_data["pages"]:
rlt_data["currentSize"] = num % int(limit)
else:
return jsonify(code=1, msg="缺少必要参数page、limit")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rlt_data
)
# 设备分类列表
def deviceClassList():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('type'):
sql = "SELECT * from device_class where type = '" + jsons['type'] + "'"
cursor.execute(sql)
cur_data = cursor.fetchall()
else:
return jsonify(code=1, msg="缺少必要参数type")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 设备类型列表
def deviceTypeList():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('deviceClassId'):
sql = "SELECT * from device_type where device_class_id = '" + jsons['deviceClassId'] + "'"
cursor.execute(sql)
cur_data = cursor.fetchall()
else:
return jsonify(code=1, msg="缺少必要参数deviceClassId")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 产品列表
def productList():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('deviceClassId'):
sql = "SELECT * from product where device_class_id = '" + jsons['deviceClassId'] + "'"
cursor.execute(sql)
cur_data = cursor.fetchall()
else:
return jsonify(code=1, msg="缺少必要参数deviceClassId")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 获取rtmp
def getRtmp():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('number'):
sql = "SELECT * from device_info where number = '" + jsons['number'] + "' and device_class = '1001'"
cursor.execute(sql)
device = cursor.fetchone()
if device is None: return jsonify(code=1, msg="设备不存在")
cmd = ''
rtmp = ''
if ':' in jsons['ip_addr']:
cmd = "nsenter -m -n -t 1 docker run -tdi --network host --rm --name " + device['number'] + " srs:encoder ffmpeg -i rtsp://" + \
device['camera_account'] + ":" + device['camera_password'] + "@" + device['camera_ip'] + ":" + device[
'camera_port'] + "/media/1 -c copy -f flv rtmp://gw.wisdiot.com:9008/live/rtmp/" + device['number']
rtmp = "rtmp://gw.wisdiot.com:9008/live/rtmp/" + device['number']
else:
cmd = "nsenter -m -n -t 1 docker run -tdi --network host --rm --name "+device['number']+" srs:encoder ffmpeg -i rtsp://"+device['camera_account']+":"+device['camera_password']+"@"+device['camera_ip']+":"+device['camera_port']+"/media/1 -c copy -f flv rtmp://"+jsons['ip_addr']+":1935/live/rtmp/"+device['number']
rtmp = "rtmp://"+jsons['ip_addr']+":1935/live/rtmp/" + device['number']
headers = {
"Content-Type": "application/json; charset=UTF-8"
}
url = "http://127.0.0.1:8001/rest/v1/cmd/run"
pyload = {"cmd": cmd}
response = requests.post(url, data=json.dumps(pyload), headers=headers, timeout=5).text
logging.debug("response:%s" % response)
else:
return jsonify(code=1, msg="缺少必要参数number")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rtmp
# data="rtmp://192.168.0.99:1935/live/"+device['number']
)
# 关闭rtmp流
def closeRtmp():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('number'):
sql = "SELECT * from device_info where number = '" + jsons['number'] + "' and device_class = '1001'"
cursor.execute(sql)
device = cursor.fetchone()
if device is None: return jsonify(code=1, msg="设备不存在")
cmd = "docker stop " + device['number']
headers = {
"Content-Type": "application/json; charset=UTF-8"
}
url = "http://127.0.0.1:8001/rest/v1/cmd/run"
pyload = {"cmd": cmd}
response = requests.post(url, data=json.dumps(pyload), headers=headers, timeout=5).text
logging.debug("response:%s" % response)
else:
return jsonify(code=1, msg="缺少必要参数number")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0
)
# 获取
def device_count():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
sql = ""
deviceClassify = " AND device_classify = '" + jsons.get("deviceClassify") + "'" if jsons.get(
"deviceClassify") is not None else ""
deviceClass = " AND device_class = '" + jsons.get("deviceClass") + "'" if jsons.get(
"deviceClass") is not None else ""
sql = "SELECT current_state,COUNT(current_state) AS count FROM device_info where 1=1 " + deviceClassify + deviceClass + " GROUP BY current_state"
cursor.execute(sql)
device_info_list = cursor.fetchall()
all_count = 0
json_data = {}
flag = jsons.__contains__('deviceClassify')
count = 0
# if flag:
# if jsons.get("deviceClass") == '2001':
# count = 4000
# else:
# count = 4000
json_data['on-line'] = 0
json_data['off-line'] = 0
for device in device_info_list:
if device['current_state'] == '1':
json_data['on-line'] = device['count'] + count
elif device['current_state'] == '3':
json_data['off-line'] = device['count']
all_count = all_count + device['count']
json_data['all_count'] = all_count + count
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close()
return jsonify(
msg="success",
code=0,
data=json_data
)
# 获取产品数量
def product_count():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
sql = "SELECT COUNT(id) as count FROM product"
cursor.execute(sql)
product_count = cursor.fetchone()
json_data = {}
json_data['normal'] = product_count['count']
json_data['disable'] = 0
json_data['all_count'] = product_count['count']
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close()
return jsonify(
msg="success",
code=0,
data=json_data
)
# 获取视频设备列表
def video_device_List():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
sql = "SELECT `name`,number,camera_ip,camera_port,camera_account,camera_password FROM device_info WHERE device_class = '1001' and device_type = '2001'"
cursor.execute(sql)
cur_data = cursor.fetchall()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close()
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 更新设备状态
def update_device_currentState(jsonData):
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
currentState = jsonData['currentState']
normal_update_time = jsonData['time']
number = jsonData['number']
sql = "UPDATE device_info SET current_state ='" + currentState + "',normal_update_time ='" + normal_update_time + "'" + " WHERE number = '" + number + "'"
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close()
return jsonify(
msg="success",
code=0
)
# 消防部件新增、编辑
def component_add_update():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
number = jsons['number']
position = jsons['position']
img = ''
if jsons.__contains__('img'): img = jsons['img']
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
part_type = jsons['part_type']
part_type_name = jsons['part_type_name']
trans_device_code = jsons['trans_device_code']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = ''
if jsons.__contains__('id'):
if id is None or id == '': return jsonify(code=1, msg="id不能为空!")
sql = "SELECT number FROM fire_fighting_component WHERE id = '" + id + "'"
cursor.execute(sql)
old_number = cursor.fetchone()
if number != old_number['number']:
sql = "SELECT number FROM fire_fighting_component WHERE number = '" + number + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="部件已存在")
sql = "DELETE FROM fire_fighting_component WHERE id = '" + id + "'"
cursor.execute(sql)
mydb.commit()
else:
id = str(uuid.uuid4())
sql = "SELECT number FROM fire_fighting_component WHERE number = '" + number + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="部件已存在")
sql = "insert into fire_fighting_component (id,name,number,position,current_state,img,create_time,normal_update_time,part_type,part_type_name,trans_device_code,remark) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" \
% (id, name, number, position, '1', img, creation_date, creation_date, part_type, part_type_name,
trans_device_code, remark)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 消防部件删除
def component_del():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('id'):
if jsons['id'] is None or jsons['id'] == '': return jsonify(code=1, msg="id不能为空!")
sql = "DELETE FROM fire_fighting_component WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 再关闭数据库连接
return jsonify(
msg="success",
code=0
)
# 消防部件分页列表
def component_list_page():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('page') and jsons.__contains__('limit'):
page = jsons.get("page")
limit = jsons.get("limit")
trans_device_code = " AND trans_device_code = '" + jsons.get("trans_device_code") + "'" if jsons.get(
"trans_device_code") is not None else ""
name = " AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
number = " AND number like '%" + jsons.get("number") + "%'" if jsons.get("number") is not None else ""
currentState = " AND current_state = '" + jsons.get("current_state") + "'" if jsons.get(
"current_state") is not None else ""
position = " AND position like '%" + jsons.get("position") + "%'" if jsons.get(
"position") is not None else ""
sql = "SELECT * from fire_fighting_component where 1=1 " + trans_device_code + trans_device_code + name + number + currentState + position + " order by create_time desc"
cursor.execute(sql)
cur_data = cursor.fetchall()
items = []
for item in cur_data[int(limit) * (int(page) - 1):int(limit) * int(page)]:
temp = {}
temp['id'] = item['id']
temp['name'] = item['name']
temp['number'] = item['number']
temp['position'] = item['position']
temp['current_state'] = item['current_state']
temp['img'] = item['img']
temp['create_time'] = item['create_time']
temp['normal_update_time'] = item['normal_update_time']
temp['part_type'] = item['part_type']
temp['part_type_name'] = item['part_type_name']
temp['trans_device_code'] = item['trans_device_code']
temp['remark'] = item['remark']
items.append(temp)
rlt_data = {}
num = len(cur_data)
rlt_data["totalSize"] = num
rlt_data["pages"] = num // int(limit) + 1
rlt_data["pageSize"] = limit
rlt_data["Items"] = items
rlt_data["currentPage"] = page
rlt_data["currentSize"] = limit
if int(page) == rlt_data["pages"]:
rlt_data["currentSize"] = num % int(limit)
else:
return jsonify(code=1, msg="缺少必要参数page、limit")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rlt_data
)
# 部件类型列表
def part_type_list():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
name = " AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
sql = "SELECT * from part_type where 1=1 " + name
cursor.execute(sql)
cur_data = cursor.fetchall()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 视频分析通道新增、编辑
def camera_channel_add_update():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
number = jsons['number']
position = jsons['position']
img = ''
if jsons.__contains__('img'): img = jsons['img']
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
camera_account = jsons['camera_account']
camera_password = jsons['camera_password']
camera_ip = jsons['camera_ip']
camera_port = jsons['camera_port']
aibox_number = jsons['aibox_number']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = ''
if jsons.__contains__('id'):
if id is None or id == '': return jsonify(code=1, msg="id不能为空!")
sql = "SELECT number FROM camera_channel WHERE id = '" + id + "'"
cursor.execute(sql)
old_number = cursor.fetchone()
if number != old_number['number']:
sql = "SELECT number FROM camera_channel WHERE number = '" + number + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="通道已存在")
sql = "DELETE FROM camera_channel WHERE id = '" + id + "'"
cursor.execute(sql)
mydb.commit()
else:
id = str(uuid.uuid4())
sql = "SELECT number FROM camera_channel WHERE number = '" + number + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="通道已存在")
sql = "insert into camera_channel (id,name,number,position,current_state,img,create_time,normal_update_time,camera_account,camera_password,camera_ip,camera_port,aibox_number,remark) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" \
% (id, name, number, position, '1', img, creation_date, creation_date, camera_account, camera_password,
camera_ip, camera_port, aibox_number, remark)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 视频分析通道删除
def camera_channel_del():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('id'):
if jsons['id'] is None or jsons['id'] == '': return jsonify(code=1, msg="id不能为空!")
sql = "DELETE FROM camera_channel WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 再关闭数据库连接
return jsonify(
msg="success",
code=0
)
# 视频分析通道列表
def camera_channel_list():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('aibox_number'):
aibox_number = " AND aibox_number = '" + jsons.get("aibox_number") + "'" if jsons.get(
"aibox_number") is not None else ""
name = " AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
number = " AND number like '%" + jsons.get("number") + "%'" if jsons.get("number") is not None else ""
position = " AND position like '%" + jsons.get("position") + "%'" if jsons.get(
"position") is not None else ""
sql = "SELECT * from camera_channel where 1=1 " + aibox_number + number + name + position
cursor.execute(sql)
cur_data = cursor.fetchall()
else:
return jsonify(code=1, msg="缺少必要参数aibox_number")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
# 通知新增
def communicationAdd():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('name') and jsons.__contains__('phone') and jsons.__contains__('type'):
sql = "SELECT phone FROM communication WHERE phone = '" + jsons['phone'] + "'and `type` = " + str(
jsons['type'])
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="此手机号已存在同一类型的通知")
# 名称
name = jsons['name']
if name == '': return jsonify(code=1, msg="name 不能为null")
# 电话
phone = jsons['phone']
if phone == '': return jsonify(code=1, msg="phone 不能为null")
# 类型
type = jsons['type']
if type == '': return jsonify(code=1, msg="type 不能为null")
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
id = str(uuid.uuid4())
sql = "insert into communication (id,name,phone,type,remark) values('%s','%s',%s,%s,'%s')" % (id, name, phone, type,remark)
insert = cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数name、phone、type")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0
)
# 通知编辑
def communicationUpdate():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('name') and jsons.__contains__('phone') and jsons.__contains__('type') and jsons.__contains__('id'):
sql = "SELECT phone FROM communication WHERE phone = '" + jsons['phone'] + "'and `type` = " + str(jsons['type'])
cursor.execute(sql)
old_phone = cursor.fetchone()
if jsons['phone'] != old_phone['phone']:
sql = "SELECT phone FROM communication WHERE phone = '" + jsons['phone'] + "'and `type` = " + str(jsons['type'])
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="此手机号已存在同一类型的通知")
# 名称
name = jsons['name']
if name == '': return jsonify(code=1, msg="name 不能为null")
# 电话
phone = jsons['phone']
if phone == '': return jsonify(code=1, msg="phone 不能为null")
# 类型
type = jsons['type']
if type == '': return jsonify(code=1, msg="type 不能为null")
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
id = jsons['id']
if id == '': return jsonify(code=1, msg="id 不能为null")
sql = "UPDATE communication SET name = '%s',phone = '%s',remark = '%s',type = %s where id = '%s'" % (name, phone,remark, type, id)
cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数name、phone、type,id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0
)
# 通知删除
def communicationDel():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('id'):
id = jsons['id']
if id == '': return jsonify(code=1, msg="id 不能为null")
sql = "DELETE FROM communication WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0
)
# 通知配置分页列表
def communication_list_page():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('page') and jsons.__contains__('limit'):
page = jsons.get("page")
limit = jsons.get("limit")
name = " AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
phone = " AND phone like '%" + jsons.get("phone") + "%'" if jsons.get("phone") is not None else ""
type = " AND type = '" + jsons.get("type") + "'" if jsons.get("type") is not None else ""
sql = "SELECT * from communication where 1=1 " + name + phone + type
cursor.execute(sql)
cur_data = cursor.fetchall()
items = []
for item in cur_data[int(limit) * (int(page) - 1):int(limit) * int(page)]:
temp = {}
temp['id'] = item['id']
temp['name'] = item['name']
temp['phone'] = item['phone']
temp['type'] = item['type']
temp['remark'] = item['remark']
items.append(temp)
rlt_data = {}
num = len(cur_data)
rlt_data["totalSize"] = num
rlt_data["pages"] = num // int(limit) + 1
rlt_data["pageSize"] = limit
rlt_data["Items"] = items
rlt_data["currentPage"] = page
rlt_data["currentSize"] = limit
if int(page) == rlt_data["pages"]:
rlt_data["currentSize"] = num % int(limit)
else:
return jsonify(code=1, msg="缺少必要参数page、limit")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rlt_data
)
# 通讯记录分页列表
def communication_record_list_page():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('page') and jsons.__contains__('limit'):
page = jsons.get("page")
limit = jsons.get("limit")
phone = " AND phone = '" + jsons.get("phone") + "'" if jsons.get("phone") is not None else ""
type = " AND type = '" + jsons.get("type") + "'" if jsons.get("type") is not None else ""
sql = "SELECT * from communication_record where 1=1 " + phone + type
cursor.execute(sql)
cur_data = cursor.fetchall()
items = []
for item in cur_data[int(limit) * (int(page) - 1):int(limit) * int(page)]:
temp = {}
temp['id'] = item['id']
temp['phone'] = item['phone']
temp['number'] = item['number']
temp['type'] = item['type']
temp['time'] = item['time']
items.append(temp)
rlt_data = {}
num = len(cur_data)
rlt_data["totalSize"] = num
rlt_data["pages"] = num // int(limit) + 1
rlt_data["pageSize"] = limit
rlt_data["Items"] = items
rlt_data["currentPage"] = page
rlt_data["currentSize"] = limit
if int(page) == rlt_data["pages"]:
rlt_data["currentSize"] = num % int(limit)
else:
return jsonify(code=1, msg="缺少必要参数page、limit")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rlt_data
)
# 获取局域网内onvif协议摄像头IP
def get_onvif_camera_ip():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
try:
# 获取IP
camera_ip = CameraDiscovery.ws_discovery()
# 返回数据
lst = list()
for ip in camera_ip:
lst.append(str(ip).strip(":"))
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
return jsonify(
msg="success",
code=0,
data=lst
)
# 批量插入onvif设备数据
def batch_insert_onvif_device():
# response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
# token = json.loads(response.data)
# if token['code'] == 1:
# return jsonify(
# msg=token['msg'],
# code=1
# )
# elif token['code'] == 401:
# return jsonify(
# msg=token['msg'],
# code=token['code']
# )
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
number = jsons['number']
installTime = jsons['installTime']
position = jsons['position']
alarmPhone = ''
if jsons.__contains__('alarmPhone'): alarmPhone = jsons['alarmPhone']
img = ''
if jsons.__contains__('img'): img = jsons['img']
install_person = ''
if jsons.__contains__('installPerson'): install_person = jsons['installPerson']
install_person_phone = ''
if jsons.__contains__('installPersonPhone'): install_person_phone = jsons['installPersonPhone']
remark = ''
if jsons.__contains__('remark'): remark = jsons['remark']
camera_account = ''
if jsons.__contains__('cameraAccount'): camera_account = jsons['cameraAccount']
camera_password = ''
if jsons.__contains__('cameraPassword'): camera_password = jsons['cameraPassword']
camera_ip = ''
if jsons.__contains__('cameraIp'): camera_ip = jsons['cameraIp']
camera_port = ''
if jsons.__contains__('cameraPort'): camera_port = jsons['cameraPort']
device_type = jsons['deviceType']
device_classify = jsons['deviceClassify']
product_id = jsons['productId']
device_class = jsons['deviceClass']
trans_device_code = ''
if jsons.__contains__('trans_device_code'): trans_device_code = jsons['trans_device_code']
is_children = jsons['is_children']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = str(uuid.uuid4())
sql = "SELECT number FROM `device_info` WHERE number = '" + jsons['number'] + "'"
cursor.execute(sql)
count = cursor.fetchall()
if count.__len__() > 0:
return jsonify(code=1, msg="设备已存在")
sql = "insert into device_info (id,name,number,install_time,position,alarm_phone,install_person,install_person_phone,remark,current_state,img,create_time,normal_update_time,device_type,device_classify,product_id,device_class,camera_account,camera_password,camera_ip,camera_port,trans_device_code,is_children) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%s)" \
% (id, name, number, installTime, position, alarmPhone, install_person, install_person_phone, remark, '1',
img, creation_date, creation_date, device_type, device_classify, product_id, device_class,
camera_account, camera_password, camera_ip, camera_port,trans_device_code,is_children)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 组态应用新增
def application_add():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
describe = ''
if jsons.__contains__('describe'): describe = jsons['describe']
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = str(uuid.uuid4())
sql = "insert into application (id,name,`describe`,creation_date) values('%s','%s','%s','%s')" % (id, name, describe, creation_date)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 组态应用更新
def application_update():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
name = jsons['name']
id = jsons['id']
describe = ''
if jsons.__contains__('describe'): describe = jsons['describe']
sql = "DELETE FROM application WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sql = "insert into application (id,name,`describe`,creation_date) values('%s','%s','%s','%s')" % (id, name, describe, creation_date)
insert = cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 组态应用删除
def application_del():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
if jsons.__contains__('id'):
sql = "DELETE FROM application WHERE id = '" + jsons['id'] + "'"
cursor.execute(sql)
mydb.commit()
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 组态应用分页列表
def application_list_page():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('page') and jsons.__contains__('limit'):
page = jsons.get("page")
limit = jsons.get("limit")
name = "AND name like '%" + jsons.get("name") + "%'" if jsons.get("name") is not None else ""
sql = "SELECT * from application where 1=1 " + name +" order by creation_date desc"
cursor.execute(sql)
cur_data = cursor.fetchall()
items = []
for item in cur_data[int(limit) * (int(page) - 1):int(limit) * int(page)]:
temp = {}
temp['id'] = item['id']
temp['name'] = item['name']
temp['describe'] = item['describe']
temp['creation_date'] = item['creation_date']
items.append(temp)
rlt_data = {}
num = len(cur_data)
rlt_data["totalSize"] = num
rlt_data["pages"] = num // int(limit) + 1
rlt_data["pageSize"] = limit
rlt_data["Items"] = items
rlt_data["currentPage"] = page
rlt_data["currentSize"] = limit
if int(page) == rlt_data["pages"]:
rlt_data["currentSize"] = num % int(limit)
else:
return jsonify(code=1, msg="缺少必要参数page、limit")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=rlt_data
)
# 组态应用数据保存
def application_data_preserve():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.json
data = request.data.decode(encoding='utf-8', errors='strict')
print(data)
if jsons.__contains__('application_id'):
application_id = jsons['application_id']
sql = "DELETE FROM application_data WHERE application_id = '" + application_id + "'"
cursor.execute(sql)
mydb.commit()
# 创建日期
creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
id = str(uuid.uuid4())
sql = "insert into application_data (id,json,creation_date,application_id) values('%s','%s','%s','%s')" % (id,data,creation_date,application_id)
insert = cursor.execute(sql)
mydb.commit()
else:
return jsonify(code=1, msg="缺少必要参数 application_id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0
)
# 组态应用数据详情
def application_data_details():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('application_id'):
sql = "select * from application_data where application_id='" + jsons.get("application_id") + "'"
cursor.execute(sql)
data = cursor.fetchone()
else:
return jsonify(code=1, msg="缺少必要参数 application_id")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭数据库
return jsonify(
msg="success",
code=0,
data=data
)
# 设备平时数据选项
def device_normal_items():
response = system_setup.getDecodeToken(request.headers.get("Authorization", "")[7:])
token = json.loads(response.data)
if token['code'] == 1:
return jsonify(
msg=token['msg'],
code=1
)
elif token['code'] == 401:
return jsonify(
msg=token['msg'],
code=token['code']
)
mydb = mysql()
mydb.connect()
cursor = mydb.cursor()
try:
jsons = request.args
if jsons.__contains__('pid') and jsons.__contains__('device_class'):
sql = "SELECT * from device_normal_items where pid = '" + jsons['pid'] + "' and device_class = '"+ jsons['device_class'] + "'"
cursor.execute(sql)
cur_data = cursor.fetchall()
else:
return jsonify(code=1, msg="缺少必要参数type")
except Exception as e:
print(e)
return jsonify(
code=1,
msg="Server internal error"
)
finally:
cursor.close() # 关闭游标
mydb.close() # 关闭连接
return jsonify(
msg="success",
code=0,
data=cur_data
)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化