Skip to content

Commit

Permalink
Regular cleanup and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
BeamCtrl committed Apr 30, 2024
1 parent dbf1139 commit 66e3a29
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
3 changes: 2 additions & 1 deletion airiana-core.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def report_alive():
message += temp.decode("utf-8") + "<br>"
message += os.popen("df |grep RAM").read() + "<br>"
message += os.popen("df |grep var").read() + "<br>"
message += "Source:('" + os.popen("./geoloc.py printIp 2>/dev/null").read() + "\')<br>"
if os.path.lexists("RAM/error_rate"):
message += os.popen("cat RAM/error_rate").read() + "<br>"
except: # noqa do not care if this is failing
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions alive_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<html>
[DA]
</html>
"""

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:
Expand All @@ -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")
Expand All @@ -49,4 +52,4 @@
if len(mac) != 0:
open(filename, "w+").write(for_file)
except IOError:
print ("file missing", filename)
print ("file missing", filename)"""
4 changes: 3 additions & 1 deletion geoloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 11 additions & 7 deletions grapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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()

Expand Down
24 changes: 16 additions & 8 deletions request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__:
Expand Down
16 changes: 9 additions & 7 deletions status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 66e3a29

Please sign in to comment.