Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 925 lines (863 sloc) 27.3 KB
import RPi.GPIO as GPIO #import GPIO
import time, socket, datetime, os, fcntl, struct, sqlite3 #import time, IP, time and date
from flask import Flask, render_template, request, jsonify, request #import web app
app = Flask(__name__) #Assigns app
#
localtime = time.asctime( time.localtime(time.time()) ) #Gets the time
print (" * Current time :" + str(localtime)) #Displays Time
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Device started <br> \n") #Logs startup
def get_ip_address(ifname): #Takes in interface name
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)#Gets socket address
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
IP = get_ip_address('wlan0') #Gets the IP address of wireless card
#IP = "192.168.0.68"
#IP = "192.168.0.3"
Stop = 0
print(" * " + IP) #Displays IP
GPIO.setwarnings(False) #Set GPIO warnings as false
Page1 = 'relay.html' #Assigns page 1
Settings = 'settings.html' #Assigns settings
global relayTime
f = open("rTime", "r") #Opens the relay time delay
relayTime = float(f.read())#Assigns to variable
#f.write("0.1")
f.close() #Closes file
global SOSVal
SOSVal = 0 #Sets SOS value to 0
def WriteLogs():
f = open("templates/Log.html", "a") #Opens the HTML log file
f.write(Log) #Writes the log to the file
f.close() #Closes the file
WriteLogs() #Starts the function
def Write():
f = open(str(RelayGPIO), "w") #OPens the file named [RelayGPIO]
f.write(ToWrite) #Writes the value
f.close() #Closes file
WriteLogs() #Starts the 'WriteLogs' function
def On():
global ToWrite
global Log
Relay(RelayGPIO) #Starts the relay function
Log = (str(time.asctime( time.localtime(time.time()) ))+ " - " +(str(RelayGPIO) + " (" + str(RelayGPIOOut) + ") has been turned on <br> \n")) #Assigns variable 'Log'
GPIO.output(RelayGPIOOut, GPIO.LOW) #Relay on
ToWrite = "1" #Assigns 'ToWrite' to 1
Write() #Starts the 'Write' function
rApply()
def Off():
global ToWrite
global Log
Relay(RelayGPIO) #Starts the relay function
Log = (str(time.asctime( time.localtime(time.time()) ))+ " - " +(str(RelayGPIO) + " (" + str(RelayGPIOOut) + ") has been turned off <br> \n")) #Assigns variable 'Log'
GPIO.output(RelayGPIOOut, GPIO.HIGH) #Relay off
ToWrite = "0" #Assigns 'ToWrite' to 0
Write() #Starts the 'Write' function
rApply()
def Setup():
global pinList
global Log
GPIO.setmode(GPIO.BCM) #Sets the GPIO mode as BCM
#listindex 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
pinList = [ 2, 3, 4, 17, 27, 22, 10, 9, 18, 23, 24, 25, 0, 7, 1, 12] #Sets the list of pins required to 'pinList'
for i in pinList: #For every value in 'pinList':
GPIO.setup(i, GPIO.OUT) #Sets the GPIO to output
GPIO.output(i, GPIO.HIGH)
Setup()
def StartTest(): #Testing feature, disabled normally
for i in pinList: #For every value in 'pinList':
GPIO.output(i, GPIO.LOW) #Relay on
time.sleep(0.03) #Delay of 0.07 seconds
GPIO.output(i, GPIO.HIGH) #Relay off
#StartTest()
def RebootSetup():
x = 0 #Sets x to 0
for i in pinList:
f = open(str(x), "r")
if f.read() == "1": #If the contents of x = 1:
GPIO.output(i, GPIO.LOW) #Relay on
print(" * Relay " + str(x) + " is on") #Print to console
elif f.read() == "0": #If the contents of x = 0:
print(" * Relay " + str(x) + " is off") #Print to console
else: #If neither are true
print(" * Relay " + str(x) + " has no value") #Print to console
GPIO.output(i, GPIO.HIGH) #Relay off
f.close()
x = x + 1
RebootSetup() #Start function
def Relay(RelayGPIO):
global RelayGPIOOut
RelayGPIOOut = pinList[RelayGPIO] #Get the value of index [RelayGPIO] in pinList and set to 'RelayGPIOOut'
def nightSailPins():
global PreOn
global PreOff
PreOn = [0, 1, 3, 8, 7, 6] #Set 'PreOn' to list
PreOff = [2] #Set 'PreOff' to list
Presetset() #Start function 'Presetset'
def leisurePins():
global PreOn
global PreOff
PreOn = [2, 4, 5, 8, 9] #Set 'PreOn' to list
PreOff = [0, 1, 3, 6, 7] #Set 'PreOff' to list
Presetset() #Start function 'Presetset'
def soloPins():
global PreOn
global PreOff
PreOn = [3, 6, 8] #Set 'PreOn' to list
PreOff = []
Presetset() #Start function 'Presetset'
def Presetset():
global RelayGPIO
for i in PreOn: #For all values in 'PreOn'
RelayGPIO = i #Sets RelayGPIO as value of i
Relay() #Start function 'Relay'
On() #Start function 'On'
for i in PreOff: #For all values in 'PreOff'
RelayGPIO = i #Sets RelayGPIO as value of i
Relay() #Start function 'Relay'
Off() #Start function 'Off'
@app.route('/') #Sets index
def index():
rApply()
return render_template('relay.html') #Displays HTML page 'Relay.html'
#-------------------------------------------
@app.route('/Settings/') #Sets 'Settings'
def Settings():
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Settings Accessed <br> \n") #Assigns variable 'Log'
WriteLogs()
return render_template('settings.html') #Displays HTML page 'settings.html'
#-------------------------------------------
@app.route('/AllOn/') #Sets 'AllOn'
def AllOn():
x = 0 #Sets x = 0
global RelayGPIO
settingrelay = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"]
while x < 16: #While x is less than 16
i = settingrelay[x]
ac = open("rSet/activation" + i , "r")
if ac.read() == "1":
RelayGPIO = x #Sets RelayGPIO to x
On() #Start function 'On'
else:
pass
x = x + 1 #x + 1
return render_template('relay.html') #Displays HTML page 'relay.html'
@app.route('/AllOff/') #Sets 'AllOff'
def AllOff():
x = 0 #Sets x = 0
global RelayGPIO
settingrelay = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"]
while x < 16: #While x is less than 16
i = settingrelay[x]
ac = open("rSet/activation" + i , "r")
if ac.read() == "1":
RelayGPIO = x #Sets RelayGPIO to x
Off() #Start function 'On'
else:
pass
x = x + 1 #x + 1
return render_template('relay.html') #Display HTML page 'relay.html'
#===========================================
#===========================================
@app.route("/ZeroOn/") #Sets 'ZeroOn'
def ZeroOn():
global RelayGPIO
RelayGPIO = 0 #Sets 'RelayGPIO' to 0
On() #Start function 'On'
return render_template(Page1)
@app.route("/ZeroOff/")
def ZeroOff():
global RelayGPIO
RelayGPIO = 0 #Sets 'RelayGPIO' to 0
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/OneOn/")
def OneOn():
global RelayGPIO
RelayGPIO = 1 #Sets 'RelayGPIO' to 1
On() #Start function 'On'
return render_template(Page1)
@app.route("/OneOff/")
def OneOff():
global RelayGPIO
RelayGPIO = 1 #Sets 'RelayGPIO' to 1
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/TwoOn/")
def TwoOn():
global RelayGPIO
RelayGPIO = 2 #Sets 'RelayGPIO' to 2
On() #Start function 'On'
return render_template(Page1)
@app.route("/TwoOff/")
def TwoOff():
global RelayGPIO
RelayGPIO = 2 #Sets 'RelayGPIO' to 2
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/ThreeOn/")
def ThreeOn():
global RelayGPIO
RelayGPIO = 3 #Sets 'RelayGPIO' to 3
On() #Start function 'On'
return render_template(Page1)
@app.route("/ThreeOff/")
def ThreeOff():
global RelayGPIO
RelayGPIO = 3 #Sets 'RelayGPIO' to 3
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/FourOn/")
def FourOn():
global RelayGPIO
RelayGPIO = 4 #Sets 'RelayGPIO' to 4
On() #Start function 'On'
return render_template(Page1)
@app.route("/FourOff/")
def FourOff():
global RelayGPIO
RelayGPIO = 4 #Sets 'RelayGPIO' to 4
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/FiveOn/")
def FiveOn():
global RelayGPIO
RelayGPIO = 5 #Sets 'RelayGPIO' to 5
On() #Start function 'On'
return render_template(Page1)
@app.route("/FiveOff/")
def FiveOff():
global RelayGPIO
RelayGPIO = 5 #Sets 'RelayGPIO' to 5
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/SixOn/")
def SixOn():
global RelayGPIO
RelayGPIO = 6 #Sets 'RelayGPIO' to 6
On() #Start function 'On'
return render_template(Page1)
@app.route("/SixOff/")
def SixOff():
global RelayGPIO
RelayGPIO = 6 #Sets 'RelayGPIO' to 6
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/SevenOn/")
def SevenOn():
global RelayGPIO
RelayGPIO = 7 #Sets 'RelayGPIO' to 7
On() #Start function 'On'
return render_template(Page1)
@app.route("/SevenOff/")
def SevenOff():
global RelayGPIO
RelayGPIO = 7 #Sets 'RelayGPIO' to 7
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/EightOn/")
def EightOn():
global RelayGPIO
RelayGPIO = 8 #Sets 'RelayGPIO' to 8
On() #Start function 'On'
return render_template(Page1)
@app.route("/EightOff/")
def EightOff():
global RelayGPIO
RelayGPIO = 8 #Sets 'RelayGPIO' to 8
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/NineOn/")
def NineOn():
global RelayGPIO
RelayGPIO = 9 #Sets 'RelayGPIO' to 9
On() #Start function 'On'
return render_template(Page1)
@app.route("/NineOff/")
def NineOff():
global RelayGPIO
RelayGPIO = 9 #Sets 'RelayGPIO' to 9
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/TenOn/")
def TenOn():
global RelayGPIO
RelayGPIO = 10 #Sets 'RelayGPIO' to 10
On() #Start function 'On'
return render_template(Page1)
@app.route("/TenOff/")
def TenOff():
global RelayGPIO
RelayGPIO = 10 #Sets 'RelayGPIO' to 10
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/ElevenOn/")
def ElevenOn():
global RelayGPIO
RelayGPIO = 11 #Sets 'RelayGPIO' to 11
On() #Start function 'On'
return render_template(Page1)
@app.route("/ElevenOff/")
def ElevenOff():
global RelayGPIO
RelayGPIO = 11 #Sets 'RelayGPIO' to 11
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/TwelveOn/")
def TwelveOn():
global RelayGPIO
RelayGPIO = 12 #Sets 'RelayGPIO' to 12
On() #Start function 'On'
return render_template(Page1)
@app.route("/TwelveOff/")
def TwelveOff():
global RelayGPIO
RelayGPIO = 12 #Sets 'RelayGPIO' to 12
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/ThirteenOn/")
def ThirteenOn():
global RelayGPIO
RelayGPIO = 13 #Sets 'RelayGPIO' to 13
On() #Start function 'On'
return render_template(Page1)
@app.route("/ThirteenOff/")
def ThirteenOff():
global RelayGPIO
RelayGPIO = 13 #Sets 'RelayGPIO' to 13
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/FourteenOn/")
def FourteenOn():
global RelayGPIO
RelayGPIO = 14 #Sets 'RelayGPIO' to 14
On() #Start function 'On'
return render_template(Page1)
@app.route("/FourteenOff/")
def FourteenOff():
global RelayGPIO
RelayGPIO = 14 #Sets 'RelayGPIO' to 14
Off() #Start function 'Off'
return render_template(Page1)
#-------------------------------------------
@app.route("/FifteenOn/")
def FifteenOn():
global RelayGPIO
RelayGPIO = 15 #Sets 'RelayGPIO' to 15
On() #Start function 'On'
return render_template(Page1)
@app.route("/FifteenOff/")
def FifteenOff():
global RelayGPIO
RelayGPIO = 15 #Sets 'RelayGPIO' to 15
Off() #Start function 'Off'
return render_template(Page1)
#----------------------------------------
@app.route('/Test/')
def Test():
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Relay Tested <br> \n") #Sets 'Log'
WriteLogs() #Start function 'WriteLogs'
global Stop
if Stop == 1:
Stop = 0
return render_template('relay.html')
else:
pass
for i in pinList: #For all values in 'pinList'
GPIO.output(i, GPIO.LOW) #Relay On
time.sleep(relayTime) #delay
for i in pinList: #For all values in 'pinList'
GPIO.output(i, GPIO.HIGH) #Relay Off
time.sleep(relayTime) #delay
for i in reversed(pinList): #For all values in 'pinList' reversed
GPIO.output(i, GPIO.LOW) #Relay On
time.sleep(relayTime) #delay
for i in reversed(pinList): #For all values in 'pinList' reversed
GPIO.output(i, GPIO.HIGH) #Relay Off
time.sleep(relayTime) #delay
RebootSetup() #Start function 'RebootSetup'
return render_template('settings.html')
@app.route('/Test2/')
def Test2():
T2Time = 0.1
global Log
global Stop
Log = ((time.asctime( time.localtime(time.time()) )) + " - Relay 2 Tested <br> \n") #Sets 'Log'
WriteLogs() #Start function 'WriteLogs'
for i in pinList: #For all values in 'pinList'
GPIO.output(i, GPIO.LOW) #Relay On
time.sleep(T2Time) #delay
GPIO.output(i, GPIO.HIGH) #Relay Off
#time.sleep(T2Time)
for i in reversed(pinList): #For all values in 'pinList' reversed
GPIO.output(i, GPIO.LOW) #Relay On
time.sleep(T2Time) #delay
GPIO.output(i, GPIO.HIGH) #Relay Off
#time.sleep(T2Time)
RebootSetup() #Start function 'RebootSetup'
return render_template('settings.html')
#----------------------------------------
@app.route('/Presets/')
def Presets():
return render_template('presets.html')
#-------------------------------------------
@app.route('/NightSail/')
def NightSail():
nightSailPins()
return render_template('relay.html')
#--------------------------------------------
@app.route('/Leisure/')
def Leisure():
leisurePins()
return render_template('relay.html')
#--------------------------------------------
@app.route('/Solo/')
def SoloSail():
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Solo Preset Selected <br> \n")
WriteLogs()
soloPins()
return render_template('relay.html')
#--------------------------------------------
@app.route('/relay2/')
def P2():
return render_template('relay2.html')
@app.route('/GUIExt/')
def GUIExt():
return render_template('GUI.html')
def SHT():
f = open("passwd", "r")
sudoPassword = f.read()
f.close()
p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))
os.popen("sudo -S %s"%(command), 'w').write('mypass')
WriteLogs()
@app.route('/ShutdownClear/')
def ShutdownClear():
global Log
global command
Log = ((time.asctime( time.localtime(time.time()) )) + " - Device Clear Shutdown <br> \n")
command = 'shutdown now'
AllOff()
SHT()
@app.route('/Shutdown/')
def Shutdown():
global Log
global command
Log = ((time.asctime( time.localtime(time.time()) )) + " - Device Shutdown <br> \n")
command = 'shutdown now'
SHT()
@app.route('/Reboot/')
def Reboot():
global Log
global command
Log = ((time.asctime( time.localtime(time.time()) )) + " - Device Rebooted <br> \n")
command = 'reboot'
SHT()
@app.route('/Logs/')
def Logs():
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Logs Accessed <br> \n")
WriteLogs()
return render_template('Log.html')
@app.route('/delLogs/')
def delLogs():
f = open("templates/Log.html", "w")
f.write('<form action="/delLogs/"> \n')
f.write('<input style="background-color:salmon; -webkit-appearance: none; width:100%; margin-bottom: 5px;font-size: 16px;padding: 12px 40px 12px 40px;" type="submit" value="Delete Logs" /> \n')
f.write("</form> \n")
f.write('<form action="/Settings/">\n')
f.write(' <input style="background-color:white; -webkit-appearance: none; width:100%;margin-bottom: 5px;font-size: 16px;padding: 12px 40px 12px 40px;" type="submit" value="Back" />\n')
f.write('</form>\n')
f.write("Logs deleted on " + (str(time.asctime( time.localtime(time.time()) )))+"<br> ===============================<br> \n")
f.close()
return render_template('Log.html')
def S():
global RelayGPIO
x = 0
global stop
SSleep = 0.2
while x <= 2:
for i in SOS:
RelayGPIO = i
Relay()
On()
time.sleep(SSleep)
for i in SOS:
RelayGPIO = i
Relay()
Off()
time.sleep(SSleep)
x = x + 1
def O():
global RelayGPIO
x = 0
global stop
OSleep = 0.5
while x <= 2:
for i in SOS:
RelayGPIO = i
Relay()
On()
time.sleep(OSleep)
for i in SOS:
RelayGPIO = i
Relay()
Off()
time.sleep(OSleep)
x = x + 1
def SOSinfo():
f = open("templates/SOSInfo.html", "w")
f.write('<style> body {background-color: lightblue;} form:active { background-color: black; box-shadow: 0 5px #666; transform: translateY(4px); <body> </style>')
f.write('<body> </body>')
f.write('<form action="/sos/"> \n')
f.write('<input style="background-color:salmon; -webkit-appearance: none; width:100%; margin-bottom: 5px;font-size: 16px;padding: 12px 40px 12px 40px;" type="submit" value="Send SOS" /> \n')
f.write("</form> \n")
f.write('<form action="/Settings/">\n')
f.write(' <input style="background-color:white; -webkit-appearance: none; width:100%;margin-bottom: 5px;font-size: 16px;padding: 12px 40px 12px 40px;" type="submit" value="Back" />\n')
f.write('</form>\n')
f.write("<h1 align = 'center' > SOS sent " + (str(SOSVal)) + " time(s) </h1>")
#f.write('</body>')
#f.write(SOSToGo)
f.close()
@app.route("/sos/")
def SOS():
global RelayGPIO
global SOS
global SOSVal
global Log
SOS = [0, 1, 7]
S()
O()
S()
SOSVal = SOSVal + 1
SOSinfo()
Log = ((time.asctime( time.localtime(time.time()) )) + " - SOS Sent <br> \n")
WriteLogs()
return render_template('SOSInfo.html')
@app.route("/StopFunc/")
def StopFunc():
global Stop
Stop = 1
return render_template('relay.html')
@app.route("/sosi/")
def SOSi():
global SOS
SOS = [0, 1, 7]
Log = ((time.asctime( time.localtime(time.time()) )) + " - SOS Sent Infinitely <br> \n")
WriteLogs()
while 0 == 0:
global Stop
if Stop == 1:
Stop = 0
Off()
return render_template('relay.html')
else:
pass
S()
O()
S()
@app.route("/random/")
def Random():
import time, random
t = (0.01)
timeDiff = 0
for i in pinList:
time.sleep(t)
h = random.randint(0, 15)
GPIO.output(pinList[h], GPIO.HIGH)
#time.sleep(timeDiff)
for i in pinList:
time.sleep(t)
h = random.randint(0, 15)
GPIO.output(pinList[h], GPIO.LOW)
return render_template('fun.html')
@app.route("/Exponential/")
def Exponential():
sleep = 0.2
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Exponential Used <br> \n")
WriteLogs()
while 1 > 0:
global Stop
if Stop == 1:
Stop = 0
return render_template('relay.html')
else:
pass
if sleep == 0.001:
return render_template('fun.html')
else:
print(sleep)
sleep = sleep - 0.001
time.sleep(sleep)
print(sleep)
for i in pinList:
GPIO.setup(i, GPIO.OUT)
#time.sleep(sleep)
GPIO.output(i, GPIO.LOW)
time.sleep(sleep)
print(sleep)
for i in pinList:
GPIO.setup(i, GPIO.OUT)
#time.sleep(sleep)
GPIO.output(i, GPIO.HIGH)
@app.route('/Splash/')
def Splash():
return render_template('StartScr.html')
@app.route('/Exit/')
def Exit():
exit()
@app.route("/randominfinite/")
def RandomInf():
import time, random
global stop
t = (0.1)
timeDiff = 0
while 0 <= 1:
global Stop
if Stop == 1:
Stop = 0
Off()
return render_template('relay.html')
else:
pass
for i in pinList:
time.sleep(t)
h = random.randint(0, 15)
GPIO.output(pinList[h], GPIO.HIGH)
#time.sleep(timeDiff)
for i in pinList:
time.sleep(t)
h = random.randint(0, 15)
GPIO.output(pinList[h], GPIO.LOW)
@app.route("/fun/")
def fun():
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + " - Fun Accessed <br> \n")
WriteLogs()
return render_template("fun.html")
@app.route("/about/")
def about():
return render_template("about.html")
@app.route("/developer/")
def developer():
return render_template("developer.html")
@app.route("/autohelm/")
def autohelm():
return render_template("autohelm.html")
@app.route("/ip/")
def displayIP():
f = open("templates/DIP.html", "w")
f.write('<center style="margin: 5px;"> <h1> Current IP </h1> <hr style="border: 1px solid black;" /> <h3>' + IP + ":5000 </h3> </center>")
f.write('<form action="/about/"> \n <hr style="border: 1px solid black;" />')
f.write('<style> body {background-color: lightblue;} form:active { background-color: black; box-shadow: 0 5px #666; transform: translateY(4px); <body> </style>')
f.write('<input style="background-color:white; -webkit-appearance: none; width:100%; margin-bottom: 5px;font-size: 16px;padding: 12px 40px 12px 40px;" type="submit" value="Back" /> \n')
f.write("</form> \n")
f.close()
return render_template("DIP.html")
@app.route("/RelaySetup/")
def relaySetup():
return render_template("RelaySetup.html")
@app.route("/RelaySetup/", methods=['POST', 'GET'])
def rSetup():
text = request.form['text']
#relay = request.form['relay']
relayform = request.form.get('relay')
processed_text = "Relay " + relayform + " has been set to " + text
print(str(relayform))
filename = "rSet/" + str(relayform)
print(filename)
f = open(filename, "w")
ac = open("rSet/activation" + relayform, "w")
ac.write("1")
ac.close()
f.write('<form action="/' + str(relayform) + 'On/"><input style="background-color:lime; float:left; margin-bottom:1px;" type="submit" value="' + text + ' On"/></form><form action="/' + str(relayform) + 'Off/"><input type="submit" style="background-color:red; float:right; margin-bottom:1px;" value="' + text + ' Off"></form>')
f.close()
h = open("templates/RsetInfo.html", "w")
s = open("rSet/static", "r")
nb = open("templates/RelaySetup.html", "r")
h.write(s.read() + "<center>" + processed_text + "</center>" + nb.read())
h.close()
global Log
Log = ((time.asctime( time.localtime(time.time()) )) + processed_text + " <br> \n")
Logs()
time.sleep(1)
return render_template("RsetInfo.html")
@app.route("/RelayDelete/")
def relayDelete():
return render_template("RelayDelete.html")
@app.route("/RelayDelete/", methods=['POST', 'GET'])
def deleteRelay():
relayform = request.form.get('relay')
processed_text = "Relay " + relayform + " has been deleted"
filename = "rSet/" + str(relayform)
print(filename)
f = open(filename, "w")
f.write("")
f.close()
ac = open("rSet/activation" + relayform, "w")
ac.write("0")
ac.close()
h = open("templates/RdelInfo.html", "w")
s = open("rSet/static", "r")
nb = open("templates/RelayDelete.html", "r")
h.write(s.read() + "<center>" + processed_text + "</center>" + nb.read())
h.close()
time.sleep(1)
return render_template("RdelInfo.html")
@app.route("/RApply/")
def rApply():
s=open("rSet/static", "r")
f=open("templates/relay.html", "w")
f.write(s.read())
s.close()
s = open("rSet/staticbuttons", "r")
f.write(s.read())
s.close()
f.close()
f=open("templates/relay.html", "a")
x = 0
settingrelay = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"]
for i in settingrelay:
g = open("rSet/" + i, "r")
button = g.read()
f.write(button)
g.close()
ac=open("rSet/activation" + i, "r")
if ac.read() == "1":
rs=open(str(x), "r")
if rs.read() == "1":
colour = "green"
else:
colour = "red"
f.write('<hr style="border: 1px solid '+colour+';" />')
rs.close()
else:
pass
ac.close()
x = x + 1
s = open("rSet/staticend", "r")
f.write(s.read())
s.close()
f.close()
time.sleep(0)
return render_template("relay.html")
@app.route("/CurrentRelaySetup/")
def CRS():
s=open("rSet/static", "r")
f=open("templates/crs.html", "w")
f.write(s.read())
s.close()
s = open("rSet/staticbuttons", "r")
f.write(s.read())
s.close()
f.close()
f=open("templates/crs.html", "a")
f.write("<center>")
settingrelay = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"]
for i in settingrelay:
g = open("rSet/" + i, "r")
if g.read == "":
f.write(i + "is unassigned \n")
else:
button = g.read()
f.write(i)
f.write(button)
f.write('<hr style="border: 1px solid black;" />')
g.close()
f.write('</center> <form action="/RelaySetup/"><input style="background-color:white; width:100%; margin-bottom: 5px; float:right;" type="submit" value="Setup Relays" /></form>')
f.close()
time.sleep(0.1)
return render_template("crs.html")
def FILESETUP():
settingrelay = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen"]
for i in settingrelay:
g = open("rSet/activation" + i, "w")
g.write("")
g.close()
def createRelayTable():
relayinfo = sqlite3.connect('relay.db')
relaycursor = relayinfo.cursor()
relaycursor.execute('''DROP TABLE RELAY''')
relaycursor.execute('''CREATE TABLE RELAY (RELAY_NAME text, GPIO_NUMBER int, RELAY_NUMBER int, RELAY_STATE text)''')
relayinfo.commit()
def relayDatabase():
relayinfo = sqlite3.connect('relay.db')
relaycursor = relayinfo.cursor()
relaycursor.execute('''DROP TABLE RELAY''')
relaycursor.execute('''CREATE TABLE RELAY (RELAY_NAME text, RELAY_NUMBER int, GPIO_NUMBER int, RELAY_STATE text)''')
RelayGPIO = 0
Relay(RelayGPIO)
relaycursor.execute("INSERT INTO RELAY VALUES ('Navigation Lights', '" + str(RelayGPIOOut) + "', 'True')")
relayinfo.commit()
for row in relaycursor.execute('SELECT * FROM RELAY'):
print row
for row in relaycursor.execute('SELECT RELAY_NUMBER FROM RELAY WHERE RELAY_STATE = "True"'):
print row
def clearRelayDatabase():
relayinfo = sqlite3.connect('relay.db')
relaycursor = relayinfo.cursor()
relaycursor.execute("DELETE FROM RELAY")
x = 0
for i in pinList:
relaycursor.execute("INSERT INTO RELAY VALUES ('NULL', '" + str(x) + "', '" + str(i) + "', 'False')")
x = x + 1
relayinfo.commit()
for row in relaycursor.execute('SELECT * FROM RELAY'):
print row
def updateName(relayNumber, relayName):
relayinfo = sqlite3.connect('relay.db')#
relaycursor = relayinfo.cursor()
relaycursor.execute("UPDATE RELAY SET RELAY_NAME = '" + relayName + "' WHERE RELAY_NUMBER = '" + relayNumber + "'")
def updateState(relayNumber, relayState):
relayinfo = sqlite3.connect('relay.db')#
relaycursor = relayinfo.cursor()
relaycursor.execute("UPDATE RELAY SET RELAY_STATE = '" + relayState + "' WHERE RELAY_NUMBER = '" + relayNumber + "'")
def displayDatabase():
relayinfo = sqlite3.connect('relay.db')
relaycursor = relayinfo.cursor()
for state in relaycursor.execute("SELECT * FROM RELAY"):
print state
def checkStateDatabase():
relayinfo = sqlite3.connect('relay.db')
relaycursor = relayinfo.cursor()
relaycursor.execute("SELECT RELAY_STATE FROM RELAY")
for i in pinList:
for state in relaycursor.execute("SELECT RELAY_STATE FROM RELAY WHERE GPIO_NUMBER = '" + str(i) + "'"):
if state == "(u'True',)":
print(str(i) + " is on")
print(str(i) + " is " + str(state))
else:
print(str(i) + " is " + str(state))
#createRelayTable()
updateState("0", "True")
checkStateDatabase()
#updateName("Navigation_Lights", "0")
displayDatabase()
#clearRelayDatabase()
#relayDatabase()
if __name__ == '__main__':
app.run(debug=True, host=(str(IP)), threaded=True)
# app.run(debug=True, host=("192.168.0.68"))
#app.run(host=(str(IP)))
GPIO.cleanup()