Created
March 29, 2018 02:44
-
-
Save ridwanbejo/23d53e6a16e364105345bf8415c7baa4 to your computer and use it in GitHub Desktop.
subscriber code in zeromq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import time | |
from elasticsearch import Elasticsearch | |
from uuid import uuid4 | |
import json | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--device_id", help="Device ID that must send the weather data", | |
action="store") | |
args = parser.parse_args() | |
device_id = "" | |
if args.device_id: | |
device_id = args.device_id | |
print("Device ID is set up for {device_id}!".format(device_id=device_id)) | |
context = zmq.Context() | |
sock = context.socket(zmq.SUB) | |
sock.setsockopt(zmq.SUBSCRIBE, device_id) | |
sock.connect("tcp://127.0.0.1:5600") | |
es = Elasticsearch() | |
if device_id != "": | |
while True: | |
raw_message = sock.recv() | |
message = json.loads(raw_message.split(" --> ")[1]) | |
print raw_message | |
doc = { | |
"device_id": device_id, | |
"message_id": message["message_id"], | |
"humidity": message["humidity"], | |
"temperature_in_celsius": message["temperature_in_celsius"], | |
"createdAt": message["createdAt"] | |
} | |
res = es.index(index="myiot", doc_type='weather', id=str(uuid4()), body=doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment