Skip to content

Commit 6737300

Browse files
author
sidey79
committed
feat: JSON streucture optimized and docs updated
1 parent cd8e09c commit 6737300

19 files changed

Lines changed: 41 additions & 142 deletions

.devcontainer/fhem-data/FHEM/99_MyUtils.pm

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,6 @@ sub MyUtils_Initialize {
1313

1414
# Enter you functions below _this_ line.
1515

16-
sub MqttSignalduino_DispatchFromJSON {
17-
my ($json_str, $name) = @_;
18-
19-
if (!defined($json_str) || !defined($name)) {
20-
Log3 $name, 3, "MqttSignalduino_DispatchFromJSON: Missing arguments (JSON or Name)";
21-
return;
22-
}
23-
24-
my $hash = $defs{$name};
25-
if (!defined($hash)) {
26-
Log3 $name, 3, "MqttSignalduino_DispatchFromJSON: Device $name not found";
27-
return;
28-
}
29-
30-
my $data;
31-
eval {
32-
$data = decode_json($json_str);
33-
};
34-
if ($@) {
35-
Log3 $name, 3, "MqttSignalduino_DispatchFromJSON: JSON decode error: $@";
36-
return;
37-
}
38-
#print Dumper($data);
39-
40-
$hash->{Clients} = 'SD_WS:';
41-
$hash->{MatchList} = { '12:SD_WS' => '^W\d+x{0,1}#.*' };
42-
43-
# Extract fields based on expected JSON structure from MQTT
44-
# The full dispatch message is now constructed by combining 'preamble' (e.g., W126#) and 'state' (e.g., HexData).
45-
46-
my $rmsg = $data->{rawmsg} // undef;
47-
my $dmsg = $data->{payload} // undef;
48-
my $rssi = $data->{metadata}->{rssi} // undef;
49-
my $id = $data->{protocol}->{id} // undef;
50-
my $freqafc = $data->{metadata}->{freqafc} // undef;
51-
52-
if (!defined($dmsg)) {
53-
Log3 $name, 4, "MqttSignalduino_DispatchFromJSON: No dmsg found in JSON";
54-
return;
55-
}
56-
57-
# Update hash with latest values
58-
#$hash->{RAWMSG} = $rmsg if (defined($rmsg));
59-
#$hash->{RSSI} = $rssi if (defined($rssi));
60-
61-
# Prepare addvals similar to SIGNALduno_Dispatch
62-
my %addvals = (
63-
Protocol_ID => $id
64-
);
65-
66-
if (defined($rmsg)) {
67-
$addvals{RAWMSG} = $rmsg;
68-
}
69-
if (defined($rssi)) {
70-
$addvals{RSSI} = $rssi;
71-
}
72-
if (defined($freqafc)) {
73-
$addvals{FREQAFC} = $freqafc;
74-
}
75-
76-
Log3 $name, 5, "MqttSignalduino_DispatchFromJSON: Dispatching $dmsg";
77-
78-
# Call FHEM Dispatch function
79-
Dispatch($hash, $dmsg, \%addvals);
80-
}
16+
# MqttSignalduino_DispatchFromJSON wurde nach FHEM/lib/SD_Dispatch.pm verschoben.
8117

8218
1;

.devcontainer/fhem-data/fhem_signalduino_example.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ attr mqtt_broker autocreate simple
4040
define PySignalDuino MQTT2_DEVICE
4141
setuuid PySignalDuino 695e9c21-f33f-c986-4f81-a9f0ab37b6bcedf8
4242
attr PySignalDuino IODev mqtt_broker
43-
attr PySignalDuino readingList signalduino/v1/state/messages:.* { MqttSignalduino_DispatchFromJSON($EVENT, $NAME);; json2nameValue($EVENT,'MSG_');; }\
43+
attr PySignalDuino readingList signalduino/v1/state/messages:.* { use FHEM::Devices::SIGNALDuino::Message;; FHEM::Devices::SIGNALDuino::Message::json2Dispatch($EVENT, $NAME);; json2nameValue($EVENT,'MSG_');; }\
4444
signalduino/v1/responses:.* { json2nameValue($EVENT, 'RESP_') }\
4545
signalduino/v1/errors:.* { json2nameValue($EVENT, 'ERR_') }
4646
# attr PySignalDuino Clients :CUL_EM:CUL_FHTTK:CUL_TCM97001:CUL_TX:CUL_WS:Dooya:FHT:FLAMINGO:FS10:FS20:Fernotron:Hideki:IT:KOPP_FC:LaCrosse:OREGON:PCA301:RFXX10REC:Revolt:SD_AS:SD_Rojaflex:SD_BELL:SD_GT:SD_Keeloq:SD_RSL:SD_UT:SD_WS07:SD_WS09:SD_WS:SD_WS_Maverick:SOMFY:Siro:SIGNALduino_un:

README.adoc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,8 @@ Die SIGNALDuino-Firmware (Microcontroller-Code) wird in einem separaten Reposito
4444
====
4545
Decodierte Nachrichten werden als JSON-Objekte publiziert. Die Protokoll-Metadaten sind nun im Feld `protocol` enthalten, das `data`-Feld enthält die reinen, von der Protokoll-Preamble bereinigten Nutzdaten, und das `raw`-Feld enthält die unveränderte, rohe Nachrichtenzeichenkette der Firmware.
4646
47-
[source,json]
48-
----
49-
{
50-
"data": "30E0A1AA4241DE6C000200000BC5",
51-
"protocol_id": "125",
52-
"raw": "MS;P0=-32001;P1=488;D=0101;CP=1;R=48;",
53-
"metadata": {
54-
"rssi": -74,
55-
"freq_afc": 123
56-
},
57-
"protocol": {
58-
"name": "WH31",
59-
"id": "125",
60-
"preamble": "W125#",
61-
"format": "2-FSK",
62-
"clock": 17257
63-
}
64-
}
65-
----
66-
====
47+
include::./docs/architecture/snippets/json_output_schema_example.adoc[]
48+
6749
6850
== Demo
6951

docs/architecture/decisions/ADR-006-json-output-schema.adoc

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Die Umbenennung des Nutzdatenfeldes von `payload` zu `data` sowie die Einführun
2121

2222
| Feld | Typ | Beschreibung |
2323
|---|---|---|
24-
| `protocol_id` | `str` | Die numerische oder alphanumerische ID des erkannten Protokolls. |
2524
| `data` | `str` | Die bereinigten Nutzdaten **ohne** Preamble und Postamble (ersetzt `payload`). |
2625
| `raw` | `str` | Die ursprüngliche, unveränderte Nachricht vom Signalduino (z.B. `MU;...`). |
2726
| `protocol` | `dict` | Strukturierte Metadaten des Protokolls. |
@@ -34,25 +33,7 @@ Die Umbenennung des Nutzdatenfeldes von `payload` zu `data` sowie die Einführun
3433

3534
=== Beispiel für die Datenstruktur (Präzisiert durch ADR-007)
3635

37-
[source,json]
38-
----
39-
{
40-
"data": "30E0A1AA4241DE6C000200000BC5",
41-
"raw": "MC;LL=-1017;LH=932;...",
42-
"protocol_id": "125",
43-
"metadata": {
44-
"rssi": -74,
45-
"freq_afc": 123
46-
},
47-
"protocol": {
48-
"name": "WH31",
49-
"id": "125",
50-
"preamble": "W125#",
51-
"format": "2-FSK",
52-
"clock": 17257
53-
}
54-
}
55-
----
36+
include::../snippets/json_output_schema_example.adoc[]
5637

5738
== Konsequenzen
5839
**Positiv:**

docs/architecture/decisions/ADR-007-data-and-raw-fields.adoc

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,7 @@ Wir passen das in ADR-006 definierte Schema wie folgt an:
2727
2828
=== Aktualisierte Datenstruktur
2929

30-
[source,json]
31-
----
32-
{
33-
"data": "30E0A1AA4241DE6C000200000BC5", // Ehemals "payload"
34-
"raw": "MC;LL=-1017;LH=932;...", // Der originale Input-String
35-
"protocol_id": "125",
36-
"metadata": {
37-
"rssi": -74,
38-
"freq_afc": 123
39-
},
40-
"protocol": {
41-
"name": "WH31",
42-
"id": "125",
43-
"preamble": "W125#",
44-
"format": "2-FSK",
45-
"clock": 17257
46-
}
47-
}
48-
----
30+
include::../snippets/json_output_schema_example.adoc[]
4931

5032
[#adr-consequences]
5133
== Konsequenzen
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[source,json]
2+
----
3+
{
4+
"data": "30E0A1AA4241DE6C000200000BC5",
5+
"raw": "MC;LL=-1017;LH=932;...",
6+
"metadata": {
7+
"rssi": -74,
8+
"freq_afc": 123
9+
},
10+
"protocol": {
11+
"name": "WH31",
12+
"id": "125",
13+
"preamble": "W125#",
14+
"format": "2-FSK",
15+
"clock": 17257
16+
}
17+
}
18+
----

signalduino/mqtt.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ def _raw_frame_to_dict(raw_frame: RawFrame) -> dict:
253253
preamble = ""
254254
if self._protocol_handler:
255255
try:
256-
# check_property returns the value or default
257-
preamble = self._protocol_handler.check_property(message.protocol_id, 'preamble', '')
256+
protocol_id = message.protocol.get('id')
257+
if protocol_id:
258+
# check_property returns the value or default
259+
preamble = self._protocol_handler.check_property(protocol_id, 'preamble', '')
258260
except Exception as e:
259261
self.logger.warning("Failed to get preamble: %s", e)
260262

@@ -289,6 +291,7 @@ async def publish(self, message: DecodedMessage) -> None:
289291
topic = f"{self.base_topic}/state/messages"
290292
payload = self._message_to_json(message)
291293
await self.client.publish(topic, payload)
292-
self.logger.debug("Published message for protocol %s to %s", message.protocol_id, topic)
294+
protocol_id = message.protocol.get('id', 'N/A')
295+
self.logger.debug("Published message for protocol %s to %s", protocol_id, topic)
293296
except Exception:
294297
self.logger.error("Failed to publish message", exc_info=True)

signalduino/parser/mc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def parse(self, frame: RawFrame) -> Iterable[DecodedMessage]:
105105
payload = raw_payload[preamble_len:]
106106

107107
yield DecodedMessage(
108-
protocol_id=protocol_id_str,
109108
data=payload,
110109
raw=frame.line,
111110
metadata=decoded.get("meta", {}),

signalduino/parser/mn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ def parse(self, frame: RawFrame) -> Iterable[DecodedMessage]:
179179
self.logger.info("MN Parse: Decoded matched MN Protocol id %s dmsg=%s", pid, final_payload)
180180

181181
yield DecodedMessage(
182-
protocol_id=str(pid),
183182
data=final_payload,
184183
raw=frame.line,
185184
metadata={
@@ -188,4 +187,5 @@ def parse(self, frame: RawFrame) -> Iterable[DecodedMessage]:
188187
"modulation": modulation,
189188
"rfmode": proto_rfmode
190189
},
190+
protocol={"id": str(pid), "model": "MN"}
191191
)

signalduino/parser/ms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def parse(self, frame: RawFrame) -> Iterable[DecodedMessage]:
5959
continue
6060

6161
yield DecodedMessage(
62-
protocol_id=str(decoded["protocol_id"]),
6362
data=str(decoded.get("payload", "")),
6463
raw=frame.line,
6564
metadata=decoded.get("meta", {}),
65+
protocol={"id": str(decoded["protocol_id"]), "model": "MS"},
6666
)
6767

6868
def _parse_to_dict(self, line: str) -> Dict[str, Any]:

0 commit comments

Comments
 (0)