-
-
Save niondir/7fe2da1924de8525fe47cfa4efe9a5a9 to your computer and use it in GitHub Desktop.
Parser for wMBus messages send from the Lobaro LoRaWAN Bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// one cache per appId with max 9 messages | |
var msgCache = make(map[string][9]types.UplinkMessage, 0) | |
func ttnWmbusBridgeHandler(req *types.UplinkMessage) { | |
// Collect messages in memory until last piece and create a complete wmbus message | |
if req.FPort >= 11 && req.FPort <= 99 { | |
if _, ok := msgCache[req.AppID]; !ok { | |
msgCache[req.AppID] = [9]types.UplinkMessage{} | |
} | |
part := int(req.FPort / 10) | |
total := int(req.FPort % 10) | |
logrus.WithField("part", part).WithField("total", total).Info("Got wmbus message part") | |
cache := msgCache[req.AppID] | |
cache[part-1] = *req | |
msgCache[req.AppID] = cache | |
if part == total { | |
logrus.Info("Got last piece, build wmbus message") | |
cache := msgCache[req.AppID] | |
delete(msgCache, req.AppID) // Free some memory | |
expectedFCnt := cache[0].FCnt | |
wmbusMsg := make([]byte, 0) | |
for i := 0; i < total; i++ { | |
if cache[i].FCnt != expectedFCnt { | |
logrus.Info("Failed to create wmbus message, bad FCnt") | |
return | |
} | |
expectedFCnt++ | |
wmbusMsg = append(wmbusMsg, cache[i].PayloadRaw...) | |
} | |
// Done got full wmbusMsg | |
wmbusMsgBase64 := base64.StdEncoding.EncodeToString(wmbusMsg) | |
logrus.WithField("msgBase64", wmbusMsgBase64).WithField("parts", total).Info("Got full wmbus message") | |
// TODO: Do something with the data (wmbusMsgBase64) | |
} | |
} else { | |
delete(msgCache, req.AppID) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment