def setrawspilights(enabledlists, CS=1): from pilib import writedatedlogmsg, systemstatuslog, systemstatusloglevel try: import spidev except ImportError: writedatedlogmsg(systemstatuslog, 'You have not installed the spidev python package. Exiting.', 0, systemstatusloglevel) exit else: spi = spidev.SpiDev() try: spi.open(0, CS) # Port 0, CS1 except: writedatedlogmsg(systemstatuslog, 'Rrror raised on spi open. exiting.', 0, systemstatusloglevel) exit else: # Notes: # Low is on. Cathodes are open drain. # This unforunately means we need to initialize # to off on start-up. not a huge deal spiassignments = [] for enabledlist in enabledlists: bytesum = 0 for index, bit in enumerate(enabledlist): bytesum += bit * (2 ** index) spiassign = 255 - bytesum spiassignments.append(spiassign) # Transfer bytes resp = spi.xfer2(spiassignments) return resp
def processsystemflags(systemflags=None): from pilib import writedatedlogmsg, systemstatuslog, systemstatusloglevel if not systemflags: systemflags = pilib.readalldbrows(pilib.systemdatadatabase, 'systemflags') flagnames = [] flagvalues = [] for flag in systemflags: flagnames.append(flag['name']) flagvalues.append(flag['value']) stop = False if 'reboot' in flagnames: if flagvalues[flagnames.index('reboot')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='reboot'") import subprocess writedatedlogmsg(systemstatuslog, 'Rebooting for system flag', 0, systemstatusloglevel) subprocess.call(['reboot']) if 'netconfig' in flagnames: if flagvalues[flagnames.index('netconfig')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, "name='netconfig'") from netconfig import runconfig writedatedlogmsg(systemstatuslog, 'Restarting network configuration', 0, systemstatusloglevel) runconfig() if 'updateiicontrollibs' in flagnames and not stop: if flagvalues[flagnames.index('updateiicontrollibs')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updateiicontrollibs\'') from misc.gitupdatelib import updateiicontrollibs writedatedlogmsg(systemstatuslog, 'Updating iicontrollibs', 0, systemstatusloglevel) updateiicontrollibs(True) if 'updatecupidweblib' in flagnames and not stop: if flagvalues[flagnames.index('updatecupidweblib')]: stop = True pilib.setsinglevalue(pilib.systemdatadatabase, 'systemflags', 'value', 0, 'name=\'updatecupidweblib\'') from misc.gitupdatelib import updatecupidweblib writedatedlogmsg(systemstatuslog, 'Updating cupidweblib', 0, systemstatusloglevel) updatecupidweblib(True)
def setrawspilights(enabledlists, CS=1): from pilib import writedatedlogmsg, systemstatuslog, systemstatusloglevel try: import spidev except ImportError: writedatedlogmsg( systemstatuslog, 'You have not installed the spidev python package. Exiting.', 0, systemstatusloglevel) exit else: spi = spidev.SpiDev() try: spi.open(0, CS) # Port 0, CS1 except: writedatedlogmsg(systemstatuslog, 'Rrror raised on spi open. exiting.', 0, systemstatusloglevel) exit else: # Notes: # Low is on. Cathodes are open drain. # This unforunately means we need to initialize # to off on start-up. not a huge deal spiassignments = [] for enabledlist in enabledlists: bytesum = 0 for index, bit in enumerate(enabledlist): bytesum += bit * (2**index) spiassign = 255 - bytesum spiassignments.append(spiassign) # Transfer bytes resp = spi.xfer2(spiassignments) return resp
# Run the script periodically based on systemstatus # This script does the following: # Processes channels data into output settings and channels data (action, etc) # Log data # What it does not do: # Read inputs (done by updateio, through periodicreadio) # Set physical outputs (this is done by updateio) systemstatus = pilib.readalldbrows(pilib.controldatabase, 'systemstatus')[0] while systemstatus['picontrolenabled']: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Running picontrol', 3, pilib.systemstatusloglevel) pilib.writedatedlogmsg(pilib.controllog, 'Running picontrol', 3, pilib.controlloglevel) # Set poll date. While intuitively we might want to set this # after the poll is complete, if we error below, we will know # from this stamp when it barfed. This is arguably more valuable # then 'last time we didn't barf' pilib.sqlitequery( pilib.controldatabase, "update systemstatus set lastpicontrolpoll='" + pilib.gettimestring() + "'") channels = pilib.readalldbrows(pilib.controldatabase, 'channels') outputs = pilib.readalldbrows(pilib.controldatabase, 'outputs') controlalgorithms = pilib.readalldbrows(pilib.controldatabase,
def rundaemon(startall=False): from subprocess import Popen, PIPE from time import sleep from pilib import writedatedlogmsg, daemonlog, daemonloglevel # Set up list of enabled statuses (whether to restart if # we find that the process is not currently running picontrolenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select picontrolenabled from systemstatus') updateioenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select updateioenabled from systemstatus') systemstatusenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select systemstatusenabled from systemstatus') sessioncontrolenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select sessioncontrolenabled from systemstatus') serialhandlerenabled = pilib.sqlitedatumquery( pilib.controldatabase, 'select serialhandlerenabled from systemstatus') enableditemlist = [(int(updateioenabled)), (int(picontrolenabled)), int(systemstatusenabled), int(sessioncontrolenabled), int(serialhandlerenabled)] itemstatuses = findprocstatuses(pilib.daemonprocs) # Set system message systemstatusmsg = '' for name, enabled, status in zip(pilib.daemonprocs, enableditemlist, itemstatuses): systemstatusmsg += name + ' - Enabled: ' + str( enabled) + ' Status: ' + str(status) + '. ' if pilib.daemonloglevel > 0: pilib.writedatedlogmsg( pilib.daemonlog, name + ' - Enabled: ' + str(enabled) + ' Status: ' + str(status) + '. ') pilib.setsinglevalue(pilib.controldatabase, 'systemstatus', 'systemmessage', systemstatusmsg) # Set up list of itemnames in the systemstatus table that # we assign the values to when we detect if the process # is running or not statustableitemnames = [ 'updateiostatus', 'picontrolstatus', 'systemstatusstatus', 'sessioncontrolstatus', 'serialhandlerstatus' ] for index, item in enumerate(pilib.daemonprocs): # set status if itemstatuses[index]: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 1') if pilib.daemonloglevel > 0: pilib.writedatedlogmsg( pilib.daemonlog, 'Process is running: ' + pilib.baselibdir + pilib.daemonprocs[index]) else: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 0') if pilib.daemonloglevel > 0: pilib.writedatedlogmsg( pilib.daemonlog, 'Process is not running: ' + pilib.baselibdir + pilib.daemonprocs[index]) # run if set to enable if enableditemlist[index]: print(pilib.baselibdir + pilib.daemonprocs[index]) if pilib.daemonloglevel > 0: pilib.writedatedlogmsg( pilib.daemonlog, 'Starting ' + pilib.baselibdir + pilib.daemonprocs[index]) procresult = Popen( [pilib.baselibdir + pilib.daemonprocs[index]], stdout=PIPE) # if pilib.daemonloglevel > 0: # pilib.writedatedlogmsg(pilib.daemonproclog, procresult.stdout.read()) sleep(1) # Refresh after set itemstatuses = findprocstatuses(pilib.daemonprocs) for item in pilib.daemonprocs: index = pilib.daemonprocs.index(item) # set status if itemstatuses[index]: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 1') else: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 0') # Set system message systemstatusmsg = '' for name, enabled, status in zip(pilib.daemonprocs, enableditemlist, itemstatuses): systemstatusmsg += name + ' - Enabled: ' + str( bool(enabled)) + ' Status: ' + str(status) + '. ' pilib.setsinglevalue(pilib.controldatabase, 'systemstatus', 'systemmessage', systemstatusmsg) # Rotate all logs pilib.rotatelogs(pilib.networklog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.systemstatuslog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.iolog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.controllog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.daemonlog, pilib.numlogs, pilib.maxlogsize)
if itemstatuses[index]: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 1') else: pilib.sqlitequery( pilib.controldatabase, 'update systemstatus set ' + statustableitemnames[index] + ' = 0') # Set system message systemstatusmsg = '' for name, enabled, status in zip(pilib.daemonprocs, enableditemlist, itemstatuses): systemstatusmsg += name + ' - Enabled: ' + str( bool(enabled)) + ' Status: ' + str(status) + '. ' pilib.setsinglevalue(pilib.controldatabase, 'systemstatus', 'systemmessage', systemstatusmsg) # Rotate all logs pilib.rotatelogs(pilib.networklog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.systemstatuslog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.iolog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.controllog, pilib.numlogs, pilib.maxlogsize) pilib.rotatelogs(pilib.daemonlog, pilib.numlogs, pilib.maxlogsize) if __name__ == "__main__": from pilib import writedatedlogmsg, daemonlog, daemonloglevel writedatedlogmsg(daemonlog, 'Running daemon.', 1, daemonloglevel) rundaemon() writedatedlogmsg(daemonlog, 'Daemon complete.', 1, daemonloglevel)
from misc.gitupdatelib import updatecupidweblib writedatedlogmsg(systemstatuslog, 'Updating cupidweblib', 0, systemstatusloglevel) updatecupidweblib(True) if __name__ == '__main__': import pilib import time from misc.gitupdatelib import updategitversions # This doesn't update git libraries. It checks current versions and updates the database try: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Checking git versions', 3, pilib.systemstatusloglevel) updategitversions() except: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Error in git version check', 0, pilib.systemstatusloglevel) else: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Git version check complete', 3, pilib.systemstatusloglevel) systemstatus = pilib.readalldbrows(pilib.controldatabase, 'systemstatus')[0] # Keep reading system status? while systemstatus['systemstatusenabled']: starttime = time.time() pilib.writedatedlogmsg(pilib.systemstatuslog, 'System status routine is starting. ', 3, pilib.systemstatusloglevel)
def processGPIOinterface(interface, prevoutputs, prevoutputvalues, prevoutputids, previnputs, previnputids, defaults, logconfig): import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) import pilib options = pilib.parseoptions(interface['options']) address = int(interface['address']) # TODO : respond to more options, like pullup and pulldown pilib.writedatedlogmsg(pilib.iolog, 'GPIO address' + str(address) + ' enabled', 4, logconfig['iologlevel']) # Get name from ioinfo table to give it a colloquial name gpioname = pilib.sqlitedatumquery(pilib.controldatabase, 'select name from ioinfo where id=\'' + interface['id'] + '\'') polltime = pilib.gettimestring() querylist = [] # Append to inputs and update name, even if it's an output (can read status as input) if options['mode'] == 'output': pilib.writedatedlogmsg(pilib.iolog, 'Setting output mode for GPIO address' + str(address), 3, logconfig['iologlevel']) try: GPIO.setup(address, GPIO.OUT) except TypeError: pilib.writedatedlogmsg(pilib.iolog, 'You are trying to set a GPIO with the wrong variable type : ' + str(type(address)), 0, logconfig['iologlevel']) pilib.writedatedlogmsg(pilib.iolog, 'Exiting interface routine for ' + interface['id'], 0, logconfig['iologlevel']) return # Set the value of the gpio. # Get previous value if exists if interface['id'] in prevoutputids: value = prevoutputvalues[prevoutputids.index(interface['id'])] else: value = 0 if value == 1: GPIO.output(address, True) pilib.writedatedlogmsg(pilib.iolog, 'Setting output ON for GPIO address' + str(address), 3, logconfig['iologlevel']) else: GPIO.output(address, False) pilib.writedatedlogmsg(pilib.iolog, 'Setting output OFF for GPIO address' + str(address), 3, logconfig['iologlevel']) # Get output settings and keep them if the GPIO previously existed if interface['id'] in prevoutputids: pollfreq = prevoutputs[prevoutputids.index(interface['id'])]['pollfreq'] ontime = prevoutputs[prevoutputids.index(interface['id'])]['ontime'] offtime = prevoutputs[prevoutputids.index(interface['id'])]['offtime'] else: try: pollfreq = defaults['defaultoutputpollfreq'] except: pollfreq = 60 ontime = '' offtime = '' # Add entry to outputs tables querylist.append('insert into outputs values (\'' + interface['id'] + '\',\'' + interface['interface'] + '\',\'' + interface['type'] + '\',\'' + str( address) + '\',\'' + gpioname + '\',\'' + str(value) + "','','" + str(polltime) + '\',\'' + str(pollfreq) + "','" + ontime + "','" + offtime + "')") else: GPIO.setup(address, GPIO.IN) value = GPIO.input(address) pilib.writedatedlogmsg(pilib.iolog, 'Setting input mode for GPIO address' + str(address), 3, logconfig['iologlevel']) # Get input settings and keep them if the GPIO previously existed if interface['id'] in previnputids: pollfreq = previnputs[previnputids.index(interface['id'])]['pollfreq'] ontime = previnputs[previnputids.index(interface['id'])]['ontime'] offtime = previnputs[previnputids.index(interface['id'])]['offtime'] pilib.writedatedlogmsg(pilib.iolog, 'Restoring values from previous inputids: pollfreq = ' + str( pollfreq) + ' ontime = ' + str(ontime) + ' offtime = ' + str( offtime), 3, logconfig['iologlevel']) else: try: pollfreq = defaults['defaultinputpollfreq'] except: pollfreq = 60 ontime = '' offtime = '' pilib.writedatedlogmsg(pilib.iolog, 'Setting values to defaults, defaultinputpollfreq = ' + str( pollfreq), 3, logconfig['iologlevel']) querylist.append( 'insert into inputs values (\'' + interface['id'] + '\',\'' + interface['interface'] + '\',\'' + interface['type'] + '\',\'' + str(address) + '\',\'' + gpioname + '\',\'' + str( value) + "','','" + str(polltime) + '\',\'' + str(pollfreq) + "','" + ontime + "','" + offtime + "')") return querylist
def processMBinterface(interface, prevoutputs, prevoutputids, previnputs, previnputids, defaults, logconfig): from netfun import readMBcodedaddresses, MBFCfromaddress import pilib # get all modbus reads that have the same address from the modbus table try: modbustable = pilib.readalldbrows(pilib.controldatabase, 'modbustcp') except: pilib.writedatedlogmsg(pilib.iolog, 'Error reading modbus table', 0, logconfig['iologlevel']) else: pilib.writedatedlogmsg(pilib.iolog, 'Read modbus table', 4, logconfig['iologlevel']) querylist = [] for entry in modbustable: # Get name from ioinfo table to give it a colloquial name # First we have to give it a unique ID. This is a bit difficult with modbus if entry['mode'] == 'read': shortmode = 'R' elif entry['mode'] == 'write': shortmode = 'W' elif entry['mode'] == 'readwrite': shortmode = 'RW' else: pilib.writedatedlogmsg(pilib.iolog, 'modbus mode error', 1, logconfig['iologlevel']) try: mbid = entry['interfaceid'] + '_' + str(entry['register']) + '_' + str(entry['length']) + '_' + shortmode except KeyError: pilib.writedatedlogmsg(pilib.iolog, 'Cannot form mbid due to key error', 0, logconfig['iologlevel']) return pilib.writedatedlogmsg(pilib.iolog, 'Modbus ID: ' + mbid, 4, logconfig['iologlevel']) mbname = pilib.sqlitedatumquery(pilib.controldatabase, "select name from ioinfo where id='" + mbid + "'") polltime = pilib.gettimestring() if entry['interfaceid'] == interface['id']: # For now, we're going to read them one by one. We'll assemble these into block reads # in the next iteration if entry['mode'] == 'read': # Get previous metadata and data # Get input settings and keep them if the input previously existed if mbid in previnputids: pollfreq = previnputs[previnputids.index(mbid)]['pollfreq'] ontime = previnputs[previnputids.index(mbid)]['ontime'] offtime = previnputs[previnputids.index(mbid)]['offtime'] prevvalue = previnputs[previnputids.index(mbid)]['offtime'] prevpolltime = previnputs[previnputids.index(mbid)]['offtime'] pilib.writedatedlogmsg(pilib.iolog, 'Restoring values from previous inputids: pollfreq = ' + str( pollfreq) + ' ontime = ' + str(ontime) + ' offtime = ' + str( offtime), 3, logconfig['iologlevel']) else: # set values to defaults if it did not previously exist try: pollfreq = defaults['defaultinputpollfreq'] except: pollfreq = 60 ontime = '' offtime = '' prevvalue = '' prevpolltime = '' pilib.writedatedlogmsg(pilib.iolog, 'Setting values to defaults, defaultinputpollfreq = ' + str(pollfreq), 3, logconfig['iologlevel']) # Read data try: readresult = readMBcodedaddresses(interface['address'], entry['register'], entry['length']) except: pilib.writedatedlogmsg(pilib.iolog, 'Uncaught reror reading modbus value', 0, logconfig['iologlevel']) else: if readresult['statuscode'] == 0: values = readresult['values'] try: FC = MBFCfromaddress(int(entry['register'])) except ValueError: pilib.writedatedlogmsg(pilib.iolog, 'Malformed address for FC determination : ' + str(entry['address']), 0, logconfig['iologlevel']) else: pilib.writedatedlogmsg(pilib.iolog, 'Function code : ' + str(FC), 4, logconfig['iologlevel']) returnvalue = 0 if len(values) > 0: pilib.writedatedlogmsg(pilib.iolog, 'Multiple values returned', 4, logconfig['iologlevel']) if not entry['bigendian']: try: values.reverse() except AttributeError: pilib.writedatedlogmsg(pilib.iolog, 'Error on reverse of MB values: ' + str(values), 0, logconfig['iologlevel']) if entry['format'] == 'float32': import struct byte2 = values[0] % 256 byte1 = (values[0] - byte2)/256 byte4 = values[1] % 256 byte3 = (values[1] - byte4)/256 byte1hex = chr(byte1) byte2hex = chr(byte2) byte3hex = chr(byte3) byte4hex = chr(byte4) hexstring = byte1hex + byte2hex + byte3hex + byte4hex returnvalue = struct.unpack('>f',hexstring)[0] else: for index, val in enumerate(values): if FC in [0, 1]: returnvalue += val * 2 ** index elif FC in [3, 4]: returnvalue += val * 256 ** index else: pilib.writedatedlogmsg(pilib.iolog, 'Invalid function code', 0, logconfig['iologlevel']) else: returnvalue = values[0] if entry['options'] != '': options = pilib.parseoptions(entry['options']) if 'scale' in options: # try: returnvalue = returnvalue * float(options['scale']) # except: # pilib.writedatedlogmsg(pilib.iolog, 'Error on scale operation', 0, logconfig['iologlevel']) if 'precision' in options: # try: returnvalue = round(returnvalue, int(options['precision'])) # except: # pilib.writedatedlogmsg(pilib.iolog, 'Error on precision operation', 0, logconfig['iologlevel']) pilib.writedatedlogmsg(pilib.iolog, 'Values read: ' + str(values), 4, logconfig['iologlevel']) pilib.writedatedlogmsg(pilib.iolog, 'Value returned: ' + str(returnvalue), 4, logconfig['iologlevel']) # Contruct entry for newly acquired data querylist.append('insert into inputs values (\'' + mbid + '\',\'' + interface['id'] + '\',\'' + interface['type'] + '\',\'' + str(entry['register']) + '\',\'' + mbname + '\',\'' + str( returnvalue) + "','','" + str(polltime) + '\',\'' + str(pollfreq) + "','" + ontime + "','" + offtime + "')") else: pilib.writedatedlogmsg(pilib.iolog, 'Statuscode ' + str(readresult['statuscode']) + ' on MB read : ' + readresult['message'] , 0, logconfig['iologlevel']) # restore previous value and construct entry if it existed (or not) querylist.append('insert into inputs values (\'' + mbid + '\',\'' + interface['interface'] + '\',\'' + interface['type'] + '\',\'' + str(entry['register']) + '\',\'' + mbname + '\',\'' + str(prevvalue) + "','','" + str(prevpolltime) + '\',\'' + str(pollfreq) + "','" + ontime + "','" + offtime + "')") pilib.writedatedlogmsg(pilib.iolog, 'Querylist: ' + str(querylist) , 4, logconfig['iologlevel']) return querylist
def updateiodata(database): # This recreates all input and output tables based on the interfaces table. # Thus way we don't keep around stale data values. We could at some point incorporate # a retention feature that keeps them around in case they disappear temporarily. # It also reads the elements if they are enabled and it's time to read them import pilib import traceback import RPi.GPIO as GPIO allowedGPIOaddresses = [18, 23, 24, 25, 4, 17, 21, 22] logconfig = pilib.getlogconfig() GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) tables = pilib.gettablenames(pilib.controldatabase) if 'interfaces' in tables: interfaces = pilib.readalldbrows(pilib.controldatabase, 'interfaces') else: pilib.writedatedlogmsg(pilib.iolog, 'interfaces table not found. Exiting', 1, logconfig['iologlevel']) return if 'inputs' in tables: previnputs = pilib.readalldbrows(pilib.controldatabase, 'inputs') # Make list of IDs for easy indexing previnputids = [] for input in previnputs: previnputids.append(input['id']) else: previnputs = [] previnputids = [] if 'outputs' in tables: prevoutputs = pilib.readalldbrows(pilib.controldatabase, 'outputs') # Make list of IDs for easy indexing prevoutputids = [] prevoutputvalues = [] for output in prevoutputs: prevoutputids.append(output['id']) prevoutputvalues.append(output['value']) else: prevoutputs = {} prevoutputids = [] if 'defaults' in tables: defaults = pilib.readalldbrows(pilib.controldatabase, 'defaults')[0] defaultinputpollfreq = defaults['inputpollfreq'] defaultoutputpollfreq = defaults['outputpollfreq'] else: defaults = [] defaultinputpollfreq = 60 defaultoutputpollfreq = 60 if 'indicators' in tables: indicatornames = [] previndicators = pilib.readalldbrows(pilib.controldatabase, 'indicators') for indicator in previndicators: indicatornames.append(indicator['name']) else: previndicators = [] indicatornames = [] # We drop all inputs and outputs and recreate # Add all into one query so there is no time when the IO don't exist. querylist = [] querylist.append('delete from inputs') querylist.append('delete from outputs') # This is temporary. Clearing the table here and adding entries below can result in a gap in time # where there are no database indicator entries. This is not too much of a problem with indicators, as we # update the hardware explicitly after we add the entries. If the interface queries the table during # this period, however, we could end up with an apparently empty table. # TODO: FIX update on indicators in updateio pilib.sqlitequery(pilib.controldatabase, 'delete from indicators') owfsupdate = False for interface in interfaces: if interface['interface'] == 'I2C': pilib.writedatedlogmsg(pilib.iolog, 'Processing I2C interface' + interface['name'], 3, logconfig['iologlevel']) if interface['enabled']: pilib.writedatedlogmsg(pilib.iolog, 'I2C Interface ' + interface['name'] + ' enabled', 3, logconfig['iologlevel']) if interface['type'] == 'DS2483': pilib.writedatedlogmsg(pilib.iolog, 'Interface type is DS2483', 3, logconfig['iologlevel']) owfsupdate = True elif interface['interface'] == 'USB': pilib.writedatedlogmsg(pilib.iolog, 'Processing USB interface' + interface['name'], 3, logconfig['iologlevel']) if interface['enabled']: pilib.writedatedlogmsg(pilib.iolog, 'USB Interface ' + interface['name'] + ' enabled', 3, logconfig['iologlevel']) if interface['type'] == 'DS9490': pilib.writedatedlogmsg(pilib.iolog, 'Interface type is DS9490', 3, logconfig['iologlevel']) owfsupdate = True elif interface['interface'] == 'MOTE': pilib.writedatedlogmsg(pilib.iolog, 'Processing Mote interface' + interface['name'], 3, logconfig['iologlevel']) if interface['enabled']: pilib.writedatedlogmsg(pilib.iolog, 'Mote Interface ' + interface['name'] + ' enabled', 3, logconfig['iologlevel']) # Grab mote entries from remotes table nodeaddress = int(interface['address']) nodeentries = pilib.dynamicsqliteread(pilib.controldatabase, 'remotes', condition="\"nodeid\"='" + str(nodeaddress) + "'") # Create queries for table insertion # TODO: process mote pollfreq, ontime, offtime moteentries = [] for nodeentry in nodeentries: datadict = pilib.parseoptions(nodeentry['data']) try: entrytype = nodeentry['msgtype'] entryid = 'MOTE' + str(nodeentry['nodeid']) + '_' + nodeentry['keyvaluename'] + '_' + nodeentry['keyvalue'] entrymetareturn = pilib.dynamicsqliteread(pilib.controldatabase, 'ioinfo', condition="\"id\"='" + entryid + "'") try: entrymeta = entrymetareturn[0] except: entrymeta = [] # print(entrymeta) entryoptions={} if entrymeta: entryname = entrymeta['name'] if entrymeta['options']: entryoptions = pilib.parseoptions(entrymeta['options']) else: entryname = '[MOTE' + str(nodeentry['nodeid']) + '] ' + nodeentry['keyvaluename'] + ':' + nodeentry['keyvalue'] except KeyError: print('OOPS KEY ERROR') else: if entrytype == 'iovalue': if 'scale' in entryoptions: entryvalue = str(float(entryoptions['scale']) * float(datadict['iovalue'])) else: entryvalue = datadict['iovalue'] elif entrytype == 'owdev': if 'owtmpasc' in datadict: if 'scale' in entryoptions: entryvalue = str(float(entryoptions['scale']) * float(datadict['owtmpasc'])) else: entryvalue = datadict['owtmpasc'] else: entryvalue = -1 else: entryvalue = -1 moteentries.append('insert into inputs values (\'' + entryid + '\',\'' + interface['interface'] + '\',\'' + interface['type'] + '\',\'' + str(address) + '\',\'' + entryname + '\',\'' + str(entryvalue) + "','','" + nodeentry['time'] + '\',\'' + str(15) + "','" + '' + "','" + '' + "')") # print('querylist') # print(moteentries) querylist.extend(moteentries) elif interface['interface'] == 'LAN': pilib.writedatedlogmsg(pilib.iolog, 'Processing LAN interface' + interface['name'], 3, logconfig['iologlevel']) if interface['enabled']: pilib.writedatedlogmsg(pilib.iolog, 'LAN Interface ' + interface['name'] + ' enabled', 3, logconfig['iologlevel']) if interface['type'] == 'MBTCP': pilib.writedatedlogmsg(pilib.iolog, 'Interface ' + interface['name'] + ' type is MBTCP', 3, logconfig['iologlevel']) try: mbentries = processMBinterface(interface, prevoutputs, prevoutputids, previnputs, previnputids, defaults, logconfig) except: pilib.writedatedlogmsg(pilib.iolog, 'Error processing MBTCP interface ' + interface['name'], 0, logconfig['iologlevel']) errorstring = traceback.format_exc() pilib.writedatedlogmsg(pilib.iolog, 'Error of kind: ' + errorstring, 0, logconfig['iologlevel']) else: pilib.writedatedlogmsg(pilib.iolog, 'Done processing MBTCP interface ' + interface['name'], 3, logconfig['iologlevel']) querylist.extend(mbentries) elif interface['interface'] == 'GPIO': try: address = int(interface['address']) except KeyError: pilib.writedatedlogmsg(pilib.iolog, 'GPIO address key not found for ' + interface['name'], 1, logconfig['iologlevel']) continue pilib.writedatedlogmsg(pilib.iolog, 'Processing GPIO interface ' + str(interface['address']), 3, logconfig['iologlevel']) if address in allowedGPIOaddresses: pilib.writedatedlogmsg(pilib.iolog, 'GPIO address' + str(address) + ' allowed', 4, logconfig['iologlevel']) # Check if interface is enabled if interface['enabled']: GPIOentries = processGPIOinterface(interface, prevoutputs, prevoutputvalues, prevoutputids, previnputs, previnputids, defaults, logconfig) querylist.extend(GPIOentries) else: pilib.writedatedlogmsg(pilib.iolog, 'GPIO address' + str(address) + ' disabled', 4, logconfig['iologlevel']) GPIO.setup(address, GPIO.IN) else: pilib.writedatedlogmsg(pilib.iolog, 'GPIO address' + str(address) + ' not allowed. Bad things can happen. ', 4, logconfig['iologlevel']) elif interface['interface'] == 'SPI0': pilib.writedatedlogmsg(pilib.iolog, 'Processing SPI0', 1, logconfig['iologlevel']) if interface['type'] == 'SPITC': import readspi spidata = readspi.readspitc(0) spitcentries = readspi.recordspidata(database, spidata) querylist.extend(spitcentries) if interface['type'] == 'CuPIDlights': import spilights spilightsentries, setlist = spilights.getCuPIDlightsentries('indicators', 0, previndicators) querylist.extend(spilightsentries) spilights.updatelightsfromdb(pilib.controldatabase, 'indicators', 0) spilights.setspilights(setlist, 0) elif interface['interface'] == 'SPI1': pilib.writedatedlogmsg(pilib.iolog, 'Processing SPI1', 1, logconfig['iologlevel']) if interface['type'] == 'CuPIDlights': import spilights spilightsentries, setlist = spilights.getCuPIDlightsentries('indicators', 1, previndicators) querylist.extend(spilightsentries) spilights.setspilights(setlist, 1) # Set tables querylist.append(pilib.makesinglevaluequery('systemstatus', 'lastiopoll', pilib.gettimestring())) if owfsupdate: from owfslib import runowfsupdate devices, owfsentries = runowfsupdate(execute=False) querylist.extend(owfsentries) pilib.writedatedlogmsg(pilib.iolog, 'Executing query: ' + str(querylist), 5, logconfig['iologlevel']) try: pilib.sqlitemultquery(pilib.controldatabase, querylist) except: errorstring = traceback.format_exc() pilib.writedatedlogmsg(pilib.iolog, 'Error executing query, message: ' + errorstring, 0, logconfig['iologlevel']) pilib.writedatedlogmsg(pilib.errorlog, 'Error executing updateio query, message: ' + errorstring) pilib.writedatedlogmsg(pilib.errorlog, 'Query: ' + str(querylist))
# Run the script periodically based on systemstatus # This script does the following: # Processes channels data into output settings and channels data (action, etc) # Log data # What it does not do: # Read inputs (done by updateio, through periodicreadio) # Set physical outputs (this is done by updateio) systemstatus = pilib.readalldbrows(pilib.controldatabase, 'systemstatus')[0] while systemstatus['picontrolenabled']: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Running picontrol', 3, pilib.systemstatusloglevel) pilib.writedatedlogmsg(pilib.controllog, 'Running picontrol', 3, pilib.controlloglevel) # Set poll date. While intuitively we might want to set this # after the poll is complete, if we error below, we will know # from this stamp when it barfed. This is arguably more valuable # then 'last time we didn't barf' pilib.sqlitequery(pilib.controldatabase, "update systemstatus set lastpicontrolpoll='" + pilib.gettimestring() + "'") channels = pilib.readalldbrows(pilib.controldatabase, 'channels') outputs = pilib.readalldbrows(pilib.controldatabase, 'outputs') controlalgorithms = pilib.readalldbrows(pilib.controldatabase, 'controlalgorithms') algorithmnames=[] for algorithm in controlalgorithms:
writedatedlogmsg(systemstatuslog, 'Updating cupidweblib', 0, systemstatusloglevel) updatecupidweblib(True) if __name__ == '__main__': import pilib import time from misc.gitupdatelib import updategitversions # This doesn't update git libraries. It checks current versions and updates the database try: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Checking git versions', 3, pilib.systemstatusloglevel) updategitversions() except: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Error in git version check', 0, pilib.systemstatusloglevel) else: pilib.writedatedlogmsg(pilib.systemstatuslog, 'Git version check complete', 3, pilib.systemstatusloglevel) systemstatus = pilib.readalldbrows(pilib.controldatabase, 'systemstatus')[0] # Keep reading system status? while systemstatus['systemstatusenabled']: