2024-05-14 22:34:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package blockservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-17 03:01:52 +02:00
|
|
|
"strings"
|
2024-05-22 06:15:11 +02:00
|
|
|
"time"
|
2024-05-14 22:34:41 +02:00
|
|
|
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
|
2024-06-12 22:47:13 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
2024-05-14 22:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockService struct{}
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (bs *BlockService) SendCommand_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
Desc: "send command to block",
|
2024-06-12 22:47:13 +02:00
|
|
|
ArgNames: []string{"blockid", "cmd"},
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (bs *BlockService) SendCommand(blockId string, cmd blockcontroller.BlockCommand) error {
|
2024-05-17 03:01:52 +02:00
|
|
|
if strings.HasPrefix(cmd.GetCommand(), "controller:") {
|
|
|
|
bc := blockcontroller.GetBlockController(blockId)
|
|
|
|
if bc == nil {
|
|
|
|
return fmt.Errorf("block controller not found for block %q", blockId)
|
|
|
|
}
|
|
|
|
bc.InputCh <- cmd
|
|
|
|
} else {
|
|
|
|
blockcontroller.ProcessStaticCommand(blockId, cmd)
|
|
|
|
}
|
2024-05-14 22:34:41 +02:00
|
|
|
return nil
|
|
|
|
}
|