136 lines
3.6 KiB
Go
136 lines
3.6 KiB
Go
package services
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
appConfig "github.com/xmpploadtesting/config"
|
|
"gosrc.io/xmpp"
|
|
"gosrc.io/xmpp/stanza"
|
|
)
|
|
|
|
const rolingStockPresenceMessage = `ROLLING STOCK PRESENCE MESSAGE\n +{
|
|
"serial": "79101X02Y0029450001CJ2000",
|
|
"model": "VEN032FSNWM00",
|
|
"displayOrientation": "left",
|
|
"macAddress": "1c:c0:e1:42:5a:c4",
|
|
"node": "v8.16.2",
|
|
"nodeJS": "v8.16.2",
|
|
"fbi": "2.10-2ubuntu1",
|
|
"libgif7": "5.1.4-2ubuntu0.1",
|
|
"bsEdgeServer": "13.3.9-1",
|
|
"initopciVersion": "0.4~24",
|
|
"bspInstallVersion": "1.0~36",
|
|
"awsConfigsVersion": "2021.21.26-e7c5738",
|
|
"outGuard": "2021.36.23-dev-028972a",
|
|
"val": "1.1.11~129",
|
|
"broadSignPlayer": "13.3.9-1",
|
|
"displayUnitExternalID": "L-0141-M7-7281-Right-3SM-G02-02-79101X02Y0020040003EJ2001",
|
|
"privateIPaddress": "10.1.1.94",
|
|
"linux": {
|
|
"os": "Ubuntu 18.04.2 LTS",
|
|
"kernel": "4.18.20+10+opci",
|
|
"architecture": "x86-64"
|
|
},
|
|
"digiID": "digi-00000000-00000000-0040FFFF-FF801D30"
|
|
}
|
|
`
|
|
|
|
// XMPPService - struct containing
|
|
type XMPPService struct {
|
|
Router *xmpp.Router
|
|
XMPPClients []XMPPClient
|
|
}
|
|
|
|
// XMPPClient - struct containing client and client configuration
|
|
type XMPPClient struct {
|
|
Config xmpp.Config
|
|
Client *xmpp.Client
|
|
}
|
|
|
|
// Instance - get instance of xmpp service
|
|
func Instance() *XMPPService {
|
|
return &xmppService
|
|
}
|
|
|
|
var xmppService XMPPService
|
|
|
|
// Init Initialise XMPP servie, and router
|
|
func Init() {
|
|
// TODO load configs and init clients from the config file
|
|
xmppService = XMPPService{
|
|
Router: xmpp.NewRouter(),
|
|
}
|
|
xmppService.Router.HandleFunc("message", handleMessage)
|
|
|
|
xmppService.XMPPClients = make([]XMPPClient, 0)
|
|
xmppClient := XMPPClient{
|
|
Config: xmpp.Config{
|
|
|
|
TransportConfiguration: xmpp.TransportConfiguration{
|
|
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
|
Domain: appConfig.AppConfig.Service.Domain,
|
|
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
|
},
|
|
Jid: "admin@localhost",
|
|
Credential: xmpp.Password("openadmin123"),
|
|
StreamLogger: os.Stdout,
|
|
Insecure: true,
|
|
},
|
|
}
|
|
|
|
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
|
|
|
// Client connection
|
|
if err := client.Connect(); err != nil {
|
|
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
|
|
|
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
|
}
|
|
xmppClient.Client = client
|
|
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
|
|
|
}
|
|
|
|
// SendOnlinePresenceStanza - send online presence stnza with data, to server
|
|
func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
|
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
|
onlinePresencePacket.Status = rolingStockPresenceMessage
|
|
|
|
//TODO All clients should send messages at once
|
|
for {
|
|
|
|
err := client.Send(onlinePresencePacket)
|
|
log.Printf("Sending online presence stanza: %v", err)
|
|
// Delay before sending another message
|
|
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
|
|
}
|
|
}
|
|
|
|
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
|
log.Printf("Handle message")
|
|
msg, ok := p.(stanza.Message)
|
|
if !ok {
|
|
log.Printf(" message not OK")
|
|
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
|
return
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
|
|
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
|
err := s.Send(reply)
|
|
log.Printf("Error sending message %v", err)
|
|
}
|
|
|
|
func errorHandler(err error) {
|
|
fmt.Println(err.Error())
|
|
}
|