Beispiel #1
0
 def connectionTimeout(self):
     '''Called after X seconds after the http stream is closed'''
     if len(MultiPartStream.getClients(self.target)):
         return
     log.msg("No more clients, closing link")
     CameraFactory.disconnect(self.target)
     del self
Beispiel #2
0
 def connectionTimeout(self):
     '''Called after X seconds after the http stream is closed'''
     if len(MultiPartStream.getClients(self.target)):
         return
     log.msg("No more clients, closing link")
     CameraFactory.disconnect(self.target)
     del self
Beispiel #3
0
 def getCamera(address):
     out = {}
     try:
         out.update(CameraFactory.getCamera(address))
     except UnknownDevice, err:
         out["address"] = address
         out["name"] = resolve_name(address)
         out["new_device"] = True
         out["types"] = CameraFactory.getTypes()
Beispiel #4
0
 def getCamera(address):
     out = {}
     try:
         out.update(CameraFactory.getCamera(address))
     except UnknownDevice, err:
         out["address"] = address
         out["name"] = resolve_name(address)
         out["new_device"] = True
         out["types"] = CameraFactory.getTypes()
Beispiel #5
0
 def do_work(self):
     error = None
     try:
         log.msg("Doing scan")
         cache = bluetooth.discover_devices(lookup_names=True)
         log.msg(cache)
         self.scancache = [{
             'address': x[0],
             'name': x[1],
             'state': CameraFactory.isConnected(x[0])
         } for x in cache]
         log.msg(self.scancache)
         remove = []
         print self.waiting
         for request in self.waiting:
             request.setHeader('Content-Type', 'application/json')
             request.setHeader('Cache-Control', 'no-cache')
             request.setHeader('Connection', 'close')
             request.write(json.dumps(self.scancache))
             request.finish()
         if len(self.waiting) == 0:
             return server.NOT_DONE_YET
     except Exception, err:
         log.err(err)
         error = err
Beispiel #6
0
 def do_work(self):
     error = None
     try:
         log.msg("Doing scan")
         cache = bluetooth.discover_devices(lookup_names=True)
         log.msg(cache)
         self.scancache = [{
                 'address':x[0], 
                 'name':x[1], 
                 'state': CameraFactory.isConnected(x[0])}
                 for x in cache]
         log.msg(self.scancache)
         remove = []
         print self.waiting
         for request in self.waiting:
             request.setHeader('Content-Type', 'application/json')
             request.setHeader('Cache-Control', 'no-cache')
             request.setHeader('Connection', 'close')
             request.write(json.dumps(self.scancache))
             request.finish()
         if len(self.waiting)==0:
             return server.NOT_DONE_YET
     except Exception, err:
         log.err(err)
         error = err
Beispiel #7
0
 def index(self, request):
     devices = list(CameraFactory.getCameras())
     for dev in devices:
         dev["viewers"] = StreamResource.getClients(dev["address"])
     return {
       "devices": devices 
     }
Beispiel #8
0
 def stream(self, request):
   if "address" not in request.args:
     raise Exception("You need to provide with an address")
   return {
     "isChrome": "chrome" in request.requestHeaders.getRawHeaders("user-agent")[0].lower(),
     "address": request.args["address"][0],
     "camera": CameraFactory.getCamera(request.args["address"][0], True)
   }
Beispiel #9
0
    def render_GET(self, request):
        try:
            request.setHeader('Content-Type', 'application/json')
            request.setHeader('Cache-Control', 'no-cache')
            request.setHeader('Connection', 'close')
            if len(request.postpath) > 0 and len(request.postpath[0]) > 0:
                address = request.postpath[0].replace("_", ":")
                if len(request.args) == 0:
                    return json.dumps(CameraFactory.getCamera(address))
                camera = settings.getCamera(address)
                for key in request.args:
                    settings.setCameraSetting(address, key,
                                              request.args[key][-1])
                settings.save()
                cli = CameraFactory.getConnected(address)
                if cli:
                    cli.updateSettings()
                return json.dumps(CameraFactory.getCamera(address))
            request.setResponseCode(500, "invalid address")
            return "Invalid address"

        except Exception, err:
            print err
            CameraFactory.connect(address, 1, "RFCOMM")
            CameraFactory.registerListener(address, ConnectionTest(request))
            return server.NOT_DONE_YET
