Exemple #1
0
def getControlDevice(mac,i):
    feed_id = GlobalVariable.device_list[mac]["feed_id"]
    url = GlobalVariable.jd_service_url + "controlDevice"
    body = GlobalVariable.service_body%(feed_id,GlobalVariable.cmds[i])
    GlobalVariable.service_headers["Authorization"] = str(getAuthorization(body,GlobalVariable.accessKey))
    res = requests.post(url, params=GlobalVariable.service_pram, headers=GlobalVariable.service_headers, data=body)
    control_device = {}
    if res.status_code == 200 and res.json()["result"] is not None:
        res = res.json()
        result = json.loads(res["result"])
        streams = result["streams"][0]
        current_value = json.loads(streams["current_value"])
        if current_value.get("data"):
            data = current_value["data"]
            if i == 0:
                # 连接的设备列表
        #         print(data)
                pass
            elif i == 1:
                # 上传与下载
                upload = data["upload"]
                download = data["download"]
                bandwidth = data["bandwidth"]
            elif i == 2:
                # 运行信息
                if isinstance(data, str):
                    print("无法获取运行信息!")
                    print("信息如下:", data)
                    control_device.update({"runInfo": False})
                mac = data["mac"]
                rom = data["rom"]
                sn = data["sn"]
                upload = data["upload"]
                download = data["download"]
                romType = data["romType"]
                model = data["model"]
                cpu = data["cpu"]
                onlineTime = data["onlineTime"]
                wanip = data["wanip"]
                mem = data["mem"]
                upload_str = ""
                download_str = ""
                if int(upload) < 10240:
                    upload_str = str(round(int(upload)/10)) + "KB/s"
                    download_str = str(round(int(download)/10)) + "KB/s"
                else:
                    upload_str = str(round(int(upload)/10/1024,2)) + "MB/s"
                    download_str = str(round(int(download)/10/1024,2)) + "MB/s"
                control_device.update({"runInfo": True,"rom":rom,"speed":"↑%s   ↓%s"%(upload_str,download_str),"cpu":cpu + "%","onlineTime":calculatingTime(onlineTime),"wanip":wanip,"model":model})
            elif i == 3:
                # 插件版本
                if isinstance(data,str):
                    print("无法获取插件信息!")
                    print("信息如下:",data)
                    control_device.update({"pluginInfo":False})
                else:
                    pcdn_list = data["pcdn_list"]
                    # print(pcdn_list)
                    status = ""
                    # name = ""
                    cache_size = ""
                    for pcdn_st in pcdn_list:
                        status += f'''{pcdn_st["nickname"]}({pcdn_st["status"]})   '''
                        # name += f'''{pcdn_st["nickname"]}({pcdn_st["name"]})   '''
                        cache_size += f'''{pcdn_st["nickname"]}({str(round(int(pcdn_st["cache_size"])/1048/1000,2))}GB)   '''
                    extstorage_exist = data["extstorage_exist"]
                    extstorage_enable = data["extstorage_enable"]
                    #board = data["board"] //delete useless code
                    control_device.update({"pluginInfo":True,"status":status,"cache_size":cache_size})
        elif current_value.get("msg"):
            print(current_value.get("msg"))
    else:
        if res.json()["error"] is not None:
            error = res.json()["error"]
            errorCode = error['errorCode']
            errorInfo = error['errorInfo']
            print("错误代码:%s,错误信息:%s"%(errorCode,errorInfo))
        control_device.update({"ControlDevice": False})
        print("Request getControlDevice failed!")
        
    index = GlobalVariable.findALocation(mac)
    if index != -1:
        point_info = GlobalVariable.final_result["pointInfos"][index]
        point_info.update(control_device)
    else:
        print("Find mac failure!")
Exemple #2
0
def getControlDevice(mac, i):
    feed_id = GlobalVariable.device_list[mac]["feed_id"]
    url = GlobalVariable.jd_service_url + "controlDevice"
    body = GlobalVariable.service_body % (feed_id, GlobalVariable.cmds[i])
    GlobalVariable.service_headers["Authorization"] = str(
        getAuthorization(body, GlobalVariable.accessKey))
    res = requests.post(url,
                        params=GlobalVariable.service_pram,
                        headers=GlobalVariable.service_headers,
                        data=body)
    control_device = {}
    if res.status_code == 200 and res.json()["result"] is not None:
        res = res.json()
        result = json.loads(res["result"])
        streams = result["streams"][0]
        current_value = json.loads(streams["current_value"])
        data = current_value["data"]
        if i == 0:
            # 连接的设备列表
            #         print(data)
            pass
        elif i == 1:
            # 上传与下载
            upload = data["upload"]
            download = data["download"]
            bandwidth = data["bandwidth"]
        elif i == 2:
            # 运行信息
            mac = data["mac"]
            rom = data["rom"]
            sn = data["sn"]
            upload = data["upload"]
            download = data["download"]
            romType = data["romType"]
            model = data["model"]
            cpu = data["cpu"]
            onlineTime = data["onlineTime"]
            wanip = data["wanip"]
            mem = data["mem"]
            upload_str = ""
            download_str = ""
            if int(upload) < 10240:
                upload_str = str(round(int(upload) / 10)) + "KB/s"
                download_str = str(round(int(download) / 10)) + "KB/s"
            else:
                upload_str = str(round(int(upload) / 10 / 1024, 2)) + "MB/s"
                download_str = str(round(int(upload) / 10 / 1024, 2)) + "MB/s"
            control_device = {
                "rom": rom,
                "speed": "↑%s   ↓%s" % (upload_str, download_str),
                "cpu": cpu + "%",
                "onlineTime": calculatingTime(onlineTime),
                "wanip": wanip,
                "model": model
            }
        elif i == 3:
            # 插件版本
            if isinstance(data, str):
                print("无法获取插件信息!")
                print("信息如下:", data)
                control_device = {"pluginInfo": False}
            else:
                pcdn_list = data["pcdn_list"]
                pcdn_st = pcdn_list[0]
                status = pcdn_st["status"]
                nickname = pcdn_st["nickname"]
                name = pcdn_st["name"]
                cache_size = pcdn_st["cache_size"]
                extstorage_exist = data["extstorage_exist"]
                extstorage_enable = data["extstorage_enable"]
                board = data["board"]
                control_device = {
                    "pluginInfo": True,
                    "status": status,
                    "nickname": nickname,
                    "pcdnname": name,
                    "cache_size":
                    str(round(int(cache_size) / 1000000, 2)) + "GB"
                }

    else:
        control_device = {"pluginInfo": False}
        print("Request getControlDevice failed!")

    index = GlobalVariable.findALocation(mac)
    if index != -1:
        point_info = GlobalVariable.final_result["pointInfos"][index]
        point_info.update(control_device)
    else:
        print("Find mac failure!")