2024-08-04 00:55:28 -04:00
|
|
|
package nexus
|
|
|
|
|
|
|
|
import (
|
2024-08-06 11:19:30 -04:00
|
|
|
"context"
|
2024-08-16 14:54:50 -04:00
|
|
|
"encoding/json"
|
2024-08-06 11:19:30 -04:00
|
|
|
|
|
|
|
"dynatron.me/x/stillbox/pkg/calls"
|
2024-08-16 14:54:50 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/database"
|
2024-08-04 00:55:28 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/pb"
|
|
|
|
|
2024-08-18 08:44:44 -04:00
|
|
|
"github.com/jackc/pgx/v5"
|
2024-08-04 00:55:28 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-08-16 14:54:50 -04:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2024-08-04 00:55:28 -04:00
|
|
|
)
|
|
|
|
|
2024-08-06 11:19:30 -04:00
|
|
|
func (c *client) HandleCommand(ctx context.Context, cmd *pb.Command) {
|
2024-08-16 14:54:50 -04:00
|
|
|
var err error
|
2024-08-04 00:55:28 -04:00
|
|
|
switch cc := cmd.Command.(type) {
|
|
|
|
case *pb.Command_LiveCommand:
|
2024-08-16 14:54:50 -04:00
|
|
|
err = c.Live(ctx, cc.LiveCommand)
|
2024-08-04 00:55:28 -04:00
|
|
|
case *pb.Command_SearchCommand:
|
2024-08-16 14:54:50 -04:00
|
|
|
case *pb.Command_TgCommand:
|
|
|
|
err = c.Talkgroup(ctx, cc.TgCommand)
|
2024-08-04 00:55:28 -04:00
|
|
|
default:
|
|
|
|
log.Error().Msgf("unknown command %T", cmd)
|
|
|
|
}
|
2024-08-16 14:54:50 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.SendError(cmd, err)
|
|
|
|
}
|
2024-08-04 00:55:28 -04:00
|
|
|
}
|
|
|
|
|
2024-08-16 14:54:50 -04:00
|
|
|
func (c *client) SendError(cmd *pb.Command, err error) {
|
2024-08-06 11:19:30 -04:00
|
|
|
e := &pb.Message{
|
|
|
|
ToClientMessage: &pb.Message_Error{
|
|
|
|
Error: &pb.Error{
|
2024-08-16 14:54:50 -04:00
|
|
|
Error: err.Error(),
|
|
|
|
Command: cmd,
|
2024-08-06 11:19:30 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.Send(e)
|
|
|
|
}
|
|
|
|
|
2024-08-16 14:54:50 -04:00
|
|
|
func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error {
|
|
|
|
db := database.FromCtx(ctx)
|
2024-08-16 16:47:39 -04:00
|
|
|
tgi, err := db.GetTalkgroupWithLearned(ctx, int(tg.System), int(tg.Talkgroup))
|
2024-08-16 14:54:50 -04:00
|
|
|
if err != nil {
|
2024-08-18 08:44:44 -04:00
|
|
|
if err != pgx.ErrNoRows {
|
|
|
|
log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("get talkgroup fail")
|
|
|
|
}
|
2024-08-16 14:54:50 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var md *structpb.Struct
|
|
|
|
if len(tgi.Metadata) > 0 {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal(tgi.Metadata, m)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("unmarshal tg metadata")
|
|
|
|
}
|
|
|
|
md, err = structpb.NewStruct(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("new pb struct for tg metadata")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &pb.TalkgroupInfo{
|
|
|
|
Tg: tg,
|
|
|
|
Name: tgi.Name,
|
|
|
|
Group: tgi.TgGroup,
|
|
|
|
Frequency: tgi.Frequency,
|
|
|
|
Metadata: md,
|
2024-08-16 16:47:39 -04:00
|
|
|
Tags: tgi.Tags,
|
|
|
|
Learned: tgi.Learned,
|
2024-08-16 14:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Send(&pb.Message{
|
|
|
|
ToClientMessage: &pb.Message_TgInfo{
|
|
|
|
TgInfo: resp,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Live(ctx context.Context, cmd *pb.Live) error {
|
2024-08-06 11:19:30 -04:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if cmd.State != nil {
|
|
|
|
c.liveState = *cmd.State
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Filter != nil {
|
|
|
|
filter, err := calls.TalkgroupFilterFromPB(ctx, cmd.Filter)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("filter create failed")
|
2024-08-16 14:54:50 -04:00
|
|
|
return err
|
2024-08-06 11:19:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.filter = filter
|
2024-08-04 00:55:28 -04:00
|
|
|
}
|
2024-08-16 14:54:50 -04:00
|
|
|
|
|
|
|
return nil
|
2024-08-04 00:55:28 -04:00
|
|
|
}
|