Пример #1
0
print "Content-type: text/html"
print

print """
<html>

<head><title>piSchoolBell - upcoming rings</title></head>
 
<body>
 
<h3> piSchoolBell - upcoming rings</h3>
"""

# connect to database
cnx = db_connect(verbose)

# create cursor
cursor = db_create_cursor(cnx, verbose)

# get current time
dateTimeNow = datetime.now()
dateNow = str(dateTimeNow.strftime('%Y-%m-%d'))
timeNow = dateTimeNow.strftime('%H:%M')


def pageLinks():

    print '\n<br><a href="upcomingRings.py">Reset page</a>'

    print '\n<br>'
Пример #2
0
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
Пример #3
0
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)
Пример #4
0
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)