-
Notifications
You must be signed in to change notification settings - Fork 0
/
sma_parser.py
executable file
·181 lines (143 loc) · 5.95 KB
/
sma_parser.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Parses SMA telegrams to MQTT messages
Queue MQTT messages
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import threading
import copy
import time
import json
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import config as cfg
from modbus_sma_sb5 import sma_definition
# Logging
import __main__
import logging
import os
script = os.path.basename(__main__.__file__)
script = os.path.splitext(script)[0]
logger = logging.getLogger(script + "." + __name__)
class ParseTelegrams(threading.Thread):
"""
"""
def __init__(self, trigger, stopper, mqtt, telegram):
"""
Args:
:param threading.Event() trigger: signals that new telegram is available
:param threading.Event() stopper: stops thread
:param mqtt.mqttclient() mqtt: reference to mqtt worker
:param list() telegram: sma telegram
"""
logger.debug(">>")
super().__init__()
self.__trigger = trigger
self.__stopper = stopper
self.__telegram = telegram
self.__mqtt = mqtt
def __del__(self):
logger.debug(">>")
def __publish_telegram(self, json_dict):
# publish the dictionaries per topic
# make resilient against double forward slashes in topic
topic = cfg.MQTT_TOPIC_PREFIX
topic = topic.replace('//', '/')
message = json.dumps(json_dict, sort_keys=True, separators=(',', ':'))
self.__mqtt.do_publish(topic, message, retain=False)
def __decode_telegram_element(self, register, element, jsonvalues):
"""
:param register:
:param element:
:param jsonvalues:
:return:
:raises modbus exceptions
"""
MIN_SIGNED = (-2147483648 + 1)
MAX_UNSIGNED = (4294967295 - 1)
mod_message = BinaryPayloadDecoder.fromRegisters(element.registers, byteorder=Endian.Big, wordorder=Endian.Big)
# provide the correct result depending on the defined datatype
datatype = register[sma_definition['DATATYPE']]
if datatype == 'S32': interpreted = mod_message.decode_32bit_int()
elif datatype == 'U32': interpreted = mod_message.decode_32bit_uint()
elif datatype == 'U64': interpreted = mod_message.decode_64bit_uint()
elif datatype == 'STR16': interpreted = mod_message.decode_string(16)
elif datatype == 'STR32': interpreted = mod_message.decode_string(32)
elif datatype == 'S16': interpreted = mod_message.decode_16bit_int()
elif datatype == 'U16': interpreted = mod_message.decode_16bit_uint()
# if no data type is defined do raw interpretation of the delivered data
else: interpreted = mod_message.decode_16bit_uint()
# check for "None" data before doing anything else
if (interpreted <= MIN_SIGNED) or (interpreted >= MAX_UNSIGNED):
json_value = None
else:
multiplier = register[sma_definition['FORMAT']]
# put the data with correct formatting into the data table
if multiplier == 'FIX3': json_value = float(interpreted) / 1000.0
elif multiplier == 'FIX2': json_value = float(interpreted) / 100.0
elif multiplier == 'FIX1': json_value = float(interpreted) / 10.0
elif multiplier == 'STRING': json_value = str(interpreted)
else: json_value = interpreted
logger.debug(f"mod_message for {register[sma_definition['NAME']]} = {json_value} {interpreted}")
try:
json_key = register[sma_definition['JSONKEY']]
except IndexError:
json_key = register[sma_definition['NAME']]
jsonvalues[json_key] = json_value
def __decode_telegrams(self, telegram):
"""
Args:
:param list telegram:
Returns:
"""
logger.debug(f">>")
json_values = dict()
# epoch, mqtt timestamp
ts = int(time.time())
# get counter and remove from telegram list
counter = telegram[0]
telegram.pop(0)
# Build a dict of key:value, for MQTT JSON
json_values["timestamp"] = ts
json_values["counter"] = counter
if cfg.INFLUXDB:
json_values["database"] = cfg.INFLUXDB
for (register, element) in telegram:
try:
self.__decode_telegram_element(register, element, json_values)
except Exception as e:
logger.warning(f"Exception {e}")
# ? break
# Parts of SMA inverter will go in standby at dusk/night
# Only serial and total_yield can be read
# Other register values are set to None
# Skip MQTT broadcast @ night
if None in json_values.values():
logger.debug(f"MQTT values = {json_values}...Not broadcasted...SMA Inverter is in night mode/standby mode")
pass
else:
logger.debug(f"MQTT values = {json_values}")
self.__publish_telegram(json_values)
def run(self):
logger.debug(">>")
while not self.__stopper.is_set():
# block till event is set, but implement timeout to allow stopper
self.__trigger.wait(timeout=1)
if self.__trigger.is_set():
logger.debug(f"Trigger set")
# Make copy of the telegram, for further parsing
telegram = copy.deepcopy(self.__telegram)
# Clear trigger to allow serial reader to read next telegram
self.__trigger.clear()
# Clear telegram list for next capture by ReadSerial class
self.__telegram.clear()
self.__decode_telegrams(telegram)
logger.debug("<<")