-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_states.py
executable file
·53 lines (45 loc) · 1.39 KB
/
get_states.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time, os.path, urllib, re
debug = False
log = False
path_to_files = '/tmp/'
states_values = [0] * 4
retry = 0
# Print debug function
def debug_print(text):
if debug:
print(text)
def log_action(text):
if log:
try:
log_file=open(path_to_files + 'log', 'a')
log_file.write(time.strftime("%d-%m-%Y, %H:%M:%S") + " " + text + "\n")
log_file.close()
except:
pass
while 1:
# Teploty z kotelny
states_file = open(path_to_files + 'states', 'w')
try:
data = urllib.urlopen("http://thermostat.pavoukovo.cz/states").read().decode() # Otevrit soubor
if data.find("html") == -1:
states_file.write(data)
retry = 0
debug_print("DEBUG: HTTP states read ok")
else:
retry += 1
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP states read failed, remote file not found (retry: " + str(retry) + ")")
if retry >= 5:
log_action("[get_states.py] HTTP read failed 5 times")
for i in range(0, 4):
states_file.write("%s\n" % "0") # Kdyz se nepodari otevrit soubor, zapsat 0
except:
retry += 1
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP states read failed (retry: " + str(retry) + ")")
if retry >= 5:
log_action("[get_states.py] HTTP read failed 5 times")
for i in range(0, 4):
states_file.write("%s\n" % "0") # Kdyz se nepodari otevrit soubor, zapsat 0
states_file.close()
time.sleep(5)