123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
- @AUTHOR: ASJRD018
- @NAME: Ansjer
- @software: PyCharm
- @DATE: 2018/6/7 9:27
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: EquipmentStat.py
- @Contact: chanjunkai@163.com
- """
- import base64
- import urllib.parse
- from Ansjer.config import BASE_DIR
- from django.http import JsonResponse
- from django.views.decorators.csrf import csrf_exempt
- import ipdb
- from Object.TokenObject import TokenObject
- from Model.models import Device_Info, UID_Preview
- from Object.ResponseObject import ResponseObject
- from Service.CommonService import CommonService
- import time
- import oss2
- from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
- '''
- http://192.168.136.45:8077/device/online?UID=C2887N9EBS87ZAT1111A
- http://192.168.136.45:8077/device/offline?UID=C2887N9EBS87ZAT1111A
- http://192.168.136.45:8077/device/updateIP?UID=C2887N9EBS87ZAT1111A?ip=190.9.194.216
- '''
- @csrf_exempt
- def EquipmentOnline(request):
- response = ResponseObject()
- if request.method == 'GET':
- request.encoding = 'utf-8'
- UID = request.GET.get('UID', None)
- if request.method == 'POST':
- request.encoding = 'utf-8'
- UID = request.POST.get('UID', None)
- qs = Device_Info.objects.filter(UID=UID)
- if not qs.exists():
- return response.json(173)
- @csrf_exempt
- def EquipmentOffline(request):
- response = ResponseObject()
- if request.method == 'GET':
- request.encoding = 'utf-8'
- UID = request.GET.get('UID', None)
- if request.method == 'POST':
- request.encoding = 'utf-8'
- UID = request.POST.get('UID', None)
- qs = Device_Info.objects.filter(UID=UID)
- if not qs.exists():
- return response.json(173)
- try:
- qs.update(Online=False)
- except Exception as e:
- print(repr(e))
- return response.json(0)
- @csrf_exempt
- def updateIP(request):
- response = ResponseObject()
- if request.method == 'GET':
- request.encoding = 'utf-8'
- request_dict = request.GET
- if request.method == 'POST':
- request.encoding = 'utf-8'
- request_dict = request.POST
- UID = request_dict.get('UID', None)
- ip = request_dict.get('ip', None)
- qs = Device_Info.objects.filter(UID=UID)
- if not qs.exists():
- return response.json(173)
- area = ''
- try:
- area = CommonService.getAddr(ip=ip)
- qs.update(ip=ip, area=area)
- except Exception as e:
- print(repr(e))
- return response.json(0, {'area': area})
- def getTZ(request):
- request.encoding = 'utf-8'
- if request.method == 'GET':
- request_dict = request.GET
- elif request.method == 'POST':
- request_dict = request.POST
- else:
- return JsonResponse(status=200, data={'code': 403, 'msg': '接口请求方法错误'})
- c = request_dict.get('etk', None)
- ip = request_dict.get('ip', None)
- lang = request_dict.get('lang', None)
- if not ip:
- ip = CommonService.get_ip_address(request)
- if c:
- c = base64.b64decode(c)
- c = c[2:-2]
- c = urllib.parse.unquote(c.decode('utf-8'))
- c = base64.b64decode(c)
- uid = c.decode('utf-8')
- if len(uid) == 20:
- print(uid)
- info = CommonService.getIpIpInfo(ip=ip, lang=lang)
- tz = info['utc_offset']
- gmtz = tz.replace('UTC','GMT')
- if ':' not in gmtz:
- gmtz = gmtz+':00'
- info['gmt_offset'] = gmtz
- # tz = info.timezone
- # of = info.utc_offset
- # from var_dump import var_dump
- # var_dump(info)
- return JsonResponse(status=200, data={'code': 0, 'msg': 'success', 'data': info})
- else:
- return JsonResponse(status=200, data={'code': 404, 'msg': '令牌错误'})
- else:
- return JsonResponse(status=200, data={'code': 444, 'msg': '接口参数错误 etk'})
- #
- # # 设备预览图
- # def addUIDPreview(request):
- # request.encoding = 'utf-8'
- # response = ResponseObject()
- # if request.method == 'GET':
- # request_dict = request.GET
- # elif request.method == 'POST':
- # request_dict = request.POST
- # else:
- # return response.json(414)
- # channel = request_dict.get('channel')
- # uid = request_dict.get('channel')
- # token = request_dict.get('token')
- # if all([token, uid, channel]):
- # tko = TokenObject(token)
- # if tko.code == 0:
- # nowTime = int(time.time())
- # upqs = UID_Preview.objects.filter(uid=uid, channel=channel)
- # if upqs.exists():
- # try:
- # is_update = upqs.update(updTime=nowTime)
- # except Exception as e:
- # print(repr(e))
- # return response.json(177)
- # else:
- # if is_update:
- # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
- # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
- # obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
- # # 设置此签名URL在60秒内有效。
- # url = bucket.sign_url('PUT', obj, 7200)
- # return response.json(0, url)
- # else:
- # return response.json(177)
- # else:
- # create_data = {
- # 'addTime': nowTime,
- # 'updTime': nowTime,
- # 'uid': uid,
- # 'channel': channel
- # }
- # try:
- # UID_Preview.objects.create(**create_data)
- # except Exception as e:
- # print(repr(e))
- # return response.json(178)
- # else:
- # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
- # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
- # obj = 'uid_preview/{uid}/{channel}.jpeg'.format(uid=uid, channel=channel)
- # # 设置此签名URL在60秒内有效。
- # url = bucket.sign_url('PUT', obj, 7200)
- # return response.json(0, url)
- # else:
- # return response.json(tko.code)
- # else:
- # return response.json(444)
|