for time in ringPattern: timeDelay = ( float(int(ringPattern[i])) / 10 ) # delay is stated in tenth of a second if i % 2 == 0: if verbose: print("*** Ringing bell for %s s..." % timeDelay) bellRelayState = True else: if verbose: print("*** Pausing bell for %s s..." % timeDelay) bellRelayState = False GPIO.output(bellRelayGpio, bellRelayState) # set bell gpio pin sleep(timeDelay) # ring or pause i += 1 GPIO.output(bellRelayGpio, False) # make sure relay is turned off if verbose: print("*** Stopped ringing") GPIO.cleanup(bellRelayGpio) # close cursor db_close_cursor(cnx, cursor, verbose) # close db db_disconnect(cnx, verbose)
def importToDb(inFile, verbose): valuesInserted = 0 valuesUpdated = 0 # connect to database cnx = db_connect(verbose) # create cursor cursor = db_create_cursor(cnx, verbose) # open file if verbose: print("\n*** Opening file: \n %s" % inFile) with open(inFile) as f: content = f.readlines() content = [x.strip() for x in content] # remove unwanted \n and so on # iterate through file lineNumber = 0 for line in content: lineNumber += 1 isHeaderLine = False oldVerbose = verbose # verbose = False if verbose: print("Line %s: %s" % (lineNumber, line)) for table in ("breaks", "ringTimes", "ringPatterns", "days"): if line.startswith(table): thisTable = table if verbose: print("\n*** Now processing table: %s" % thisTable) isHeaderLine = True break if not isHeaderLine: valuesInserted += 1 values = line.split(";") selection = ", %s" % tableSelection(thisTable, verbose) selection = selection.replace(" ", "").split(",") i = 0 for column in selection: if i != 0: if verbose: print(" %s: %s" % (column, values[i])) if i == 1 and values[i]: print("This should be updated") elif i == 1: print("This should be added") i += 1 verbose = oldVerbose if verbose: print("\n*** %s rows parsed" % lineNumber) # close cursor db_close_cursor(cnx, cursor, verbose) # close db db_disconnect(cnx, verbose) return valuesInserted, valuesUpdated
def dumpToFile(): filesWritten = 0 timeNow = datetime.today() timeStamp = timeNow.strftime("%Y%m%d%H%M%S") USBPath = findUSBMountPoint(USBDir, labelMatch, verbose) if not USBPath: LCDMessage = "No USB inserted" if verbose: print("\n*** Printing to LCD: \n %s" % LCDMessage) displayOnLCD("", LCDMessage, verbose) onError(5, "No matching USB mounted") else: displayOnLCD("", "Writing files...", verbose) # database outFilePath = os.path.join(tempDir, "piSchoolBellDb-%s.csv" % timeStamp) if verbose: print("\n*** Creating file %s ..." % outFilePath) outFile = open(outFilePath, "a") # connect to database cnx = db_connect(verbose) # create cursor cursor = db_create_cursor(cnx, verbose) for table in ("breaks", "ringTimes", "ringPatterns", "days"): if verbose: print("\n*** Reading table %s..." % table) selection = tableSelection(table, verbose) query = "SELECT " + selection + " FROM " + table + " " result, rowCount = db_query(cursor, query, verbose) # run query if rowCount: if verbose: print("\n*** Table: %s" % table) columnNames = selection.replace(", ", ";") outFile.write("%s;%s\n" % (table, columnNames)) selection = selection.replace(" ", "").split(",") for row in result: i = 0 values = "" for columnName in selection: column = row[i] values = "%s;%s" % (values, column) if verbose: print(" %s: %s" % (columnName, column)) i += 1 outFile.write("%s\n" % values) if verbose: print() if verbose: print("\n*** Closing file %s ..." % outFilePath) outFile.close() if verbose: print("\n*** Moving file \n %s \n to \n %s" % (outFilePath, USBPath)) copy(outFilePath, USBPath) filesWritten += 1 # log files dirName, fileName, extension = splitPath(logFile, verbose) logFileName = os.path.join(USBPath, "%s-%s.%s" % (fileName, timeStamp, extension)) if verbose: print("\n*** Copying \n %s \n to \n %s ..." % (logFile, logFileName)) copyfile(logFile, logFileName) filesWritten += 1 if verbose: print(" %s" % gpioWatchLog) dirName, fileName, extension = splitPath(gpioWatchLog, verbose) gpioWatchLogName = os.path.join( USBPath, "%s-%s.%s" % (fileName, timeStamp, extension) ) if verbose: print( "\n*** Copying \n %s \n to \n %s ..." % ( gpioWatchLog, gpioWatchLogName, ) ) copyfile(gpioWatchLog, gpioWatchLogName) filesWritten += 1 # close cursor db_close_cursor(cnx, cursor, verbose) # close db db_disconnect(cnx, verbose) LCDMessage = "%s files written" % filesWritten if verbose: print("\n*** Printing to LCD: \n %s" % LCDMessage) displayOnLCD("", LCDMessage, verbose)
def button1Pressed(): # find days in database line_1 = "No data" # connect to database cnx = db_connect(verbose) # create cursor cursor = db_create_cursor(cnx, verbose) query = "SELECT date, DATEDIFF(date,CURDATE()) FROM days ORDER BY date DESC LIMIT 1" result, rowCount = db_query(cursor, query, verbose) # run query try: result, rowCount = db_query(cursor, query, verbose) # run query except MySQLdb.Error as e: print "\n<br>Error: Could not get number of days \n<br>%s" % e print "\n<br>SQL: %s" % query else: if rowCount: if verbose: print "\n*** Got a result" for row in result: lastDate = row[0] daysToEnd = row[1] if verbose: print " Days in db: %s" % daysToEnd print " Days in db: %s" % lastDate line_1 = "%s, %s" % (daysToEnd, lastDate.strftime("%Y-%m-%d")) # find this devices ip address interfaceIPs = [] line_2 = "Not connected" if verbose: print "\n*** Finding interfaces..." interfaces = ni.interfaces() if verbose: print " Found %s interfaces" % len(interfaces) print "\n*** Looking up ip addresses..." i = 0 for interface in interfaces: try: ip = ni.ifaddresses(interface)[ni.AF_INET][0]['addr'] except: ip = "NA" interfaceIPs.append({"interface%s" % i: interface, "ip%s" % i: ip}) i += 1 i = 0 for interfaceIP in interfaceIPs: if verbose: print " Interface: %s" % interfaceIP['interface%s' % i] print " IP: %s" % interfaceIP['ip%s' % i] if (not interfaceIP['ip%s' % i].startswith('127') and not interfaceIP['ip%s' % i].startswith('169') and not interfaceIP['ip%s' % i] == "NA"): line_2 = interfaceIP['ip%s' % i] if verbose: print "*** This is the one we will display" if verbose: print i += 1 if internetAccess(testAddress, verbose): line_2 = "*%s" % line_2 else: line_2 = "-%s" % line_2 return line_1, line_2 # close cursor db_close_cursor(cnx, cursor, verbose) # close db db_disconnect(cnx, verbose)