Commits

Mark Sundstrom committed bcae1ac

webcam_monitor now calls webcam.sh to upload to webfaction

Comments (0)

Files changed (2)

+#!/bin/sh
+# Make sure the folder exists
+mkdir -p ~/Pictures/webcam
+
+# Grab the current images
+curl --silent -o ~/Pictures/webcam/city.jpg http://192.168.0.20/image.jpg
+curl --silent -o ~/Pictures/webcam/river.jpg http://192.168.0.21/image.jpg
+
+# Rsync everything
+rsync -a ~/Pictures/webcam/ \
+    msundstr@msundstr.webfactional.com:webapps/view/webcam

webcam_monitor.py

 import collections
 import threading
 import logging
+import logging.handlers
 import logging_tree
 import random
+import subprocess
 
 TIMELAPSE = os.path.expanduser('~/Desktop/timelapse')
 assert os.path.exists(TIMELAPSE),"No timelapse folder!"
 assert os.path.exists(SUN_MOON_PATH),"No sun-moon.txt!"
 LOG = os.path.join(HERE, 'timelapse.log')
 
-logging.basicConfig(level=logging.DEBUG, filename=LOG,
+handler = logging.handlers.TimedRotatingFileHandler(LOG,
+    when='D', interval=1, backupCount=7)
+logging.basicConfig(level=logging.DEBUG,
+    handlers=[handler],
     format="%(asctime)s %(levelname)s %(name)s %(threadName)s %(message)s")
 # requests is a bit noisy, so quiet it down some
 logging.getLogger('requests').setLevel(logging.WARNING)
         time.sleep(1)
     logging.info("Shutdown")
 
+def webf(event):
+    time.sleep(random.randint(1, 4))
+    command_path = os.path.abspath(os.path.join(HERE, 'webcam.sh'))
+    assert os.path.exists(command_path)
+    logging.info('Startup')
+    next_time = beginning
+    while not event.is_set():
+        if event.wait(timeout=0.1):
+            break
+        now = datetime.datetime.utcnow()
+        if now >= next_time:
+            try:
+                subprocess.check_call(command_path, timeout=60)
+            except subprocess.TimeoutExpired:
+                logging.error("Timeout expired")
+            except subprocess.CalledProcessError as e:
+                logging.error("OOPS: {}".format(e))
+            next_time = now + datetime.timedelta(seconds=600)
+            logging.info("Waiting until {:%H:%M:%S}".format(next_time))
+        time.sleep(1)
+    logging.info("Shutdown")
+
 if __name__ == '__main__':
     logging.info('{} startup'.format(sys.argv[0]))
     load_sun_moon()
+
     city_event = threading.Event()
     t = threading.Thread(target=monitor,
         args=(city_event, 'city',
              'http://192.168.0.20/image.jpg', city_rule),
         name='City')
     t.start()
+
     river_event = threading.Event()
     t = threading.Thread(target=monitor,
         args=(river_event, 'river',
               'http://192.168.0.21/image.jpg', river_rule),
         name='River')
     t.start()
+
+    webf_event = threading.Event()
+    t = threading.Thread(target=webf,
+        args=(webf_event,),
+        name='Webf')
+    t.start()
+
     while True:
         try:
             time.sleep(1)
             break
     city_event.set()
     river_event.set()
+    webf_event.set()
     time.sleep(1)
     logging.info('Shutdown')