Beispiel #10
0
    def render_GET(self, request):
        try:
            request.setHeader('Content-Type', 'application/json')
            request.setHeader('Cache-Control', 'no-cache')
            request.setHeader('Connection', 'close')
            if len(request.postpath) > 0 and len(request.postpath[0])>0:
                address=request.postpath[0].replace("_", ":")
                if len(request.args) == 0:
                    return json.dumps(CameraFactory.getCamera(address))
                camera = settings.getCamera(address)
                for key in request.args:
                        settings.setCameraSetting(address, key, request.args[key][-1])
                settings.save()
                cli = CameraFactory.getConnected(address)
                if cli:
                    cli.updateSettings()
                return json.dumps(CameraFactory.getCamera(address))
            request.setResponseCode(500, "invalid address")
            return "Invalid address"

        except Exception, err:
            print err
            CameraFactory.connect(address, 1, "RFCOMM")
            CameraFactory.registerListener(address, ConnectionTest(request))
            return server.NOT_DONE_YET
Beispiel #11
0
 def render_GET(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         if len(request.postpath) > 0 and len(request.postpath[0])>0:
             address=request.postpath[0].replace("_", ":")
             if not CameraFactory.isConnected(address):
                 return json.dumps({"result": "Not Connected"})
             CameraFactory.disconnect(address)
             return json.dumps({"result": "Disconnected"})
         return json.dumps({"result": "No Address"})
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (traceback.format_exc())
Beispiel #12
0
 def render_GET(self, request):
   address = request.path.split("/",2)[-1].replace("_", ":")
   request.transport.socket.settimeout(5)
   multipart = MultiPartStream(request)
   multipart.process()
   multipart.target = address
   if len(address) == 17:
       ready = CameraFactory.isConnected(address) or CameraFactory.isPending(address)
       if multipart.thumbnail and not ready:
           multipart.sendPart("")
           return
       if not ready:
           method = request.args.get("method", ["RFCOMM",])[-1]
           try:
               CameraFactory.connect(address, 1, method)
           except Exception, err:
               log.msg("Failed while trying to connect")
               log.err(err)
Beispiel #13
0
 def render_POST(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         address = request.args["address"][0]
         if CameraFactory.isConnected(address):
             CameraFactory.disconnect(address)
             return json.dumps({"result": "disconnected"})
         else:
             CameraFactory.connect(address)
             return json.dumps({"result": "connecting"})
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (traceback.format_exc())
Beispiel #14
0
 def render_GET(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         if len(request.postpath) > 0 and len(request.postpath[0]) > 0:
             address = request.postpath[0].replace("_", ":")
             if not CameraFactory.isConnected(address):
                 return json.dumps({"result": "Not Connected"})
             CameraFactory.disconnect(address)
             return json.dumps({"result": "Disconnected"})
         return json.dumps({"result": "No Address"})
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (
             traceback.format_exc())
Beispiel #15
0
 def render_POST(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         address = request.args["address"][0]
         if CameraFactory.isConnected(address):
             CameraFactory.disconnect(address)
             return json.dumps({"result": "disconnected"})
         else:
             CameraFactory.connect(address)
             return json.dumps({"result": "connecting"})
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (
             traceback.format_exc())
Beispiel #16
0
 def render_POST(self, request):
   address = request.args["address"][0]
   option = request.args["option"][0]
   value = request.args["value"][0]
   print address, option, value
   try:
     cli = CameraFactory.getConnected(address)
     if cli:
       cli.client.set(option, value)
     return "updated"
   except Exception, err:
     return "failed: %s" % err
Beispiel #17
0
 def render_POST(self, request):
     address = request.args["address"][0]
     option = request.args["option"][0]
     value = request.args["value"][0]
     if value.lower() in ["true", "false"]:
         value = value.lower() == "true"
     log.msg("doConfiguration.POST(%s, %s, %s)" % (address, option, value))
     try:
         cli = CameraFactory.getConnected(address)
         if cli:
             cli.client.set(option, value)
         return "updated"
     except Exception, err:
         return "failed: %s" % err
Beispiel #18
0
 def render_GET(self, request):
     address = request.path.split("/", 2)[-1].replace("_", ":")
     request.transport.socket.settimeout(5)
     multipart = MultiPartStream(request)
     multipart.process()
     multipart.target = address
     if len(address) == 17:
         ready = CameraFactory.isConnected(
             address) or CameraFactory.isPending(address)
         if multipart.thumbnail and not ready:
             multipart.sendPart("")
             return server.NOT_DONE_YET
         if not ready:
             method = CameraFactory.getCamera(address).get(
                 "transport", "rfcomm")
             method = request.args.get("method", [
                 method,
             ])[-1].upper()
             try:
                 CameraFactory.connect(address, method=method)
             except Exception, err:
                 log.msg("Failed while trying to connect")
                 log.err(err)
Beispiel #19
0
 def render_POST(self, request):
     address = request.args["address"][0]
     option = request.args["option"][0]
     value = request.args["value"][0]
     if value.lower() in ["true", "false"]:
         value = value.lower()=="true"
     log.msg("doConfiguration.POST(%s, %s, %s)" % (address, option, value))
     try:
         cli = CameraFactory.getConnected(address)
         if cli:
             cli.client.set(option, value)
         return "updated"
     except Exception, err:
         return "failed: %s" % err
Beispiel #20
0
 def render_POST(self, request):
     out = {}
     print str(request.args)
     for key, value in request.args.iteritems():
         if len(value) > 1:
             out[key] = True
         else:
             out[key] = value[0]
     settings.setCamera(out)
     print out
     settings.save()
     cli = CameraFactory.getConnected(out['address'])
     if cli:
         cli.updateSettings()
     return "saved"
Beispiel #21
0
 def render_POST(self, request):
     out = {}
     print str(request.args)
     for key, value in request.args.iteritems():
         if len(value) > 1:
             out[key] = True
         else:
             out[key]=value[0]
     settings.setCamera(out)
     print out
     settings.save()
     cli = CameraFactory.getConnected(out['address'])
     if cli:
         cli.updateSettings()
     return "saved"
Beispiel #22
0
 def stream(self, request):
     if "address" not in request.args:
         raise Exception("You need to provide with an address")
     camera = CameraFactory.getCamera(request.args["address"][0], True)
     if camera["enable_pincode"]:
         camera["pincode"] = "************"
     return {
         "isChrome":
         "chrome"
         in request.requestHeaders.getRawHeaders("user-agent")[0].lower(),
         "address":
         request.args["address"][0],
         "camera":
         camera
     }
Beispiel #23
0
 def scan(self, request):
     try:
         log.msg("Doing scan")
         cache = bluetooth.discover_devices(lookup_names=True)
         log.msg(cache)
         out = {
             "devices": [{
                 'address': x[0],
                 'name': x[1],
                 'state': CameraFactory.isConnected(x[0])
             } for x in cache]
         }
         log.msg(out)
         return out
     except Exception, err:
         log.err(err)
         return {"error": str(err)}
Beispiel #24
0
 def scan(self, request):
     try:
         log.msg("Doing scan")
         cache = bluetooth.discover_devices(lookup_names=True)
         log.msg(cache)
         out = {"devices":
                 [{
                   'address':x[0],
                   'name':x[1],
                   'state': CameraFactory.isConnected(x[0])
                 }
                     for x in cache
                 ]
             }
         log.msg(out)
         return out
     except Exception, err:
         log.err(err)
         return {"error": str(err)}
Beispiel #25
0
 def render_GET(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         if len(request.postpath) > 0 and len(request.postpath[0])>0:
             address=request.postpath[0].replace("_", ":")
             cli = CameraFactory.getCamera(address, silent=True)
             if cli or not request.args.get("test", None):
                 return json.dumps(CameraFactory.getCamera(address))
             CameraFactory.connect(address, 1, "RFCOMM")
             CameraFactory.registerListener(address, ConnectionTest(request))
             return server.NOT_DONE_YET
         else:
             return json.dumps(self.render_all(request))
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (traceback.format_exc())
Beispiel #26
0
 def render_GET(self, request):
     try:
         request.setHeader('Content-Type', 'application/json')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         if len(request.postpath) > 0 and len(request.postpath[0]) > 0:
             address = request.postpath[0].replace("_", ":")
             cli = CameraFactory.getCamera(address, silent=True)
             if cli or not request.args.get("test", None):
                 return json.dumps(CameraFactory.getCamera(address))
             CameraFactory.connect(address, 1, "RFCOMM")
             CameraFactory.registerListener(address,
                                            ConnectionTest(request))
             return server.NOT_DONE_YET
         else:
             return json.dumps(self.render_all(request))
     except Exception, err:
         request.setHeader('Content-Type', 'text/html')
         request.setHeader('Cache-Control', 'no-cache')
         request.setHeader('Connection', 'close')
         request.setResponseCode(500, str(err))
         return "<html><h1>ERROR:</h1>\n<pre>%s</pre></html>" % (
             traceback.format_exc())
Beispiel #27
0
 def gotFrame(self, frame, address):
     print "ConnectionTest.gotFrame", address
     cli = CameraFactory.getCamera(address)
     self.request.write(json.dumps(cli))
     self.cleanup(address)
Beispiel #28
0
 def cleanup(self, address):
     CameraFactory.disconnect(address)
     CameraFactory.removeListener(address, self)
     self.request.finish()
Beispiel #29
0
 def render_GET(self, request):
     request.setHeader('Content-Type', 'application/json')
     request.setHeader('Cache-Control', 'no-cache')
     request.setHeader('Connection', 'close')
     return json.dumps(list(CameraFactory.getCameras()))
Beispiel #30
0
 def render_GET(self, request):
     request.setHeader('Content-Type', 'application/json')
     request.setHeader('Cache-Control', 'no-cache')
     request.setHeader('Connection', 'close')
     return json.dumps(list(CameraFactory.getCameras()))
Beispiel #31
0
        multipart.process()
        multipart.target = address
        if len(address) == 17:
            ready = CameraFactory.isConnected(address) or CameraFactory.isPending(address)
            if multipart.thumbnail and not ready:
                multipart.sendPart("")
                return server.NOT_DONE_YET
            if not ready:
                method = CameraFactory.getCamera(address).get("transport", "rfcomm")
                method = request.args.get("method", [method,])[-1].upper()
                try:
                    CameraFactory.connect(address, method=method)
                except Exception, err:
                    log.msg("Failed while trying to connect")
                    log.err(err)
        CameraFactory.registerListener(address, self)
        StreamResource.tellClientCount(address)
        return server.NOT_DONE_YET

class TestStreamResource(Resource):
    isLeaf = True

    def render_GET(self, request):
        log.msg(str(request))
        log.msg(str(request.requestHeaders))
        multipart = MultiPartStream(request)
        multipart.process()
        return server.NOT_DONE_YET

    def render_POST(self, request):
        return self.render_GET(request)
Beispiel #32
0
 def gotFrame(self, frame, address):
     print "ConnectionTest.gotFrame", address
     cli = CameraFactory.getCamera(address)
     self.request.write(json.dumps(cli))
     self.cleanup(address)
Beispiel #33
0
                address) or CameraFactory.isPending(address)
            if multipart.thumbnail and not ready:
                multipart.sendPart("")
                return server.NOT_DONE_YET
            if not ready:
                method = CameraFactory.getCamera(address).get(
                    "transport", "rfcomm")
                method = request.args.get("method", [
                    method,
                ])[-1].upper()
                try:
                    CameraFactory.connect(address, method=method)
                except Exception, err:
                    log.msg("Failed while trying to connect")
                    log.err(err)
        CameraFactory.registerListener(address, self)
        StreamResource.tellClientCount(address)
        return server.NOT_DONE_YET


class TestStreamResource(Resource):
    isLeaf = True

    def render_GET(self, request):
        log.msg(str(request))
        log.msg(str(request.requestHeaders))
        multipart = MultiPartStream(request)
        multipart.process()
        return server.NOT_DONE_YET

    def render_POST(self, request):
Beispiel #34
0
 def cleanup(self, address):
     CameraFactory.disconnect(address)
     CameraFactory.removeListener(address, self)
     self.request.finish()
Beispiel #35
0
 def render_all(self, request):
     out = {}
     for addr, cli in CameraFactory.getConnected().iteritems():
         out[addr] = CameraFactory.getCamera(addr)
     return out
Beispiel #36
0
 def index(self, request):
     devices = list(CameraFactory.getCameras())
     for dev in devices:
         dev["viewers"] = StreamResource.getClients(dev["address"])
     return {"devices": devices}
Beispiel #37
0
 def render_all(self, request):
     out = {}
     for addr, cli in CameraFactory.getConnected().iteritems():
         out[addr] = CameraFactory.getCamera(addr)
     return out