-
Notifications
You must be signed in to change notification settings - Fork 25
/
objects.py
98 lines (79 loc) · 3.63 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
import datetime
import functools
from enums import *
from utils import *
@functools.total_ordering
class DateTimeInterval:
def __init__(self, entity):
self.duration = getEntity(entity, 'espi:duration',
lambda e: datetime.timedelta(seconds=int(e.text)))
self.start = getEntity(entity, 'espi:start',
lambda e: datetime.datetime.fromtimestamp(int(e.text)))
def __repr__(self):
return '<DateTimeInterval (%s, %s)>' % (self.start, self.duration)
def __eq__(self, other):
if not isinstance(other, DateTimeInterval):
return False
return (self.start, self.duration) == (other.start, other.duration)
def __lt__(self, other):
if not isinstance(other, DateTimeInterval):
return False
return (self.start, self.duration) < (other.start, other.duration)
@functools.total_ordering
class IntervalReading:
def __init__(self, entity, parent):
self.intervalBlock = parent
self.cost = getEntity(entity, 'espi:cost', lambda e: int(e.text) / 100000.0)
self.timePeriod = getEntity(entity, 'espi:timePeriod',
lambda e: DateTimeInterval(e))
self._value = getEntity(entity, 'espi:value', lambda e: int(e.text))
self.readingQualities = set([ReadingQuality(rq, self) for rq in entity.findall('espi:ReadingQuality', ns)])
def __repr__(self):
return '<IntervalReading (%s, %s: %s %s)>' % (self.timePeriod.start, self.timePeriod.duration, self.value, self.value_symbol)
def __eq__(self, other):
if not isinstance(other, IntervalReading):
return False
return (self.timePeriod, self.value) == (other.timePeriod, other.value)
def __lt__(self, other):
if not isinstance(other, IntervalReading):
return False
return (self.timePeriod, self.value) < (other.timePeriod, other.value)
@property
def value(self):
if self.intervalBlock is not None and \
self.intervalBlock.meterReading is not None and \
self.intervalBlock.meterReading.readingType is not None and \
self.intervalBlock.meterReading.readingType.powerOfTenMultiplier is not None:
multiplier = 10 ** self.intervalBlock.meterReading.readingType.powerOfTenMultiplier
else:
multiplier = 1
return self._value * multiplier
@property
def cost_units(self):
if self.intervalBlock is not None and \
self.intervalBlock.meterReading is not None and \
self.intervalBlock.meterReading.readingType is not None and \
self.intervalBlock.meterReading.readingType.currency is not None:
return self.intervalBlock.meterReading.readingType.currency
else:
return CurrencyCode.na
@property
def cost_symbol(self):
return self.cost_units.symbol
@property
def value_units(self):
if self.intervalBlock is not None and \
self.intervalBlock.meterReading is not None and \
self.intervalBlock.meterReading.readingType is not None and \
self.intervalBlock.meterReading.readingType.uom is not None:
return self.intervalBlock.meterReading.readingType.uom
else:
return UomType.notApplicable
@property
def value_symbol(self):
return UOM_SYMBOLS[self.value_units]
class ReadingQuality:
def __init__(self, entity, parent):
self.intervalReading = parent
self.quality = getEntity(entity, 'espi:quality', lambda e: QualityOfReading(int(e.text)))