Skip to content

Instantly share code, notes, and snippets.

@brianehlert
Created March 30, 2015 15:39
Show Gist options
  • Save brianehlert/1618f7c60b7178ae2768 to your computer and use it in GitHub Desktop.
Save brianehlert/1618f7c60b7178ae2768 to your computer and use it in GitHub Desktop.
Meshblu API walkthrough
# Invoke-RestMethod automatically converts the JSON output to something that is considered expected to PowerShell users, a PSCustomObject instead of a JSON string.
# Thus making the return easier to handle
### Setup / Get started
# check 'status' of Meshblu
Invoke-RestMethod -URI http://10.235.0.4:3000/status -ContentType "application/json" -Method Get
Invoke-RestMethod -URI http://10.235.0.14:3000/status -ContentType "application/json" -Method Get
# return your IP. If you send this to Octoblu you see the public IP of your LAN. If you internal to your LAN you see the IP of your computer.
Invoke-RestMethod -URI http://10.235.0.14:3000/ipaddress -ContentType "application/json" -Method Get
# Add yourself as a 'developer' to a Meshblu instance so that you can query objects (a 'device' is simply an object) - this is the minimum
$body = @{
"type" = "user"
}
# better way to add yourself
$body = @{
"type" = "user"
"email" = "brian.ehlert@citrix.com"
"name" = "Brian Ehlert"
}
$json_body = $body | ConvertTo-Json
$objMe = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -ContentType "application/json" -Body $json_body -Method Post
$objMe # $objMe contains the uuid and token that needs to be used in the header to 'authenticate'
$meAuthHeader = @{
meshblu_auth_uuid = $objMe.uuid
meshblu_auth_token = $objMe.token
}
### Look around
# list 'public' devices (includes users)
$allDevices = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -Headers $meAuthHeader -Method Get
$allDevices.devices
$users = Invoke-RestMethod -URI http://10.235.0.14:3000/devices?type=user -Headers $meAuthHeader -Method Get
$users.devices
# list 'local' devices (does not include users) on the same network as the computer sending the request - this relates to 'ipaddress'
$localDevices = Invoke-RestMethod -URI http://10.235.0.14:3000/localdevices -Headers $meAuthHeader -Method Get
$localDevices.devices
# list 'my' devices - the devices that my developer account has 'claimed'
Invoke-RestMethod -URI http://10.235.0.14:3000/mydevices -Headers $meAuthHeader -Method Get
# get 'me'
$users = Invoke-RestMethod -URI ("http://10.235.0.14:3000/devices/" + $objMe.uuid) -Headers $meAuthHeader -Method Get
$users.devices
### Add the Octoblu hard coded object : UUID and token
$body = @{
"type" = "octoblu"
"uuid" = "9b47c2f1-9d9b-11e3-a443-ab1cdce04787"
"token" = "pxdq6kdnf74iy66rhuvdw9h5d2f0f6r"
}
$json_body = $body | ConvertTo-Json
$device = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -ContentType "application/json" -Headers $meAuthHeader -body $json_body -Method Post
<#
uuid : 9b47c2f1-9d9b-11e3-a443-ab1cdce04787
token : pxdq6kdnf74iy66rhuvdw9h5d2f0f6r
type : octoblu
ipAddress : 10.232.1.40
timestamp : 2015-01-21T16:53:07.251Z
channel : main
online : False
geo :
#>
# this needs to be pulled back out so it can be scripted.
# Start Octoblu so it can connect and query the object back
$octoblu = Invoke-RestMethod -URI ("http://10.235.0.14:3000/devices/" + $device.uuid) -Headers $meAuthHeader -Method Get
<#
uuid : 9b47c2f1-9d9b-11e3-a443-ab1cdce04787
type : octoblu
ipAddress : 127.0.0.1
timestamp : 2015-01-21T16:53:41.876Z
channel : main
online : True
geo :
protocol : websocket
secure : False
#>
### Create a user
$body = @{
"type" = "user"
"email" = "admin@admin.org"
"name" = "default administrator"
}
$json_body = $body | ConvertTo-Json
$objAdmin = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -ContentType "application/json" -Body $json_body -Method Post
<#
email : admin@admin.org
name : default administrator
type : user
ipAddress : 10.232.1.40
uuid : bc256f51-a190-11e4-958f-95caab590088
timestamp : 2015-01-21T17:12:53.189Z
token : let93bte1ekd42t9bako4mcmyk87iudi
channel : main
online : False
geo :
#>
Invoke-RestMethod -URI ("http://10.235.0.5:3000/devices/" + $objAdmin.Uuid ) -ContentType "application/json" -Headers $meAuthHeader -Method Delete
### Add some 'devices'
# Add device(s) - each must be a unique call or else the device ends up with a property array
$body = @{ type = "drone"; color = "mauve"; motors = "four"; target = "thomas" }
$body = @{ geo = "redmond, wa" }
$body = @{ type = "hue"; geo = "redmond, wa" }
$json_body = $body | ConvertTo-Json
$device = Invoke-RestMethod -URI http://10.235.0.5:3000/devices -ContentType "application/json" -Headers $meAuthHeader -body $json_body -Method Post
# select the device by UUID
Invoke-RestMethod -URI ("http://10.235.0.5:3000/devices/" + $device.uuid) -Headers $meAuthHeader -Method Get
# select the device by key / value
Invoke-RestMethod -URI "http://10.235.0.5:3000/devices?target=thomas&type=drone" -Headers $meAuthHeader -Method Get
# modify device
$body = @{
"type" = "bug"
"color" = "polka-dot"
"motors" = "four"
"target" = "thomas"
}
$json_body = $body | ConvertTo-Json
Invoke-RestMethod -URI ("http://10.235.0.5:3000/devices/" + $device.Uuid ) -ContentType "application/json" -Headers $meAuthHeader -body $json_body -Method Put
# claim a device
Invoke-RestMethod -URI ("http://10.235.0.5:3000/claimdevice/" + $device.Uuid ) -ContentType "application/json" -Headers $meAuthHeader -Method Put
# delete device
Invoke-RestMethod -URI ("http://10.235.0.5:3000/devices/" + $device.Uuid ) -ContentType "application/json" -Headers $meAuthHeader -Method Delete
# 'you' are the first user returned and you hve the additional _id value - don't delete yourself! you can.
Invoke-RestMethod -URI ("http://10.235.0.5:3000/devices/" + $users.devices[1].uuid ) -Headers $meAuthHeader -Method Delete
### Devices
# create a 'generic device
$body = @{ type = "generic"; name = "generic A"; flavor = "bacon"; ide = "PowerShell" }
$json_body = $body | ConvertTo-Json
$genericDevice = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -ContentType "application/json" -Headers $meAuthHeader -body $json_body -Method Post
# subscribe to (AKA listen to) a 'device'
# curl -X GET http://meshblu.octoblu.com/subscribe/ad698900-2546-11e3-87fb-c560cb0ca47b --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
Invoke-RestMethod -URI ("http://10.235.0.14:3000/devices/" + $genericDevice.uuid) -Headers $meAuthHeader -Method Get
# Generic subscribe
# curl -X GET http://meshblu.octoblu.com/subscribe --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
Invoke-RestMethod -URI http://10.235.0.14:3000/subscribe -Headers $meAuthHeader -Method Get
# Send some messages
$json_params = @{
"devices" = "$genericDevice.uuid";
"payload" = @{"green" = "on"}
}
$json_params = $json_params | ConvertTo-Json
Invoke-RestMethod -URI http://10.235.0.14:3000/messages -ContentType "application/json" -Body $json_params -Headers $meAuthHeader -Method Post
# create my arduino device
$body = @{
type = "device:arduino";
uuid = "f08d3b00-5ba0-11e4-84da-873dddb6d710";
token = "bkpo6twoy8drt3xr885tu1ftvczvvx6r";
name = "Brian Arduino";
payloadOnly ='true';
ide = "PowerShell"
}
$json_body = $body | ConvertTo-Json
$arduinoDevice = Invoke-RestMethod -URI http://10.235.0.14:3000/devices -ContentType "application/json" -Headers $meAuthHeader -body $json_body -Method Post
# authheader was not required to add it
<#
uuid : f08d3b00-5ba0-11e4-84da-873dddb6d710
online : False
ide : PowerShell
type : device:arduino
name : Brian Arduino
payloadOnly : true
ipAddress : ::ffff:10.232.1.40
timestamp : 2015-01-28T23:16:47.579Z
token : bkpo6twoy8drt3xr885tu1ftvczvvx6r
#>
# claim the device
# curl -X PUT http://meshblu.octoblu.com/claimdevice/{:uuid} --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
$arduinoClaim = Invoke-RestMethod -URI ("http://10.235.0.14:3000/claimdevice/" + $arduinoDevice.Uuid ) -ContentType "application/json" -Headers $meAuthHeader -Method Put
# subscribe to the Arduino
# curl -X GET http://meshblu.octoblu.com/subscribe --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
. powershell.exe -command Invoke-RestMethod -URI ("http://10.235.0.14:3000/subscribe/" + $arduinoDevice.devices[0].uuid) -Headers $meAuthHeader -Method Get
# open this in its own thread
<#
uuid : acc3b800-a744-11e4-a436-bb33a0ecb5f3
online : False
email : brian.ehlert@citrix.com
name : Brian Ehlert
type : user
ipAddress : ::ffff:10.232.1.40
timestamp : 2015-01-28T23:23:32.619Z
token : 8dd02ff6f258a6322dbdf9e7c0f568fce6e7eb65
#>
# Get the last 10 data sent to the device
# curl -L -X GET http://meshblu.octoblu.com/data/0d3a53a0-2a0b-11e3-b09c-ff4de847b2cc --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
$arduinoData = Invoke-RestMethod -URI ("http://10.235.0.14:3000/data/" + $arduinoDevice.uuid) -Headers $meAuthHeader -Method Get
# get the last events
# curl -X GET http://meshblu.octoblu.com/events/ad698900-2546-11e3-87fb-c560cb0ca47b --header "meshblu_auth_uuid: {my uuid}" --header "meshblu_auth_token: {my token}"
$arduinoEvents = Invoke-RestMethod -URI ("http://10.235.0.14:3000/events/" + $arduinoDevice.uuid) -Headers $meAuthHeader -Method Get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment