Skip to content

Commit

Permalink
- In advance_control_status.html change button name to send latch sig…
Browse files Browse the repository at this point in the history
…nal.

- In advance_control.py simplify code.
- In advance_control.py use reboot signal to stop thread.
- In advance_control.py in latch send signal, wait same time if turn on.
  • Loading branch information
PedroFRCSantos committed Oct 25, 2022
1 parent 95b19a6 commit cb13a49
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 87 deletions.
4 changes: 2 additions & 2 deletions advance_control.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
<option value="">None</option>

$if commandsAdv[u"typeOutput"][sid] == 'comandLine':
<option selected="checked" value="comandLine">Command line</option>
<option selected="checked" value="comandLine">Command Line</option>
$else:
<option value="comandLine">Command line</option>
<option value="comandLine">Command Line</option>

$if commandsAdv[u"typeOutput"][sid] == 'shellyHTTP':
<option selected="checked" value="shellyHTTP">Shelly Divices</option>
Expand Down
200 changes: 116 additions & 84 deletions advance_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,60 @@ def httpResquestJSON(commandURL):

return resposeIsOk, response

def generateVarFunctionsNet(idx):

port2Use = "80"
if len(str(commandsAdv[u"devicePort"][idx])) > 0:
port2Use = str(commandsAdv[u"devicePort"][idx])

if commandsAdv[u"typeOutput"][idx] == "shellyHTTP":
# use credentials, if present
if len(commandsAdv[u"deviceUserName"][idx]) > 0:
userData = commandsAdv[u"deviceUserName"][idx] + ":" + commandsAdv[u"devicePassword"][idx] + "@"
else:
userData = ""

shellyChannel = "0"
if commandsAdv[u"deviceModel"][idx] == "shelly2_2":
shellyChannel = "1"

return port2Use, userData, shellyChannel

def generateONFunctionNet(idx):
# Turn on net device

port2Use, userData, shellyChannel = generateVarFunctionsNet(idx)

if commandsAdv[u"typeOutput"][idx] == "shellyHTTP":
turnOnURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + userData + commandsAdv[u"deviceIP"][idx] + u"/relay/" + shellyChannel + u"?turn=on"
else:
turnOnURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + commandsAdv[u"deviceIP"][idx] + u":" + port2Use + u"/zeroconf/switch"

return turnOnURL

def generateOFFFunctionNet(idx):
# Turn off net device

port2Use, userData, shellyChannel = generateVarFunctionsNet(idx)

if commandsAdv[u"typeOutput"][idx] == "shellyHTTP":
turnOffURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + userData + commandsAdv[u"deviceIP"][idx] + u"/relay/" + shellyChannel + u"?turn=off"
else:
turnOffURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + commandsAdv[u"deviceIP"][idx] + u":" + port2Use + u"/zeroconf/switch"

return turnOffURL

def generateStatusFunctionNet(idx):
# device status

port2Use, userData, shellyChannel = generateVarFunctionsNet(idx)
if commandsAdv[u"typeOutput"][idx] == "shellyHTTP":
statusURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + userData + commandsAdv[u"deviceIP"][idx] + u":" + port2Use + u"/status"
else:
statusURL = commandsAdv[u"deviceProtocol"][idx] + u"://" + commandsAdv[u"deviceIP"][idx] + u":" + port2Use + u"/zeroconf/info"

return statusURL

################################################################################
# Control functions: #
################################################################################
Expand All @@ -90,24 +144,12 @@ def run_check_valves_on_line_keep_state():

for i in range(len(gv.srvals)):
if commandsAdv[u"typeOutput"][i] == "shellyHTTP" or commandsAdv[u"typeOutput"][i] == "sonOff":
port2Use = "80"
if len(str(commandsAdv[u"devicePort"][i])) > 0:
port2Use = str(commandsAdv[u"devicePort"][i])

if commandsAdv[u"typeOutput"][i] == "shellyHTTP":
# use credentials, if present
if len(commandsAdv[u"deviceUserName"][i]) > 0:
userData = commandsAdv[u"deviceUserName"][i] + ":" + commandsAdv[u"devicePassword"][i] + "@"
else:
userData = ""
statusURL = generateStatusFunctionNet(i)

statusURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/status"
else:
statusURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/zeroconf/info"
turnOffURL = generateOFFFunctionNet(i)
turnOnURL = generateONFunctionNet(i)

