-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.py
171 lines (146 loc) · 5.95 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from peewee import *
import os
from toml import dump, load
import requests
from models import ChaZuo, KongTiao, YuE
from utils import get_crontab
script_dir = os.path.dirname(os.path.abspath(__file__))
config = load(script_dir + "/config.toml")
try:
if "root_url" not in config["student"] or config["student"]["root_url"] == "":
root_url = "http://10.128.13.25"
else:
root_url = config["student"]["root_url"]
if root_url[-1] == "/":
root_url = root_url[:-1]
assert root_url.startswith("http")
except Exception as e:
print(e)
print(
"请重新设置 config.toml 中的 proxy 字段,以 http:// 或 https:// 开头,或者删除该字段并连接校园网。"
)
exit(1)
electricity_fee = config["student"]["electricity_fee"]
if __name__ == "__main__":
id = os.getenv("student_id") or config["student"]["id"]
headers = {
"accept": "*/*",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,es-ES;q=0.5,es;q=0.4",
"content-type": "application/json;charset=UTF-8",
"systemsign": "se-pc",
"token": "9f7c6e76979c4cb9dd3828f8cc44a5ef",
"x-requested-with": "XMLHttpRequest",
}
sno_payload = {"sno": id}
response = requests.post(
f"{root_url}/hydxcas/getCadByNo", headers=headers, json=sno_payload
)
if response.status_code == 200:
response_data = response.json()
value = response_data["value"]
# print("Value:", value)
try:
value_dict = eval(value)
# NameError
except NameError as e:
print(e)
print("获取数据失败,请检查 config 配置并重新初始化。")
exit(1)
card = value_dict["card"]
try:
card_dict = eval(card)[0]
account = card_dict["account"]
except NameError as e:
print(e)
print("获取数据失败,请检查 config 配置并重新初始化。")
exit(1)
eqptData = value_dict["eqptData"]
eqptNum = len(eqptData)
if eqptNum > 2:
category = []
for i in range(eqptNum):
if eqptData[i]["categoryEnergyName"] == "空调末端":
category.append(
{
"categoryEnergyName": eqptData[i]["categoryEnergyName"],
"roomName": eqptData[i]["roomName"],
"index": i,
}
)
print("请选择一个空调末端:")
for i in range(len(category)):
print(f"[{category[i]['index']}]: {category[i]['roomName']}")
categoryEnergy_id = os.getenv("categoryEnergy_id") or input("请输入方框内的编号:")
index = int(categoryEnergy_id)
# 保留 "照明与插座" 和选择的空调末端
eqptData = [
eqptData[i]
for i in range(eqptNum)
if i == index or eqptData[i]["categoryEnergyName"] == "照明与插座"
]
eqptNum = len(eqptData)
# print(eqptData)
# 在 config.toml 中添加 eqptNum 和各个设备的 equipmentInfoId
config["student"]["equipments"] = {}
for i in range(eqptNum):
categoryEnergyName = eqptData[i]["categoryEnergyName"]
try:
if categoryEnergyName == "空调末端":
KongTiao.create_table()
categoryEnergyName = "kongtiao"
elif categoryEnergyName == "照明与插座":
ChaZuo.create_table()
categoryEnergyName = "chazuo"
except Exception as e:
print(e)
print("创建数据库表失败,请检查 config 配置并重新初始化。")
exit(1)
config["student"]["equipments"][categoryEnergyName] = {}
config["student"]["equipments"][categoryEnergyName]["equipmentInfoId"] = (
eqptData[i]["equipmentInfoId"]
)
config["student"]["equipments"][categoryEnergyName]["roomName"] = eqptData[
i
]["roomName"]
dz_payload = {"account": account}
response = requests.post(
f"{root_url}/hydxcas/getDzByNo", headers=headers, json=dz_payload
)
if response.status_code == 200:
try:
YuE.create_table()
config["student"]["account"] = account
except Exception as e:
print(e)
print("处理账户信息失败,请检查 config 配置并重新初始化。")
exit(1)
else:
print("获取账户信息失败,请检查网络连接或配置。")
exit(1)
with open("config.toml", "w", encoding="utf-8") as f:
if "visualize" not in config or "title" not in config["visualize"]:
config["visualize"] = {}
config["visualize"]["title"] = "Electricity!"
if "notify" not in config:
config["notify"] = {}
config["notify"]["chazuo_threshold"] = 10
config["notify"]["kongtiao_threshold"] = 10
config["notify"]["yue_threshold"] = 10
else:
if "chazuo_threshold" not in config["notify"]:
config["notify"]["chazuo_threshold"] = 10
if "kongtiao_threshold" not in config["notify"]:
config["notify"]["kongtiao_threshold"] = 10
if "yue_threshold" not in config["notify"]:
config["notify"]["yue_threshold"] = 10
get_crontab()
dump(config, f)
from get import get_latest_data
data = get_latest_data()
if data["status"] == 0:
exit(1)
else:
ChaZuo.create(charge=data["chazuo"], time=data["time"])
KongTiao.create(charge=data["kongtiao"], time=data["time"])
YuE.create(balance=data["yue"], time=data["time"])
print("初始化成功并已更新数据库!")