| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 | # -*- coding: utf-8 -*-# 高复用性函数封装到CommonService类import base64import datetimeimport timefrom base64 import encodebytesfrom pathlib import Pathfrom random import Randomimport OpenSSL.crypto as ctimport ipdbimport requestsimport simplejson as jsonfrom django.core import serializersfrom django.utils import timezonefrom pyipip import IPIPDatabasefrom Ansjer.config import BASE_DIR, SERVER_DOMAIN_SSL, CONFIG_INFO, CONFIG_TEST, CONFIG_CNfrom Controller.CheckUserData import RandomStrfrom Model.models import iotdeviceInfoModel, Device_Info, CountryModel, RegionModel, UIDModelfrom Object.ResponseObject import ResponseObjectfrom Object.TokenObject import TokenObjectclass CommonService:    # 添加模糊搜索    @staticmethod    def get_kwargs(data=None):        if data is None:            data = {}        kwargs = {}        for (k, v) in data.items():            if v is not None and v != u'':                kwargs[k + '__icontains'] = v        return kwargs    # 定义静态方法    # 格式化query_set转dict    @staticmethod    def qs_to_dict(query_set):        sqlJSON = serializers.serialize('json', query_set)        sqlList = json.loads(sqlJSON)        sqlDict = dict(zip(["datas"], [sqlList]))        return sqlDict    # 格式化query_set转dict    @staticmethod    def request_dict_to_dict(request_dict):        # 传参格式转换,键包含meta获取meta[]中的值,值'true'/'false'转为True,False        key_list = []        value_list = []        for k, v in request_dict.items():            key_list.append(k[k.index('[') + 1:k.index(']')] if 'meta' in k else k)            if v == 'true':                v = True            elif v == 'false':                v = False            value_list.append(v)        data_dict = dict(zip(key_list, value_list))        print(data_dict)        return data_dict    # 获取文件大小    @staticmethod    def get_file_size(file_path='', suffix_type='', decimal_point=0):        # for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:        # path = Path() / 'D:/TestServer/123444.mp4'        path = Path() / file_path        size = path.stat().st_size        mb_size = 0.0        if suffix_type == 'MB':            mb_size = size / 1024.0 / 1024.0        if decimal_point != 0:            mb_size = round(mb_size, decimal_point)        return mb_size    @staticmethod    def get_param_flag(data=None):        # print(data)        if data is None:            data = []        flag = True        for v in data:            if v is None:                flag = False                break        return flag    @staticmethod    def get_ip_address(request):        """        获取ip地址        :param request:        :return:        """        try:            real_ip = request.META['HTTP_X_FORWARDED_FOR']            clientIP = real_ip.split(",")[0]        except:            try:                clientIP = request.META['REMOTE_ADDR']            except Exception as e:                clientIP = ''        return clientIP    # @获取一天每个小时的datetime.datetime    @staticmethod    def getTimeDict(times):        time_dict = {}        t = 0        for x in range(24):            if x < 10:                x = '0' + str(x)            else:                x = str(x)            a = times.strftime("%Y-%m-%d") + " " + x + ":00:00"            time_dict[t] = timezone.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')            t += 1        return time_dict    # 根据ip获取地址    @staticmethod    def getAddr(ip):        print('start_time=' + str(time.time()))        base_dir = BASE_DIR        # ip数据库        db = IPIPDatabase(base_dir + '/DB/17monipdb.dat')        addr = db.lookup(ip)        # ModelService.add_tmp_log(addr)        ts = addr.split('\t')[0]        print('end_time=' + str(time.time()))        return ts    # 通过ip检索ipip指定信息 lang为CN或EN    @staticmethod    def getIpIpInfo(ip, lang, update=False):        ipbd_dir = BASE_DIR + "/DB/mydata4vipday2.ipdb"        db = ipdb.City(ipbd_dir)        if update:            rr = db.reload(ipbd_dir)        info = db.find_map(ip, lang)        return info    @staticmethod    def getUserID(userPhone='13800138000', getUser=True, setOTAID=False, μs=True):        if μs == True:            if getUser == True:                timeID = str(round(time.time() * 1000000))                userID = timeID + userPhone                return userID            else:                if setOTAID == False:                    timeID = str(round(time.time() * 1000000))                    ID = userPhone + timeID                    return ID                else:                    timeID = str(round(time.time() * 1000000))                    eID = '13800' + timeID + '138000'                    return eID        else:            if getUser == True:                timeID = str(round(time.time() * 1000))                userID = timeID + userPhone                return userID            else:                if setOTAID == False:                    timeID = str(round(time.time() * 1000))                    ID = userPhone + timeID                    return ID                else:                    timeID = str(round(time.time() * 1000))                    eID = '13800' + timeID + '138000'                    return eID    # 生成随机数    @staticmethod    def RandomStr(randomlength=8, number=True):        str = ''        if number == False:            characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \                           'tUuVvWwXxYyZz0123456789'        else:            characterSet = '0123456789'        length = len(characterSet) - 1        random = Random()        for index in range(randomlength):            str += characterSet[random.randint(0, length)]        return str    # 生成订单好    @staticmethod    def createOrderID():        random_id = CommonService.RandomStr(6, True)        order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random_id)        print('orderID:')        print(order_id)        return order_id    # qs转换list datetime处理    @staticmethod    def qs_to_list(qs):        res = []        # print(qs)        for ps in qs:            try:                if 'time' in ps:                    ps['time'] = ps['time'].strftime("%Y-%m-%d %H:%M:%S")                if 'add_time' in ps:                    ps['add_time'] = ps['add_time'].strftime("%Y-%m-%d %H:%M:%S")                if 'update_time' in ps:                    ps['update_time'] = ps['update_time'].strftime("%Y-%m-%d %H:%M:%S")                if 'end_time' in ps:                    ps['end_time'] = ps['end_time'].strftime("%Y-%m-%d %H:%M:%S")                if 'data_joined' in ps:                    if ps['data_joined']:                        ps['data_joined'] = ps['data_joined'].strftime("%Y-%m-%d %H:%M:%S")                    else:                        ps['data_joined'] = ''                if 'userID__data_joined' in ps:                    if ps['userID__data_joined']:                        ps['userID__data_joined'] = ps['userID__data_joined'].strftime("%Y-%m-%d %H:%M:%S")                    else:                        ps['userID__data_joined'] = ''            except Exception as e:                pass            res.append(ps)        return res    # 获取当前时间    @staticmethod    def get_now_time_str(n_time, tz, lang):        print(n_time)        print(tz)        print(lang)        n_time = int(n_time) + 3600 * float(tz)        if lang == 'cn':            return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))        else:            return time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(int(n_time)))    # 生成随机数    @staticmethod    def encrypt_data(randomlength=8, number=False):        str = ''        if number == False:            characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \                           'tUuVvWwXxYyZz0123456789'        else:            characterSet = '0123456789'        length = len(characterSet) - 1        random = Random()        for index in range(randomlength):            str += characterSet[random.randint(0, length)]        return str    @staticmethod    def decode_data(content, start=1, end=4):        if not content:            return ''        try:            for i in range(start, end):                if i == 1:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[1:-1]                if i == 2:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[2:-2]                if i == 3:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[3:-3]            return content        except Exception as e:            print(e)            return None    @staticmethod    def encode_data(content, start=1, end=4):        if not content:            return ''        for i in range(start, end):            if i == 1:                content = RandomStr(3, False) + content + RandomStr(3, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')            if i == 2:                content = RandomStr(2, False) + str(content) + RandomStr(2, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')            if i == 3:                content = RandomStr(1, False) + str(content) + RandomStr(1, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')        return content    # 把格式化时间转换成时间戳    @staticmethod    def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):        if str_time:            time_tuple = time.strptime(str_time, format)  # 把格式化好的时间转换成元祖            result = time.mktime(time_tuple)  # 把时间元祖转换成时间戳            return int(result)        return int(time.time())    # 把时间戳转换成格式化    @staticmethod    def timestamp_to_str(timestamp=None, format='%Y-%m-%d %H:%M:%S'):        if timestamp:            time_tuple = time.localtime(timestamp)  # 把时间戳转换成时间元祖            result = time.strftime(format, time_tuple)  # 把时间元祖转换成格式化好的时间            return result        else:            return time.strptime(format)    # 计算N个月后的时间戳    @staticmethod    def calcMonthLater(addMonth, unix_timestamp=None):        if unix_timestamp:            now_year = time.localtime(unix_timestamp).tm_year            now_month = time.localtime(unix_timestamp).tm_mon            now_day = time.localtime(unix_timestamp).tm_mday            now_hour = time.localtime(unix_timestamp).tm_hour            now_min = time.localtime(unix_timestamp).tm_min            now_second = time.localtime(unix_timestamp).tm_sec        else:            now_year = datetime.datetime.now().year            now_month = datetime.datetime.now().month            now_day = datetime.datetime.now().day            now_hour = datetime.datetime.now().hour            now_min = datetime.datetime.now().minute            now_second = datetime.datetime.now().second        for add in range(addMonth):            if now_month == 12:                now_year += 1                now_month = 1            else:                now_month += 1        for is_format in range(4):            try:                date_format = '{now_year}-{now_month}-{now_day} {now_hour}:{now_min}:{now_second}' \                    .format(now_year=now_year, now_month=now_month, now_day=now_day, now_hour=now_hour,                            now_min=now_min, now_second=now_second)                timestamps = CommonService.str_to_timestamp(date_format)            except Exception as e:                if str(e) == 'day is out of range for month':                    now_day = now_day - 1        return timestamps    @staticmethod    def updateMac(mac: str):        macArray = mac.split(':')        macArray[0] = int(macArray[0], 16)        macArray[1] = int(macArray[1], 16)        macArray[2] = int(macArray[2], 16)        first = int(macArray[5], 16)        second = int(macArray[4], 16)        three = int(macArray[3], 16)        # print(macArray)        # print(first)        # print(second)        # print(three)        if first == 255 and second == 255 and three == 255:            return None        first += 1        if first / 256 == 1:            second += 1            first = first % 256        if second / 256 == 1:            three += 1            second = second % 256        macArray[3] = three        macArray[4] = second        macArray[5] = first        # print(macArray)        tmp = ':'.join(map(lambda x: "%02x" % x, macArray))        # print(tmp)        return tmp.upper()    @staticmethod    def decode_data(content, start=1, end=4):        if not content:            return ''        try:            for i in range(start, end):                if i == 1:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[1:-1]                if i == 2:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[2:-2]                if i == 3:                    content = base64.b64decode(content)                    content = content.decode('utf-8')                    content = content[3:-3]                    print(content)            return content        except Exception as e:            print(e)            return None    @staticmethod    def encode_data(content, start=1, end=4):        if not content:            return ''        for i in range(start, end):            if i == 1:                content = CommonService.RandomStr(3, False) + content + CommonService.RandomStr(3, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')            if i == 2:                content = CommonService.RandomStr(2, False) + str(content) + CommonService.RandomStr(2, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')            if i == 3:                content = CommonService.RandomStr(1, False) + str(content) + CommonService.RandomStr(1, False)                content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')        return content    @staticmethod    def encode_data_without_salt(content):        return base64.b64encode(str(content).encode("utf-8")).decode('utf8')    @staticmethod    def check_time_stamp_token(token, time_stamp):        # 时间戳token校验        if not all([token, time_stamp]):            return False        try:            token = int(CommonService.decode_data(token))            time_stamp = int(time_stamp)            now_time = int(time.time())            distance = now_time - time_stamp            if token != time_stamp or distance > 60000 or distance < -60000:  # 为了全球化时间控制在一天内                return False            return True        except Exception as e:            print(e)            return False    @staticmethod    def check_time_stamp_token_without_distance(time_stamp_token, time_stamp):        """        用于没有RTC设备的时间戳token校验        @param time_stamp: 时间戳        @param time_stamp_token: 时间戳token        @return: boolean True/False        """        if not all([time_stamp_token, time_stamp]):            return False        try:            token = CommonService.decode_data(time_stamp_token)            if token != time_stamp:                return False            return True        except Exception as e:            print(e)            return False    @staticmethod    def req_publish_mqtt_msg(identification_code, topic_name, msg, qos=1):        """        通用发布MQTT消息函数        @param identification_code: 标识码        @param topic_name: 主题名        @param msg: 消息内容        @param qos: mqtt qos等级        @return: boolean        """        if not all([identification_code, topic_name]):            return False        if identification_code.endswith('11L'):            thing_name = 'LC_' + identification_code        else:            thing_name = 'Ansjer_Device_' + identification_code        try:            # 获取数据组织将要请求的url            iot = iotdeviceInfoModel.objects.filter(                thing_name=thing_name).values(                'endpoint', 'token_iot_number')            if not iot.exists():                return False            endpoint = iot[0]['endpoint']            Token = iot[0]['token_iot_number']            # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html            # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1            # post请求url发布MQTT消息            url = 'https://{}/topics/{}?qos={}'.format(endpoint, topic_name, qos)            authorizer_name = 'Ansjer_Iot_Auth'            signature = CommonService.rsa_sign(Token)  # Token签名            headers = {                'x-amz-customauthorizer-name': authorizer_name,                'Token': Token,                'x-amz-customauthorizer-signature': signature}            r = requests.post(url=url, headers=headers, json=msg, timeout=2)            if r.status_code == 200:                res = r.json()                if res['message'] == 'OK':                    return True                return False            else:                return False        except Exception as e:            return False    @staticmethod    def rsa_sign(Token):        # 私钥签名Token        if not Token:            return ''        private_key_file = '''-----BEGIN RSA PRIVATE KEY-----MIIEpQIBAAKCAQEA5iJzEDPqtGmFMggekVro6C0lrjuC2BjunGkrFNJWpDYzxCzEX5jf4/Fq7hcIaQd5sqHugDxPVollSLPe9zNilbrd0sZfU+Ed8gRVuKW9KwfE9XFrL0pt6bKRQ0IIRfiZ9TuR0tsQysvcO1GZSXcYfPue3tGM1zOnWFThWDqZ06+sOxztRMRl4yNfbpCG4MfxG3itNXOfrjZv2OMLSXrxmzubSvRpUYSvQPs4fm9302SAnySY0MKzx6H6528ZQm/IDDSZy6EmNBIyTRDfxC56vnYcXvqedAQh7jJnjdvt6Q4MhASHeIYi1FBSdu2NT6wgpnrqXzx5pq9kR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CJOwanzwYV/zrex84WuXxlsGgPQ871lTs5APH1QLfLfFXH+UVrCEC2yv4eml/cqFkpB3gE5i4MQ8GPVIOSs5tsIyl8YUA03vdNdBGCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=-----END RSA PRIVATE KEY-----'''        # 使用密钥文件方式        # private_key_file_path = os.path.join(BASE_DIR, 'static/iotCore/private.pem')#.replace('\\', '/')        # private_key_file = open(private_key_file_path, 'r')        private_key = ct.load_privatekey(ct.FILETYPE_PEM, private_key_file)        signature = ct.sign(private_key, Token.encode('utf8'), 'sha256')        signature = encodebytes(signature).decode('utf8').replace('\n', '')        # print('signature:', signature)        return signature    @staticmethod    def get_payment_status_url(lang, payment_status):        # 返回相应的支付状态url        if lang == 'cn':            file_name = 'success.html' if payment_status == 'success' else 'fail.html'        else:            file_name = 'en_success.html' if payment_status == 'success' else 'en_fail.html'        pay_failed_url = "{}web/paid2/{}".format(SERVER_DOMAIN_SSL, file_name)        return pay_failed_url    # 根据uid查询序列号,存在则返回序列号,否则返uid    @staticmethod    def query_serial_with_uid(uid):        device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number')        if device_info_qs.exists():            serial_number = device_info_qs[0]['serial_number']            if serial_number:                return serial_number        return uid    # 根据序列号查询uid,存在则返回uid,否则返回序列号    @staticmethod    def query_uid_with_serial(serial_number):        device_info_qs = Device_Info.objects.filter(serial_number=serial_number).values('UID')        if device_info_qs.exists():            uid = device_info_qs[0]['UID']            if uid:                return uid        return serial_number    @staticmethod    def get_full_serial_number(uid, serial_number, device_type):        """        根据uid查询返回完整序列号        @param uid: uid        @param serial_number: 序列号        @param device_type: 设备类型        @return: full_serial_number        """        p2p_type = str(UIDModel.objects.filter(uid=uid).values('p2p_type')[0]['p2p_type'])        # 设备类型转为16进制并补齐4位        device_type = hex(device_type)[2:]        device_type = (4 - len(device_type)) * '0' + device_type        full_serial_number = serial_number + p2p_type + device_type        return full_serial_number    # 根据企业标识返回物品名    @staticmethod    def get_thing_name(company_mark, thing_name_suffix):        if company_mark == '11A':            return 'Ansjer_Device_' + thing_name_suffix        elif company_mark == '11L':            return 'LC_' + thing_name_suffix        else:            return thing_name_suffix    @staticmethod    def confirm_region_id(request):        """        根据配置信息确定region_id        @param request: 请求体        @return: region_id        """        region_id = 3        if CONFIG_INFO == CONFIG_TEST or CONFIG_INFO == CONFIG_CN:            region_id = 1        else:  # 国外配置暂时通过ip确认            ip = CommonService.get_ip_address(request)            ipInfo = CommonService.getIpIpInfo(ip, 'CN')            if ipInfo['country_code']:                country_qs = CountryModel.objects.filter(country_code=ipInfo['country_code']).values('region__id')                if country_qs.exists():                    region_id = country_qs[0]['region__id']            else:  # 不存在默认返回美洲地区api                region_qs = RegionModel.objects.filter(continent_code='NA').values("id")                region_id = region_qs[0]['id']        return region_id    @staticmethod    def verify_token_get_user_id(request_dict, request):        """        认证token,获取user id        @param request_dict: 请求参数        @param request: 请求体        @return: token_obj.code, token_obj.userID, response        """        try:            token_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))            lang = request_dict.get('lang', None)            response = ResponseObject(lang if lang else token_obj.lang)            return token_obj.code, token_obj.userID, response        except Exception as e:            print(e)
 |