def printPwrAndSWdata():
    now = datetime.datetime.now()
    print( '-'*75,now.strftime("%Y-%m-%d %H:%M:%S") )
    
    # connect to the (local) mySQL DB on the Raspi
    #lclConn  = getMySQLconn()
    #localSQL = lclConn.cursor()        
    # get power and switch status data
    #localSQL.execute('SELECT * FROM `building_power` ORDER BY datetime DESC LIMIT 1')
    #data = localSQL.fetchone() 
    # close all open ends
    #localSQL.close     # sql cursor
    #lclConn.close      # sql connection        
    now = datetime.datetime.now()
    timestamp = round( datetime.datetime.timestamp( now )) 
    #print("DATA:", data)
    #lastReading = data[1].timestamp()
    httpSession = requests.Session()    # create the httpSession object
    data        = getCurrentPower( httpSession )     # get the initial reading    
    #print("timestamp, lastReading", timestamp, lastReading)
    #age = round(timestamp - lastReading) 
    age = 2 # since it's a live request
    color = 'green'
    if age > 150: color = 'yellow'
    if age > 450: color = 'red'
    agetext = ", last update " + hilite(formatSeconds( age ),color) + " ago"
    #power = round(data[2])
    power = data
    color = 'green'
    if power > 300: color = 'yellow'
    if power > 500: color = 'red'
    print( "    Power:", hilite(str(power).rjust(5),'none',True) + agetext ) 
    
    # now get DR status
    timestamp = round( datetime.datetime.timestamp( now )) 
    try: 
        line = open('DRstatus.log').read().split()
    except:
        broadcast("Unable to determine RD switches status - error when trying to open or read DRstatus.log!")
        broadcast(line)
        return 20, False
    if len(line)>0:
        age = timestamp - int(line[len(line)-1]) 
        color = 'green'
        if age > 150: color = 'yellow'
        if age > 450: color = 'red'
        agetext = hilite(formatSeconds( age ), color)
        color = 'green'
        if line[0]==1: 
            color = 'red'
            if line[1]==0: color = 'yellow'
        print( "DualRelay: ", hilite( (str(line[0])+' '+str(line[1])),color), ' last update', agetext, 'ago' )
    else:
        broadcast(" WARNING! Unable to determine RD switches status - DRstatus.log content is: "+str(line) )
        return 10, False
    return 0, line[0]
def getTodaysEvent():
    # connect to the (local) mySQL DB on the Raspi
    print('-'*95)
    conn  = getMySQLconn()
    if conn==-1: 
        print("Unable to get mySQL connection!")
        return
    cur = conn.cursor()        
    # get power and switch status data
    eventName, evtActive, toStart, room, sinceEnd, targetTemp, eventID, online_id = checkCurrentEvent(cur)
    if not eventName=='':
        print( "        eventName         | evtActive |    toStart | room |   sinceEnd | targetTemp | eventID" )
        print( hilite( ( 
                         eventName.center(25)+' | '+str(evtActive).center(9)+' | '+formatSeconds( toStart ) + ' |  '+
                          str(room).center(3)+' | '+formatSeconds(sinceEnd) +' | '+str(targetTemp).center(10)+' | ' + str(eventID).center(7) 
                       ), 'green' ) )
    else:
        print("No scheduled event today.")
    
    print('-'*95)
    
    lclConn  = getMySQLconn()
    localSQL = lclConn.cursor()      
    if not localSQL==1:
        # get last event data
        localSQL.execute('SELECT * FROM `heating_logbook` ORDER BY timestamp DESC LIMIT 1')
        data = localSQL.fetchone() 
        try:
            text  = "Heating activity: " 
            if data[0].date() == datetime.date.today():
                text += hilite( 'today at '+data[0].strftime("%H:%M"), 'none', True ) 
            else:
                text += 'on ' + hilite( data[0].strftime("%A, %Y-%m-%d %H:%M"), 'none', True ) 
            if data[3].seconds > 0:
                text += hilite(" Estimate on: ",'none', True) + hilite( formatSeconds(data[3].seconds, False), 'green' )
            if data[4].seconds > 0:
                text += hilite(" Actual on: ",  'none', True) + hilite( formatSeconds(data[4].seconds, False), 'green' )
            if data[5].seconds > 0:
                text += hilite(" Actual off: ", 'none', True) + hilite( formatSeconds(data[5].seconds, False), 'green' )
            print(text)
        except:
            print(data)
    # close all open ends
    localSQL.close     # sql cursor
    lclConn.close      # sql connection     

    if oneoff: 
        print('-' * 75)
        print( "Current Event DB TAN is:", hilite( getCurrentTAN(localSQL), 'none', True ) )
    
    print('-'*75, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") )
def printSensorLogFiles():
    now = datetime.datetime.now()
    timestamp = round( datetime.datetime.timestamp( now )) 
    
    # read all logfiles (filenames must containt 'temp')
    files = os.listdir()
    ages,nr,oneGood = 0,0,False
    for fi in files:
        if fi[:4]=='temp':
            line = open(fi).read().split()
            if len(line)>5:     # we seem to have a valid content
                age = timestamp-int(line[5]) 
                color = 'green'
                if age < 599: oneGood=True       # if we have one good reading, TF is still working!
                else: color = 'yellow'
                ages+=age
                nr  +=1
                agetext = "last update " + hilite(formatSeconds(age),color) + " ago" 
                fname = fi.split('.')[0].rjust(9) + ":" 
                print( fname, hilite(line[0].rjust(6),'none',True), agetext )
            else:
                broadcast( 'Sensor Logfile was empty! ' + str(fi) + ', ' + str(line) )
    if onWindows: return 0
    
    # get babyroom temp
    #bts, babyTemp = getLastTempHumid()
    #age = timestamp-bts
    #color = 'green'
    #if age > 50: color = 'yellow'
    #if age > 150: color = 'red'
    #print( " Babyroom: ", (hilite(babyTemp,'none',True)+',').rjust(7), "last update", hilite(formatSeconds(age),color), "ago" )
    
    print( '-'*75 )

    # restart rPi if no new data since 'maxDelay' (on avg)
    if nr == 0: 
        broadcast(" No sensor logfiles found! REBOOT!")
        return 199    # ===> REBOOT!
    if not oneGood and ages/nr > maxDelay: 
        broadcast(" WARNING! Sensor logfiles found on average more than "+str(round(maxDelay/60))+" minutes old!")
        return 20
    if oneGood: return 0
    # return a "warning" if there was not one recent reading
    return 3