diff options
Diffstat (limited to 'sublab_project/sublab_monitor')
-rw-r--r-- | sublab_project/sublab_monitor/tasks.py | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/sublab_project/sublab_monitor/tasks.py b/sublab_project/sublab_monitor/tasks.py index 33dcad6..20f8ad8 100644 --- a/sublab_project/sublab_monitor/tasks.py +++ b/sublab_project/sublab_monitor/tasks.py @@ -3,6 +3,11 @@ from datetime import timedelta from celery.task import PeriodicTask import subprocess +import urllib2 +from PIL import Image, ImageOps +from StringIO import StringIO +from django.conf import settings +import os import sublab_monitor @@ -14,7 +19,7 @@ class NetworkStatus(PeriodicTask): 'trieste': '172.22.80.4', 'nautilus': '172.22.80.7', } - run_every = timedelta(minutes=4) + run_every = timedelta(minutes=2) ignore_result = True def __init__(self, *args, **kwargs): @@ -44,3 +49,65 @@ class NetworkStatus(PeriodicTask): storage.set(host, status) return repr(results) + + +class ImageFetcher(object): + """ + A task mixin which downloads, processes and stores an Image + """ + + @property + def fetch_url(self): + """ + The location from which the image should be fetched + """ + raise NotImplementedError + + @property + def store_name(self): + """ + The name under which the image should be stored + """ + raise NotImplementedError + + def process_image(self, image): + """ + This method may be overwritten to perform some + processing on the image + """ + raise NotImplementedError + + def run(self, **kwargs): + upstream = urllib2.urlopen(self.fetch_url).read() + image = Image.open(StringIO(upstream)) + + try: + image = self.process_image(image) + except NotImplementedError: + pass + + fn = os.path.join(settings.MEDIA_ROOT, self.store_name) + fn_new = os.path.join(settings.MEDIA_ROOT, 'new-%s' % self.store_name) + + image.save(fn_new) + os.rename(fn_new, fn) + + +class EnhancingImageFetcher(ImageFetcher): + def process_image(self, image): + rv = ImageOps.autocontrast(image) + rv.im = ImageOps.unsharp_mask(rv, 10.0, 40, 7) + + return rv + + +class KarlHeineCamFetcher(EnhancingImageFetcher, PeriodicTask): + run_every = timedelta(minutes=5) + fetch_url = 'http://taifun.local.sublab.org/webcam.jpg' + store_name = 'karlheine_cam.jpg' + + +class TempGraphFetcher(ImageFetcher, PeriodicTask): + run_every = timedelta(minutes=5) + fetch_url = 'http://taifun.local.sublab.org/temperature/temp-2hour.png' + store_name = 'tempgraph.png' |