balluff_html.py 896 B

123456789101112131415161718192021222324252627282930313233343536
  1. from datetime import datetime
  2. import time
  3. import requests, json
  4. import re
  5. from inputs.common import Input
  6. class Balluff(Input):
  7. cpu_start_time = None
  8. cpu_last_time = None
  9. local_start_time = time.time()
  10. db = 1
  11. interval = 0.05
  12. url = "http://192.168.10.20/ports.jsn"
  13. port = 0
  14. def __init__(self):
  15. super().__init__(self.read_handler)
  16. def read_handler(self):
  17. try:
  18. req = requests.get(self.url)
  19. except requests.exceptions.ConnectionError:
  20. return
  21. timestamp = datetime.utcnow()
  22. response = json.loads(req.text)
  23. if not re.match("^DF210[01]$", response['ports'][self.port]['productId']):
  24. raise Exception("unsupported device " + response['ports'][self.port]['productId'])
  25. data = response['ports'][self.port]['processInputs'].split(" ")
  26. data = bytes([int(x, 16) for x in data])
  27. self.queue_ifm_from_bytes(timestamp, data)