2022-05-17 11:12:12 +08:00
|
|
|
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-05-19 17:14:48 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
@file :requestIot.py
|
2022-05-20 16:49:51 +08:00
|
|
|
@author :elian.wang@quectel.com
|
|
|
|
@brief :cloud that communicate using HTTP,Dtu communicate interface
|
2022-05-19 17:14:48 +08:00
|
|
|
@version :0.1
|
|
|
|
@date :2022-05-19 10:49:01
|
|
|
|
@copyright :Copyright (c) 2022
|
|
|
|
"""
|
|
|
|
|
2022-04-24 21:03:42 +08:00
|
|
|
import ujson
|
2022-05-25 19:59:26 +08:00
|
|
|
import request
|
2022-04-24 21:03:42 +08:00
|
|
|
|
2022-04-29 14:12:38 +08:00
|
|
|
from usr.modules.common import CloudObservable
|
2022-04-28 20:43:41 +08:00
|
|
|
from usr.modules.logging import getLogger
|
2022-04-24 21:03:42 +08:00
|
|
|
|
2022-04-28 20:43:41 +08:00
|
|
|
log = getLogger(__name__)
|
|
|
|
class DtuRequest(CloudObservable):
|
2022-05-26 15:42:55 +08:00
|
|
|
"""This is a class for http iot.
|
2022-04-24 21:03:42 +08:00
|
|
|
|
2022-05-26 15:42:55 +08:00
|
|
|
This class extend CloudObservable.
|
|
|
|
|
|
|
|
This class has the following functions:
|
|
|
|
1. send data to http server
|
|
|
|
2. parse http server ack data
|
|
|
|
|
|
|
|
Attribute:
|
|
|
|
conn_type:cloud name
|
|
|
|
|
|
|
|
Run step:
|
|
|
|
1. cloud = DtuRequest(request_id_dict, reg_data)
|
|
|
|
2. cloud.addObserver(RemoteSubscribe)
|
|
|
|
3. cloud.init()
|
|
|
|
4. cloud.through_post_data(data)
|
|
|
|
"""
|
2022-06-14 13:55:18 +08:00
|
|
|
def __init__(self, request_id_dict, post_data=""):
|
2022-05-25 19:59:26 +08:00
|
|
|
super().__init__()
|
2022-05-06 19:47:27 +08:00
|
|
|
self.conn_type = "http"
|
2022-06-14 13:55:18 +08:00
|
|
|
self.__post_data = post_data
|
2022-05-24 16:24:22 +08:00
|
|
|
self.__request_id_dict = request_id_dict
|
|
|
|
self.__url = None
|
|
|
|
self.__method = None
|
2022-05-25 19:59:26 +08:00
|
|
|
self.__data_methods = ("PUT", "POST", "DELETE", "HEAD", "GET")
|
2022-05-24 16:24:22 +08:00
|
|
|
try:
|
|
|
|
for k,v in self.__request_id_dict.items():
|
2022-05-25 19:59:26 +08:00
|
|
|
if v.get("method").upper() in self.__data_methods:
|
2022-05-24 16:24:22 +08:00
|
|
|
self.__method = v.get("method").upper()
|
2022-05-25 19:59:26 +08:00
|
|
|
self.__url = v.get("url")
|
2022-05-24 16:24:22 +08:00
|
|
|
else:
|
2022-05-25 19:59:26 +08:00
|
|
|
raise Exception("http param error")
|
2022-05-24 16:24:22 +08:00
|
|
|
except Exception as e:
|
|
|
|
log.error("request id dict error:", e)
|
|
|
|
|
|
|
|
def init(self, enforce=False):
|
|
|
|
return True
|
2022-04-24 21:03:42 +08:00
|
|
|
|
2022-05-25 19:59:26 +08:00
|
|
|
def __req(self, data, request_id):
|
2022-04-24 21:03:42 +08:00
|
|
|
global resp
|
2022-05-24 16:24:22 +08:00
|
|
|
print("url", self.__url)
|
|
|
|
print("method", self.__method)
|
|
|
|
uri = self.__url
|
2022-04-24 21:03:42 +08:00
|
|
|
try:
|
2022-05-24 16:24:22 +08:00
|
|
|
if self.__method in self.__data_methods:
|
|
|
|
func = getattr(request, self.__method.lower())
|
|
|
|
resp = func(uri, data=data)
|
2022-04-24 21:03:42 +08:00
|
|
|
else:
|
2022-05-24 16:24:22 +08:00
|
|
|
resp = request.get(uri, data=data)
|
2022-04-24 21:03:42 +08:00
|
|
|
except Exception as e:
|
2022-05-27 11:00:10 +08:00
|
|
|
log.error("http request error: {}".format(e))
|
2022-05-24 16:24:22 +08:00
|
|
|
return False
|
2022-04-24 21:03:42 +08:00
|
|
|
else:
|
2022-05-25 19:59:26 +08:00
|
|
|
try:
|
|
|
|
read_data = ""
|
|
|
|
for i in resp.content:
|
|
|
|
read_data += i
|
|
|
|
except Exception as e:
|
|
|
|
log.error("resp.content fault:", e)
|
2022-06-06 20:28:16 +08:00
|
|
|
return False
|
2022-04-24 21:03:42 +08:00
|
|
|
if resp.status_code == 200:
|
2022-05-24 16:24:22 +08:00
|
|
|
try:
|
2022-05-25 19:59:26 +08:00
|
|
|
self.notifyObservers(self, *("raw_data", {"request_id":request_id, "data":read_data}))
|
2022-05-24 16:24:22 +08:00
|
|
|
except Exception as e:
|
|
|
|
log.error("{}".format(e))
|
2022-06-06 20:28:16 +08:00
|
|
|
return False
|
2022-05-24 16:24:22 +08:00
|
|
|
return True
|
2022-05-25 19:59:26 +08:00
|
|
|
else:
|
|
|
|
log.error("http error:", resp.status_code)
|
|
|
|
return False
|
2022-04-24 21:03:42 +08:00
|
|
|
|
2022-05-24 16:24:22 +08:00
|
|
|
|
|
|
|
# http发送的数据为json类型
|
2022-05-25 19:59:26 +08:00
|
|
|
def through_post_data(self, data, request_id):
|
2022-05-24 16:24:22 +08:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = data
|
|
|
|
else:
|
|
|
|
data = ujson.dumps(data)
|
|
|
|
try:
|
|
|
|
for k,v in self.__request_id_dict.items():
|
|
|
|
if k == request_id:
|
|
|
|
self.__method = v.get("method").upper()
|
2022-05-25 19:59:26 +08:00
|
|
|
self.__url = v.get("url")
|
2022-05-24 16:24:22 +08:00
|
|
|
except Exception as e:
|
|
|
|
log.error("request id dict error:", e)
|
2022-06-06 20:28:16 +08:00
|
|
|
return False
|
2022-05-25 19:59:26 +08:00
|
|
|
return self.__req(data, request_id)
|
2022-05-24 16:24:22 +08:00
|
|
|
|
2022-05-19 17:14:48 +08:00
|
|
|
def get_status(self):
|
2022-05-24 16:24:22 +08:00
|
|
|
resp = request.get(self.__url)
|
|
|
|
if resp.status_code == 200:
|
|
|
|
return True
|
|
|
|
else:
|
2022-05-25 19:59:26 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
def post_data(self, data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ota_request(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ota_action(self, action, module=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def device_report(self):
|
|
|
|
pass
|