def dbvntovalue(dbvn, interprettype=False): dbvndict = parsedbvn(dbvn) # try: value = pilib.getsinglevalue(dbvndict['dbpath'], dbvndict['tablename'], dbvndict['valuename'], dbvndict['condition']) # except: # print('error getting value') # print(dbvndict) # return None if interprettype: # get type vartype = getvartype(dbvndict['dbpath'], dbvndict['tablename'], dbvndict['valuename']) if vartype == 'boolean': return bool(value) elif vartype == 'float': return float(value) elif vartype == 'int': return int(value) return value
actiondicts = pilib.readalldbrows(pilib.controldatabase, 'actions') for actiondict in actiondicts: alert = False thisaction = pilib.action(actiondict) thisaction.statusmsg = '' # process condition if thisaction.conditiontype == 'dbvalue': if thisaction.enabled: thisaction.statusmsg += 'Action enabled.' dbpath = getattr(pilib, thisaction.database + 'database') # variablename is columnname for dbvalue conditiontype thisaction.variablevalue = pilib.getsinglevalue(dbpath, thisaction.tablename, thisaction.variablename, 'rowid=' + str(thisaction.rowid)) # get variable type to handle variablestypedict = pilib.getpragmanametypedict(pilib.controldatabase, thisaction.tablename) vartype = variablestypedict[thisaction.variablename] thisaction.statusmsg += ' Variablevalue: ' + str(thisaction.variablevalue) + '. Criterion: ' + str(thisaction.criterion) + ' . ' # process criterion according to type curstatus = False if vartype == 'boolean': thisaction.statusmsg += ' Processing boolean. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True elif vartype == 'integer' or vartype == 'real': thisaction.statusmsg += ' Processing integer/real. '
def processactions(): # Read database to get our actions actiondicts = pilib.readalldbrows(pilib.controldatabase, 'actions') for actiondict in actiondicts: alert = False thisaction = pilib.action(actiondict) thisaction.statusmsg = '' # print(actiondict) # process condition if thisaction.conditiontype == 'dbvalue': if thisaction.enabled: thisaction.statusmsg += 'Action enabled.' dbdir = getattr(pilib, 'databasedir') # print(dbdir) dbpath = dbdir + thisaction.database + '.db' # variablename is columnname for dbvalue conditiontype thisaction.variablevalue = pilib.getsinglevalue( dbpath, thisaction.tablename, thisaction.variablename, 'rowid=' + str(thisaction.channelindex)) # get variable type to handle variablestypedict = pilib.getpragmanametypedict( pilib.controldatabase, thisaction.tablename) vartype = variablestypedict[thisaction.variablename] thisaction.statusmsg += ' Variablevalue: ' + str( thisaction.variablevalue) + '. Criterion: ' + str( thisaction.criterion) + ' . ' # process criterion according to type curstatus = False if vartype == 'boolean': thisaction.statusmsg += ' Processing boolean. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True elif vartype == 'integer' or vartype == 'real': thisaction.statusmsg += ' Processing integer/real. ' # print(thisaction.operator) thisaction.variablevalue = float(thisaction.variablevalue) thisaction.criterion = float(thisaction.criterion) if thisaction.operator == '>': if thisaction.variablevalue > thisaction.criterion: curstatus = True elif thisaction.operator == '>=': if thisaction.variablevalue >= thisaction.criterion: curstatus = True elif thisaction.operator == '<': if thisaction.variablevalue < thisaction.criterion: curstatus = True elif thisaction.operator == '<=': if thisaction.variablevalue <= thisaction.criterion: curstatus = True elif thisaction.operator == '=': if thisaction.variablevalue == thisaction.criterion: curstatus = True else: thisaction.statusmsg += 'Operator error. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True elif vartype == 'text': thisaction.statusmsg += ' Processing text. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True else: thisaction.statusmsg += ' Mode Error for vartype ' + vartype + '. ' if curstatus: thisaction.statusmsg += 'Status is true. ' else: thisaction.statusmsg += 'Status is not true. ' currenttime = pilib.gettimestring() # if status is true and current status is false, set ontime if curstatus and not thisaction.status: # print(str(curstatus) + ' ' + str(thisaction.status)) thisaction.statusmsg += 'Setting status ontime. ' thisaction.ontime = pilib.gettimestring() thisaction.status = 1 elif not curstatus and thisaction.status: thisaction.statusmsg += 'Setting status offtime. ' thisaction.offtime = pilib.gettimestring() thisaction.status = 0 # Set current status if curstatus: thisaction.status = 1 # print('settings status') else: thisaction.status = 0 # print('resetting status') # if status is true and alarm isn't yet active, see if ondelay exceeded if curstatus and not thisaction.active: # print(pilib.timestringtoseconds(currenttime)) statusontime = pilib.timestringtoseconds( currenttime) - pilib.timestringtoseconds( thisaction.ontime) # print(statusontime) if statusontime > thisaction.ondelay: thisaction.statusmsg += 'Setting action active' thisaction.active = 1 else: thisaction.statusmsg += 'On delay not reached' # if status is not true and alarm is active, see if offdelay exceeded if not curstatus and thisaction.active: statusofftime = pilib.timestringtoseconds( currenttime) - pilib.timestringtoseconds( thisaction.offtime) if statusofftime > thisaction.offdelay: thisaction.statusmsg += 'Setting action inactive' thisaction.active = 0 # Send an alert / reset indicator if activereset is on if thisaction.activereset: thisaction.offact() else: thisaction.statusmsg += 'Off delay not reached' # test to see if it is time to alert, based on delay ond alert time if thisaction.active: # check to see if it is time to alert # print(pilib.timestringtoseconds(currenttime)) # print(pilib.timestringtoseconds(thisaction.lastactiontime)) # print(float(thisaction.actionfrequency)) # print(pilib.timestringtoseconds(currenttime)-pilib.timestringtoseconds(thisaction.lastactiontime)) if pilib.timestringtoseconds( currenttime) - pilib.timestringtoseconds( thisaction.lastactiontime) > float( thisaction.actionfrequency): alert = True thisaction.statusmsg += "Time to act. " else: alert = False thisaction.statusmsg += "Not yet time to act." if alert: # We're ready to alert or alert again. thisaction.lastactiontime = currenttime if curstatus: thisaction.onact() else: thisaction.offact() else: thisaction.statusmsg += 'Action disabled.' thisaction.status = 0 else: thisaction.statusmsg += 'Mode unrecognized.' print(thisaction.statusmsg) thisaction.publish()
__email__ = "*****@*****.**" __status__ = "Development" # This script runs the input reading scripts # specified interval, sends to log, channels and plot dbs # TODO: Update to include better logging import pilib import updateio from time import sleep readtime = 10 # default, seconds # Read from systemstatus to make sure we should be running updateioenabled = pilib.getsinglevalue(pilib.controldatabase, 'systemstatus', 'updateioenabled') while updateioenabled: pilib.log(pilib.iolog, 'Running periodicupdateio', 3, pilib.iologlevel) pilib.log(pilib.syslog, 'Running periodicupdateio', 3, pilib.sysloglevel) # Set last run time pilib.setsinglevalue(pilib.controldatabase, 'systemstatus', 'lastinputpoll', pilib.gettimestring()) pilib.setsinglevalue(pilib.controldatabase, 'systemstatus', 'updateiostatus', '1') # Read and record everything as specified in controldatabase # Update database of inputs with read data # DEBUG runupdate = True
__version__ = "1.0" __maintainer__ = "Colin Reese" __email__ = "*****@*****.**" __status__ = "Development" # This script runs the input reading scripts # specified interval, sends to log, channels and plot dbs import pilib import updateio from time import sleep readtime = 10 # default, seconds # Read from systemstatus to make sure we should be running updateioenabled = pilib.getsinglevalue(pilib.controldatabase, 'systemstatus', 'updateioenabled') while updateioenabled: #print("runtime") #print("reading input") # Read again, once inside each loop so we terminate if the # variable name is changed inputsreadenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select inputsreadenabled from systemstatus') # Set last run time pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set lastinputspoll=\'' + pilib.gettimestring() + '\'') pilib.sqlitequery(pilib.controldatabase,
offlinetime = netstatus['offlinetime'] except: print('netstatus ERROR') pilib.log(pilib.networklog, 'Error gettng offlinetime. ', 2, pilib.networkloglevel) offlineperiod = pilib.timestringtoseconds(pilib.gettimestring()) - pilib.timestringtoseconds(offlinetime) pilib.log(pilib.networklog, 'We have been offline for ' + str(offlineperiod)) if offlineperiod > int(netconfigdata['WANretrytime']): pilib.log(pilib.networklog, 'Offline period of ' + str(offlineperiod) + ' has exceeded retry time of ' + str(int(netconfigdata['WANretrytime']))) # Note that although we obey this period once, after we start this process we don't reset the offlinetime, # so it will just continue to run. This is good in a way, as it will continually set the netstateok to bad, # which will eventually cause us to reboot # We do reset the WAN offline time in the reboot sequence, hwoever. restarts = int(pilib.getsinglevalue(pilib.systemdatadatabase, 'netstatus','WANaccessrestarts')) restarts += 1 pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'WANaccessrestarts', restarts) pilib.log(pilib.networklog, 'Going to run netconfig to correct WAN access.') else: pilib.log(pilib.networklog, 'Not yet time to run netconfig to correct WAN access. Retry time set at ' + str(netconfigdata['WANretrytime'])) runconfig = True else: pilib.log(pilib.networklog, 'WANAccess is fine. ') if runconfig: # Set bad status in netstatus pilib.setsinglevalue(pilib.systemdatadatabase, 'netstatus', 'netstate', 0) # Set ok time to '' to trigger rewrite next time status is ok
def processactions(): # Read database to get our actions actiondicts = pilib.readalldbrows(pilib.controldatabase, 'actions') for actiondict in actiondicts: alert = False # Instantiate the action class thisaction = pilib.action(actiondict) thisaction.statusmsg = '' # print(actiondict) # process condition if thisaction.conditiontype == 'dbvalue': if thisaction.enabled: thisaction.statusmsg += 'Action enabled.' dbdir = getattr(pilib, 'databasedir') # print(dbdir) dbpath = dbdir + thisaction.database + '.db' # variablename is columnname for dbvalue conditiontype thisaction.variablevalue = pilib.getsinglevalue(dbpath, thisaction.tablename, thisaction.variablename, 'rowid=' + str(thisaction.valuerowid)) # get variable type to handle variablestypedict = pilib.getpragmanametypedict(dbpath, thisaction.tablename) vartype = variablestypedict[thisaction.variablename] thisaction.statusmsg += ' Variablevalue: ' + str(thisaction.variablevalue) + '. Criterion: ' + str(thisaction.criterion) + ' . ' # process criterion according to type curstatus = False if vartype == 'boolean': thisaction.statusmsg += ' Processing boolean. ' # TODO: String conversion is a hack here and needs series work. if str(thisaction.variablevalue) == str(thisaction.criterion): curstatus = True elif vartype == 'integer' or vartype == 'real': thisaction.statusmsg += ' Processing integer/real. ' # print(thisaction.operator) thisaction.variablevalue = float(thisaction.variablevalue) thisaction.criterion = float(thisaction.criterion) if thisaction.operator == 'greater': if thisaction.variablevalue > thisaction.criterion: curstatus = True elif thisaction.operator == 'greaterorequal': if thisaction.variablevalue >= thisaction.criterion: curstatus = True elif thisaction.operator == 'less': if thisaction.variablevalue < thisaction.criterion: curstatus = True elif thisaction.operator == 'lessorequal': if thisaction.variablevalue <= thisaction.criterion: curstatus = True elif thisaction.operator == 'equal': if thisaction.variablevalue == thisaction.criterion: curstatus = True else: thisaction.statusmsg += 'Operator error. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True elif vartype == 'text': thisaction.statusmsg += ' Processing text. ' if thisaction.variablevalue == thisaction.criterion: curstatus = True else: thisaction.statusmsg += ' Mode Error for vartype ' + vartype + '. ' if curstatus: thisaction.statusmsg += 'Status is true. ' else: thisaction.statusmsg += 'Status is not true. ' currenttime = pilib.gettimestring() # if status is true and current status is false, set ontime if curstatus and not thisaction.status: # print(str(curstatus) + ' ' + str(thisaction.status)) thisaction.statusmsg += 'Setting status ontime. ' thisaction.ontime = pilib.gettimestring() thisaction.status = 1 elif not curstatus and thisaction.status: thisaction.statusmsg += 'Setting status offtime. ' thisaction.offtime = pilib.gettimestring() thisaction.status = 0 # Set current status if curstatus: thisaction.status = 1 # print('settings status') else: thisaction.status = 0 # print('resetting status') # if status is true and alarm isn't yet active, see if ondelay exceeded if curstatus and not thisaction.active: # print(pilib.timestringtoseconds(currenttime)) statusontime = pilib.timestringtoseconds(currenttime) - pilib.timestringtoseconds(thisaction.ontime) # print(statusontime) if statusontime > thisaction.ondelay: thisaction.statusmsg += 'Setting action active' thisaction.active = 1 else: thisaction.statusmsg += 'On delay not reached' # if status is not true and alarm is active, see if offdelay exceeded if not curstatus and thisaction.active: statusofftime = pilib.timestringtoseconds(currenttime) - pilib.timestringtoseconds(thisaction.offtime) if statusofftime > thisaction.offdelay: thisaction.statusmsg += 'Setting action inactive' thisaction.active = 0 # Send an alert / reset indicator if activereset is on if thisaction.activereset: thisaction.offact() else: thisaction.statusmsg += 'Off delay not reached' # test to see if it is time to alert, based on delay ond alert time if thisaction.active: # check to see if it is time to alert # For things like outputs, actionfrequency should be zero to always enforce that action is on. # print(pilib.timestringtoseconds(currenttime)) # print(pilib.timestringtoseconds(thisaction.lastactiontime)) # print(float(thisaction.actionfrequency)) # print(pilib.timestringtoseconds(currenttime)-pilib.timestringtoseconds(thisaction.lastactiontime)) if pilib.timestringtoseconds(currenttime) - pilib.timestringtoseconds(thisaction.lastactiontime) > float(thisaction.actionfrequency): alert = True thisaction.statusmsg += "Time to act. " else: alert = False thisaction.statusmsg += "Not yet time to act." if alert: # We're ready to alert or alert again. thisaction.lastactiontime = currenttime if curstatus: thisaction.onact() else: thisaction.offact() else: thisaction.statusmsg += 'Action disabled.' thisaction.status = 0 else: thisaction.statusmsg += 'Mode unrecognized.' # print(thisaction.statusmsg) thisaction.publish()
__version__ = "1.0" __maintainer__ = "Colin Reese" __email__ = "*****@*****.**" __status__ = "Development" # This script runs the input reading scripts # specified interval, sends to log, channels and plot dbs import pilib import updateio from time import sleep readtime = 10 # default, seconds # Read from systemstatus to make sure we should be running updateioenabled = pilib.getsinglevalue(pilib.controldatabase, 'systemstatus', 'updateioenabled') while updateioenabled: #print("runtime") #print("reading input") # Read again, once inside each loop so we terminate if the # variable name is changed inputsreadenabled = pilib.sqlitedatumquery(pilib.controldatabase, 'select inputsreadenabled from systemstatus') # Set last run time pilib.sqlitequery(pilib.controldatabase, 'update systemstatus set lastinputspoll=\'' + pilib.gettimestring() + '\'') pilib.sqlitequery(pilib.controldatabase, 'update systemstatus set updateiostatus=\'1\'') # Read and record everything as specified in controldatabase # Update database of inputs with read data