PyCharm编程和调试
其中token 我偷懒了,只是调试,打开HiperMATRIX界面,登录,从浏览器console里面找到token value。
代码片段
import random, time, requests, json
hipermatrix_api_url = 'http://192.168.1.240:9030/api/edge-management/equipment/7123766549772505088/write/values'
hipermatrix_login_url = "http://192.168.1.240:9000/login/v1"
send_interval = 60 # 1 min
def connect_login():
client ="login"
return cl
"Content-Type": "application/json"
}
headers_request = {
"token": "xx",
"accept": "*/*",
"Content-Type": "application/json"
}
while True:
sleep_time = send_interval * msg_count #by seconds
sleep_hours = round(sleep_time/60 /60, 0)
sleep_days = round(sleep_time / 60 /60 / 24, 0)
power = round((sleep_time + random.random())/60 /60 /24 * 100, 4) # 100 degree power per day, 4 decimal
#Login
data = "{ \"account\": \"admin\", \"password\": \"111111\"}"
print(f"Sending msg_count {msg_count}`request` ")
response = requests.post(hipermatrix_login_url , headers=headers_login, data=data)
print(f"Login Response `{response.text}` ")
j = json.loads(response.text)
status = j['code']
print(f"Login Status `{status}` ")
token = j['data']['token']
print(f"Login token `{token}` ")
#Request for data
headers_request['token'] = token;
print(f"headers_request `{headers_request}` ")
# data = "[{ \"fieldName\": \"power2\", \"value\": \"" + str(power) + "\"} ]"
data = [
{"fieldName": "stPV_Ua", "value": "220.0001"},
{"fieldName": "stPV_Ub", "value": "12.0002"},
{"fieldName": "stPV_Positive_P_Energy", "value": "3.0003"}
]
data[2]['value'] = str(power)
print(f"Sending `{data}` ")
response = requests.post(hipermatrix_api_url, headers=headers_request, data=json.dumps(data))
print(f"Response `{response.text}` ")
status = 0
j = json.loads(response.text)
status = j['code']
msg_count += 1
time.sleep(send_interval) # every 1 min
print(f"Running {sleep_days} days {sleep_hours} hours {msg_count} min")
def run():
client = connect_login()
publish(client)
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
if __name__ == '__main__':
print_hi('PyCharm')
run()