data.py 884 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter database
  5. name:data.py
  6. author:rabin
  7. """
  8. from base import Base
  9. class Data(Base):
  10. __table__ = 'data'
  11. def setTable(self, data):
  12. self.table = self.__table__ + '_' + data['farm'] + '_' + data['device']
  13. def insert(self, data):
  14. self.setTable(data)
  15. json = self.insert_json(data)
  16. self.db.write_points(json)
  17. self.select()
  18. def insert_json(self, data):
  19. json = [
  20. {
  21. "measurement": self.table,
  22. "tags": {
  23. "hard": data['hard'],
  24. "type":data['type'],
  25. "gateway": data['gateway']
  26. },
  27. #"time": data['time'],
  28. "fields": {
  29. 'source' : data['source'],
  30. 'value' : data['value']
  31. }
  32. }]
  33. return json
  34. def select(self, data):
  35. self.setTable(data)
  36. result = self.db.query('select * from ' + self.table)
  37. print("Result: {0}".format(result))