Skip to content

Commit

Permalink
Moving MQTT message format to JSON (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
winterMate authored Oct 1, 2019
1 parent c2bf746 commit 4725a00
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
20 changes: 13 additions & 7 deletions maas-server/events/event_handler_mqtt.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package events

import (
"encoding/json"
"log"
"strconv"
"time"

MQTT "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -34,8 +34,10 @@ func (eh *EventHandlerMqtt) ItemConsumed(userID uint32, username string, itemID
func (eh *EventHandlerMqtt) buildItemConsumedMessage(userID uint32, username string, itemID uint32, itemName string, itemCost int32, count uint32) string {
//TODO - shouldn't the username used instead of the user ID?
//TODO - the int casts are messy ... improve!
message := "matomat;item-consumed;" + strconv.Itoa(int(userID)) + ";" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(count)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
return message
//message := "matomat;item-consumed;" + strconv.Itoa(int(userID)) + ";" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(count)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
messageObject := NewItemConsumedMessage(time.Now().Unix(), userID, itemID, itemName, itemCost, count)
message, _ := json.Marshal(messageObject)
return string(message)
}

//SORRY adding the following is an evil hack, abusing the concept
Expand All @@ -48,8 +50,10 @@ func (eh *EventHandlerMqtt) TotalItemConsumedForUserChanged(userID uint32, usern
func (eh *EventHandlerMqtt) buildTotalItemConsumedForUserChanged(userID uint32, username string, itemID uint32, itemName string, itemCost int32, totalCount uint32) string {
//TODO - shouldn't the username used instead of the user ID?
//TODO - the int casts are messy ... improve!
message := "matomat;total-item-consumed-for-user-changed;" + strconv.Itoa(int(userID)) + ";" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(totalCount)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
return message
//message := "matomat;total-item-consumed-for-user-changed;" + strconv.Itoa(int(userID)) + ";" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(totalCount)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
messageObject := NewTotalItemConsumedForUserChangedMessage(time.Now().Unix(), userID, itemID, itemName, itemCost, totalCount)
message, _ := json.Marshal(messageObject)
return string(message)
}

//SORRY adding the following is an even more evil hack, abusing the concept even more
Expand All @@ -60,8 +64,10 @@ func (eh *EventHandlerMqtt) TotalItemConsumedChanged(itemID uint32, itemName str
}

func (eh *EventHandlerMqtt) buildTotalItemConsumedChanged(itemID uint32, itemName string, itemCost int32, totalCount uint32) string {
message := "matomat;total-items-consumed;" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(totalCount)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
return message
//message := "matomat;total-items-consumed;" + strconv.Itoa(int(itemID)) + ";" + itemName + ";" + strconv.Itoa(int(itemCost)) + ";" + strconv.Itoa(int(totalCount)) + ";" + strconv.FormatInt(time.Now().Unix(), 10) //TODO implement proper message format
messageObject := NewTotalItemConsumedChangedMessage(time.Now().Unix(), itemID, itemName, itemCost, totalCount)
message, _ := json.Marshal(messageObject)
return string(message)
}

func (eh *EventHandlerMqtt) publishMessage(message string) {
Expand Down
45 changes: 45 additions & 0 deletions maas-server/events/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package events

type ItemConsumedMessage struct {
EventSource string
EventName string
EventTime int64
UserID uint32
ItemID uint32
ItemName string
ItemCost int32
ItemCount uint32
}

type TotalItemConsumedForUserChangedMessage struct {
EventSource string
EventName string
EventTime int64
UserID uint32
ItemID uint32
ItemName string
ItemCost int32
TotalItemCount uint32
}

type TotalItemConsumedChangedMessage struct {
EventSource string
EventName string
EventTime int64
ItemID uint32
ItemName string
ItemCost int32
TotalItemCount uint32
}

func NewItemConsumedMessage(eventTime int64, userID uint32, itemID uint32, itemName string, itemCost int32, count uint32) *ItemConsumedMessage {
return &ItemConsumedMessage{EventSource: "matomat", EventName: "item-consumed", EventTime: eventTime, UserID: userID, ItemID: itemID, ItemName: itemName, ItemCost: itemCost, ItemCount: count}
}

func NewTotalItemConsumedForUserChangedMessage(eventTime int64, userID uint32, itemID uint32, itemName string, itemCost int32, totalCount uint32) *TotalItemConsumedForUserChangedMessage {
return &TotalItemConsumedForUserChangedMessage{EventSource: "matomat", EventName: "total-item-consumed-for-user-changed", EventTime: eventTime, UserID: userID, ItemID: itemID, ItemName: itemName, ItemCost: itemCost, TotalItemCount: totalCount}
}

func NewTotalItemConsumedChangedMessage(eventTime int64, itemID uint32, itemName string, itemCost int32, totalCount uint32) *TotalItemConsumedChangedMessage {
return &TotalItemConsumedChangedMessage{EventSource: "matomat", EventName: "total-items-consumed", EventTime: eventTime, ItemID: itemID, ItemName: itemName, ItemCost: itemCost, TotalItemCount: totalCount}
}

0 comments on commit 4725a00

Please sign in to comment.