Exemple #1
0
def main():
    """start mqtt server and subscribe the topic"""
    # check the number of arguments
    if len(argv) != 6:
        print "usage: %s [hostname] [port] [keepalive] [client_id] [topic]" % argv[
            0]
        exit(1)

    try:
        mqtt_client = Mosquitto(argv[4])

        # set callback
        mqtt_client.on_message = cb_on_message

        # connect to MQTT broker
        # client.connect(hostname, port=1883, keepalive=60)
        mqtt_client.connect(argv[1], argv[2], int(argv[3]))

        # subscribe the topic
        mqtt_client.subscribe(argv[5])
        print "%s subscribing topic '%s' from %s:%s" % (argv[4], argv[5],
                                                        argv[1], argv[2])

        while True:
            # call loop method frequently
            # client.loop(timeout=-1); 0 to return immediately; -1 to return
            # after 1 second.
            result = mqtt_client.loop()
            if result != 0:
                # rc != 0; loop failed
                print "Loop failed (%s), disconnecting..." % result
                mqtt_client.disconnect()
                break

    except KeyboardInterrupt:
        # disconnect from the broker
        mqtt_client.disconnect()
        print "Disconnected..."
Exemple #2
0
        collection = [
            get('Bid'),
            get('Ask'),
            get('High'),
            get('Low'),
            get('Direction'),
            get('Last'),
        ]

        message = '/'.join(
            (c[0].childNodes[0].nodeValue for c in collection)).encode('utf-8')

        print message
        if dates.get(symbol, None) != collection[-1]:
            mq.publish('rates/%s' % symbol, message, qos=1, retain=True)
            dates[symbol] = collection[-1]


if __name__ == '__main__':
    args = parser.parse_args()
    mq = Mosquitto('fxrates')
    mq.connect(args.hostname, args.port, 60)
    try:
        while True:
            sleep(.1)
            getFXRate(mq)
    except KeyboardInterrupt:
        print 'closing...'
        mq.disconnect()