Created
May 2, 2014 23:22
-
-
Save wadewegner/267d37ef201ab05406c3 to your computer and use it in GitHub Desktop.
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 os | |
| import psycopg2 | |
| import urlparse | |
| import pika, logging | |
| urlparse.uses_netloc.append("postgres") | |
| url = urlparse.urlparse(os.environ["DATABASE_URL"]) | |
| logging.basicConfig() | |
| conn = psycopg2.connect( | |
| database=url.path[1:], | |
| user=url.username, | |
| password=url.password, | |
| host=url.hostname, | |
| port=url.port | |
| ) | |
| cur = conn.cursor() | |
| cloudAMQP_URL = '' | |
| queueName = "message" | |
| url_str = os.environ.get('CLOUDAMQP_URL', cloudAMQP_URL) | |
| url = urlparse.urlparse(url_str) | |
| params = pika.ConnectionParameters(host=url.hostname, virtual_host=url.path[1:], | |
| credentials=pika.PlainCredentials(url.username, url.password)) | |
| connection = pika.BlockingConnection(params) # connect to CloudAMQP | |
| channel = connection.channel() # start a channel | |
| # create a function which is called on incoming messages | |
| def callback(ch, method, properties, body): | |
| "Found entry:" | |
| records = [x.strip() for x in body.split(',')] | |
| print " Records: " + str(records) | |
| cur.execute('INSERT INTO iot_monitortemps.readings__c (celsius__c, fahrenheit__c, readingdatetime__c) ' + | |
| 'VALUES (%s, %s, %s)', | |
| (records[0],records[1],records[2])) | |
| conn.commit() | |
| print " Inserted" | |
| # set up subscription on the queue | |
| channel.basic_consume(callback, | |
| queue=queueName, | |
| no_ack=True) | |
| channel.start_consuming() # start consuming (blocks) | |
| connection.close() | |
| cur.close() | |
| conn.close() |
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 pika, os, urlparse, logging, random, pytz | |
| from time import sleep | |
| from datetime import datetime | |
| logging.basicConfig() | |
| cloudAMQP_URL = '' | |
| queueName = "message" | |
| url_str = os.environ.get('CLOUDAMQP_URL', cloudAMQP_URL) | |
| url = urlparse.urlparse(url_str) | |
| params = pika.ConnectionParameters(host=url.hostname, virtual_host=url.path[1:], | |
| credentials=pika.PlainCredentials(url.username, url.password)) | |
| connection = pika.BlockingConnection(params) # connect to CloudAMQP | |
| channel = connection.channel() # start a channel | |
| channel.queue_declare(queue=queueName) # declare a queue | |
| while True: | |
| # send a message | |
| c_r = random.uniform(-1, 1) | |
| f_r = random.uniform(-5, 5) | |
| c = 25.875 + c_r | |
| f = 78.575 + f_r | |
| now = datetime.now(pytz.utc) | |
| message = "{0},{1},{2}".format(c, f, now) | |
| channel.basic_publish(exchange='', routing_key=queueName, body=message) | |
| print " [x] Sent: %s" % message | |
| sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment