Skip to content

Instantly share code, notes, and snippets.

@niondir
Created Apr 12, 2018
Embed
What would you like to do?
Parser for wMBus messages send from the Lobaro LoRaWAN Bridge
// 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