Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
smarthotels/web/index.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
166 lines (135 sloc)
5.65 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from flask import Flask | |
from flask_restful import Resource, Api | |
from flask import request, Response | |
from flask.json import jsonify | |
from flask import render_template | |
import requests | |
import json | |
import postgresLibrary as pL | |
import uuid | |
import peripherals as peripherals | |
from datetime import date, datetime | |
def json_serial(obj): | |
"""JSON serializer for objects not serializable by default json code""" | |
if isinstance(obj, (datetime, date)): | |
return obj.isoformat() | |
raise TypeError ("Type %s not serializable" % type(obj)) | |
PORT_NUMBER = 8000 | |
app = Flask(__name__) | |
api = Api(app) | |
# class Index(Resource): | |
# def get(self): | |
# return app.send_static_file('/src/static/ManagementApp.html') | |
# api.add_resource(Index, '/') | |
#################################### | |
# Management App API Requirements | |
#################################### | |
#################################### | |
# Room Info | |
#################################### | |
@app.route('/api/peripheral/<uuid>', methods=['GET']) | |
def getPeripheral(uuid): | |
per = pL.getPeripheralbyUUID(uuid) | |
return jsonify(per) | |
@app.route('/api/peripheral/<uuid>', methods=['POST']) | |
def setPeripheralID(uuid): | |
body = request.get_json() | |
if body == None: | |
return None | |
per = pL.getPeripheralbyUUID(uuid) | |
roomRoutes = pL.roomRoutingEntries(per['room_uuid']) | |
peripheralRoutes = pL.peripheralRoutingEntries(uuid) | |
# At the room level, we simply proxy requests. | |
# If there's a specific peripheral IP in a peripheral route, it will be ignored if there is a room route. | |
if len(roomRoutes) == 0: | |
# We're doing a peripheral specific handling. | |
return peripherals.handlePeripheral(per, body, peripheralRoutes) | |
else: | |
dest = roomRoutes[0] | |
resp = requests.request( | |
method=request.method, | |
url='http://' + dest['target'] + request.script_root + request.full_path, | |
headers={key: value for (key, value) in request.headers if key != 'Host'}, | |
data=request.get_data(), | |
cookies=request.cookies, | |
timeout=0.5, | |
allow_redirects=False | |
) | |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] | |
headers = [(name, value) for (name, value) in resp.raw.headers.items() | |
if name.lower() not in excluded_headers] | |
response = Response(resp.content, resp.status_code, headers) | |
return response | |
@app.route('/api/guest/rooms', methods=['GET','POST']) | |
def getGuestRooms(): | |
rooms = pL.getRooms() | |
for room in rooms: | |
print(room) | |
room['peripherals'] = pL.getPeripheralsbyRoom(room['uuid']) | |
return jsonify({ | |
'rooms': rooms, | |
'hotel_name': 'Hotel' | |
}) | |
@app.route('/api/routes/<uuid>/entries', methods=['GET','POST']) | |
def getRoutes(uuid): | |
return jsonify(pL.routingEntries(uuid, uuid)) | |
# Get all rooms | |
@app.route('/api/admin/rooms', methods=['GET', 'POST']) | |
def getRooms(): | |
rooms = pL.getRooms() | |
for room in rooms: | |
print(room) | |
return jsonify({'rooms': rooms}) | |
# Get guest by room | |
@app.route('/api/admin/rooms/<room_uuid>', methods=['GET', 'POST']) | |
def getGuest(room_uuid): | |
guest = pL.getGuestbyRoomUUID(room_uuid) | |
print(guest) | |
return jsonify({'guest': guest}) | |
#################################### | |
# Room Service Requests | |
#################################### | |
# Get all room service requests | |
@app.route('/api/admin/requests/room-service', methods=['GET', 'POST']) | |
def getRoomServiceRequests(): | |
requestsRoomService = pL.getRoomServiceRequests() | |
for request in requestsRoomService: | |
print(request) | |
return jsonify({'Room Servive Requests': requestsRoomService}) | |
# Get a specific room service request given a room service request uuid | |
@app.route('/api/admin/requests/room-service/<rsreqUUID>', methods=['GET', 'POST']) | |
def getRoomServiceRequest(rsreqUUID): | |
requestRoomService = pL.getRoomServiceRequestbyUUID(rsreqUUID) | |
print(requestRoomService) | |
return jsonify({'Room Servive Request': requestRoomService}) | |
# Set room service request as completed given a room service request uuid | |
@app.route('/api/admin/requests/room-service/<rsreqUUID>', methods=['GET', 'POST']) | |
def completeRoomServiceRequest(rsreqUUID): | |
completeRoomSerive = pL.updateRoomServiceRequestCompleted('TRUE', rsreqUUID) | |
print(completeRoomSerive) | |
return jsonify({'Room Servive Request': pL.getRoomServiceRequestbyUUID(rsreqUUID)}) | |
#################################### | |
# Maintenace Requests | |
#################################### | |
# Get all maintenance requests | |
@app.route('/api/admin/requests/maintenance', methods=['GET', 'POST']) | |
def getMaintenanceRequests(): | |
requestsMaintenance = pL.getMaintenanceRequests() | |
for request in requestsMaintenance: | |
print(request) | |
return jsonify({'Maintenance Request': requestsMaintenance}) | |
# Get a specific maintenance request given a maintenance request uuid | |
@app.route('/api/admin/requests/maintenance/<mreqUUID>', methods=['GET', 'POST']) | |
def getMaintenanceRequest(mreqUUID): | |
requestMaintenance = pL.getMaintenanceRequestbyUUID(mreqUUID) | |
print(requestMaintenance) | |
return jsonify({'Room Servive Request': requestMaintenance}) | |
# Set maintenance request as completed given a maintenance request uuid | |
@app.route('/api/admin/requests/maintenance/complete/<mreqUUID>', methods=['GET', 'POST']) | |
def completeMaintenanceRequest(mreqUUID): | |
completeMaintenance = pL.updateMaintenanceRequestCompleted('TRUE', mreqUUID) | |
print(completeMaintenance) | |
return jsonify({'Maintenance Request': pL.getMaintenanceRequestbyUUID(mreqUUID)}) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=PORT_NUMBER) |