If the payload types are distinguisable from each other (using the port number, payload length or some bit/byte inside the payload) you can build a handler with alternative function:
fun (Fields, <<16#0402:16, Temp:16/signed>>) -> Fields#{temp => Temp}; (Fields, <<16#0405:16, Level>>) -> Fields#{level => Level} end.
This is effectively two functions: The first part is used when the payload starts 0402, the second when it starts 0405.
You can have two or more parts. Even in your example, there are also two parts: the first is used when port is 24 and the payload matches what you specified, the second part is used otherwise.
Please note that the first match is selected, so e.g. this:
fun (Fields, <<Num1, Num2>>) -> Fields; (Fields, <<NumA, NumB>>) -> Fields end.
will never match the second part because it is indistinguishable from the first.
In your case, the last part of your string (Data/binary) is dangerous as it matches "any remaining bytes". It can easily match binaries that shouldn't be matched.
Regards,
Petr
______________________________________________________________
> Od: "Mr. PO" <Panul...@gmail.com>
> Komu: "LoRaWAN Server Users" <lorawan...@googlegroups.com>
> Datum: 05.03.2019 20:57
> Předmět: handling variable length of payload
>
fun(#{port := 24}=Fields, <<8#43:8, Channels:4, SSI:1, MBUS:1, UsrTrg:1, Rfu:1, Bat:8, Temp:8/signed, RSSI:8/signed, Dig1control:8, Dig1counter:32, Dig2control:8, Dig2counter:32>>) ->
Fields#{ sensor => 'digital', data => #{battery => Bat, temperature => Temp, rssi => RSSI, d1counter => Dig1counter, d2counter => Dig2counter}};
(Fields, <<8#4F:8, Channels:4, SSI:1, MBUS:1, UsrTrg:1, Rfu:1, Bat:8, Temp:8/signed, RSSI:8/signed, Dig1control:8, Dig1counter:32, Dig2control:8, Dig2counter:32, Ana1settings:8, Ana1float:32/float, Ana2settings:8, Ana2float:32/float >>) ->Fields#{ sensor => 'digital', data => #{battery => Bat, temperature => Temp, rssi => RSSI, d1counter => Dig1counter, d2counter => Dig2counter, ana1setting => Ana1settings, ana1value => Ana1float, ana2setting => Ana2settings, ana2value => Ana2float}};
(Fields, _) ->Fieldsend.
A good practice is to use your Erlang compiler to syntax check your functions. Just type ‘erl’ to start the command line and then copy paste your function there. If you get no error and the console prints something like ‘#Fun<erl_eval.12.118419387>’ then your function is syntactically correct.
Your function is incorrect because of “8#4F”-- “F” is not a valid digit in an octal number system.
Petr
--
You received this message because you are subscribed to the Google Groups "LoRaWAN Server Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lorawan-serve...@googlegroups.com.
To post to this group, send email to lorawan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lorawan-server/ff71df56-64a5-49b3-bf4a-bd21719dfc2a%40googlegroups.com.