stillbox/pkg/nexus/commands.go

124 lines
2.6 KiB
Go
Raw Normal View History

2024-08-04 00:55:28 -04:00
package nexus
import (
2024-08-06 11:19:30 -04:00
"context"
"dynatron.me/x/stillbox/pkg/calls"
2024-08-04 00:55:28 -04:00
"dynatron.me/x/stillbox/pkg/pb"
2024-11-03 09:45:51 -05:00
"dynatron.me/x/stillbox/pkg/talkgroups"
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-10-19 14:14:15 -04:00
type CommandIDKeyT int64
const CommandIDKey CommandIDKeyT = 0
func CtxWithCID(ctx context.Context, cmd *pb.Command) context.Context {
return context.WithValue(ctx, CommandIDKey, cmd.CommandId)
}
func CommandID(ctx context.Context) *int64 {
id, has := ctx.Value(CommandIDKey).(*int64)
if !has {
return nil
}
return id
}
2024-08-06 11:19:30 -04:00
func (c *client) HandleCommand(ctx context.Context, cmd *pb.Command) {
2024-10-19 14:14:15 -04:00
ctx = CtxWithCID(ctx, cmd)
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
},
},
}
2024-10-31 16:50:08 -04:00
_ = c.Send(e)
2024-08-06 11:19:30 -04:00
}
2024-08-16 14:54:50 -04:00
func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error {
2024-11-03 09:45:51 -05:00
tgi, err := talkgroups.StoreFrom(ctx).TG(ctx, talkgroups.TG(tg.System, tg.Talkgroup))
2024-08-16 14:54:50 -04:00
if err != nil {
2024-11-03 14:16:26 -05:00
if err != talkgroups.ErrNotFound {
2024-08-18 08:44:44 -04:00
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
2024-11-03 08:44:34 -05:00
if len(tgi.Talkgroup.Metadata) > 0 {
2024-11-13 09:24:11 -05:00
md, err = structpb.NewStruct(tgi.Talkgroup.Metadata)
2024-08-16 14:54:50 -04:00
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{
2024-08-20 08:17:02 -04:00
Tg: tg,
2024-11-03 08:44:34 -05:00
Name: tgi.Talkgroup.Name,
2024-11-17 21:46:10 -05:00
Group: tgi.Talkgroup.TGGroup,
2024-11-03 08:44:34 -05:00
Frequency: tgi.Talkgroup.Frequency,
2024-08-20 08:17:02 -04:00
Metadata: md,
2024-11-03 08:44:34 -05:00
Tags: tgi.Talkgroup.Tags,
2024-08-20 08:17:02 -04:00
Learned: tgi.Learned,
2024-11-03 08:44:34 -05:00
AlphaTag: tgi.Talkgroup.AlphaTag,
SystemName: tgi.System.Name,
2024-08-16 14:54:50 -04:00
}
2024-10-31 16:50:08 -04:00
_ = c.Send(&pb.Message{
2024-10-19 23:09:31 -04:00
ToClientMessage: &pb.Message_Response{
Response: &pb.CommandResponse{
CommandId: CommandID(ctx),
CommandResponse: &pb.CommandResponse_TgInfo{
TgInfo: resp,
},
},
2024-08-16 14:54:50 -04:00
},
})
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
}