def postDailySummary(self): '''Posts a daily summary to Twitter''' # Get current minutes from system time currentTime = datetime.now() # create time difference to be used to work out time period timeDiff = timedelta(days=1) # Get current minutes currentHour = currentTime.strftime("%H") # Check if the hours of the time is 00 which means midnight and the current day # has changed if(currentHour == "00" and (self.LAST_DAY_POST == "" or currentTime.strftime("%d") != self.LAST_DAY_POST)): Debug.writeOut("Daily condition met (hour of day:" + currentHour + " == 00 && day:" + currentTime.strftime("%d") + " == " + self.LAST_DAY_POST + "). Posting to Twitter") # Create SQL to get data for tweet sql = " SELECT COALESCE(ROUND(AVG(energy), 2), 0), COALESCE(MAX(energy), 0), COALESCE(ROUND(AVG(temperature), 1), 0) FROM historical_data WHERE date_time >= ADDDATE(NOW(), INTERVAL -1 DAY)" self.LOGGER.debug(sql) # Get statistics from DB stats = MySQL.executeOneUpdate(sql, None) # Create tweet message = (currentTime - timeDiff).strftime("%d-%b-%Y") + " Summary: " + str(stats[0]) +\ "w was used. " +\ "Energy usage peaked at " + str(stats[1]) + "w. " +\ "The average temperature was " + str(stats[2]) + "c." # Save new day of tweet self.LAST_DAY_POST = currentTime.strftime("%d") # Check if tweet should be a Direct Message or just a tweet if self.CONFIG.getBooleanConfig("Twitter", "directMessagePost"): self.postDirectMessage(self.CONFIG.getBooleanConfig("Twitter", "directMessageUser"), message) else: # Post message to twitter self.tweet(message)
def postHourlySummary(self): '''Posts an hourly summary to Twitter''' # Get current system time currentTime = datetime.now() # create time difference to be used to work out time period timeDiff = timedelta(hours=1) # Get current hour currentHour = currentTime.strftime("%H") # If current hour does not match last post hour then it's been a new hour since last post if(currentHour != self.LAST_HOURLY_POST): Debug.writeOut("Hourly condtion met (" + currentHour + " != " + self.LAST_HOURLY_POST + "). Posting to Twitter") # Create SQL to get data for tweet sql = "SELECT COALESCE(ROUND(AVG(energy), 2), 0), " +\ "COALESCE(MAX(energy), 0), COALESCE(ROUND(AVG(temperature), 1), 0) " +\ "FROM historical_data WHERE date_time >= ADDDATE(NOW(), INTERVAL -1 HOUR)" self.LOGGER.debug(sql) # Get statistics from DB stats = MySQL.executeOneUpdate(sql, None) # Create tweet message = (currentTime - timeDiff).strftime("%H:%M") + "-" + currentTime.strftime("%H:%M") +\ " Summary: " + str(stats[0]) + "w was used." +\ " Energy usage peaked at " + str(stats[1]) + "w" +\ ". The average temperature was " + str(stats[2]) + "c." # Check if tweet should be a Direct Message or just a tweet if self.CONFIG.getBooleanConfig("Twitter", "directMessagePost"): self.postDirectMessage(self.CONFIG.getBooleanConfig("Twitter", "directMessageUser"), message) else: # Post message to twitter self.tweet(message) # Set last hourly post to current hour self.LAST_HOURLY_POST = currentHour