Added support for json commands

This commit is contained in:
2021-09-24 14:57:03 +02:00
parent 56d3ec3f90
commit b35cb62e1e

View File

@@ -44,6 +44,61 @@ const rolingStockPresenceMessage = `ROLLING STOCK PRESENCE MESSAGE
"digiID": "digi-00000000-00000000-0040FFFF-FF801D30"
}`
const commandMessageExample = `{"device_jid":"device1@rolling-stock-sandbox2-openfire.onsmartengineering.com",
"message_id":"df26acfa-bdd4-40f4-a980-0638bbdb096e-1632473349400",
"api_version":1,
"from_id":"agent@rolling-stock-sandbox2-openfire.onsmartengineering.com",
"commands":[{"name":"demo_command","uuid":"0388ffd0-3f69-4600-b545-ed19f6378a0a",
"params":{"user_id":"21","api_version":1,"command":"demo_command","player_id":"8","arg":"senad.uka@toptal.com help"}}]}`
const commandResponse = `{"api_version": 1,
"message_id": "766318f3-d1d3-4359-bf2d-e69d508733f4-1632410186320",
"responses": [{"params": {"message": "Unsupported command. Type help for the list of supported commands", "response_code": "SUCCESS"},
"uuid": "5a041416-4184-4529-a7d3-7bfdd9fc1739",
"name": "demo_command"}],
"player_id": "3"}`
// CommandMessage -
type CommandMessage struct {
APIVersion int `json:"api_version"`
MessageID string `json:"message_id"`
Commands []Command `json:"commands"`
}
// Command -
type Command struct {
Params CommandParams `json:"params"`
UUID string `json:"uuid"`
Name string `json:"name"`
}
// CommandParams -
type CommandParams struct {
Command string `json:"message"`
PlayerID string `json:"player_id"`
}
// CommandMessageResponse -
type CommandMessageResponse struct {
APIVersion int `json:"api_version"`
MessageID string `json:"message_id"`
Responses []Response `json:"responses"`
PlayerID string `json:"player_id"`
}
// Response -
type Response struct {
Params ResponseParams `json:"params"`
UUID string `json:"uuid"`
Name string `json:"name"`
}
// ResponseParams -
type ResponseParams struct {
Message string `json:"message"`
ResponseCode string `json:"response_code"`
}
// XMPPService - struct containing
type XMPPService struct {
Router *xmpp.Router
@@ -197,6 +252,13 @@ func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
func handleMessage(s xmpp.Sender, p stanza.Packet) {
commandMessageResponse := CommandMessageResponse{}
err := json.Unmarshal([]byte(commandResponse), &commandMessageResponse)
if err != nil {
log.Printf("unable to unmarshal Command Meassage Response: %v", commandResponse)
}
msg, ok := p.(stanza.Message)
if !ok {
log.Printf(" message not OK")
@@ -209,14 +271,35 @@ func handleMessage(s xmpp.Sender, p stanza.Packet) {
return
}
commandMessage := CommandMessage{}
err = json.Unmarshal([]byte(msg.Body), &commandMessage)
if err != nil {
log.Printf("<%v> unable to unmarshal Command Meassage: %v", msg.To, msg.Body)
}
if len(commandMessageResponse.Responses) > 0 || len(commandMessage.Commands) > 0 {
commandMessageResponse.Responses[0].UUID = commandMessage.Commands[0].UUID
commandMessageResponse.PlayerID = commandMessage.Commands[0].Params.PlayerID
commandMessageResponse.MessageID = commandMessage.MessageID
} else {
log.Printf("<%v> There are no commands inside command message : %v", msg.To, commandMessage)
}
m, err := json.Marshal(commandMessageResponse)
if err != nil {
log.Printf("<%v> unable to marshal Command Meassage response : %v", msg.To, commandMessageResponse)
}
log.Printf("<%v> received following message: %v", msg.To, msg.Body)
commandReplyDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.CommandReplyDelay))
log.Printf("Waiting FOR: %v", commandReplyDelay*1000000000)
// Delay For two seccond to allow all clients to connect
time.Sleep(time.Duration(commandReplyDelay * 1000000000))
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: `{"status": "OK"}`}
err := s.Send(reply)
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: string(m)}
err = s.Send(reply)
if err != nil {
log.Printf("Error sending to %v message %v", msg.From, err)