summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/app.py9
-rw-r--r--server/buttonDispatcher.py10
-rw-r--r--server/dispatcher.py7
-rw-r--r--server/event.py4
-rw-r--r--server/history.py93
5 files changed, 117 insertions, 6 deletions
diff --git a/server/app.py b/server/app.py
new file mode 100644
index 0000000..716867a
--- /dev/null
+++ b/server/app.py
@@ -0,0 +1,9 @@
+import json
+from bottle import route, request, run
+from button import loadAll, Button
+
+@route('/button')
+def show_all():
+ return json.dumps(loadAll())
+
+run(host='localhost', port=8080, debug=True)
diff --git a/server/buttonDispatcher.py b/server/buttonDispatcher.py
index 8938288..d3331bd 100644
--- a/server/buttonDispatcher.py
+++ b/server/buttonDispatcher.py
@@ -1,12 +1,12 @@
import json
-import cherrypy
+from bottle import Bottle, route
from button import loadAll, Button
from dispatcher import Dispatcher
from lib import uuid
# Tweak: consider using serpy for serializing objects for JSON.
-@cherrypy.expose
+@app.route('/')
class ButtonDispatcher(Dispatcher):
@cherrypy.tools.json_out()
def GET(self, id=None):
@@ -39,7 +39,7 @@ class ButtonDispatcher(Dispatcher):
return button.toDict()
@cherrypy.tools.json_out()
- def PUT(self, id, status):
+ def PUT(self, id):
# POST /button/{id}?status={} - updates button status
self.validate_uuid(id)
@@ -54,8 +54,10 @@ class ButtonDispatcher(Dispatcher):
return button.toDict()
@cherrypy.tools.json_out()
- def DELETE(self, id):
+ def DELETE(self, id, status, *path, **args):
# DELETE /button/{id} - deletes button
+ self.validate_uuid(id)
+
button = Button()
button.loadById(id)
button.delete()
diff --git a/server/dispatcher.py b/server/dispatcher.py
index 26216ae..fdec47a 100644
--- a/server/dispatcher.py
+++ b/server/dispatcher.py
@@ -5,11 +5,11 @@ from lib import uuid
class Dispatcher(object):
def validate_uuid(self, uuid4):
if not uuid.validate(uuid4):
- self.error(1, 'Invalid UUID in request')
+ self.error(1, 'Invalid UUID in request: ' + str(uuid4))
def error(self, code, message='An application error occurred'):
raise cherrypy.HTTPError(400,
- self.response({'error': {'code': int(code), 'message': str(message)}})
+ json.dumps({'error': {'code': int(code), 'message': str(message)}})
)
def GET(self, id=None):
@@ -23,3 +23,6 @@ class Dispatcher(object):
def DELETE(self):
raise cherrypy.HTTPError(405)
+
+ def PATCH(self):
+ raise cherrypy.HTTPError(405)
diff --git a/server/event.py b/server/event.py
new file mode 100644
index 0000000..4069465
--- /dev/null
+++ b/server/event.py
@@ -0,0 +1,4 @@
+Create = 0
+Update = 1
+Delete = 2
+Press = 3
diff --git a/server/history.py b/server/history.py
new file mode 100644
index 0000000..d20dbe7
--- /dev/null
+++ b/server/history.py
@@ -0,0 +1,93 @@
+import cherrypy
+import json
+from lib import uuid
+from lib import database
+from time import time
+
+import event
+
+def loadAll():
+ db = database.connect()
+ db.execute("SELECT event,button_id,time FROM history")
+
+ results = []
+ while True:
+ try:
+ button = ()
+ button.loadFromDb(db.fetchone())
+ results.append(button.toDict())
+ except:
+ break
+
+ return results
+
+class Button(object):
+ def __init__(self):
+ self.persisted = False
+ self.id = None
+ self.status = STATUS_INVENTORY
+ self.last_pressed = 0
+ self.history = []
+
+ def toDict(self):
+ return {
+ 'id': self.id,
+ 'status': self.status,
+ 'last_pressed': self.last_pressed
+ }
+
+ def toJSON(self):
+ return self.toDict()
+
+ def loadById(self, id):
+ db = database.connect()
+ db.execute("SELECT id,status,last_pressed FROM buttons WHERE id = ?" , (str(id),))
+
+ return self.loadFromDb(db.fetchone())
+
+ def loadFromDb(self, dbrs):
+ if dbrs is None:
+ raise cherrypy.HTTPError(404, 'Button not found')
+
+ self.persisted = True
+ self.id, self.status, self.last_pressed = dbrs
+
+ return self
+
+ def press(self):
+ if self.last_pressed:
+ self.history.insert(0, self.last_pressed)
+
+ self.last_pressed = int(time())
+ return self
+
+ def save(self):
+ db = database.connect()
+
+ # validate
+ if self.id is None:
+ raise cherrypy.HTTPError(500, "What u doin?")
+
+ # upsert: update
+ elif self.persisted:
+ db.execute("UPDATE buttons SET status = ?, last_pressed = ? WHERE id = ?",
+ (self.status, self.last_pressed, self.id,)
+ )
+
+ # upsert: insert
+ else:
+ db.execute("INSERT INTO buttons VALUES (?, ?, ?)", (self.id, self.status, self.last_pressed))
+
+ db.connection.commit()
+ return self
+
+ def delete(self):
+ db = database.connect()
+
+ if self.id is None:
+ raise cherrypy.HTTPError(500, "uhh wat")
+
+ db.execute("DELETE FROM buttons WHERE id = ?", (self.id,))
+ db.connection.commit()
+
+ return None