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

Flask API prototype ( Hackathon ) #9

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
21 changes: 0 additions & 21 deletions .editorconfig

This file was deleted.

15 changes: 0 additions & 15 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
venvhermes
.idea/*
__pycache__
*.egg-info
Expand Down
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

15 changes: 5 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,13 @@ And to test it:

.. code-block:: bash

$ curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT
{"todo1": "Remember the milk"}

curl http://endereco_ip:5000/hermes/BASE -d "data=DO COM OFF" -X POST
### Ordena o equipamento "BASE" a "DO" (fazer) a porta "COM" entrar em estado "OFF"

$ curl http://localhost:5000/todo1
{"todo1": "Remember the milk"}
curl http://endereco_ip:5000/hermes/RIVA -d "data=DO LED2 ON" -X POST
### Ordena o equipamento "RIVA" a "DO" (fazer) a porta "LED2" entrar em estado "ON"

$ curl http://localhost:5000/todo2 -d "data=Change my brakepads" -X PUT
{"todo2": "Change my brakepads"}

$ curl http://localhost:5000/todo2
{"todo2": "Change my brakepads"}


(resource: `[3]`)

Expand Down
Binary file added hermes/database.sqlite
Binary file not shown.
209 changes: 160 additions & 49 deletions hermes/hermes.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,180 @@
from flask import Flask, request, abort
from flask import Flask, request, abort, g
from flask_restful import Resource, Api


import click

import serial.tools.list_ports

import xbee
import serial
from datetime import datetime
#import sqlite3
#from threading import Lock, Thread

# import json


class act():

"""
lock = Lock()
lock.acquire()
lock.release() # This block can be used later to implement further instances of Threading

"""

adr={
'SAMUEL':b'\x00\x13\xA2\x00\x40\x8C\x70\x27',
'RIVA':b'\x00\x13\xA2\x00\x40\x8C\x70\x1E'
} # This are the MAC adresses, that will be fetched by the act.find() method in future releases

commands = {
'COM':b'P0',
'LED2':b'P2', #LED 2
'LEDA':b'D5', #LED ASSOC AZUL
'LED4':b'D7', #LED LED 4
'LED7':b'P1' #LED 7
} #Possibele commands to be used in the prototype. This can provide safty so that no unwanted code can be used.

parameters={
'ON':b'\x05',
'OFF':b'\x04'
} # Same as commands, but for the paramenters


try:
bee = xbee.ZigBee(serial.Serial('/dev/ttyUSB0', 9600))
except:
print("Failed to connect to the master node.\n\nExiting...")
exit
'''
def bdrecord(datanow, cmd, val, code):
database = 'database.sqlite'
con=sqlite3.connect(database)
cur = con.cursor()
cur.execute("INSERT INTO Actions VALUES (?,?,?,?)", (datanow, cmd, val, code))
con.close()
''' # This method can be used to store the actions executed in a SQLite Database

def add(code, info):
'''
TODO
'''
info=info.split(' ')
name = code
addr = info[1]
return {name:addr}

def find(code=None):
'''
TODO
'''
if code in act.network:
act.bee.send('at', b'AT')
#while True:

def return_message(data):
'''
TODO
'''
print(data)

def get(code=None):
'''
TODO
'''
return "Result"

'''
def parse(**kwargs):
act.lock.acquire()
try:
print(ser = serial.Serial('/dev/ttyUSB0', 9600))
bee = xbee.ZigBee(ser)
act.bee.remote_tr(**kwargs)
act.bee.wait_read_frame()
ser.close()
except:
print('connection falied')
finally:
act.lock.release()
'''# This method can be used to encapsulate the act of sending all the parameters

def do(code, cmd):
'''
This method sends commands to the devices
'''
args = cmd.split(' ')
args.pop(0)
print(code)
cmd = args.pop(0)
val = args.pop(0)
print(code)
if code == 'BASE':
act.bee.remote_at( command=act.commands.get(cmd), parameter=act.parameters.get(val))
#act.bdrecord(datetime.now(), cmd, val, code)
#act.cur.execute("INSERT INTO Actions VALUES (?,?,?,?)", (datetime.now(), cmd, val, code ))
return str(datetime.now()) + " "+ cmd + " " + val + " " + code
#if args.__len__() is 1 :
# act.parse( **{'command':act.commands.get(cmd), 'parameter':act.parameters.get(val)})
elif args.__len__() is 0 :
act.bee.remote_at( command=act.commands.get(cmd), parameter=act.parameters.get(val), dest_addr_long=act.adr.get(code) )
#act.bdrecord(datetime.now(), cmd, val, code)
return str(datetime.now()) + " "+ cmd + " " + val + " " + code
else:
return "Command error. Possible combinations: Name, COMMAND, PORT, VALUE, and/or ADDRESS"



######################
app = Flask(__name__)
api = Api(app)

todos = {}

sensors = {}

@click.command()
@click.option('-p', default=5000, help='Port Number')
def startup(p):
"""

:param p:
:return:
"""
app.run(host='0.0.0.0', port=p, debug=True)


class SimpleRest(Resource):
def get(self, todo_id):
return {todo_id: todos[todo_id]}

def put(self, todo_id):
todos[todo_id] = request.form['data']
return {todo_id: todos[todo_id]}
"""
:param p:
:return:
"""
app.run(host='0.0.0.0', port=p, debug=True)


class serialDevices(Resource):
"""
Class that returns the ports connected to the device.

"""
def get(self, nome=None):
# When the name is not passed in the url, it returns all the available
# ports.
if not nome:
# Returns list in format: ('device', 'name', description).
devices = serial.tools.list_ports.comports()
else:
try:
devices = next(serial.tools.list_ports.grep(nome))
except:
# When you can not find devices
abort(404)
if len(devices) == 0:
# When you can not find devices
abort(404)
else:
res = {'devices': devices}
# Use return json.dumps(res) to return in string
return res


api.add_resource(SimpleRest, '/<string:todo_id>')
api.add_resource(
serialDevices,
'/serialDevices',
'/serialDevices/<string:nome>'
)
"""
Class that returns the ports connected to the device.
"""
def get(self, code=None):
if not act.find(code):
pass
else:
# while True:

resp = act.get(code)
return resp

def post(self, code):
comm = request.form['data']
print("Executing " + comm + " in " + code)
if comm.startswith("ADD") is True:
pass # not yes implemented
#return act.add(code,comm)

if comm.startswith("DO"):
return act.do(code, comm)

if not act.find(comm):
pass
#return "code not found"
else:
resp = act.put({code:comm})
return resp

api.add_resource(serialDevices,'/hermes','/hermes/<string:code>')

if __name__ == '__main__':
startup()
startup()
Loading