Advertisement
Guest User

360_mqtt_broker

a guest
Apr 22nd, 2017
381
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. #Set variables
  4. username360="your@email.com"
  5. password360="PASS"
  6. mosquitto_pub="/path/to/mosquitto_pub"
  7. mqtt_host="127.0.0.1"
  8. mqtt_port="1883"
  9. mqtt_user="USER"
  10. mqtt_pass="PASS"
  11. timeout=300
  12.  
  13.  
  14. function bearer {
  15. echo "$(date +%s) INFO: requesting access token"
  16. bearer_id=$(curl -s -X POST -H "Authorization: Basic cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg==" -F "grant_type=password" -F "username=$username360" -F "password=$password360" https://api.life360.com/v3/oauth2/token.json | grep -Po '(?<="access_token":")\w*')
  17. }
  18.  
  19. function circles () {
  20. echo "$(date +%s) INFO: requesting circles."
  21. read -a circles_id <<<$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles.json | grep -Po '(?<="id":")[\w-]*')
  22. }
  23.  
  24. function members () {
  25. echo "$(date +%s) INFO: requesting members"
  26. members=$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles/$2)
  27. }
  28.  
  29. bearer
  30. circles $bearer_id
  31.  
  32. #Main Loop
  33. while :
  34. do
  35.  
  36. #Check if circle id is valid. If not request new token.
  37. if [ -z "$circles_id" ]; then
  38. bearer
  39. circles $bearer_id
  40. fi
  41.  
  42. #Loop through circle ids
  43. for i in "${circles_id[@]}"
  44. do
  45.  
  46. #request member list
  47. members $bearer_id $i
  48.  
  49. #Check if member array is valid. If not request new token
  50. if [ -z "$members" ]; then
  51. bearer
  52. circles $bearer_id
  53. members $bearer_id $i
  54. fi
  55.  
  56. members_count=$(echo $members | jq '.members | (length)')
  57. count=0
  58. while [ $count -lt $members_count ]
  59. do
  60.     id=$(echo $members | jq .members[$count].id)
  61.     firstName=$(echo $members | jq .members[$count].firstName)
  62.     lastName=$(echo $members | jq .members[$count].lastName)
  63.     latitude=$(echo $members | jq .members[$count].location.latitude)
  64.     longitude=$(echo $members | jq .members[$count].location.longitude)
  65.     accuracy=$(echo $members | jq .members[$count].location.accuracy)
  66.     battery=$(echo $members | jq .members[$count].location.battery)
  67.     avatar=$(echo $members | jq .members[$count].avatar)
  68.     echo $firstName
  69.     #echo "$(date +%s) INFO: owntracks/${firstName//\"/}/${id//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/}}"
  70.     $mosquitto_pub -h $mqtt_host -p $mqtt_port -u $mqtt_user -P $mqtt_pass -t "owntracks/${firstName//\"/}/${id//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/}}"
  71.     count=$(($count+1))
  72. done
  73. done
  74. sleep $timeout
  75. done
Advertisement
RAW Paste Data Copied
Advertisement