Skip to content

Instantly share code, notes, and snippets.

@colagrosso
Last active March 13, 2016 20:18
Show Gist options
  • Save colagrosso/598493 to your computer and use it in GitHub Desktop.
Save colagrosso/598493 to your computer and use it in GitHub Desktop.
Publish CDOT I-70 traffic to SimpleGeo
Copyright (c) 2016 Mike Colagrosso
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# traffic-clouddot-simplegeo.py
#
# Author: Mike Colagrosso
#
# Subscribes to a CloudDot XMPP PubSub node for CDOT I-70 traffic and
# adds a record to a SimpleGeo layer.
#
# See the traffic data here: http://cloud.labjack.com/traffic/explore/
import simplegeo
import sleekxmpp
import logging
from optparse import OptionParser
import time
import sys
SIMPLEGEO_OAUTH_TOKEN = ""
SIMPLEGEO_OAUTH_SECRET = ""
SIMPLEGEO_LAYER = "com.labjack.clouddot.traffic"
XMPP_USERNAME = ""
XMPP_PASSWORD = ""
PUBSUB_JID = "pubsub.cloud.labjack.com"
CHANNEL_ID = "596"
TRAFFIC_CHANNEL_PUBSUB_NODE = "traffic_channel_" + CHANNEL_ID # EB East Vail to MM 211
HIGHWAY_POINT = (39.6437675734185, -106.34696960449219)
class TrafficClient(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.start)
self.simplegeoClient = simplegeo.Client(SIMPLEGEO_OAUTH_TOKEN, SIMPLEGEO_OAUTH_SECRET)
def start(self, event):
self.getRoster()
self.sendPresence()
self.ps = self.plugin['xep_0060']
self.ps.subscribe(PUBSUB_JID, TRAFFIC_CHANNEL_PUBSUB_NODE)
self.add_handler("<message xmlns='jabber:client'><event xmlns='http://jabber.org/protocol/pubsub#event' /></message>", self.pubsubEventHandler, threaded=True)
def pubsubEventHandler(self, xml):
"""
This method is called every time a new traffic delay is published.
Take the delay value and create a SimpleGeo record using it.
"""
for item in xml.findall('{http://jabber.org/protocol/pubsub#event}event/{http://jabber.org/protocol/pubsub#event}items/{http://jabber.org/protocol/pubsub#event}item/{http://jabber.org/protocol/pubsub#event}value'):
trafficDelay = float(item.text)
r = simplegeo.Record(SIMPLEGEO_LAYER, CHANNEL_ID, HIGHWAY_POINT[0], HIGHWAY_POINT[1], delay = trafficDelay)
self.simplegeoClient.add_record(r)
logging.info("Added record r = " + str(r))
if __name__ == '__main__':
#parse command line arguements
optp = OptionParser()
optp.add_option('-q','--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO)
optp.add_option('-d','--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO)
optp.add_option('-v','--verbose', help='set logging to COMM', action='store_const', dest='loglevel', const=5, default=logging.INFO)
optp.add_option("-c","--config", dest="configfile", default="config.xml", help="set config file to use")
opts,args = optp.parse_args()
logging.basicConfig(level=opts.loglevel, format='%(levelname)-8s %(message)s')
xmpp = TrafficClient(XMPP_USERNAME, XMPP_PASSWORD)
xmpp.registerPlugin('xep_0030')
xmpp.registerPlugin('xep_0004')
xmpp.registerPlugin('xep_0060')
xmpp.registerPlugin('xep_0199')
if xmpp.connect(('xmpp.labjack.com', 5222)):
xmpp.process(threaded=False)
print("done")
else:
print("Unable to connect.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment