Skip to content

Instantly share code, notes, and snippets.

@KentaroAOKI
Last active December 23, 2016 11:44
Show Gist options
  • Save KentaroAOKI/704b87a962d872ed6746582973867d79 to your computer and use it in GitHub Desktop.
Save KentaroAOKI/704b87a962d872ed6746582973867d79 to your computer and use it in GitHub Desktop.
#coding: utf-8
import json
import time
import os
import requests
import datetime
import hashlib
import hmac
import base64
# Update the customer ID to your Operations Management Suite workspace ID
customer_id = 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
# For the shared key, use either the primary or the secondary Connected Sources client authentication key
shared_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# The log type is the name of the event that is being submitted
log_type = 'IotRaspiOms'
# Build the API signature
def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource):
x_headers = 'x-ms-date:' + date
string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
bytes_to_hash = bytes(string_to_hash).encode('utf-8')
decoded_key = base64.b64decode(shared_key)
encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest())
authorization = "SharedKey {}:{}".format(customer_id,encoded_hash)
return authorization
# Build and send a request to the POST API
def post_data(customer_id, shared_key, body, log_type):
method = 'POST'
content_type = 'application/json'
resource = '/api/logs'
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
content_length = len(body)
signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource)
uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'
headers = {
'content-type': content_type,
'Authorization': signature,
'Log-Type': log_type,
'x-ms-date': rfc1123date
}
response = requests.post(uri,data=body, headers=headers)
if (response.status_code >= 200 and response.status_code <= 299):
print 'Accepted'
else:
print "Response code: {}".format(response.status_code)
hostid = os.popen('hostid').read().strip()
# Sensor data
#jsontsl2561 = os.popen('python tsl2561_lux.py').read().strip()
#jsonadxl345 = os.popen('python adxl345.py').read().strip()
#jsonhdc1000 = os.popen('python hdc1000.py').read().strip()
#jsonbme280 = os.popen('python bme280.py').read().strip()
# Dummy data for test
jsonbme280 = '{"Pressure": 1006.66, "Temp": 19.26, "Hum": 44.47}'
jsonts12561 = '{"LuxLowGain": 0, "Hum": 44.47, "LuxHighGain": 0.0919410937743379}'
dict = {}
dict["DeviceId"] = hostid
dict.update(json.loads(jsontsl2561))
#dict.update(json.loads(jsonadxl345))
#dict.update(json.loads(jsonhdc1000))
dict.update(json.loads(jsonbme280))
body = json.dumps(dict)
print body
post_data(customer_id, shared_key, body, log_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment