import
os,re,time,json,logging
open_temperature_min
=
19.0
open_temperature_max
=
22.0
start_time1
=
"21:30"
end_time1
=
"23:59"
start_time2
=
"0:0"
end_time2
=
"7:0"
temperature_file_path
=
"/sys/bus/w1/devices/28-011432931aaf/w1_slave"
socket_ip
=
"192.168.0.100"
socket_token
=
"b000001ac900000000db40000000008"
temperature_num
=
0.0
LOG_FORMAT
=
"%(asctime)s %(levelname)s %(pathname)s %(message)s "
DATE_FORMAT
=
'%Y-%m-%d %H:%M:%S %a '
logging.basicConfig(level
=
logging.DEBUG,
format
=
LOG_FORMAT,
datefmt
=
DATE_FORMAT ,
filename
=
"/var/log/xiaomi.log"
)
def
read_temperature(file_path):
global
temperature_num
try
:
logging.debug(
"开始读取温度文件"
)
file
=
open
(file_path,mode
=
"r"
)
val
=
file
.read()
crc
=
re.
compile
(
'crc=.*? (.*?)\n'
).findall(val)[
0
]
if
crc
=
=
'YES'
:
t
=
re.
compile
(
't=(.*?)\n'
).findall(val)[
0
]
t
=
float
(t)
/
1000.0
temperature_num
=
t
logging.debug(
"温度文件解析成功,温度为:"
+
str
(t))
return
(
True
,t)
else
:
logging.warning(
"温度文件读取失败"
)
return
False
except
Exception as err:
logging.error(
"温度文件读取失败:"
+
str
(err))
raise
err
def
str_to_dict(data:
str
):
try
:
data
=
str
(data).split(
"\n"
)[
-
2
].replace(
"'"
,'
"').replace("
","
").replace("
False
","
false
").replace("
True
","
true")
data
=
json.loads(data)
return
data
except
Exception as err:
logging.error(
"智能插座返回信息解析失败:"
+
str
(err)
+
"源数据为:"
+
str
(data))
raise
err
def
get_socket_status(socket_ip:
str
,socket_token:
str
):
base_order
=
'miiocli device --ip "%s" --token "%s" raw_command get_properties "[{\'did\': \'MYDID\', \'siid\': 2, \'piid\': 1 }]"'
now_order
=
base_order
%
(socket_ip,socket_token)
try
:
logging.debug(
"智能插座开始查询状态"
)
answer
=
os.popen(now_order).read()
answer
=
str_to_dict(answer)
return
answer[
0
][
"value"
]
except
Exception as err:
logging.error(
"请求智能插座状态出现异常,报错信息为:"
+
str
(err))
raise
err
def
close_socket(socket_ip:
str
,socket_token:
str
):
base_order
=
'miiocli device --ip "%s" --token "%s" raw_command set_properties "[{\'did\': \'MYDID\', \'siid\': 2, \'piid\': 1, \'value\':False}]"'
now_order
=
base_order
%
(socket_ip,socket_token)
try
:
logging.debug(
"开始关闭智能插座"
)
answer
=
os.popen(now_order).read()
answer
=
str_to_dict(answer)
answer
=
get_socket_status(socket_ip
=
socket_ip,socket_token
=
socket_token)
if
answer
=
=
False
:
logging.debug(
"关闭智能插座成功"
)
return
True
else
:
logging.warning(
"关闭智能插座失败"
)
return
False
except
Exception as err:
logging.error(
"关闭智能插座发生报错,内容为:"
+
str
(err))
return
False
def
open_socket(socket_ip:
str
,socket_token:
str
):
base_order
=
'miiocli device --ip "%s" --token "%s" raw_command set_properties "[{\'did\': \'MYDID\', \'siid\': 2, \'piid\': 1, \'value\':True}]"'
now_order
=
base_order
%
(socket_ip,socket_token)
try
:
logging.debug(
"开始打开智能插座"
)
answer
=
os.popen(now_order).read()
answer
=
str_to_dict(answer)
answer
=
get_socket_status(socket_ip
=
socket_ip,socket_token
=
socket_token)
if
answer
=
=
True
:
logging.debug(
"智能插座打开成功"
)
return
True
else
:
logging.warning(
"智能插座打开失败"
)
return
False
except
Exception as err:
logging.error(
"智能插座打开时发生异常,报错为:"
+
str
(err))
return
False
def
temperature_judge(open_min:
float
,open_max:
float
,temperature_file_path:
str
):
now_temperature
=
read_temperature(file_path
=
temperature_file_path)
if
now_temperature[
0
]
=
=
False
:
exit()
else
:
now_temperature
=
now_temperature[
-
1
]
if
now_temperature<open_min:
logging.debug(
"当前温度为%s,最低温度为:%s,程序判定打开开关"
%
(
str
(now_temperature),
str
(open_min)))
return
True
if
now_temperature>open_max:
logging.debug(
"当前温度为%s,最低温度为:%s,程序判定关闭开关"
%
(
str
(now_temperature),
str
(open_min)))
return
False
if
now_temperature>
=
open_min
and
now_temperature<
=
open_max:
logging.debug(
"当前温度为%s,最低温度为:%s,程序判定无需操作"
%
(
str
(now_temperature),
str
(open_min)))
return
-
1
def
change_time_to_dict(time_data:
str
):
time_data
=
str
(time_data).replace(
" "
,"
").split("
:")
return
{
"hour"
:
int
(time_data[
0
]),
"min"
:
int
(time_data[
-
1
])
}.copy()
def
time_judge(start_time:
str
,end_time:
str
):
localtime
=
time.localtime(time.time())
now_hour
=
localtime.tm_hour
now_min
=
localtime.tm_min
start_time
=
change_time_to_dict(start_time)
end_time
=
change_time_to_dict(end_time)
if
now_hour > start_time[
"hour"
]:
if
now_hour<end_time[
"hour"
]:
return
True
elif
now_hour
=
=
end_time[
"hour"
]:
if
now_min <
=
end_time[
"min"
]:
return
True
elif
now_hour
=
=
start_time[
"hour"
]:
if
now_min>
=
start_time[
"min"
]:
if
now_hour<end_time[
"hour"
]:
return
True
elif
now_hour
=
=
end_time[
"hour"
]:
if
now_min <
=
end_time[
"min"
]:
return
True
return
False
def
main():
global
temperature_num
logging.debug(
"程序启动"
)
try
:
check_time1
=
time_judge(start_time1,end_time1)
check_time2
=
time_judge(start_time2,end_time2)
if
check_time1
=
=
True
or
check_time2
=
=
True
:
logging.debug(
"程序判断在任务执行时间段中"
)
temperature
=
temperature_judge(open_min
=
open_temperature_min,open_max
=
open_temperature_max,temperature_file_path
=
temperature_file_path)
if
temperature
=
=
-
1
:
pass
else
:
socket_status
=
get_socket_status(socket_ip
=
socket_ip,socket_token
=
socket_token)
if
socket_status!
=
temperature:
if
temperature:
open_Answer
=
open_socket(socket_ip
=
socket_ip,socket_token
=
socket_token)
if
open_Answer:
print
(
"开关打开操作成功"
)
else
:
print
(
"开关打开操作失败"
)
else
:
close_Answer
=
close_socket(socket_ip
=
socket_ip,socket_token
=
socket_token)
if
close_Answer:
print
(
"开关关闭操作成功"
)
else
:
print
(
"开关关闭操作失败"
)
except
Exception as err:
logging.error(
"主程序报错,报错内容为:"
+
str
(err))
finally
:
time.sleep(
10
)
main()