Skip to content
Snippets Groups Projects
Commit 77441937 authored by stephanputzke's avatar stephanputzke
Browse files

Better error handling in case cluster is offline

parent 22d17acf
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -25,6 +25,8 @@ def isValidValveNumString(num):
def errorNotAValidValveNumber(num):
return '%s not a number or not in allowed range [1-24]' % num
clusterOfflineError = {'success': False, 'status': 'Valvecluster is offline'}
#####
# Define routes
......@@ -44,6 +46,8 @@ Get description of all valves
@get('/description')
def recipe_description():
valveStates = getValveStates()
if valveStates is None:
return {'success': False, 'version': 1.1, 'name': 'Schrankenwaerter', 'status': 'Valvecluster is offline'}
valveNums = range(1,len(valveStates)+1)
valves = {
......@@ -114,6 +118,9 @@ Get state of one or all control valves
def recipe_controlValveState(num=None):
valveStates = getControlValveStates()
if valveStates is None:
return clusterOfflineError
if num is None:
return { 'success': True, 'state': dict(zip(range(1,len(valveStates)+1),valveStates)) }
......@@ -133,6 +140,9 @@ of each valve
def recipe_valveState(num=None):
valveStates = getValveStates()
if valveStates is None:
return clusterOfflineError
if num is None:
return { 'success': True, 'state': dict(zip(range(1,len(valveStates)+1),valveStates)) }
if not isValidValveNumString(num):
......@@ -166,19 +176,24 @@ def recipe_canOpenCloseValve(num,action):
def send(command):
url = 'http://10.0.0.31/http_in_ci?ci:%s' % command
content = None
try:
content = urllib2.urlopen(url, timeout=0.2).read()
except urllib2.URLError, e:
# For Python 2.6
if isinstance(e.reason, socket.timeout):
print 'timeout',e
return None
else:
print 'urlerror:', e
#return None
except httplib.BadStatusLine as e:
#print e.args
return None
print e
#return None
except:
import sys
print "Unexpected error:", sys.exc_info()[0]
#print content
#print 'Content:', content
return content
def int2bitField(n):
......@@ -276,11 +291,19 @@ def getControlValveStates():
# 6 = 255 = 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
state = send('DAW%i'%unitNum)[1:] # Cluster returns sth like '=130'
# -> chop of leading '='
state = send('DAW%i'%unitNum)
if state==None: # Cluster is offline!
return None
state = state[1:] # Cluster returns sth like '=130'
# -> chop of leading '='
return int2bitField(int(state))[:8] # only first 8 bits are used
states = [getControlValveState(unitNum) for unitNum in ausgangsWort]
for state in states:
if state is None:# Cluster is offline!
return None
states = [item for sublist in states for item in sublist]
states = states[:32:2]+states[32:]
......@@ -307,11 +330,18 @@ def getValveStates():
'''
assert(unitNum in eingangsWort), \
'Unit number not in range of allowed eingangswörter'
state = send('DEW%i'%unitNum)[1:] # Cluster returns sth like '=130'
# -> chop of leading '='
state = send('DEW%i'%unitNum)
if state==None: # Cluster is offline!
return None
state = state[1:] # Cluster returns sth like '=130'
# -> chop of leading '='
return int2bitField(int(state))
states = np.array([getUnitState(unitNum) for unitNum in eingangsWort])
for state in states:
if state is None:# Cluster is offline!
return None
states = states.reshape(-1,2)
def stateListToNumber(state):
......
......@@ -61,8 +61,11 @@
// Look for Schrankenwärter server. If found, adjust connect button and set up the valve list.
function openConnectionToSchrankenwarter() {
// If connection is already open then close it (toggle open/close state)
if ($('#connectButton').button( "option", "label" )=='Connect') {
if ($('#connectButton').button( "option", "label" )=='Connect') { //Lousy definition of state...
$('#statusTextField').text('Status: Connecting...')
if ($('#buttoncontainer').is(':empty')){
setupValveList();
}
$.getJSON(inselAdresse+'description',
function(data) {
console.log(data)
......@@ -78,6 +81,8 @@
refreshIntervalId = setInterval(function() {
updateValveStates()
},200)
} else {
$('#statusTextField').text('Status: '+data.status)
}
}
).error(function() { closeConnectionToSchrankenwarter(); })
......@@ -102,7 +107,7 @@
function setupValveList() {
$.getJSON(inselAdresse+'description',
function(data) {
if (data.success!=true) {
if (data.success==false) {
$('#statusTextField').text('Error during communication with valve cluster');
console.log('Communication error, response of valve cluster: ' + data);
} else {
......@@ -155,6 +160,9 @@
listItem.removeClass(classes).addClass('undef');
}
})
} else {
console.log('Error in updateValveStates, cluster seems to be offline');
closeConnectionToSchrankenwarter()
}
}).error(function() {
console.log('Error in updateValveStates');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment