respondd.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. import json
  3. import zlib
  4. import lib.helper
  5. class Respondd:
  6. def __init__(self, config):
  7. self._config = config
  8. self._aliases = {}
  9. try:
  10. with open("alias.json", 'r') as cfg_handle:
  11. self._aliases = json.load(cfg_handle)
  12. except IOError:
  13. raise
  14. pass
  15. def getNode_ID(self):
  16. if 'node_id' in self._aliases["nodeinfo"]:
  17. return self._aliases["nodeinfo"]["node_id"]
  18. else:
  19. return lib.helper.getDevice_MAC(self._config["batman"]).replace(':', '')
  20. def getJSON(self, root=None):
  21. print(root)
  22. j = self.get()
  23. j['node_id'] = self.getNode_ID()
  24. if not root is None:
  25. j_tmp = j
  26. j = {}
  27. j[root] = j_tmp
  28. return bytes(json.dumps(j, separators=(',', ':')), 'UTF-8')
  29. def getJSONCompressed(self, root=None):
  30. return self.compress(self.getJSON(root))
  31. def compress(self, data):
  32. encoder = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15) # The data may be decompressed using zlib and many zlib bindings using -15 as the window size parameter.
  33. gzip_data = encoder.compress(data)
  34. gzip_data = gzip_data + encoder.flush()
  35. return gzip_data
  36. def get(self):
  37. return {}
  38. pass