Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pi zeugs, db zeugs und aiocoap server #13

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions Backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,49 @@ https://levelup.gitconnected.com/full-stack-web-app-with-python-react-and-bootst

# packages
# coapthon (for py3)
pip install CoAPthon3
pip install CoAPthon3
# aiocoap
pip install aiocoap
# install flask package
pip install Flask
pip install Flask
# install flask_cors package
pip install Flask_cors
pip install Flask_cors
# Only if you don't have sqlite installed
pip install db-sqlite3
pip install db-sqlite3


# api
path
Payload if needed
description
response

/getall (GET)
-> Return alls rooms with their puzzles
{'Roomname':['Puzzlename']}

/Rooms/room (DELETE)
-> delete room and returns the deleted room
{'name': text, 'state': 'text'}

/Rooms/room (POST)
-> adds room
{'name': text, 'state': 'text'}

/Rooms/room (PUT)
Payload: {'state': text}
-> update the state of room and returns the updated room
{'name': text, 'state': 'text'}

'/Rooms/room (GET)
-> returns name and state of the room
{'name': text, 'state': 'text'}

/Rooms/movepuzzle/puzzle (PUT)
Payload:{'newRoom': text, 'oldRoom': text}
-> moves puzzle from oldRoom to newRoom and returns the room after update
{'name': text, 'room':text, 'state': text}

/Rooms/room/puzzle (GET)
-> Returns info about puzzle in room
{'name': text, 'room': text, 'state': text}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top of the Readme is outdated (not touched in this PR)

Binary file removed Backend/database.db
Binary file not shown.
45 changes: 45 additions & 0 deletions Backend/src/coap_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import asyncio
import aiocoap.resource as resource
from aiocoap import *


class WhoAmI(resource.Resource):
async def render_get(self, request):
text = ["Used protocol: %s." % request.remote.scheme]

text.append("Request came from %s." % request.remote.hostinfo)
text.append("The server address used %s." % request.remote.hostinfo_local)

claims = list(request.remote.authenticated_claims)
if claims:
text.append("Authenticated claims of the client: %s." % ", ".join(repr(c) for c in claims))
else:
text.append("No claims authenticated.")

return Message(content_format=0,
payload="\n".join(text).encode('utf8'))


async def main():
root = resource.Site()

root.add_resource(['.well-known', 'core'],
resource.WKCResource(root.get_resources_as_linkheader))
root.add_resource(['whoami'], WhoAmI())

con = await Context.create_server_context(root,bind=("0.0.0.0",5555))

request = Message(code=GET, uri="coap://127.0.0.1:5683/resource-lookup/", observe=0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Move host/port into a configuration or as constant at the top of the file

req = con.request(request)
res = await req.response
print(res.payload)
print("start async loop")
async for r in req.observation:
print(r.payload)
# Run forever
print("server running now")
await asyncio.get_running_loop().create_future()


if __name__ == '__main__':
asyncio.run(main())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misses newline at EOF

6 changes: 3 additions & 3 deletions Backend/src/coapthon_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from coapthon.client.helperclient import HelperClient
host = "127.0.0.1"
host = "2001:db8::814c:35fc:fd31:5fde"
port = 5683
Copy link
Collaborator

@Teufelchen1 Teufelchen1 May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Configuration constants are usually all uppercase, reference pep8 or use a linter.
E.g. from pep8:

Constants are usually defined on a module level and written in all capital letters with underscores separating
words. Examples include MAX_OVERFLOW and TOTAL.



def get_led(led):
path = "led{}".format(led)
path = "led/{}".format(led)

client = HelperClient(server=(host, port))
response = client.get(path)
Expand All @@ -14,7 +14,7 @@ def get_led(led):


def set_led(led, value):
path = "led{}".format(led)
path = "led/{}".format(led)

client = HelperClient(server=(host, port))
response = client.put(path, payload=value)
Expand Down
48 changes: 0 additions & 48 deletions Backend/src/coapthon_server.py

This file was deleted.

Binary file added Backend/src/requirements.txt
Binary file not shown.
71 changes: 51 additions & 20 deletions Backend/src/rest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
from coapthon_client import *
from sqlite import *
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})


# get all rooms and puzzles
@app.route('/getall', methods=['GET'])
def api_getall():
response = get_rooms_with_puzzles()
return jsonify(response)


# rooms

@app.route('/Rooms/<room>', methods=['DELETE'])
def api_delete_room(room):
response = delete_room(room)
return jsonify(response)


@app.route('/Roomm/<room>', methods=['POST'])
def api_add_room(room):
response = add_room(room)
return jsonify(response)

@app.route('/Rooms/<room>', methods=['PUT'])
def api_update_room(room):
request_data = request.get_json()
response = update_room(request_data['state'],room)
return jsonify(response)


@app.route('/Rooms/<room>', methods=['GET'])
def api_get_room(room):
response = get_room_by_name(room)
return jsonify(response)


@app.route('/Rooms/movepuzzle/<puzzle>', methods=['PUT'])
def api_moovepuzzle(puzzle):
request_data = request.get_json()
response = update_puzzle(request_data['newRoom'],[request_data['oldRoom']], puzzle)
return jsonify({'name': response.name, 'room':response.room, 'state': response.state})


# puzzles
@app.route('/Rooms/<room>/<puzzle>', methods=['GET'])
def api_get_puzzle(room, puzzle):
response = get_room_by_name(room, puzzle)
return jsonify({'name': response.name, 'room':response.room, 'state': response.state})


# coap
@app.route('/coap/led<id>', methods=['GET'])
def api_get_led_value(id):
Expand All @@ -32,24 +79,8 @@ def api_set_box_value(id):
return jsonify(response)


# db
@app.route('/db/led<id>', methods=['GET'])
def api_db_get_led_value(id):
return jsonify(get_led_by_name("led" + id))


@app.route('/db/led<id>', methods=['POST'])
def api_db_set_led_value(id):
request_data = request.get_json()
to_update = {"name": "led{}".format(id),
"value": request_data["value"]}
return(update_led(to_update))


@app.route('/db/add/led<id>', methods=['POST'])
def api_db_add_led(id):
return jsonify(insert_led("led{}".format(id)))


if __name__ == "__main__":
# from waitress import serve

# serve(app, host="0.0.0.0")
app.run(host="0.0.0.0")
Loading