Last active
September 26, 2019 02:26
-
-
Save tjukanovt/ce8f84026db01582fbc23bfd67d56f8b to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Feb 14 16:54:39 2019 | |
@author: topit | |
MQTT part based on: http://www.steves-internet-guide.com/into-mqtt-python-client/ | |
API docs for HSL high-frequency positioning: https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/ | |
""" | |
import paho.mqtt.client as mqtt | |
import json | |
import csv | |
# The callback for when the client receives a CONNACK response from the server. | |
def on_connect(client, userdata, flags, rc): | |
if rc==0: | |
print("connected OK Returned code=",rc) | |
else: | |
print("Bad connection Returned code=",rc) | |
# Subscribing in on_connect() means that if we lose the connection and | |
# reconnect then subscriptions will be renewed. | |
# This particular topic collects ALL traffic | |
client.subscribe("/hfp/v1/journey/#") | |
# The callback for when a PUBLISH message is received from the server. | |
def on_message(client, userdata, msg): | |
j = msg.payload.decode('utf8').replace("'", '"') | |
d = json.loads(j) | |
v = d["VP"] | |
# I don't want all of the data, so I define here the values I want | |
values=v['long'],v['lat'],v['desi'],v['oper'],v['jrn'],v['line'],v['spd'], v['dl'], v['hdg'],v['drst'],v['veh'],v['tsi'] | |
# There are some nulls in there, so this is just a stupid way to exclude those | |
if v['lat'] > 0: | |
with open(r'hsl_mqtt.csv', 'a') as f: | |
writer = csv.writer(f, quoting=csv.QUOTE_NONE, lineterminator='\n') | |
writer.writerow(values) | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect("mqtt.hsl.fi", 1883, 60) | |
# Blocking call that processes network traffic, dispatches callbacks and | |
# handles reconnecting. | |
# Other loop*() functions are available that give a threaded interface and a | |
# manual interface. | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment