def run(self, db, request): if request.method == HTTPMethods.POST: # POST if request.data is None: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: new, _ = CameraStreamRefresh.get_fn() fn = os.path.basename(new) console.debug('Newest filename = {}'.format( os.path.basename(new))) console.debug('Newest number = {}'.format(fn[:-4])) num = int(fn[:-4]) n_fn = os.path.join(settings.STREAM_DIR, str(num + 1) + '.jpg') console.debug('Saving to {}...'.format(n_fn)) if num % 10 == 0: os.remove(settings.STREAM_DIR + str(num - 10) + '.jpg') with open(n_fn, 'wb+') as f: f.write(request.data) console.debug('Saved file to {}'.format(n_fn)) return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.PLAIN, data='Processed'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.GET: # GET if request.params is not None and 'email' in request.params.keys( ) and 'session' in request.params.keys(): console.debug('Stream Refresh from {} : {}'.format( request.params['email'], request.params['session'])) if db.check_session(request.params['email'], request.params['session']): fn, ft = self.get_fn() data = open(fn, 'rb').read() console.debug('Sending {}'.format(fn)) return Response(code=HTTPResponseCodes.OK, content_type=ft, data=data) else: return Response(code=HTTPResponseCodes.UNAUTHORIZED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.POST: # POST if request.data is None or 'email' not in request.data.keys( ) or 'password' not in request.data.keys(): return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: console.debug("Login Attempt - {}: {}".format( request.data['email'], request.data['password'])) session = db.login(request.data['email'], request.data['password']) if session is not None: console.debug("Login Successful - {}: {}".format( request.data['email'], session)) return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.JSON, data=json.dumps({ 'status': 0, 'session': session }).encode()) else: console.debug("Login Unsuccessful - {}".format( request.data['email'])) return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.JSON, data=json.dumps({ 'status': 1, 'session': 0 }).encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def gen_constants(self, request): if request.method == HTTPMethods.GET: return Response( code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.JS, data='const hostname = "{}";\nconst port = {};\n'.format( self.host, self.port).encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def default(request): if request.method == HTTPMethods.GET: return load_static(join(settings.PROJECT_DIR, request.path[1:])) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.GET: # GET return load_static(settings.PROJECT_DIR + LOGIN_P) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def close(self): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) console.debug("Socket successfully created") except socket.error as err: console.error("socket creation failed with error {}".format(err)) s.connect((settings.GARAGE_DOOR_IP_ADDRESS, settings.GARAGE_DOOR_PORT)) console.debug("the socket has successfully connected to {}".format( settings.GARAGE_DOOR_IP_ADDRESS)) data = self.build(cmd=1) s.send(data) console.debug(data.decode('utf-8')) console.debug("Successfully sent message") resp = Response(raw=s.recv(4096)) if resp.status == HTTPResponseCodes.OK: s.close() return True else: s.close() return False
def run(self, db, request): if request.method == HTTPMethods.POST: # POST if request.data is not None and 'status' in request.data.keys( ) and 'time' in request.data.keys(): console.debug("Garage Status - {}: {}".format( request.data['time'], request.data['status'])) return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.PLAIN, data='Processed'.encode()) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.POST: # POST if request.data is None or 'epoch' not in request.data.keys() or 'time' not in request.data.keys() \ or 'message' not in request.data.keys(): return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: console.debug("Alert - {}: {}".format(request.data['time'], request.data['message'])) db.alert(request.data['epoch'], request.data['time'], request.data['message']) return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.PLAIN, data='Processed'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def load_static(path): response = Response() path = os.path.abspath(path) try: size = os.stat(path).st_size with open(path, 'rb') as f: response.data = f.read(size) response.status = HTTPResponseCodes.OK response.content_type = None for t in HTTPContentTypes: if path.lower().endswith(t.name.lower()): response.content_type = t if response.content_type is None: response.content_type = HTTPContentTypes.PLAIN except (OSError, FileNotFoundError): console.error('Unable to read the requested file at {}'.format(path)) response.status = HTTPResponseCodes.NOT_FOUND response.content_type = HTTPContentTypes.PLAIN response.data = 'Failure'.encode() return response
def run(self, db, request): if request.method == HTTPMethods.GET: # GET if request.params is not None and 'email' in request.params.keys() and 'session' in request.params.keys() and 'epoch' in request.params.keys(): console.debug('Refresh from {} : {}, {}'.format(request.params['email'], request.params['session'], request.params['epoch'])) alerts = db.get_alerts(int(request.params['epoch'])) data = dict() if len(alerts.keys()) == 0: data['status'] = 1 else: data['alerts'] = alerts data['status'] = 0 data = json.dumps(data).encode() # TODO add another field that has the epoch time to be used for comparisons in the JS code - whats the # latest notification received? return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.JSON, data=data) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.POST: # POST if 'email' in request.params.keys( ) and 'session' in request.params.keys(): console.debug('Database: {}'.format(db.users)) if db.check_session(request.params['email'], request.params['session']): return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.PLAIN, data='Logged out'.encode()) else: return Response(code=HTTPResponseCodes.UNAUTHORIZED, content_type=HTTPContentTypes.PLAIN, data='Authorization Failure'.encode()) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.GET: # GET if 'email' in request.params.keys( ) and 'session' in request.params.keys(): console.debug('Session Exists: {}'.format( db.users[request.params['email']])) if db.check_session(request.params['email'], request.params['session']): console.debug('Session is valid.') return load_static(settings.PROJECT_DIR + HOME_P) else: console.debug('Session is invalid.') return Response(code=HTTPResponseCodes.UNAUTHORIZED, content_type=HTTPContentTypes.PLAIN, data='Authorization Failure'.encode()) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())
def run(self, db, request): if request.method == HTTPMethods.POST: # POST if request.data is not None and 'status' in request.data.keys() and 'email' in request.data.keys() \ and 'session' in request.data.keys(): if db.check_session(request.data['email'], request.data['session']): console.debug("Garage Status Updating to {}".format( request.data['status'])) if request.data['status'] == 0: s = self.open() elif request.data['status'] == 1: s = self.close() else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) if s: return Response(code=HTTPResponseCodes.OK, content_type=HTTPContentTypes.PLAIN, data='Success'.encode()) else: return Response( code=HTTPResponseCodes.INTERNAL_SERVER_ERROR, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.UNAUTHORIZED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.BAD_REQUEST, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode()) else: return Response(code=HTTPResponseCodes.METHOD_NOT_ALLOWED, content_type=HTTPContentTypes.PLAIN, data='Failure'.encode())