예제 #1
0
파일: handlers.py 프로젝트: bovine/cowguard
    def get(self):
        keyname = self.request.get('event')
        if keyname is None:
            self.error(500)
            self.response.out.write("missing event")
            return

        event = CameraEvent.get(db.Key(keyname))
        if event is None or event.deleted == True:
            self.error(404)
            self.response.out.write("unknown event")
            return
            
        if not event.viewed:
            event.viewed = True
            event.put()

        q2 = CameraFrame.all()
        #q2.filter("event_id =", event.key()).order("image_time")
        q2.filter("camera_id =", event.camera_id).filter("image_time >=", event.event_start).filter("image_time <=", event.event_end).order("image_time")

        template_values = {
            'user': users.get_current_user(),
            'logout_url': users.create_logout_url('/'),
            'event': event,
            'framelist': q2.fetch(100),
            'timenow': datetime.utcnow(),
            }

        path = os.path.join(os.path.dirname(__file__), 'inc/imgseq.html')
        self.response.out.write(template.render(path, template_values))
예제 #2
0
파일: handlers.py 프로젝트: bovine/cowguard
    def post(self):
        keyname = self.request.get('event')
        if keyname is not None:
            event = CameraEvent.get(db.Key(keyname))
            event.deleted = True
            event.put()

        self.response.out.write("deleted")
예제 #3
0
파일: handlers.py 프로젝트: bovine/cowguard
    def pollCamera(self, cam):
        # update the last time we attempted to poll this camera.
        # do this first in case we hit an exception while attempting to poll.
        memcache.set("camera{%s}.lastpoll_time" % cam.key(), datetime.now())

        # capture an image from the remote camera.
        response = self.captureImage(cam)
        capture_time = datetime.now()
        if response is None:
            return False
        
        # store the full frame in memcache.
        memcache.set("camera{%s}.lastimg_orig" % cam.key(), response.content)
        memcache.set("camera{%s}.lastimg_time" % cam.key(), capture_time)

        # decide whether there is motion found.
        (motion_rating, motion_found) = self.detectMotion(cam, response.content)
        
        # add to an existing event if needed
        eventkey = memcache.get("camera{%s}.eventkey" % cam.key())
        if eventkey == "trigger":
            motion_found = True
            eventkey = None

        if eventkey is not None:
            #
            # An existing event was found, so just add this frame to it.
            #
            event = CameraEvent.get(db.Key(eventkey))

            # store frame in the database
            frame = CameraFrame(camera_id = cam.key(),
                                event_id = event.key(),
                                full_size_image = response.content,
                                image_time = capture_time,
                                alarmed = motion_found,
                                motion_rating = motion_rating)
            frame.put()

            # update the event in the database
            if motion_rating > event.max_motion_rating:
                event.max_motion_rating = motion_rating

            event.total_frames += 1
            event.total_motion_rating += motion_rating
            event.avg_motion_rating = event.total_motion_rating / event.total_frames

            event.event_end = capture_time
            if motion_found:
                event.alarm_frames += 1
                event.last_motion_time = capture_time

            event.put()

            # stop the event, if it's time
            td = (capture_time - event.last_motion_time)
            total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
            if (not motion_found) and (total_seconds > cam.num_secs_after):
                memcache.delete("camera{%s}.eventkey" % cam.key())
                return False

            return True

        elif motion_found:
            #
            # No existing event found, so start a new event.
            #
            event = CameraEvent(total_frames = 1,
                                alarm_frames = 1,
                                event_start = capture_time,
                                event_end = capture_time,
                                max_motion_rating = motion_rating,
                                total_motion_rating = motion_rating,
                                avg_motion_rating = motion_rating,
                                last_motion_time = capture_time,
                                camera_id = cam.key(),
                                viewed = False,
                                archived = False,
                                deleted = False)
            event.put()

            # store frame in the database
            frame = CameraFrame(camera_id = cam.key(),
                                event_id = event.key(),
                                full_size_image = response.content,
                                image_time = capture_time,
                                alarmed = motion_found,
                                motion_rating = motion_rating)
            frame.put()

            # keep the event open.
            memcache.set("camera{%s}.eventkey" % cam.key(), str(event.key()))
            return True
        else:
            # No motion found and not currently in an event.
            return False