common.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from dataclasses import fields
  2. from structures.common import BaseMeasurement
  3. from structures.measurement import Measurement24v, Measurement480v
  4. from structures.plant import S7State, CompactLogixState
  5. from structures.correlated import CorrelatedMeasurements
  6. class MatchSeries:
  7. def __init__(self, series) -> None:
  8. self._series = series
  9. def get_series(self, measurement: BaseMeasurement):
  10. if measurement.series == self._series:
  11. return measurement
  12. else:
  13. # find the series in the data
  14. for key, value in measurement.__dict__.items():
  15. if isinstance(value, BaseMeasurement) and value.series == self._series:
  16. return value
  17. def set_series(self, measurement: BaseMeasurement, series: BaseMeasurement):
  18. if measurement.series == self._series:
  19. return series
  20. else:
  21. # find the series in the data
  22. for key, value in measurement.__dict__.items():
  23. if isinstance(value, BaseMeasurement) and value.series == self._series:
  24. return type(measurement)(**{**measurement.__dict__, key: series})
  25. ALLOWED_GLOBALS = {
  26. 'sum': sum,
  27. 'min': min,
  28. 'max': max,
  29. 'avg': lambda x: sum(x) / len(x),
  30. 'count': len,
  31. 'last': lambda x: x[-1],
  32. }
  33. ALLOWED_NAMES = \
  34. [x.name for x in fields(Measurement24v)] + \
  35. [x.name for x in fields(Measurement480v)] + \
  36. [x.name for x in fields(CompactLogixState)] + \
  37. [x.name for x in fields(S7State)] + \
  38. [x.name for x in fields(CorrelatedMeasurements)] + \
  39. list(ALLOWED_GLOBALS.keys())
  40. ALLOWED_NAMES = set([name for name in ALLOWED_NAMES if not name.startswith('_')])