-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendStream2M2x.py
executable file
·37 lines (35 loc) · 1.76 KB
/
sendStream2M2x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# NOTES:
# 1) Through trial and error, it appears that I can only send one measurement per HTTP PUT,
# so I will call a function repetitively.
# 2) The documentation says you can only submit one stream per second, so I must put a delay
# example timestamp format
# body = json.dumps({"value": value, "timestamp":"2015-10-15T18:23:01.000Z"})
# or body = json.dumps({"value": value, "timestamp":"2015-10-15T18:23:01Z"})
import time
import datetime
import httplib
import json
def sendStream2M2x(primary_key, device_id, stream, timestamp, value):
time.sleep(1.1) # Delay required by M2x, could be optimized, cause they specify 1 sec/stream
m2x_host = 'api-m2x.att.com'
url = '/v2/devices/' + device_id + '/streams/' + stream + '/value'
headers = {"Content-Type": "application/json", "X-M2X-KEY": primary_key}
body = json.dumps({"value": value, "timestamp": timestamp})
print "Connecting to " + m2x_host + url + str(datetime.datetime.now())
m2x_conn = httplib.HTTPConnection(m2x_host)
m2x_conn.request("PUT", url, body, headers)
response = m2x_conn.getresponse()
http_status = response.status
# 202 = 'Accepted' This is what I see from ATT in the success case
# 404 = 'Not Found' This is what I see from ATT in the case of incorrect keys (primary/device)
# or other poorly formed url
if http_status == 202:
result = response.read()
return result
elif http_status == 404:
print 'ERROR(sendStream2M2x.py): HTTP Status = ', http_status, datetime.datetime.now()
print 'DEBUG: Check that the m2x_primary_key, m2x_device_id are correct in create_devices.py'
return http_status
else:
print 'ERROR(sendStream2M2x.py): HTTP Status = ', http_status, datetime.datetime.now()
return http_status