Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathandreyer/27240800460df59f171cd861450f7c0e to your computer and use it in GitHub Desktop.
Save jonathandreyer/27240800460df59f171cd861450f7c0e to your computer and use it in GitHub Desktop.
Script to setup environment with user, thing & channels followed by the configuration of a one device for bootstrapping
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Bash command used to launch mainflux architecture with bootstrap service:
# $ docker-compose -f docker/docker-compose.yml -f docker/aedes.yml up
# $ docker-compose -f docker/addons/bootstrap/docker-compose.yml up
import requests
import json
requests.packages.urllib3.disable_warnings()
HOST = 'localhost'
CERTIFICATE = '../docker/ssl/certs/ca.crt'
CREATE_USER = True
USER_EMAIL = 'admin@localhost.local'
USER_PASSWORD = '12345678'
# Create user
header = {'Content-Type': 'application/json'}
user = {'email': USER_EMAIL, 'password': USER_PASSWORD}
if CREATE_USER:
response = requests.post('https://' + HOST + '/users', headers=header, data=json.dumps(user), verify=CERTIFICATE)
if response.status_code == 201:
print('New user create')
elif response.status_code == 409:
print('User already created')
# Get token
response = requests.post('https://' + HOST + '/tokens', headers=header, data=json.dumps(user), verify=CERTIFICATE)
if response.status_code != 201:
exit('Error to get token!')
token = json.loads(response.text)['token']
print('user : ' + str(user))
print('token : ' + token)
# Create device & channels
# - Device
header = {'Content-Type': 'application/json', 'Authorization': token}
things_names = [{'name': 'device1'}]
response = requests.post('https://' + HOST + '/things/bulk', headers=header, data=json.dumps(things_names),
verify=CERTIFICATE)
if response.status_code != 201:
exit('Error when creation device!')
thing = json.loads(response.text)['things'][0]
print('Thing created : ' + str(thing))
# - Channels
header = {'Content-Type': 'application/json', 'Authorization': token}
channels_names = [{'name': 'channel1'}, {'name': 'channel2'}]
response = requests.post('https://' + HOST + '/channels/bulk', headers=header, data=json.dumps(channels_names),
verify=CERTIFICATE)
if response.status_code != 201:
exit('Error when creation channels!')
channels = json.loads(response.text)['channels']
channel1 = channels[0]
channel2 = channels[1]
print('Channels created : ' + str(channels))
# Create config for thing
external_id = 'F3:3A:22:F1:B1:64'
external_key = 'my_super_secret_key'
name = 'my_device1'
# - Define attributs
thing_id = thing['id']
ch1_id = channel1['id']
ch2_id = channel2['id']
config = json.dumps({'timer': 10})
data_str = {'external_id': external_id,
'thing_id': thing_id,
'external_key': external_key,
'name': name,
'channels': [ch1_id, ch2_id],
'content': config}
data = json.dumps(data_str)
# - Add configuration
header = {'Content-Type': 'application/json', 'Authorization': token}
response = requests.post('http://' + HOST + ':8202/things/configs', headers=header, data=data)
if response.status_code != 201:
exit('Error to create configuration!')
# TODO fix!
# - Enable things
header = {'Content-Type': 'application/json', 'Authorization': token}
data = json.dumps({"state": 1})
response = requests.put('http://' + HOST + ':8202/things/state/' + thing_id, headers=header, data=data)
if response.status_code != 200:
print('Error on enable thing')
print(response.status_code)
print(response.text)
exit('Error to create configuration!')
# - Get the thing config
header = {'Content-Type': 'application/json', 'Authorization': token}
response = requests.get('http://' + HOST + ':8202/things/configs/' + thing_id, headers=header)
if response.status_code != 200:
exit('Error to get configuration!')
print('Thing configuration : ' + str(json.loads(response.text)))
# Get bootstrap for thing
header = {'Content-Type': 'application/json', 'Authorization': external_key}
response = requests.get('http://' + HOST + ':8202/things/bootstrap/' + external_id, headers=header)
print('Bootstrap configuration : ' + str(json.loads(response.text)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment