Alter 8 files
Update `config.go` Add `feed.go` Add `messageTypes.go` Add `process.go` Update `main.go` Update `structures.go` Update `go.mod` Update `go.sum`
This commit is contained in:
parent
1049f53f39
commit
70d0fdc809
8 changed files with 237 additions and 1 deletions
|
@ -9,6 +9,9 @@ var (
|
|||
RedisHostname = os.Getenv("TRAINS_REDIS_HOST")
|
||||
RedisUsername = os.Getenv("TRAINS_REDIS_USER")
|
||||
RedisPassword = os.Getenv("TRAINS_REDIS_PASS")
|
||||
|
||||
NationalRailUsername = os.Getenv("TRAINS_NR_USER")
|
||||
NationalRailPassword = os.Getenv("TRAINS_NR_PASS")
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
|
69
feedprocessor/feed/feed.go
Normal file
69
feedprocessor/feed/feed.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package feed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/config"
|
||||
"github.com/go-stomp/stomp/v3"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
log.Info().Msg("starting feed processor")
|
||||
|
||||
if config.NationalRailUsername == "" || config.NationalRailPassword == "" {
|
||||
log.Fatal().Msg("missing National Rail login information")
|
||||
}
|
||||
|
||||
// Connect to Redis
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: config.RedisHostname,
|
||||
Username: config.RedisUsername,
|
||||
Password: config.RedisPassword,
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
_, err := client.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to PING Redis")
|
||||
}
|
||||
|
||||
// Setup cancellation thing
|
||||
stopSig := make(chan os.Signal, 1)
|
||||
signal.Notify(stopSig, syscall.SIGINT)
|
||||
|
||||
log.Debug().Msg("attempting connection")
|
||||
|
||||
log.Debug().Str("user", config.NationalRailUsername).Str("pass", config.NationalRailPassword).Send()
|
||||
|
||||
stompConn, err := stomp.Dial("tcp", "datafeeds.networkrail.co.uk:61618", stomp.ConnOpt.Login(config.NationalRailUsername, config.NationalRailPassword))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug().Msg("connected")
|
||||
|
||||
// Setup watcher
|
||||
|
||||
subscription, err := stompConn.Subscribe("/topic/TRAIN_MVT_ALL_TOC", stomp.AckAuto)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case msg := <-subscription.C:
|
||||
log.Debug().Bytes("dat", msg.Body).Send()
|
||||
case <-stopSig:
|
||||
if err := subscription.Unsubscribe(); err != nil {
|
||||
_ = stompConn.MustDisconnect()
|
||||
return err
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return stompConn.Disconnect()
|
||||
}
|
11
feedprocessor/feed/messageTypes.go
Normal file
11
feedprocessor/feed/messageTypes.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package feed
|
||||
|
||||
const (
|
||||
MessageTypeActivation = "0001"
|
||||
MessageTypeCancellation = "0002"
|
||||
MessageTypeMovement = "0003"
|
||||
MessageTypeReinstatement = "0005"
|
||||
MessageTypeChangeOfOrigin = "0006"
|
||||
MessageTypeChangeOfIdentity = "0007"
|
||||
MessageTypeChangeOfLocation = "0008"
|
||||
)
|
118
feedprocessor/feed/process.go
Normal file
118
feedprocessor/feed/process.go
Normal file
|
@ -0,0 +1,118 @@
|
|||
package feed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func ProcessMessage(redisClient *redis.Client, rawMessage []byte) error {
|
||||
hab := new(headerAndBody)
|
||||
if err := json.Unmarshal(rawMessage, hab); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch hab.Header.MsgType {
|
||||
case MessageTypeActivation:
|
||||
return processActivation(redisClient, hab.Body)
|
||||
case MessageTypeMovement:
|
||||
return processMovement(redisClient, hab.Body)
|
||||
case MessageTypeCancellation:
|
||||
return processCancellation(redisClient, hab.Body)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func processActivation(redisClient *redis.Client, body json.RawMessage) error {
|
||||
am := new(activationMessage)
|
||||
if err := json.Unmarshal(body, am); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if am.ScheduleSource == "V" {
|
||||
// This is a VSTP schedule - cba to deal with that.
|
||||
return nil
|
||||
}
|
||||
|
||||
ts := new(dbTrainStatus)
|
||||
ts.OriginTimestamp = am.TpOriginTimestamp
|
||||
ts.ScheduleID = am.TrainUid
|
||||
|
||||
res := redisClient.HMSet(context.Background(), fmt.Sprintf("position:%s", am.TrainId), ts.ToMap())
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func processMovement(redisClient *redis.Client, body json.RawMessage) error {
|
||||
mm := new(movementMessage)
|
||||
if err := json.Unmarshal(body, mm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if mm.CorrectionInd == "true" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func processCancellation(redisClient *redis.Client, body json.RawMessage) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type headerAndBody struct {
|
||||
Header struct {
|
||||
MsgType string `json:"msg_type"`
|
||||
OriginalDataSource string `json:"original_data_source"`
|
||||
MsgQueueTimestamp string `json:"msg_queue_timestamp"`
|
||||
SourceSystemId string `json:"source_system_id"`
|
||||
} `json:"header"`
|
||||
Body json.RawMessage `json:"body"`
|
||||
}
|
||||
|
||||
type activationMessage struct {
|
||||
ScheduleSource string `json:"schedule_source"`
|
||||
ScheduleEndDate string `json:"schedule_end_date"`
|
||||
TrainId string `json:"train_id"`
|
||||
TpOriginTimestamp string `json:"tp_origin_timestamp"`
|
||||
OriginDepTimestamp string `json:"origin_dep_timestamp"`
|
||||
TrainServiceCode string `json:"train_service_code"`
|
||||
TocId string `json:"toc_id"`
|
||||
TrainUid string `json:"train_uid"`
|
||||
TrainCallMode string `json:"train_call_mode"`
|
||||
ScheduleType string `json:"schedule_type"`
|
||||
ScheduleWttId string `json:"schedule_wtt_id"`
|
||||
ScheduleStartDate string `json:"schedule_start_date"`
|
||||
}
|
||||
|
||||
type movementMessage struct {
|
||||
EventType string `json:"event_type"`
|
||||
CorrectionInd string `json:"correction_ind"`
|
||||
TrainTerminated string `json:"train_terminated"`
|
||||
TrainId string `json:"train_id"`
|
||||
VariationStatus string `json:"variation_status"`
|
||||
LocStanox string `json:"loc_stanox"`
|
||||
PlannedEventType string `json:"planned_event_type"`
|
||||
NextReportStanox string `json:"next_report_stanox"`
|
||||
}
|
||||
|
||||
type dbTrainStatus struct {
|
||||
OriginTimestamp string
|
||||
ScheduleID string // aka train uid
|
||||
CurrentTIPLOC string
|
||||
NextTIPLOC string
|
||||
PreviousTIPLOC string
|
||||
Running bool
|
||||
}
|
||||
|
||||
func (dts *dbTrainStatus) ToMap() map[string]any {
|
||||
return map[string]any{
|
||||
"originTimestamp": dts.OriginTimestamp,
|
||||
"scheduleID": dts.ScheduleID,
|
||||
"currentTIPLOC": dts.CurrentTIPLOC,
|
||||
"nextTIPLOC": dts.NextTIPLOC,
|
||||
"previousTIPLOC": dts.PreviousTIPLOC,
|
||||
"runningg": dts.Running,
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/feed"
|
||||
"git.tdpain.net/codemicro/hacknotts23/feedprocessor/schedule"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
|
@ -16,7 +17,9 @@ func main() {
|
|||
do := os.Args[1]
|
||||
|
||||
if do == "feed" {
|
||||
// TODO
|
||||
if err := feed.Run(); err != nil {
|
||||
log.Fatal().Err(err).Str("location", "schedule-ingest").Send()
|
||||
}
|
||||
} else if do == "schedule-ingest" {
|
||||
if err := schedule.Run(); err != nil {
|
||||
log.Fatal().Err(err).Str("location", "schedule-ingest").Send()
|
||||
|
|
|
@ -100,6 +100,7 @@ type Record struct {
|
|||
|
||||
type TIPLOC struct {
|
||||
Code string `json:"tiploc_code"`
|
||||
Stanox string `json:"stanox"`
|
||||
Description string `json:"tps_description"`
|
||||
HumanDescription string `json:"description"`
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -11,6 +11,7 @@ require (
|
|||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/go-stomp/stomp/v3 v3.0.5 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||
|
|
30
go.sum
30
go.sum
|
@ -6,7 +6,13 @@ github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/go-stomp/stomp/v3 v3.0.5 h1:yOORvXLqSu0qF4loJjfWrcVE1o0+9cFudclcP0an36Y=
|
||||
github.com/go-stomp/stomp/v3 v3.0.5/go.mod h1:ztzZej6T2W4Y6FlD+Tb5n7HQP3/O5UNQiuC169pIp10=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
|
@ -20,8 +26,32 @@ github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
|||
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
|
||||
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
|
Reference in a new issue