Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from influxdb import InfluxDBClient
  3. from datetime import datetime
  4. import calendar
  5.  
  6. import samsara
  7. from samsara.apis import SamsaraClient
  8.  
  9. access_token = "MX9e5tDJMnmNyadjuU1ueRIDZXG0xZ"
  10. group_id = 5423
  11. sensor_id = 212014918091278
  12.  
  13. def get_sensors_history():
  14.     # Create an instance of the SamsaraClient.
  15.     client = SamsaraClient()
  16.     # Get a sensor's temperature history at hour-intervals for the past two hours.
  17.     now = datetime.utcnow()
  18.     end_ms = calendar.timegm(now.utctimetuple()) * 1000
  19.     #Use every 10 seconds as datapoint
  20.     step_ms = 10000
  21.     start_ms = end_ms - (3600000)
  22.     series = [{"widgetId": sensor_id, "field": "currentLoop1Mapped"}, {"widgetId": sensor_id, "field": "currentLoop2Mapped"}]
  23.     fill_missing = "withNull"
  24.     params = samsara.HistoryParam(group_id, start_ms, end_ms, step_ms, series, fill_missing)
  25.  
  26.     history = client.get_sensors_history(access_token, params)
  27.     for result in history.results:
  28.         #[0]: Temperature
  29.         #[1]: CO2
  30.         influxWrite("temperature", result.series[0], result.time_ms)
  31.         influxWrite("co2", result.series[1], result.time_ms)
  32.  
  33.         print result
  34.  
  35. #def get_sensor_live(access_token, group_id, sensor_id):
  36.  
  37. if __name__ == "__main__":
  38.     get_sensors_history()
  39.     #get_sensor_live()
  40.  
  41. def influxWrite(measurement, value, time):
  42.     """Instantiate a connection to the InfluxDB."""
  43.     host='192.168.166.36'
  44.     port=8086
  45.     user = 'root'
  46.     password = '!@#$QWERasdf'
  47.     dbname = 'samsara'
  48.     dbuser = 'bogie'
  49.     dbuser_password = '!@#$QWERasdf'
  50.     json_body = [
  51.         {
  52.             "measurement": measurement,
  53.             "time": time,
  54.             "fields": {
  55.                 "value": value
  56.             }
  57.         }
  58.     ]
  59.  
  60.     client = InfluxDBClient(host, port, user, password, dbname)
  61.  
  62.     print("Write points: {0}".format(json_body))
  63.     client.write_points(json_body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement