Skip to content

Commit

Permalink
mission create notification
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Mar 17, 2024
1 parent 4a94d23 commit a315904
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 23 deletions.
26 changes: 13 additions & 13 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/kdudkov/goatak/internal/pm"
"github.com/kdudkov/goatak/internal/repository"
"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/cotproto"
"github.com/kdudkov/goatak/pkg/model"
"github.com/kdudkov/goatak/pkg/tlsutil"
)
Expand Down Expand Up @@ -346,13 +345,13 @@ func (app *App) getTLSConfig() *tls.Config {
panic(err)
}

tlsCert := tls.Certificate{ //nolint:exhaustruct
tlsCert := tls.Certificate{ //nolint:exhaustruct,typeassert
Certificate: [][]byte{cert.Raw},
PrivateKey: key.(crypto.PrivateKey),
Leaf: cert,
}

return &tls.Config{Certificates: []tls.Certificate{tlsCert}, InsecureSkipVerify: true}
return &tls.Config{Certificates: []tls.Certificate{tlsCert}, InsecureSkipVerify: true} //nolint:exhaustruct
}

func (app *App) MessageProcessor() {
Expand All @@ -371,6 +370,7 @@ func (app *App) MessageProcessor() {
func (app *App) route(msg *cot.CotMessage) {
if missions := msg.GetDetail().GetDestMission(); len(missions) > 0 {
app.logger.Debug(fmt.Sprintf("point %s %s: missions: %s", msg.GetUID(), msg.GetCallsign(), strings.Join(missions, ",")))

for _, missionName := range missions {
app.processMissionPoint(missionName, msg)
}
Expand All @@ -380,13 +380,13 @@ func (app *App) route(msg *cot.CotMessage) {

if dest := msg.GetDetail().GetDestCallsign(); len(dest) > 0 {
for _, s := range dest {
app.SendToCallsign(s, msg)
app.sendToCallsign(s, msg)
}

return
}

app.SendBroadcast(msg)
app.sendBroadcast(msg)
}

func (app *App) processMissionPoint(missionName string, msg *cot.CotMessage) {
Expand All @@ -407,18 +407,18 @@ func (app *App) processMissionPoint(missionName string, msg *cot.CotMessage) {
}

if change != nil {
app.NotifyMissionSubscribers(m, change)
app.notifyMissionSubscribers(m, change)
}
}

func (app *App) NotifyMissionSubscribers(mission *im.Mission, c *im.Change) {
func (app *App) notifyMissionSubscribers(mission *im.Mission, c *im.Change) {
if mission == nil || c == nil {
return
}

msg := im.MissionChangePountMsg(mission.Name, c)
msg := im.MissionChangeNotificationMsg(mission.Name, mission.Scope, c)
for _, uid := range app.missions.GetSubscribers(mission.ID) {
app.SendToUID(uid, msg)
app.sendToUID(uid, msg)
}
}

Expand Down Expand Up @@ -457,7 +457,7 @@ func (app *App) cleanOldUnits() {
}
}

func (app *App) SendBroadcast(msg *cot.CotMessage) {
func (app *App) sendBroadcast(msg *cot.CotMessage) {
app.ForAllClients(func(ch client.ClientHandler) bool {
if ch.GetName() != msg.From {
if err := ch.SendMsg(msg); err != nil {
Expand All @@ -469,7 +469,7 @@ func (app *App) SendBroadcast(msg *cot.CotMessage) {
})
}

func (app *App) SendToCallsign(callsign string, msg *cot.CotMessage) {
func (app *App) sendToCallsign(callsign string, msg *cot.CotMessage) {
app.ForAllClients(func(ch client.ClientHandler) bool {
for _, c := range ch.GetUids() {
if c == callsign {
Expand All @@ -483,10 +483,10 @@ func (app *App) SendToCallsign(callsign string, msg *cot.CotMessage) {
})
}

func (app *App) SendToUID(uid string, msg *cotproto.TakMessage) {
func (app *App) sendToUID(uid string, msg *cot.CotMessage) {
app.ForAllClients(func(ch client.ClientHandler) bool {
if ch.HasUID(uid) {
if err := ch.SendCot(msg); err != nil {
if err := ch.SendMsg(msg); err != nil {
app.logger.Error("error", "error", err)
}
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/goatak_server/mission_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,17 @@ func getMissionPutHandler(app *App) air.Handler {

if err := app.missions.PutMission(m); err != nil {
res.Status = http.StatusConflict
app.logger.Warn("mission add error", "error", err.Error())

return res.WriteString(err.Error())
}

res.Status = http.StatusCreated

if !m.InviteOnly {
app.NewCotMessage(model.MissionCreateNotificationMsg(m))
}

return res.WriteJSON(makeAnswer(missionType, []*model.MissionDTO{model.ToMissionDTO(m, app.packageManager, true)}))
}
}
Expand Down Expand Up @@ -478,7 +484,7 @@ func getMissionContentDeleteHandler(app *App) air.Handler {
if uid := getStringParam(req, "uid"); uid != "" {
change := app.missions.DeleteMissionPoint(mission.ID, uid, author)

app.NotifyMissionSubscribers(mission, change)
app.notifyMissionSubscribers(mission, change)
}

if hash := getStringParam(req, "hash"); hash != "" {
Expand Down Expand Up @@ -508,6 +514,7 @@ func getInvitePutHandler(app *App) air.Handler {
if typ != "clientUid" {
app.logger.Warn(fmt.Sprintf("we do not support invitation with type %s now", typ))
res.Status = http.StatusBadRequest

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/goatak_server/tak_ws/tak_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (w *WsClientHandler) GetUser() *imodel.User {
return w.user
}

func (w *WsClientHandler) CanSeeScope(scope string) bool {
return true
}

func (w *WsClientHandler) GetVersion() int32 {
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/webclient/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (app *App) InitMessageProcessors() {
// u-r-b-c-c R&b - Circle
// u-d-c-c Drawing Shapes – Circle
// u-d-r Drawing Shapes – Rectangle
// u-d-f Drawing Shapes - Free Form
// u-d-f Drawing Shapes - Line & Polygon
// u-d-c-e Drawing Shapes – Ellipse
}

Expand Down
11 changes: 8 additions & 3 deletions internal/client/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/spf13/viper"
"io"
"log/slog"
"net"
Expand All @@ -14,6 +13,8 @@ import (
"sync/atomic"
"time"

"github.com/spf13/viper"

"github.com/kdudkov/goatak/internal/model"
"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/cotproto"
Expand Down Expand Up @@ -41,8 +42,8 @@ type ClientHandler interface {
GetUser() *model.User
GetVersion() int32
SendMsg(msg *cot.CotMessage) error
SendCot(msg *cotproto.TakMessage) error
GetLastSeen() *time.Time
CanSeeScope(scope string) bool
}

type ConnClientHandler struct {
Expand Down Expand Up @@ -96,6 +97,10 @@ func (h *ConnClientHandler) GetName() string {
return h.addr
}

func (h *ConnClientHandler) CanSeeScope(scope string) bool {
return h.user.CanSeeScope(scope)
}

func (h *ConnClientHandler) GetUser() *model.User {
return h.user
}
Expand Down Expand Up @@ -436,7 +441,7 @@ func (h *ConnClientHandler) sendEvent(evt *cot.Event) error {
}

func (h *ConnClientHandler) SendMsg(msg *cot.CotMessage) error {
if h.GetUser().CanSeeScope(msg.Scope) {
if h.CanSeeScope(msg.Scope) {
return h.SendCot(msg.GetTakMessage())
}

Expand Down
27 changes: 24 additions & 3 deletions internal/model/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"github.com/kdudkov/goatak/pkg/cotproto"
)

func MissionChangePountMsg(missionName string, c *Change) *cotproto.TakMessage {
msg := cot.BasicMsg("t-x-m-c", uuid.NewString(), time.Second*5)
const missionNotificationStale = time.Second * 5

func MissionChangeNotificationMsg(missionName string, scope string, c *Change) *cot.CotMessage {
msg := cot.BasicMsg("t-x-m-c", uuid.NewString(), missionNotificationStale)
msg.CotEvent.How = "h-g-i-g-o"

xd := cot.NewXMLDetails()
Expand All @@ -27,5 +29,24 @@ func MissionChangePountMsg(missionName string, c *Change) *cotproto.TakMessage {

msg.CotEvent.Detail = &cotproto.Detail{XmlDetail: xd.AsXMLString()}

return msg
return &cot.CotMessage{From: "local", TakMessage: msg, Detail: xd, Scope: scope}
}

func MissionCreateNotificationMsg(m *Mission) *cot.CotMessage {
msg := cot.BasicMsg("t-x-m-n", uuid.NewString(), missionNotificationStale)
msg.CotEvent.How = "h-g-i-g-o"

xd := cot.NewXMLDetails()

params := map[string]string{"type": "CREATE", "name": m.Name, "creatorUid": m.CreatorUID}

if m.Tool != "" {
params["tool"] = m.Tool
}

xd.AddChild("mission", params, "")

msg.CotEvent.Detail = &cotproto.Detail{XmlDetail: xd.AsXMLString()}

return &cot.CotMessage{From: "local", TakMessage: msg, Detail: xd, Scope: m.Scope}
}
14 changes: 12 additions & 2 deletions pkg/cot/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package cot
import "strings"

var messages = map[string]string{
"b-a-g": "Alert: GeoFenceBreach",
"b-a-o-can": "Alert: Cancel",
"b-a-o-opn": "Alert: TroopsInContact",
"b-a-o-pan": "Alert: RingTheBell",
"b-a-o-tbl": "Alert: 911",
"b-d": "Detection",
"b-d-a": "Detection/Acoustic",
"b-d-a-c": "Detection/Acoustic/Cyclostationary",
Expand Down Expand Up @@ -57,8 +62,10 @@ var messages = map[string]string{
"b-r": "Report",
"b-r-f-h-c": "Casevac",
"b-t-f": "Chat message",
"b-t-f-r": "Chat read notification",
"b-t-f-d": "Chat delivery notification",
"b-t-f-d": "Chat delivery receipt",
"b-t-f-r": "Chat read receipt",
"b-t-f-p": "Chat pending receipt",
"b-t-f-s": "Chat delivery failure",
"b-w": "Weather",
"c": "Capability",
"r": "Area restrictions",
Expand All @@ -76,6 +83,9 @@ var messages = map[string]string{
"t-x-c-t": "Ping",
"t-x-c-t-r": "Pong",
"t-x-d-d": "Delete by link",
"t-x-m-n": "Mission Creation Notification",
"t-x-m-c": "Mission Change Notification",
"t-x-m-c-l": "Mission Log Added Notification",
"u-d": "Drawing",
"u-d-c-c": "Drawing Shapes – Circle",
"u-d-c-e": "Drawing Shapes – Ellipse",
Expand Down

0 comments on commit a315904

Please sign in to comment.