diff --git a/airiana-core.py b/airiana-core.py index 45bb090..5882165 100755 --- a/airiana-core.py +++ b/airiana-core.py @@ -199,6 +199,7 @@ def report_alive(): message += temp.decode("utf-8") + "
" message += os.popen("df |grep RAM").read() + "
" message += os.popen("df |grep var").read() + "
" + message += "Source:('" + os.popen("./geoloc.py printIp 2>/dev/null").read() + "\')
" if os.path.lexists("RAM/error_rate"): message += os.popen("cat RAM/error_rate").read() + "
" except: # noqa do not care if this is failing @@ -2263,8 +2264,8 @@ def fireplace_mode(dev): device.status_field[14] = round(device.exhaust_ave, 2) device.status_field[15] = round(device.pressure_diff, 2) device.status_field[16] = round(device.det_limit, 0) - report_alive() + if "humidity" in sys.argv: device.get_local() if "temperatur.nu" in sys.argv: diff --git a/alive_logger.py b/alive_logger.py index 84f3740..50ec8dd 100755 --- a/alive_logger.py +++ b/alive_logger.py @@ -5,18 +5,20 @@ import time import os import pathlib +import time + path = pathlib.Path(__file__).parent.resolve() os.chdir(path) print ("started in", os.getcwd()) -sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP +#sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP html = """ [DA] """ -sock.bind(("0.0.0.0", 59999)) +#sock.bind(("0.0.0.0", 59999)) while True: print(int(time.time()%60)) if int(time.time()) % 300 == 0: @@ -25,7 +27,8 @@ os.system("unzip -o RAM/filebin.zip -d public/local_links/ 2> RAM/unzip.out") os.system("rm RAM/filebin.zip") os.system("rm RAM/unzip.out") - if sock in select.select( [sock], [], [], 1)[0]: + time.sleep(1) + """if sock in select.select( [sock], [], [], 1)[0]: incoming_msg, addr=sock.recvfrom(37000) # print "***", incomming_msg, "***" # print incomming_msg.find("ether") @@ -49,4 +52,4 @@ if len(mac) != 0: open(filename, "w+").write(for_file) except IOError: - print ("file missing", filename) + print ("file missing", filename)""" diff --git a/geoloc.py b/geoloc.py index a97a38d..764cdd3 100755 --- a/geoloc.py +++ b/geoloc.py @@ -8,7 +8,9 @@ if ip.count(".") != 3: ip = eval(os.popen("curl \'https://api.ipify.org?format=json\'").read()) ip = ip["ip"] - + if "printIp" in sys.argv: + print(ip) + exit(0) # try to read a location location = os.popen("curl http://www.geoplugin.net/json.gp?ip=" + ip) try: diff --git a/grapher.py b/grapher.py index 0a4b6f7..92c34fa 100755 --- a/grapher.py +++ b/grapher.py @@ -20,15 +20,18 @@ else: day = 3600 * 24 lines = int(math.ceil(day / 5)) +seconds_remainder = tm.time() % 3600 +quarter_day = day / 24 * 4 if day > 3600 * 24: fil = os.popen("tail -n " + str(lines) + " ./data.log") data = fil.readlines() fil = os.popen("tail -n " + str(lines) + " ./RAM/data.log") data += fil.readlines() - else: fil = os.popen("tail -n " + str(lines) + " ./RAM/data.log") data = fil.readlines() + + sen_hum = [] sen_temp = [] extract = [] @@ -105,6 +108,7 @@ red_hum = [] red_time = [] i = 0 +max_time = max(time[-day:-1]) + 4 * 3600 for each in measured_hum: i += 1 if float(each) != 0.0: @@ -145,8 +149,8 @@ ax.set_xlim(min(time[-day:-1]), max(time[-day:-1])) except ValueError: sys.exit(0) -ax.xaxis.set_ticks(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) -ax.set_xticklabels(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) +ax.xaxis.set_ticks(np.arange(seconds_remainder, max_time, quarter_day)) +ax.set_xticklabels(np.arange(seconds_remainder, max_time, quarter_day)) ax.invert_xaxis() lgd = legend(bbox_to_anchor=(0.5, -0.3), loc=1, ncol=2, mode="expand", borderaxespad=.0) @@ -168,8 +172,8 @@ ax.yaxis.set_ticks(np.arange(low, high, 10)) ax.set_xlim(min(time[-day:-1]), max(time[-day:-1])) - ax.xaxis.set_ticks(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) - ax.set_xticklabels(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) + ax.xaxis.set_ticks(np.arange(seconds_remainder, max_time, quarter_day)) + ax.set_xticklabels(np.arange(seconds_remainder, max_time, quarter_day)) ax.invert_xaxis() lgd = legend(bbox_to_anchor=(0.5, -0.3), loc=2, ncol=2, mode="expand", borderaxespad=.0) @@ -187,8 +191,8 @@ low, high = ax.get_ylim() ax.yaxis.set_ticks(np.arange(low, high, 200)) ax.set_xlim(min(time[-day:-1]), max(time[-day:-1])) - ax.xaxis.set_ticks(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) - ax.set_xticklabels(np.arange(tm.time() % 3600, max(time[-day:-1]) + 4 * 3600, day / 24 * 4)) + ax.xaxis.set_ticks(np.arange(seconds_remainder, max_time, quarter_day)) + ax.set_xticklabels(np.arange(seconds_remainder, max_time, quarter_day)) lgd = legend(bbox_to_anchor=(0.5, -0.5), loc=0, ncol=2, mode="expand", borderaxespad=.0) ax.invert_xaxis() diff --git a/request.py b/request.py index f34adbb..cb98454 100644 --- a/request.py +++ b/request.py @@ -45,6 +45,7 @@ def __init__(self): self.latest_request_mode = "Single" self.latest_request_decimals = 0 self.latest_request_count = 0 + self.connection_timeout = 0 def setup(self, unit, mode): self.unit = unit @@ -80,6 +81,9 @@ def setup(self, unit, mode): test = self.comm_test() print("Comm test: ", test, ";\n") + def close(self): + self.client.serial.close() + def comm_test(self): print("Running Comm test;") fd = os.open("RAM/request.log", os.O_WRONLY | os.O_CREAT) @@ -128,9 +132,9 @@ def modbusregisters(self, start, count, signed=False): self.modbusregisters(start, count) except IOError: self.connect_errors += 1 - if self.connect_errors > 100 or self.multi_errors > 100: + if self.connect_errors > 1000 or self.multi_errors > 1000: self.error_review() - if self.rate < 0.9: + if self.rate < 0.99: self.modbusregisters(start, count) self.client.precalculate_read_size = False @@ -144,15 +148,19 @@ def error_review(self): self.multi_errors) / delta else: rate = 0.0 - if rate >= 0.9: + if rate >= 0.99: os.read(self.bus, 1000) - time.sleep(1) + self.connection_timeout += 1 + time.sleep(10) fd = os.open("RAM/err", os.O_WRONLY) os.lseek(fd, os.SEEK_SET, os.SEEK_END) os.write(fd, bytes("""read error high rate, - possible no communication with unit, error rate over 90%\n""", "utf-8")) + possible no communication with unit, error rate over 99%\n""", "utf-8")) + os.fsync(fd) os.close(fd) - exit(-1) + self.close() + self.setup(self.unit, self.mode) + #exit(-1) os.system("echo " + str(rate) + " " + str(self.wait_time) @@ -182,7 +190,7 @@ def modbusregister(self, address, decimals): signed=True) except (IOError, ValueError, TypeError): self.connect_errors += 1 - if self.connect_errors > 100: + if self.connect_errors > 1000: self.error_review() try: self.buff += os.read(self.bus, 20) # bus purge @@ -238,7 +246,7 @@ def write_register(self, reg, value, tries=10): except: with os.open("RAM/err", os.O_WRONLY) as fd: - os.write(fd, bytes("TCP write error on addrs:" + str(reg) + "\n", "utf-8")) + os.write(fd, bytes("TCP write error on addrs:" + str(reg) + "\n", "utf-8")) if "__main__" == __name__: diff --git a/status.py b/status.py index 40fcbb0..416ca40 100755 --- a/status.py +++ b/status.py @@ -47,12 +47,14 @@ def checkLocation(user): #print "user known",user, location[user] return None except KeyError: - with open("./public/local_links/"+user+".html") as log: - ip = log.read().split("Source:('")[-1].split("\'")[0] - #print "checking ip:",ip - loc = os.popen("./geoloc.py " + ip ).read() - location.update( {user:loc}) - #print "got new location", loc + if user.count("_") > 0: + with open("./public/local_links/"+user+".html") as log: + ip = log.read().split("Source:('")[-1].split("\'")[0] + if ip.count(".") == 3: + print ("checking ip:",ip) + loc = os.popen("./geoloc.py " + ip + " 2>/dev/null").read() + location.update( {user:loc}) + #print "got new location", loc def analyse_stat(status,user): # if "debug" in sys.argv: # print "Analyze", status, users[user] @@ -119,7 +121,7 @@ def analyse_stat(status,user): content = stat_field.replace("nan", " -1 ") #print content user = str(each.split(".")[0]) - #checkLocation(user) + checkLocation(user) #try: print "userID",user,"location:", location[user] #except: # print "\n"