server/api.py
def api_sensor_in_timeframe(sensor_id, start, end):
  """ Returns all datapoints for given sensor between unixtime start, end
  """

  # Parse callback name. If not found take default
  callbackFunc = request.args.get("callback")
  if not callbackFunc:
    callbackFunc = DEFAULT_CALLBACK_FUNC

  start = "'{}'".format(datetime.datetime.utcfromtimestamp(int(start)).strftime('%Y-%m-%d %H:%M:%S'))
  end = "'{}'".format(datetime.datetime.utcfromtimestamp(int(end)).strftime('%Y-%m-%d %H:%M:%S'))

  # Retrieve sensor data
  cur = app.sqlite.cursor()
  query = """
      SELECT 
        device_name,
        value,
        value_timestamp
        FROM {0}
        WHERE device_uid = '{1}'
        AND polling_timestamp BETWEEN {2} AND {3};
    """.format(DB_TABLE, sensor_id, start, end)

  # Execute query
  cur.execute(query)
  rows = cur.fetchall()

  print(rows)

  # Parse and return
  device_name = ""
  sensor_values = []
  for row in rows:
    device_name = row[0]

    # Parse value and special cases
    value = row[1]
    if "temperature" in row[0].lower() or "temperature" in row[2].lower():
      try:
        value = float(value)/100
      except:
        pass
    if "True" in str(value):
      value = 1
    if "False" in str(value):
      value = 0
    if "none" in str(value).lower():
      value = 0
    if "light" in row[0].lower() or "light" in row[2].lower():
      try:
        value = float(value)/1000
      except:
        pass

    # Sensors emit None when turned off. Ignore
    if not "none" in str(row[2]).lower() and value != "":
      sensor_values.append("\"{0}\":{1}".format(row[2], value))

  return """{0}({{
    "sensor_id":"{1}",
    "sensor_name":"{2}",
    "values":{{{3}}}
  }});""".format(callbackFunc, sensor_id, device_name, ','.join(sensor_values))
 Returns all datapoints for given sensor between unixtime start, end
  
Returns all datapoints for given sensor between unixtime start, end
Returns all datapoints for given sensor between unixtime start end
def api_sensor_in_timeframe(sensor_id, start, end):
  callbackFunc = request.args.get("callback")
  if not callbackFunc:
    callbackFunc = DEFAULT_CALLBACK_FUNC
  start = "'{}'".format(datetime.datetime.utcfromtimestamp(int(start)).strftime('%Y-%m-%d %H:%M:%S'))
  end = "'{}'".format(datetime.datetime.utcfromtimestamp(int(end)).strftime('%Y-%m-%d %H:%M:%S'))
  cur = app.sqlite.cursor()
  query = """
      SELECT 
        device_name,
        value,
        value_timestamp
        FROM {0}
        WHERE device_uid = '{1}'
        AND polling_timestamp BETWEEN {2} AND {3};
    """.format(DB_TABLE, sensor_id, start, end)
  cur.execute(query)
  rows = cur.fetchall()
  print(rows)
  device_name = ""
  sensor_values = []
  for row in rows:
    device_name = row[0]
    value = row[1]
    if "temperature" in row[0].lower() or "temperature" in row[2].lower():
      try:
        value = float(value)/100
      except:
        pass
    if "True" in str(value):
      value = 1
    if "False" in str(value):
      value = 0
    if "none" in str(value).lower():
      value = 0
    if "light" in row[0].lower() or "light" in row[2].lower():
      try:
        value = float(value)/1000
      except:
        pass
    if not "none" in str(row[2]).lower() and value != "":
      sensor_values.append("\"{0}\":{1}".format(row[2], value))
  return """{0}({{
    "sensor_id":"{1}",
    "sensor_name":"{2}",
    "values":{{{3}}}
  }});""".format(callbackFunc, sensor_id, device_name, ','.join(sensor_values))
def api_sensor_in_timeframe ( sensor_id , start , end ) : callbackFunc = request . args . get ( "callback" ) if not callbackFunc : callbackFunc = DEFAULT_CALLBACK_FUNC start = "'{}'" . format ( datetime . datetime . utcfromtimestamp ( int ( start ) ) . strftime ( '%Y-%m-%d %H:%M:%S' ) ) end = "'{}'" . format ( datetime . datetime . utcfromtimestamp ( int ( end ) ) . strftime ( '%Y-%m-%d %H:%M:%S' ) ) cur = app . sqlite . cursor ( ) query = """
      SELECT 
        device_name,
        value,
        value_timestamp
        FROM {0}
        WHERE device_uid = '{1}'
        AND polling_timestamp BETWEEN {2} AND {3};
    """ . format ( DB_TABLE , sensor_id , start , end ) cur . execute ( query ) rows = cur . fetchall ( ) print ( rows ) device_name = "" sensor_values = [ ] for row in rows : device_name = row [ 0 ] value = row [ 1 ] if "temperature" in row [ 0 ] . lower ( ) or "temperature" in row [ 2 ] . lower ( ) : try : value = float ( value ) / 100 except : pass if "True" in str ( value ) : value = 1 if "False" in str ( value ) : value = 0 if "none" in str ( value ) . lower ( ) : value = 0 if "light" in row [ 0 ] . lower ( ) or "light" in row [ 2 ] . lower ( ) : try : value = float ( value ) / 1000 except : pass if not "none" in str ( row [ 2 ] ) . lower ( ) and value != "" : sensor_values . append ( "\"{0}\":{1}" . format ( row [ 2 ] , value ) ) return """{0}({{
    "sensor_id":"{1}",
    "sensor_name":"{2}",
    "values":{{{3}}}
  }});""" . format ( callbackFunc , sensor_id , device_name , ',' . join ( sensor_values ) )
Returns all datapoints for given sensor between unixtime start, end
Returns all datapoints for given sensor between unixtime start end
""" Returns all datapoints for given sensor between unixtime start, end
  """ # Parse callback name. If not found take default # Retrieve sensor data # Execute query # Parse and return # Parse value and special cases # Sensors emit None when turned off. Ignore
sensor_id
start
end