Skip to content

Commit

Permalink
Fixed authentication check. Added ipmailer script to send mails when …
Browse files Browse the repository at this point in the history
…public ip changes. Added phonecharger script. Added styling for the homepage.
  • Loading branch information
Ankit Rajpoot committed Nov 29, 2018
1 parent d25c1a4 commit acebf9b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

CHARGER_GPIO = 2
CHARGER_RUNTIME = 10800 # 3 Hours
CHARGER_OFFTIME = 10800 # 3 Hours
CHARGER_OFFTIME = 10800 # 3 Hours
5 changes: 3 additions & 2 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def logPumpRun(entry):
def getLatestPumpRun():
with rlock:
pumpRun = Query()
if (len(pumpDB.search(pumpRun.event == 'Pump Run')) > 0):
return pumpDB.all()[-1]
searchResult = pumpDB.search(pumpRun.event == "Pump Run")
if (len(searchResult) > 0):
return searchResult[-1]
else:
return None

Expand Down
18 changes: 18 additions & 0 deletions ipmailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import smtplib
import json
import urllib.request

config = {}
ip = urllib.request.urlopen("https://api.ipify.org").read().decode("utf-8")

try:
with open("../config.json", "r") as configJson:
config = json.load(configJson)
finally:
if ("wanip" in config) is False or config["wanip"] != ip:
#Update IP
urllib.request.urlopen("https://www.randomgarage.com/postip.php?ip=" + ip)
config["wanip"] = ip

with open("../config.json", "w") as configJson:
json.dump(fp = configJson, obj = config)
16 changes: 16 additions & 0 deletions phonecharger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
#
#
import gpiozero
import time

chargeRelay = gpiozero.OutputDevice(1, active_high = False,
initial_value = False)

while True:
# Charge it for 3 hours
chargeRelay.on()
time.sleep(10800)
# Switch off power for 20 minutes
chargeRelay.off()
time.sleep(1200)
10 changes: 10 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
html {
font-size: 1em;
background: antiquewhite;
}

body {
width: 50%;
margin: auto;
background: white;
padding: 10px;
border-left: 1px solid lightblue;
border-right: 1px solid lightblue;
}

table {
Expand Down
26 changes: 17 additions & 9 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@

class Index:
def GET(self):
if web.ctx.env.get('HTTP_AUTHORIZATION') is None:
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
else:
temp = os.popen("vcgencmd measure_temp").readline().replace('temp=', '')
return html.index(temp)

class Logs:
def GET(self, logcat, since):
if web.ctx.env.get('HTTP_AUTHORIZATION') is None:
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
elif(logcat == 'pump'):
if (since == 'all'):
Expand All @@ -48,25 +50,31 @@ def GET(self):
if auth is None:
authreq = True
else:
auth = re.sub('^Basic ','',auth)
username,password = base64.decodestring(auth).split(':')
if (username,password) in users:
raise web.seeother('/')
else:
authreq = True
if validateUser(auth):
raise web.seeother('/')
else:
authreq = True
if authreq:
web.header('WWW-Authenticate','Basic realm="WaterPI"')
web.ctx.status = '401 Unauthorized'
return

class Operations:
def GET(self, operation):
if web.ctx.env.get('HTTP_AUTHORIZATION') is None:
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
elif(operation == 'runpump'):
os.popen("python3 pumpcontroller.py runnow &")
return "Success"

def validateUser(auth):
auth = re.sub('^Basic ','',auth)
username,password = base64.decodestring(auth).split(':')
if (username, password) in users:
return True
else:
return False

def startServer():
app = web.application(urls, globals())
Expand Down
1 change: 0 additions & 1 deletion webtemplates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ <h3>System Status</h3>

<button onclick="window.open('/operation/runpump')">Run Pump Now</button>
<br />
<button onclick="window.open('https://www.youtube.com/watch?v=9xeAiB_-MD0&feature=youtu.be')">Start Live Feed</button>
<h3>Recent Pump Logs (Auto-updates every 30secs)</h3>
<div id="pump-logs-div">

Expand Down

0 comments on commit acebf9b

Please sign in to comment.