Exemplo n.º 1
0
    def on_get(self, req, resp):
        id = req.get_param_as_int("id", False)

        if id:
            if AUTH:
                camera_status = requests.get(STATUS_URL % id, auth=HTTPBasicAuth(AUTH.get("name"), AUTH.get("pass")))
            else:
                camera_status = requests.get(STATUS_URL % id)

            soup = BeautifulSoup(camera_status.content, "html.parser")
            status = soup.find("body").text.split("status")[1].strip()
            resp.body = "ON" if status == "ACTIVE" else "OFF"

        else:
            if AUTH:
                cameras = requests.get(MOTION_URL, auth=HTTPBasicAuth(AUTH.get("name"), AUTH.get("pass")))
            else:
                cameras = requests.get(MOTION_URL)

            soup = BeautifulSoup(cameras.content, "html.parser")
            cams = []
            for link in soup.find_all("a"):
                cams.append(
                    {
                        "id": int(link.get("href").strip("/")),
                        "name": link.text,
                        "link": req.uri + "?id=%i" % int(link.get("href").strip("/")),
                    }
                )
            resp.body = json.dumps(cams)
Exemplo n.º 2
0
class CamResource:

    camera = Cam(MOTION_URL, AUTH.get('name', None), AUTH.get('pass', None))

    def on_get(self, req, resp):
        cam_id = req.get_param_as_int('id', False)

        if cam_id:
            self.camera.get_state(cam_id)
            resp.body = self.camera.get_state(cam_id)
        else:
            cameras = self.camera.list_cameras_no_parse()
            soup = BeautifulSoup(cameras.content, 'html.parser')
            cams = []
            for link in soup.find_all('a'):
                cams.append({
                    'id': int(link.get('href').strip('/')),
                    'name': link.text,
                    'link': req.uri + '?id=%i' % int(link.get('href').strip('/'))
                })
            resp.body = json.dumps(cams)

    def on_post(self, req, resp):
        cam_id = req.get_param_as_int('id', False)
        if cam_id:
            status = self.camera.get_state()

            if status:
                self.camera.pause(cam_id)
                resp.body = 'OFF'
            else:
                self.camera.start(cam_id)
                resp.body = 'ON'
Exemplo n.º 3
0
def notify_temp():
    data = Temp.query.order_by(Temp.id.desc()).first()
    if AUTH:
        requests.post(
                'http://%s/temperature' % BACKEND_SERVER,
                data=data.__dict__,
                auth=HTTPBasicAuth(AUTH.get('name'), AUTH.get('pass'))
        )
    else:
        requests.post(
                'http://%s/temperature' % BACKEND_SERVER,
                data=data.__dict__
        )
Exemplo n.º 4
0
def notify_volt():
    data = Volt.query.order_by(Volt.id.desc()).first()
    if AUTH:
        requests.post(
                'http://%s/voltage' % BACKEND_SERVER,
                data=data.__dict__,
                auth=HTTPBasicAuth(AUTH.get('name'), AUTH.get('pass'))
        )
    else:
        requests.post(
                'http://%s/voltage' % BACKEND_SERVER,
                data=data.__dict__
        )
Exemplo n.º 5
0
def notify_hum():
    data = Humidity.query.order_by(Humidity.id.desc()).first()
    if AUTH:
        requests.post(
                'http://%s/humidity' % BACKEND_SERVER,
                data=data.__dict__,
                auth=HTTPBasicAuth(AUTH.get('name'), AUTH.get('pass'))
        )
    else:
        requests.post(
                'http://%s/humidity' % BACKEND_SERVER,
                data=data.__dict__
        )
Exemplo n.º 6
0
    def on_post(self, req, resp):
        id = req.get_param_as_int("id", False)
        if id:
            if AUTH:
                camera_status = requests.get(STATUS_URL % id, auth=HTTPBasicAuth(AUTH.get("name"), AUTH.get("pass")))
            else:
                camera_status = requests.get(STATUS_URL % id)

            soup = BeautifulSoup(camera_status.content, "html.parser")
            sta = soup.find("body").text.split("status")[1].strip()
            status = True if sta == "ACTIVE" else False

            if status:
                if AUTH:
                    requests.get(PAUSE_URL % id, auth=HTTPBasicAuth(AUTH.get("name"), AUTH.get("pass")))
                else:
                    requests.get(PAUSE_URL % id)
                resp.body = "OFF"
            else:
                if AUTH:
                    requests.get(START_URL % id, auth=HTTPBasicAuth(AUTH.get("name"), AUTH.get("pass")))
                else:
                    requests.get(START_URL % id)
                resp.body = "ON"
Exemplo n.º 7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import falcon
from camera.api.resources import CamResource
from paho.mqtt.client import Client
import argparse
import logging
import coloredlogs
import sys

from camera.camera import Cam
from settings import MOTION_URL, AUTH, MQTT_ADRESS, MQTT_PORT

__author__ = 'magnusknutas'
logger = logging.getLogger(__name__)
camera = Cam(MOTION_URL, AUTH.get('name', None), AUTH.get('pass', None))

api = falcon.API()
api.add_route('/cams', CamResource())
help_text = "Example"


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    logger.info("Connected with result code " + str(rc))
    logger.info("Connected with result code " + str(userdata))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    cameras = camera.list_cameras()
    for cam in cameras: