Skip to content

Instantly share code, notes, and snippets.

@niondir
Created April 12, 2018 08:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niondir/7fe2da1924de8525fe47cfa4efe9a5a9 to your computer and use it in GitHub Desktop.
Save niondir/7fe2da1924de8525fe47cfa4efe9a5a9 to your computer and use it in GitHub Desktop.
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