Guest User

Untitled

a guest
Sep 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import connexion
  2. import six
  3. # using MySQL connector
  4. import mysql.connector
  5. from swagger_server.controllers import dbconfig as cfg
  6.  
  7. from swagger_server.models.measure import Measure # noqa: E501
  8. from swagger_server import util
  9.  
  10. def createConnection():
  11. # MySQL Environment Parameters read from dbconfig.py
  12. # using connection pool
  13.  
  14. conn = mysql.connector.connect(user=cfg.mysql['user'], password=cfg.mysql['passwd'],
  15. host=cfg.mysql['host'],
  16. database=cfg.mysql['db'],
  17. pool_name = "proxy_pool",
  18. pool_size = 5) # default size
  19. return conn
  20.  
  21. def find_measure(device_id): # noqa: E501
  22. """retrieve the last measure of Air Conditions
  23. # noqa: E501
  24. :param device_id: the id of the device (id_list)
  25. :type device_id: int
  26. :rtype: Measure
  27. """
  28. conn = createConnection()
  29.  
  30. print("Connection created...")
  31.  
  32. cur = conn.cursor()
  33.  
  34. query = ('SELECT id, ts_op, lux, temperature, humidity, pressure, iaq, tvoc, co2e, pm2_5, pm10, hflevel, lflevel'
  35. ' FROM dev_aircare_data WHERE id_list = %(device_id)s AND id = (SELECT MAX(id) FROM dev_aircare_data WHERE id_list = %(device_id)s)')
  36.  
  37. # selecting a single device
  38. cur.execute(query, {"device_id": device_id})
  39.  
  40. # we're sure that the query returns a single record
  41.  
  42. # this way we avoid error if device_id is not existing in table
  43. ms = {}
  44.  
  45. for (id, ts_op, lux, temperature, humidity, pressure, iaq, tvoc, co2e, pm2_5, pm10, hflevel, lflevel) in cur:
  46. ms = Measure(id, ts_op, float(lux), float(temperature), float(humidity), float(pressure), float(iaq), int(tvoc),
  47. int(co2e), int(pm2_5), int(pm10), float(hflevel), int(lflevel))
  48.  
  49. cur.close()
  50. conn.close()
  51.  
  52. print("Connection closed...")
  53.  
  54. return ms
  55.  
  56.  
  57. def find_measures(device_id, limit): # noqa: E501
  58. """List all latest measurements of Air conditions
  59. # noqa: E501
  60. :param device_id: the id of the device (id_list))
  61. :type device_id: int
  62. :param limit: max number of record retrieved
  63. :type limit: int
  64. :rtype: List[Measure]
  65. """
  66.  
  67. conn = createConnection()
  68.  
  69. print("Connection created...")
  70.  
  71. cur = conn.cursor()
  72.  
  73. query = ('SELECT id, ts_op, lux, temperature, humidity, pressure, iaq, tvoc, co2e, pm2_5, pm10, hflevel, lflevel'
  74. ' FROM dev_aircare_data WHERE id_list = %(device_id)s ORDER BY id DESC LIMIT %(limit)s')
  75.  
  76. cur.execute(query, {"device_id": device_id, "limit": limit})
  77.  
  78. # the structure to return data
  79. vetResult = []
  80.  
  81. for (id, ts_op, lux, temperature, humidity, pressure, iaq, tvoc, co2e, pm2_5, pm10, hflevel, lflevel) in cur:
  82. # build a single Measure
  83. ms = Measure(id, ts_op, float(lux), float(temperature), float(humidity), float(pressure), float(iaq), int(tvoc),
  84. int(co2e), int(pm2_5), int(pm10), float(hflevel), int(lflevel))
  85. # add ms to the array
  86. vetResult.append(ms)
  87.  
  88. cur.close()
  89. conn.close()
  90.  
  91. print("Connection closed...")
  92.  
  93. return vetResult
Add Comment
Please, Sign In to add comment