def checkstopcondition(element): print "Evaluating End of Blocking period ++++++++++++" actionmodeafterfirst = interruptdbmod.searchdata("element", element, "actionmode_afterfirst") print actionmodeafterfirst if actionmodeafterfirst == "Extend blocking state" or actionmodeafterfirst == "Extend and Follo-up": # extend only the pre-emption blocking period, no action seonsormode = interruptdbmod.searchdata("element", element, "sensor_mode") print seonsormode if seonsormode == "Edge + Level": sensor = interruptdbmod.searchdata("element", element, "sensor") recordkey = hardwaremod.HW_INFO_NAME recordvalue = sensor keytosearch = hardwaremod.HW_CTRL_PIN PIN = hardwaremod.searchdata(recordkey, recordvalue, keytosearch) reading = hardwaremod.readinputpin(PIN) keytosearch = hardwaremod.HW_CTRL_LOGIC logic = hardwaremod.searchdata(recordkey, recordvalue, keytosearch) # pin high according to the set logic print "logic:", logic, " reading:", reading if logic == "pos" and reading == "1": return False if logic == "neg" and reading == "0": return False return True
def checkstopcondition(element): #print "Evaluating End of Blocking period ++++++++++++" actionmodeafterfirst=interruptdbmod.searchdata("element",element,"actionmode_afterfirst") #print "actionafter " , actionmodeafterfirst sensor=interruptdbmod.searchdata("element",element,"sensor") if actionmodeafterfirst=="Extend blocking state" or actionmodeafterfirst=="Extend and Follow-up": # extend the pre-emption blocking period seonsormode=interruptdbmod.searchdata("element",element,"sensor_mode") #print "SENSORMODE" , seonsormode recordkey=hardwaremod.HW_INFO_NAME recordvalue=sensor keytosearch=hardwaremod.HW_CTRL_PIN PIN=hardwaremod.searchdata(recordkey,recordvalue,keytosearch) reading=hardwaremod.readinputpin(PIN) if seonsormode=="First Edge + Level": keytosearch=hardwaremod.HW_CTRL_LOGIC logic=hardwaremod.searchdata(recordkey,recordvalue,keytosearch) # pin high according to the set logic #print "logic:", logic , " reading:" ,reading if (logic=="pos" and reading=="1")or(logic=="neg" and reading=="0"): return False elif seonsormode=="Second Edge + Level (inv)": keytosearch=hardwaremod.HW_CTRL_LOGIC logic=hardwaremod.searchdata(recordkey,recordvalue,keytosearch) #pin LOW according to the set logic #print "logic:", logic , " reading:" ,reading if (logic=="pos" and reading=="0")or(logic=="neg" and reading=="1"): return False return True
def cyclereset(element): #AUTO_data["default"]={"lasteventtime":datetime.utcnow()- timedelta(minutes=waitingtime),"lastinterrupttime":datetime.utcnow(), #"validinterruptcount":0,"eventactivated":False,"lastactiontime":datetime.utcnow()- timedelta(minutes=waitingtime), #"actionvalue":0, "alertcounter":0, "infocounter":0, "status":"ok" , "threadID":None , "blockingstate":False} #SENSOR_data["default"]={"Startcounttime":datetime.utcnow(),"InterruptCount":0} # this is for the actual frequency sensor #PIN_attributes["default"]={"logic":"pos","refsensor":"","bouncetimeSec":0.001} # this is relebant to the PINs #BLOCKING_data["default"]={"BlockingNumbers":0,"BlockingNumbersThreadID":None} # tihs is relenat to the Interrupt trigger global AUTO_data waitingtime = hardwaremod.toint( interruptdbmod.searchdata("element", element, "preemptive_period"), 0) statusdataDBmod.write_status_data( AUTO_data, element, "lastactiontime", datetime.utcnow() - timedelta(minutes=waitingtime)) statusdataDBmod.write_status_data( AUTO_data, element, "lasteventtime", datetime.utcnow() - timedelta(minutes=waitingtime)) statusdataDBmod.write_status_data(AUTO_data, element, "status", "ok") statusdataDBmod.write_status_data(AUTO_data, element, "actionvalue", 0) statusdataDBmod.write_status_data(AUTO_data, element, "alertcounter", 0) statusdataDBmod.write_status_data(AUTO_data, element, "infocounter", 0) statusdataDBmod.write_status_data(AUTO_data, element, "validinterruptcount", 0) # start procedure to stop blocking on this element endblocking(element) # reassess all the interrupt sensor related shit elementlist = interruptdbmod.getelementlist() sensorblockingcounter = {} #print "elementlist" , elementlist #print elementlist for element in elementlist: #print " ELEMENT " , element workmode = checkworkmode(element) if (workmode != "None") and (workmode != ""): sensor = interruptdbmod.searchdata("element", element, "sensor") #print " SENSOR " , sensor if sensor != "": #print "Blocking starte " , statusdataDBmod.read_status_data(AUTO_data,element,"blockingstate") if statusdataDBmod.read_status_data( AUTO_data, element, "blockingstate"): # blocking state is TRUE #print " TRUE ------------------------------------------------------- " , element if sensor in sensorblockingcounter: sensorblockingcounter[ sensor] = sensorblockingcounter[sensor] + 1 else: sensorblockingcounter[sensor] = 1 print(sensorblockingcounter) global BLOCKING_data for sensor in sensorblockingcounter: statusdataDBmod.write_status_data(BLOCKING_data, sensor, "BlockingNumbers", sensorblockingcounter[sensor])
def interruptcheck(refsensor, mode): #logger.info('Starting Interrupt Evaluation, Sensor: %s' , refsensor) # iterate among the actuators elementlist = interruptdbmod.getelementlist() #print elementlist for element in elementlist: sensor = interruptdbmod.searchdata("element", element, "sensor") #print sensor if sensor == refsensor: sensormode = interruptdbmod.searchdata("element", element, "sensor_mode") #print "mode ", mode , "sensormode ", sensormode if (mode in sensormode) or ("both" in sensormode): interruptexecute(refsensor, element) return
def CheckandFollowup(element): global AUTO_data actionmodeafterfirst=interruptdbmod.searchdata("element",element,"actionmode_afterfirst") actuatoroutputfollowup=hardwaremod.tonumber(interruptdbmod.searchdata("element",element,"folloup_output"),0) # section below is the follow-up if actionmodeafterfirst=="Follow-up action" or actionmodeafterfirst=="Remove and Follow-up" or actionmodeafterfirst=="Extend and Follow-up": # execute the action followup, no variation in the preemption period value=actuatoroutputfollowup # followup action print("Implement Actuator Value followup", value) logger.info('Procedure to start actuator followup %s, for value = %s', element, value) isok=activateactuator(element, value) if isok: statusdataDBmod.write_status_data(AUTO_data,element,"lastactiontime",datetime.utcnow()) statusdataDBmod.write_status_data(AUTO_data,element,"actionvalue",value) return True return False
def interruptcheck(refsensor): logger.info('Starting Interrupt Evaluation, Sensor: %s', refsensor) # iterate among the actuators elementlist = interruptdbmod.getelementlist() for element in elementlist: sensor = interruptdbmod.searchdata("element", element, "sensor") if sensor == refsensor: interruptexecute(refsensor, element) return
def endblocking(element, saveend=True): sensor = interruptdbmod.searchdata("element", element, "sensor") if checkstopcondition(element): global AUTO_data #save data if saveend: saveblockingdiff(sensor) #-- threadID = statusdataDBmod.read_status_data(AUTO_data, element, "threadID") if threadID != None and threadID != "": #print "cancel the Thread of element=",element threadID.cancel() global BLOCKING_data # change blocking counter BlockingNumbers = statusdataDBmod.read_status_data( BLOCKING_data, sensor, "BlockingNumbers") BlockingNumbers = BlockingNumbers - 1 statusdataDBmod.write_status_data(BLOCKING_data, sensor, "BlockingNumbers", BlockingNumbers) #-- #save data saveblockingdiff(sensor) #-- #print "Start removing blocking status" statusdataDBmod.write_status_data( hardwaremod.Blocking_Status, element, "priority", NONBLOCKINGPRIORITY) #put the priority to lower levels statusdataDBmod.write_status_data(AUTO_data, element, "threadID", None) statusdataDBmod.write_status_data(AUTO_data, element, "blockingstate", False) else: # renew the blocking status print("Interrupt LEVEL High, Do not stop blocking period, Extend it") # reload the period in case this is chnaged preemptiontimemin = hardwaremod.toint( interruptdbmod.searchdata("element", element, "preemptive_period"), 0) period = preemptiontimemin * 60 if period > 0: startblockingstate(element, period)
def cyclereset(element): global AUTO_data waitingtime = hardwaremod.toint( interruptdbmod.searchdata("element", element, "preemptive_period"), 0) statusdataDBmod.write_status_data( AUTO_data, element, "lastactiontime", datetime.utcnow() - timedelta(minutes=waitingtime)) statusdataDBmod.write_status_data( AUTO_data, element, "lasteventtime", datetime.utcnow() - timedelta(minutes=waitingtime)) statusdataDBmod.write_status_data(AUTO_data, element, "status", "ok") statusdataDBmod.write_status_data(AUTO_data, element, "actionvalue", 0) statusdataDBmod.write_status_data(AUTO_data, element, "alertcounter", 0) statusdataDBmod.write_status_data(AUTO_data, element, "infocounter", 0) statusdataDBmod.write_status_data(AUTO_data, element, "validinterruptcount", 0)
def startblockingstate(element, periodsecond, saveend=True): #logger.warning("StartBOLCKINGSTATE Started ---> Period %d", periodsecond) global BLOCKING_data sensor = interruptdbmod.searchdata("element", element, "sensor") #save data print("first save in database") if saveend: saveblockingdiff(sensor) #-- if periodsecond > 0: global AUTO_data # in case another timer is active on this element, cancel it threadID = statusdataDBmod.read_status_data(AUTO_data, element, "threadID") if threadID != None and threadID != "": #print "cancel the Thread of element=",element threadID.cancel() else: # change blocking counter BlockingNumbers = statusdataDBmod.read_status_data( BLOCKING_data, sensor, "BlockingNumbers") BlockingNumbers = BlockingNumbers + 1 statusdataDBmod.write_status_data(BLOCKING_data, sensor, "BlockingNumbers", BlockingNumbers) #-- #save data if saveend: saveblockingdiff(sensor) #-- statusdataDBmod.write_status_data(AUTO_data, element, "blockingstate", True) statusdataDBmod.write_status_data( hardwaremod.Blocking_Status, element, "priority", ACTIONPRIORITYLEVEL) #increse the priority to execute a command nonblockingpriority = 0 #logger.warning("Trigger EndblockingStart ---> Period %d", periodsecond) threadID = threading.Timer(periodsecond, endblocking, [element, saveend]) threadID.start() statusdataDBmod.write_status_data(AUTO_data, element, "threadID", threadID)
def cyclereset(element): global AUTO_data waitingtime = hardwaremod.toint( interruptdbmod.searchdata("element", element, "preemptive_period"), 0) AUTO_data[element] = { "lastactiontime": datetime.utcnow() - timedelta(minutes=waitingtime), "status": "ok", "actionvalue": 0, "alertcounter": 0, "infocounter": 0 } AUTO_data[element] = { "lasteventtime": datetime.utcnow() - timedelta(minutes=waitingtime), "status": "ok", "actionvalue": 0, "alertcounter": 0, "infocounter": 0 }
def checkworkmode(element): return interruptdbmod.searchdata("element", element, "workmode")
def interruptexecute(refsensor, element): sensor = refsensor #logger.info('interrupt Pairing OK ---> Actuator: %s , Sensor: %s', element, sensor) workmode = checkworkmode(element) if (workmode == "None"): # None case print("No Action required, workmode set to None, element: ", element) logger.info("No Action required, workmode set to None, element: %s ", element) return if (workmode == ""): logger.info("Not able to get the workmode: %s ", element) return #logger.info('Interrupt, Get all the parameters') interrupt_validinterval = hardwaremod.tonumber( interruptdbmod.searchdata("element", element, "interrupt_validinterval"), 0) #"Counter Only" if workmode == "Counter Only": CounterOnlyNew(element, sensor, interrupt_validinterval) return interrupt_triggernumber = hardwaremod.tonumber( interruptdbmod.searchdata("element", element, "interrupt_triggernumber"), 1) actuatoroutput = hardwaremod.tonumber( interruptdbmod.searchdata("element", element, "actuator_output"), 0) actuatoroutputfollowup = hardwaremod.tonumber( interruptdbmod.searchdata("element", element, "folloup_output"), 0) # evaluate variables for operational period check starttime = datetime.strptime( interruptdbmod.searchdata("element", element, "allowedperiod")[0], '%H:%M').time() endtime = datetime.strptime( interruptdbmod.searchdata("element", element, "allowedperiod")[1], '%H:%M').time() # get other parameters seonsormode = interruptdbmod.searchdata("element", element, "sensor_mode") triggermode = interruptdbmod.searchdata("element", element, "trigger_mode") preemptiontimemin = hardwaremod.toint( interruptdbmod.searchdata("element", element, "preemptive_period"), 0) if preemptiontimemin == 0: # if relay, meaning cmd = pulse actuatortype = hardwaremod.searchdata(hardwaremod.HW_INFO_NAME, element, hardwaremod.HW_CTRL_CMD) if actuatortype == "pulse": preemptiontime = actuatoroutput # if set to zero then get the time as the actuator (in case of relay) else: preemptiontime = 0 else: preemptiontime = preemptiontimemin * 60 mailtype = interruptdbmod.searchdata("element", element, "mailalerttype") actionmodeafterfirst = interruptdbmod.searchdata("element", element, "actionmode_afterfirst") # time check # ------------------------ interrupt alghoritm if workmode == "Pre-emptive Blocking": # check if inside the allowed time period #print "Pre-emptive Blocking Mode" #logger.info('Pre-emptive Blocking mode --> %s', element) timeok = isNowInTimePeriod( starttime, endtime, datetime.now().time()) # don't use UTC here! #print "inside allowed time ", timeok , " starttime ", starttime , " endtime ", endtime if timeok: CheckActivateNotify(element, sensor, preemptiontime, actuatoroutput, actionmodeafterfirst, actuatoroutputfollowup, mailtype, interrupt_triggernumber, interrupt_validinterval, triggermode) else: logger.info('out of allowed operational time') # implment Critical alert message in case the sensor value is one interval more than Max_threshold return
def setinterruptevents(): hardwaremod.removeallinterruptevents() print("load interrupt list ") interruptlist = interruptdbmod.sensorlist() print("len interrupt list ", len(interruptlist)) for item in interruptlist: print("got into the loop ") # get PIN number recordkey = hardwaremod.HW_INFO_NAME recordvalue = item keytosearch = hardwaremod.HW_CTRL_PIN PINstr = hardwaremod.searchdata(recordkey, recordvalue, keytosearch) print("set event for the PIN ", PINstr) if not PINstr == "": keytosearch = hardwaremod.HW_CTRL_LOGIC logic = hardwaremod.searchdata(recordkey, recordvalue, keytosearch) # set Sw pull up / down mode if logic == "pos": hardwaremod.GPIO_setup(PINstr, "in", "pull_down") evenslopetype = "both" else: hardwaremod.GPIO_setup(PINstr, "in", "pull_up") evenslopetype = "both" #GPIO.RISING, GPIO.FALLING or GPIO.BOTH. # link to the callback function # the bouncetime is set by the frequency parameter, if this parameter is empty, the default bouncetime would be 200 keytosearch = hardwaremod.HW_CTRL_FREQ frequency = hardwaremod.searchdata(recordkey, recordvalue, keytosearch) if frequency == "": bouncetimeINT = 200 else: frequencyINT = hardwaremod.toint(frequency, 5) bouncetimeINT = old_div( 1000, frequencyINT ) # in ms. this is ok to be trunk of the int. For frequencies higher than 1000 the bouncetime is exactly zero # RPI.GPIO library does not accept bouncetime=0, it gives runtime error if bouncetimeINT <= 0: bouncetimeINT = 1 #ms hardwaremod.GPIO_add_event_detect(PINstr, evenslopetype, eventcallback, bouncetimeINT) # set fast reference call indexed with the PIN number which is the variable used when interrupt is called: # search now to avoid searching later global PIN_attributes PIN = hardwaremod.toint(PINstr, 0) statusdataDBmod.write_status_data(PIN_attributes, PIN, "logic", logic) recordkey = hardwaremod.HW_CTRL_PIN recordvalue = PINstr keytosearch = hardwaremod.HW_INFO_NAME refsensor = hardwaremod.searchdata( recordkey, recordvalue, keytosearch) # return first occurence statusdataDBmod.write_status_data(PIN_attributes, PIN, "refsensor", refsensor) statusdataDBmod.write_status_data( PIN_attributes, PIN, "bouncetimeSec", 0.4 * float(bouncetimeINT) / 1000) # code below to enable blocking for N sec, it is necessary to trigger the bloccking status in case of levels already present when starting. elementlist = interruptdbmod.getelementlist() #print elementlist for element in elementlist: workmode = checkworkmode(element) if (workmode != "None") and (workmode != ""): sensor = interruptdbmod.searchdata("element", element, "sensor") #saveblockingdiff(sensor) print(" what a sensor ", sensor) if sensor != "": startblockingstate(element, 10, False) t.sleep(0.02) return ""