|
@@ -43,6 +43,8 @@ type MQTTCoupler struct {
|
|
|
ctx context.Context
|
|
ctx context.Context
|
|
|
cancel context.CancelFunc
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
|
|
+ executor *shell.Executor // 本地执行器, 单实例-串行执行指令
|
|
|
|
|
+
|
|
|
isConnected atomic.Bool /// 标记是否已连接MQTT的Broker服务
|
|
isConnected atomic.Bool /// 标记是否已连接MQTT的Broker服务
|
|
|
|
|
|
|
|
// 注册本地的远程方法, 连接成功后用于让客户端能够主动下发指令
|
|
// 注册本地的远程方法, 连接成功后用于让客户端能够主动下发指令
|
|
@@ -67,6 +69,7 @@ func ModuleInit(mqttBroker, mqttUsername, mqttPassword string) bool {
|
|
|
pubTopic: "",
|
|
pubTopic: "",
|
|
|
ctx: ctx,
|
|
ctx: ctx,
|
|
|
cancel: cancel,
|
|
cancel: cancel,
|
|
|
|
|
+ executor: shell.NewExecutor(),
|
|
|
isConnected: atomic.Bool{},
|
|
isConnected: atomic.Bool{},
|
|
|
registerRpcMeths: &singletask.OnceTask{},
|
|
registerRpcMeths: &singletask.OnceTask{},
|
|
|
}
|
|
}
|
|
@@ -194,7 +197,7 @@ func (c *MQTTCoupler) instRPCMethods() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (c *MQTTCoupler) handleRequests(client mqtt.Client, msg mqtt.Message) {
|
|
func (c *MQTTCoupler) handleRequests(client mqtt.Client, msg mqtt.Message) {
|
|
|
- go c.execOneCmd(msg)
|
|
|
|
|
|
|
+ c.execOneCmd(msg)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (c *MQTTCoupler) execOneCmd(msg mqtt.Message) {
|
|
func (c *MQTTCoupler) execOneCmd(msg mqtt.Message) {
|
|
@@ -220,8 +223,12 @@ func (c *MQTTCoupler) execOneCmd(msg mqtt.Message) {
|
|
|
resp = jsonrpc2.BuildError(req, -32700, err.Error())
|
|
resp = jsonrpc2.BuildError(req, -32700, err.Error())
|
|
|
goto retp
|
|
goto retp
|
|
|
}
|
|
}
|
|
|
- result, err := shell.Execute(params)
|
|
|
|
|
|
|
+ result, err := c.executor.Exec(params)
|
|
|
resp = buildResp(req, result, err)
|
|
resp = buildResp(req, result, err)
|
|
|
|
|
+ // Call-3:中断本地shell的执行,等价Ctrl+C
|
|
|
|
|
+ case "shell.interrupt":
|
|
|
|
|
+ err := c.executor.Interrupt()
|
|
|
|
|
+ resp = buildResp(req, "interrupted", err)
|
|
|
// Call-?:无效, 远端调用了还不支持的-方法
|
|
// Call-?:无效, 远端调用了还不支持的-方法
|
|
|
default:
|
|
default:
|
|
|
resp = jsonrpc2.BuildError(req, jsonrpc2.ErrMethodNotFound, "")
|
|
resp = jsonrpc2.BuildError(req, jsonrpc2.ErrMethodNotFound, "")
|