import hashlib
import requests
import json
import time
usrId = "xxx"
password = "xxx"
token_start = None
token_final = None
usrId_final = None
ssId = None
realFamilyId = None
familyId = None
deviceId1 = None
deviceId2 = None
headers = {
'User-Agent': 'SmartApp',
'Content-Type': 'application/json',
'Accept': 'application/json',
'Host': 'app.psmartcloud.com'
}
# 获取初始Token
def get_initial_token():
global usrId, token_start
url = "https://app.psmartcloud.com/App/UsrGetToken"
payload = json.dumps({
"id": 4,
"uiVersion": 4.0,
"params": {"usrId": usrId}
})
response = requests.post(url, headers=headers, data=payload, verify=False)
data = response.json()
token_start = data['results']['token']
return token_start
# 获取密码MD5
def get_password_md5(password):
return hashlib.md5(password.encode()).hexdigest().upper()
# 生成最终Token
def generate_final_token(password_md5):
global usrId, token_start, token_final
intermediate_md5 = hashlib.md5((password_md5 + usrId).encode()).hexdigest().upper()
final_md5 = hashlib.md5((intermediate_md5 + token_start).encode()).hexdigest().upper()
token_final = final_md5
print(token_final)
return token_final
# 登录并获取主要参数
def login_and_get_params():
global usrId, token_final, usrId_final, ssId, realFamilyId, familyId
url = "https://app.psmartcloud.com/App/UsrLogin"
payload = json.dumps({
"id": 5,
"uiVersion": 4.0,
"params": {
"telId": "02:00:00:00:00:00",
"checkFailCount": 1,
"usrId": usrId,
"pwd": token_final
}
})
response = requests.post(url, headers=headers, data=payload, verify=False)
data = response.json()
# 检查是否登陆成功
print(data)
usrId_final = data['results']['usrId']
ssId = data['results']['ssId']
realFamilyId = data['results']['realFamilyId']
familyId = data['results']['familyId']
# 获取绑定设备信息
def get_bind_device_info():
global ssId, usrId_final, realFamilyId, familyId, deviceId1, deviceId2
url = "https://app.psmartcloud.com/App/UsrGetBindDevInfo"
headers_with_cookie = headers.copy()
headers_with_cookie['Cookie'] = f"SSID={ssId}"
payload = json.dumps({
"id": 5,
"uiVersion": 4.0,
"params": {
"realFamilyId": realFamilyId,
"familyId": familyId,
"usrId": usrId_final
}
})
response = requests.post(url, headers=headers_with_cookie, data=payload, verify=False)
data = response.json()
print(data)
dev_list = data['results']['devList']
deviceId1 = dev_list[0]['deviceId']
return dev_list
# 设置设备状态
def set_device_status(device_id, usr_id, token, params):
url = "https://app.psmartcloud.com/App/ACDevGetStatusInfoAW"
headers_with_cookie = headers.copy()
headers_with_cookie['Cookie'] = f"SSID={ssId}"
payload = {
"id": 1,
"uiVersion": 4.0,
"deviceId": device_id,
"usrId": usr_id,
"token": token
}
response = requests.post(url, headers=headers_with_cookie, json=payload, verify=False)
return response.json()
def main():
global usrId, password
token_start = get_initial_token()
password_md5 = get_password_md5(password)
generate_final_token(password_md5)
login_and_get_params()
device_info = get_bind_device_info()
# 编辑params中的指令
# 还需要设备对应token的加密算法,替换掉token_final
print(set_device_status(deviceId1, usrId_final, token_final, {"your": "params"}))
if __name__ == "__main__":
main()