def get(self): app_status = status.load() # Just get the latest tweets tweets = twitter.get_timeline_tweets(800) tweets = filter_tweets(tweets) tweets = filter(lambda t:not t['user']['screen_name'] == twitter_settings.screen_name, tweets) if len(tweets) <= 1: self.response.out.write('<p>Could not generate tweet (not enough eligible tweets)</p>') app_status['latest_tweet'] = 'Could not generate tweet (not enough eligible tweets)' return recent_tweets = twitter.get_tweets(twitter_settings.screen_name, True) best_tweet = create_markovated_tweet(tweets, 140, map(lambda t: t['text'].strip(), recent_tweets)) if best_tweet != None: twitter.post_tweet(best_tweet) self.response.out.write('<p>' + best_tweet + '</p>' + '(' + str(len(best_tweet)) + ')') app_status['latest_tweet'] = best_tweet; else: self.response.out.write('<p>Could not generate tweet</p>') app_status['latest_tweet'] = 'Could not generate tweet' status.save(app_status)
def get(self): self.response.out.write('<html><body>') app_status = status.load() since_id = app_status.get('reply_since_id', -1) if since_id: mentions = twitter.get_mentions(since_id) else: mentions = twitter.get_mentions() self.response.out.write("<pre>" + str(since_id) + ":" + str(mentions) + "</pre>") if not mentions: return for mention in reversed(mentions): self.response.out.write("<p>" + str(mention['id']) + " | " + mention['user']['screen_name'] + " protected("+ str(mention['user']['protected']) + "): " + mention['text'] +"</p>") self.reply_to_user(mentions[-1]['user'], app_status) twitter.follow_user(mentions[-1]['user']['screen_name']) app_status['reply_since_id'] = mentions[-1]['id'] app_status['latest_user_replied_to'] = mentions[-1]['user']['screen_name'] status.save(app_status) self.response.out.write('</body></html>')
def cleanup(signal, frame): if child_process != None: if child_process.poll() == None: child_process.send_signal(signal) # Need to kill the child as well print("Finished.") with open(s.status["directory"]+"/experiment", "w") as f: f.write("Stopped") s.status = None s.save() sys.exit(0)
def feed(): writeLog(path, "INFO") turnOnPump() status.status = "FEEDING" status.save() writeLog("Watering for {0}s...".format(feed_duration), "INFO") time.sleep(feed_duration) turnOffPump() status.status = "PAUSE" status.last_watering = datetime.datetime.now() status.save()
def checkIsWet(): initialize() GPIO.output(port_sensor_cmd, GPIO.HIGH) time.sleep(2) value = GPIO.input(port_sensor_read) == 0 GPIO.output(port_sensor_cmd, GPIO.LOW) writeHistory(value) status.last_reading = datetime.datetime.now() status.soil_was_wet = value status.save() GPIO.cleanup() return value
def doWork(): status.load() try: autoIsOn = status.auto isWet = checkIsWet() sw = shouldWork(isWet) s = "AUTO: {0}; SOIL: {1}; SHOULD_WORK: {2}".format(autoIsOn, get_wet_dry(isWet), sw) writeLog(s, "INFO" if isWet else "WARN") if sw: writeLog("Start watering.", "INFO") feed() isWet = checkIsWet() status.last_watering = datetime.datetime.now() status.soil_was_wet_after = isWet status.save() writeLog("After watering the soil is {0}.".format(get_wet_dry(isWet)), "INFO" if isWet else "ERROR") except KeyboardInterrupt: writeLog("Cleanup GPIO ports", "INFO") GPIO.cleanup() status.status = "SLEEPING" status.save()
def post(self): status.save(json.loads(self.request.get("json_status"))) self.get()
def main(argv): global child_process os.environ["LD_LIBRARY_PATH"] = os.environ.get("LD_LIBRARY_PATH", "")+":"+os.getcwd()+"/contrib/lib" signal.signal(signal.SIGINT, cleanup) signal.signal(signal.SIGQUIT, cleanup) if s.status == None: try: maxFrames = int(argv[2]) except: maxFrames = -1 try: direct = str(argv[1]) except: direct = None s.status = { "directory" : direct, "pid" : os.getpid(), "maxFrames" : maxFrames, "currentFrame" : 0, "timestamp" : int(datetime.datetime.now().strftime("%s")) } s.save() try: if s.status["directory"][-1] == '/': s.status["directory"] = s.status["directory"][0:-1] except: s.status["directory"] = "data/"+datetime.datetime.now().strftime("%F/%H:%M") if not os.path.exists(s.status["directory"]): os.makedirs(s.status["directory"]) with open(s.status["directory"]+"/experiment", "w") as f: f.write("Running") s.status["pid"] = os.getpid() s.status["currentFrame"] = 0 s.save() lasttime = float(datetime.datetime.now().strftime("%s.%f")) initialFiles = len([f for f in os.listdir("data") if 'png' in f]) maxFrames = int(s.status["maxFrames"]) while (s.status != None and (s.status["maxFrames"] < 0 or s.status["currentFrame"] < s.status["maxFrames"])): s.load() #error = subprocess.call(["./FLIRA65-Capture", "-f", "-1", "-D", s.status["directory"]]) child_process = subprocess.Popen(["./FLIRA65-Capture", "-f", str(maxFrames), "-D", s.status["directory"], '-F', s.status["bitdepth"], '-T', s.status["period"]]) while child_process.poll() is None: time.sleep(1) files = [f for f in os.listdir("data") if 'png' in f] s.status["currentFrame"] = len(files)-initialFiles if len(files) == 0: continue latest = s.status["directory"]+"/"+files[-1] s.status["lastFrame"] = "<a href='%s'>%s</a>" % (s.status["directory"]+"/"+files[-1], files[-1]) s.save() if (maxFrames > 0): maxFrames = maxFrames-s.status["currentFrame"] s.save() pass cleanup(None,None) return 0 # redundant
def startAutoWatering(): if checkLogin(): status.auto = True status.save() return redirect(url_for('index'))
def stopWork(): GPIO.cleanup() autoIsOn = False status.auto = autoIsOn status.save()