-
Notifications
You must be signed in to change notification settings - Fork 0
/
MqttVar.py
137 lines (118 loc) · 5.16 KB
/
MqttVar.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MqttVar.py
MIT License (c) Faure Systems
Class to track and publish variable changes.
"""
import gettext
try:
gettext.find("MqttVar")
traduction = gettext.translation('MqttVar', localedir='locale', languages=['fr'])
traduction.install()
except:
_ = gettext.gettext # cool, this hides PyLint warning Undefined name '_'
class MqttVar:
# __________________________________________________________________
def __init__(self, name, type, initial, decimal=None, precision=1, alias=("1", "0"), logger=None):
super().__init__()
self._logger = logger
self._name = name
self._type = type
self._decimal = decimal
self._precision = precision
self._true, self._false = alias
if type == int:
v = int(str(initial))
self._value = v
self._reference = v + 1
if self._logger:
self._logger.info(
"{0} '{1}' {2}={3} {4}={5}".format(_("New int Publishable"), self._name, _("with initial"), initial,
_("and precision"), self._precision))
elif type == float:
v = float(str(initial))
self._value = v
self._reference = v + 1.0
if self._logger and self._decimal:
self._logger.info("{0} '{1}' {2}={3} {4}={5} {6}={7}".format(_("New float Publishable"), self._name,
_("with initial"), initial,
_("and precision"), self._precision,
_("and decimal"), self._decimal))
elif self._logger:
self._logger.info(
"{0} '{1}' {2}={3} {4}={5}".format(_("New float Publishable"), self._name, _("with initial"),
initial, _("and precision"), self._precision))
elif type == str:
self._value = initial
self._reference = initial + "_"
if self._logger:
if initial:
self._logger.info(
"{0} '{1}' {2}={3}".format(_("New str Publishable"), self._name, _("with initial"), initial))
else:
self._logger.info(
"{0} '{1}' {2}=''".format(_("New str Publishable"), self._name, _("with initial")))
elif type == bool:
self._value = initial
self._reference = not initial
if self._logger:
self._logger.info(
"{0} '{1}' ({2}/{3}) {4}={5}".format(_("New boolean Publishable"), self._name, self._true,
self._false, _("with initial"), initial))
else:
self._type = int
v = 0
self._value = v
self._reference = v + 1
self._decimal = None
self._precision = 1
if self._logger:
self._logger.info(
"{0} '{1}' {2}={3} {4}={5}".format(_("New incorrect Publishable created as int"), self._name,
_("with initial"), initial, _("and precision"), self._precision))
# __________________________________________________________________
def __str__(self):
self._reference = self._value
if self._type == float and self._decimal:
return "{0}={1:.{prec}f}".format(self._name, self._value, prec=self._decimal)
elif self._type == int:
return "{0}={1}".format(self._name, str(self._value))
elif self._type == bool:
if self._value:
return "{0}={1}".format(self._name, self._true)
else:
return "{0}={1}".format(self._name, self._false)
else:
if isinstance(self._value, str):
if not self._value:
return "{0}=-".format(self._name)
else:
return "{0}={1}".format(self._name, self._value)
else:
try:
v = str(self._value)
if v:
return "{0}={1}".format(self._name, v)
except:
pass
finally:
return "{0}=-".format(self._name)
# __________________________________________________________________
def change(self):
if self._type == int or self._type == float:
if abs(self._reference - self._value) > self._precision:
return self.__str__()
else:
return None
else:
if self._reference != self._value:
return self.__str__()
else:
return None
# __________________________________________________________________
def update(self, value):
self._value = value
# __________________________________________________________________
def value(self):
return self._value