shellyChannel = "0"
if commandsAdv[u"deviceModel"][i] == "shelly2_2":
shellyChannel = "1"
port2Use, userData, shellyChannel = generateVarFunctionsNet(i)

devicesAccessProtection[i].acquire()
resposeIsOk, response = httpResquestJSON(statusURL)
Expand All @@ -124,20 +166,10 @@ def run_check_valves_on_line_keep_state():
newState = response['data']['switch'] == 'on'

if newState and gv.srvals[i] == 0:
if commandsAdv[u"typeOutput"][i] == "shellyHTTP":
turnOffURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u"/relay/" + shellyChannel + u"?turn=off"
else:
turnOffURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/zeroconf/switch"

resposeIsOkOff, response = httpResquestJSON(turnOffURL)
if resposeIsOkOff != 0:
print("Fail to turn off in keep state")
elif not newState and gv.srvals[i] == 1:
if commandsAdv[u"typeOutput"][i] == "shellyHTTP":
turnOnURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u"/relay/" + shellyChannel + u"?turn=on"
else:
turnOnURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/zeroconf/switch"

resposeIsOkOn, response = httpResquestJSON(turnOnURL)
if resposeIsOkOn != 0:
print("Fail to turn on in keep state")
Expand Down Expand Up @@ -216,36 +248,15 @@ def on_zone_change(name, **kw):
if command:
subprocess.call(command.split(), shell=True)
elif commandsAdv[u"typeOutput"][i] == "shellyHTTP" or commandsAdv[u"typeOutput"][i] == "sonOff":
#start to lock device to avoid same http requets
devicesAccessProtection[i].acquire()
statusURL = generateStatusFunctionNet(i)

port2Use = "80"
if len(str(commandsAdv[u"devicePort"][i])) > 0:
port2Use = str(commandsAdv[u"devicePort"][i])
turnOffURL = generateOFFFunctionNet(i)
turnOnURL = generateONFunctionNet(i)

# Check type of shelly, if any use name and password, need to check if relay
if commandsAdv[u"typeOutput"][i] == "shellyHTTP":
# use shelly HTTP protocol
# use credentials, if present
if len(commandsAdv[u"deviceUserName"][i]) > 0:
userData = commandsAdv[u"deviceUserName"][i] + ":" + commandsAdv[u"devicePassword"][i] + "@"
else:
userData = ""

shellyChannel = "0"
if commandsAdv[u"deviceModel"][i] == "shelly2_2":
shellyChannel = "1"
port2Use, userData, shellyChannel = generateVarFunctionsNet(i)

turnOnURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/relay/" + shellyChannel + u"?turn=on"
turnOffURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/relay/" + shellyChannel + u"?turn=off"

statusURL = commandsAdv[u"deviceProtocol"][i] + u"://" + userData + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/status"
else:
# TODO, need to be tested: SonOff Code
turnOnURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/zeroconf/switch"
turnOffURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"zeroconf/switch"

statusURL = commandsAdv[u"deviceProtocol"][i] + u"://" + commandsAdv[u"deviceIP"][i] + u":" + port2Use + u"/zeroconf/info"
#start to lock device to avoid same http requets
devicesAccessProtection[i].acquire()

resposeIsOk, response = httpResquestJSON(statusURL)

Expand Down Expand Up @@ -327,6 +338,15 @@ def on_zone_change(name, **kw):
zones = signal(u"zone_change")
zones.connect(on_zone_change)

def restart_clean_up(name, **kw):
global runValveOnLine, threadCheckOnLine
if threadCheckOnLine.is_alive():
runValveOnLine = False
threadCheckOnLine.join()

rebootAction = signal(u"restarting")
rebootAction.connect(restart_clean_up)

################################################################################
# Web pages: #
################################################################################
Expand Down Expand Up @@ -461,8 +481,6 @@ def GET(self):
# Restart thread to check if is network valves are on-line
runValveOnLine = False
threadCheckOnLine.join()
threadCheckOnLine = Thread(target = run_check_valves_on_line_keep_state)
threadCheckOnLine.start()

raise web.seeother(u"/restart")

Expand Down Expand Up @@ -511,40 +529,54 @@ def GET(self):
# already lock, avoid multy test in the same time
return "Waitting"

