Commit d6c6e19c authored by zwarag's avatar zwarag

register devices and list them

parent 05898f0d
......@@ -16,9 +16,11 @@ host = "localhost"
port = 1883
keepalive = 60 # seconds
nodes = Nodes()
def on_connect(client, userdata, flags, rc):
print("[Index] con: Connected with result code "+str(rc))
client.subscribe("#") # hashtag means subscribe to all channels, no filtering is done here.
client.subscribe("#") # hashtag means subscribe to all channels, no filtering is done here.
def on_message(client, userdata, msg):
print("[Index] msg: "+msg.topic+" "+json.dumps(json.loads(msg.payload))) # if message is not JSON, programm will crash here without error message.
......@@ -27,7 +29,7 @@ def on_message(client, userdata, msg):
def handle_register(payload):
node = Node(payload)
Nodes.add(node)
nodes.add(node)
print("starting register")
#print(json.dumps(Nodes.get(), indent = 4))
......@@ -35,7 +37,7 @@ def handle_register(payload):
def hello_world():
return 'Welcome to API. This is a REST Service.'
api.add_resource(Nodes, '/nodes');
api.add_resource(Nodes, '/nodes')
if __name__ == '__main__':
client = mqtt.Client()
......
......@@ -4,8 +4,8 @@ class Node:
def __init__(self, data):
decode = json.loads(data)
self.rep = decode
self.id = decode['id']
self.type = decode['type']
self.input = decode['input']
self.output = decode['output']
def __str__(self):
return str(self.rep)
......@@ -4,18 +4,15 @@ from pprint import pprint
class Nodes(Resource):
def __init__(self):
self.nodes = {}
nodes = {}
def add(self, node):
print("whad up")
self.nodes[node.id] = node
print("[Nodes] Added node with id " + node.id)
Nodes.nodes[node.id] = node.rep
print("y")
print(("[Nodes] Added node with id %s. Nodes: %s" % (node.id, Nodes.nodes)))
print("n")
def get(self):
pprint(self.nodes)
return self.nodes
def put(self, node):
add(self, node)
#pprint(("test %s" % Nodes.nodes))
return Nodes.nodes
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment