-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
51 lines (40 loc) · 1.55 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import logging
from sanji.core import Sanji
from sanji.core import Route
from mxserial import Serials
_logger = logging.getLogger("sanji.serial.index")
class Index(Sanji):
def init(self, *args, **kwargs):
path_root = os.path.abspath(os.path.dirname(__file__))
self.serials = Serials(name="serials", path=path_root)
try:
self.serials.set_all()
except Exception, e:
_logger.error(str(e))
@Route(methods="get", resource="/system/serial")
def get(self, message, response):
return response(data=self.serials.getAll())
@Route(methods="get", resource="/system/serial/:id")
def get_by_id(self, message, response):
serial = self.serials.get(int(message.param["id"]))
if serial is None:
return response(code=404)
return response(data=serial)
@Route(methods="put", resource="/system/serial/:id")
def put(self, message, response):
updatedTemplate = self.serials.update(
id=int(message.param["id"]), newObj=message.data)
if updatedTemplate is None:
return response(code=404)
return response(data=updatedTemplate)
if __name__ == "__main__":
from sanji.connection.mqtt import Mqtt
FORMAT = "%(asctime)s - %(levelname)s - %(lineno)s - %(message)s"
logging.basicConfig(level=0, format=FORMAT)
_logger = logging.getLogger("sanji.serial")
logging.getLogger("sh").setLevel(logging.WARN)
index = Index(connection=Mqtt())
index.start()