devicesAccessProtection[valveId].acquire()
statusURL = generateStatusFunctionNet(valveId)

if commandsAdv[u"typeOutput"][valveId] == "shellyHTTP":
# use credentials, if present
if len(commandsAdv[u"deviceUserName"][valveId]) > 0:
userData = commandsAdv[u"deviceUserName"][valveId] + ":" + commandsAdv[u"devicePassword"][valveId] + "@"
else:
userData = ""
turnOffURL = generateOFFFunctionNet(valveId)
turnOnURL = generateONFunctionNet(valveId)

shellyChannel = "0"
if commandsAdv[u"deviceModel"][valveId] == "shelly2_2":
shellyChannel = "1"
port2Use, userData, shellyChannel = generateVarFunctionsNet(valveId)

port2Use = "80"
if len(str(commandsAdv[u"devicePort"][valveId])) > 0:
port2Use = str(commandsAdv[u"devicePort"][valveId])

turnOnURL = commandsAdv[u"deviceProtocol"][valveId] + u"://" + userData + commandsAdv[u"deviceIP"][valveId] + u":" + port2Use + u"/relay/" + shellyChannel + u"?turn=on"
turnOffURL = commandsAdv[u"deviceProtocol"][valveId] + u"://" + userData + commandsAdv[u"deviceIP"][valveId] + u":" + port2Use + u"/relay/" + shellyChannel + u"?turn=off"
else:
pass

devicesAccessProtection[valveId].release()
devicesAccessProtection[valveId].acquire()

# Guaranty that valve is turn off
resposeIsOkOff, response = httpResquestJSON(turnOffURL)
time.sleep(2 * commandsAdv[u"latchDutyCicle"][valveId])
resposeIsOk, response = httpResquestJSON(statusURL)
if resposeIsOk != 0:
print("Fail to check initial state")
devicesAccessProtection[valveId].release()
return "NOK"

if resposeIsOkOff == 0:
resposeIsOkOn, response = httpResquestJSON(turnOnURL)
if resposeIsOkOn == 0:
time.sleep(commandsAdv[u"latchDutyCicle"][valveId])
resposeIsOkOff, response = httpResquestJSON(turnOffURL)
if resposeIsOkOff == 0:
return "OK"
try:
if commandsAdv[u"typeOutput"][valveId] == "shellyHTTP":
lastState = bool(response['relays'][int(shellyChannel)]['ison'])
else:
lastState = response['data']['switch'] == 'on'
except NameError:
print("No data fount in respond")
devicesAccessProtection[valveId].release()
return "NOK"

# if relay is on, turn off for a while to send latch signal
if lastState:
resposeIsOkOff, response = httpResquestJSON(turnOffURL)
time.sleep(2 * commandsAdv[u"latchDutyCicle"][valveId])

if resposeIsOkOff == 0:
print("Fail to turn off for a while")
devicesAccessProtection[valveId].release()
return "NOK"

resposeIsOkOn, response = httpResquestJSON(turnOnURL)
if resposeIsOkOn == 0:
time.sleep(commandsAdv[u"latchDutyCicle"][valveId])
resposeIsOkOff, response = httpResquestJSON(turnOffURL)
if resposeIsOkOff == 0:
devicesAccessProtection[valveId].release()
return "OK"
else:
devicesAccessProtection[valveId].release()
return "NOK"
else:
devicesAccessProtection[valveId].release()
return "NOK"
else:
return "NOK"
2 changes: 1 addition & 1 deletion advance_control_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<tr>
<td>${gv.snames[sid]}</td>
<td align="center" width="20%"><img src="static/images/${commandsAdv[u"deviceModel"][sid]}.jpg" width="40%"></td>
<td>${commandsAdv[u"deviceIP"][sid]}<br /><button id="latchManualSignal${sid}" class="submit" onclick="sendLatchToValve(${sid})"><b>$_(u'Submit')</b></button><div id="lactchStatus${sid}"></div></td>
<td>${commandsAdv[u"deviceIP"][sid]}<br /><button id="latchManualSignal${sid}" class="submit" onclick="sendLatchToValve(${sid})"><b>Send latch signal</b></button><div id="lactchStatus${sid}"></div></td>
<td id="valve${sid}"></td>
</tr>
$else:
Expand Down

0 comments on commit cb13a49

Please sign in to comment.