| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package main
- import (
- "encoding/json"
- "time"
- "hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
- "hnyfkj.com.cn/rtu/linux/utils/shell"
- )
- var (
- rpc_ping = "executor.ping"
- rpc_exec = "executor.exec"
- rpc_stop = "executor.interrupt"
- rpc_quit = "executor.close"
- )
- // 串行执行
- func (c *MQTTCoupler) needSerialize(method string) bool {
- switch method {
- case rpc_ping, rpc_stop:
- return false
- case rpc_exec, rpc_quit:
- return true
- default:
- return true
- }
- }
- // 超时结束
- func (c *MQTTCoupler) needTimeoutEnd(method string) bool {
- switch method {
- case rpc_exec:
- return false
- case rpc_ping, rpc_stop, rpc_quit:
- return true
- default:
- return true
- }
- }
- // 心跳检测
- func (c *MQTTCoupler) Ping() (jsonrpc2.Response, error) {
- params := struct {
- ClientID string `json:"client_id"`
- }{
- ClientID: c.clientID,
- }
- return c.doCmd(rpc_ping, params)
- }
- // 执行命令
- func (c *MQTTCoupler) Exec(
- cmd string) (shell.ExecuteResult, error) {
- params := struct {
- ClientID string `json:"client_id"`
- shell.ExecuteParams
- }{
- ClientID: c.clientID,
- ExecuteParams: shell.ExecuteParams{
- Cmd: cmd,
- Timeout: int(shell.DefaultTimeout / time.Second),
- },
- }
- timeout := getCmdTimeoutByPrefix(cmd)
- if timeout > 0 {
- params.Timeout = timeout
- }
- exrs := shell.ExecuteResult{}
- resp, err := c.doCmd(rpc_exec, params)
- if err != nil {
- return exrs, err
- }
- if resp.Error != nil { ////////////////// 错误应答
- exrs.ExitCode = int(resp.Error.Code)
- exrs.Stderr = resp.Error.Message
- } else if len(resp.Result) > 0 { //////// 正确应答
- if err := json.Unmarshal(resp.Result, &exrs); err != nil {
- exrs.ExitCode = 1
- exrs.Stderr = err.Error()
- }
- }
- return exrs, nil
- }
- // 中断执行
- func (c *MQTTCoupler) Stop() (jsonrpc2.Response, error) {
- params := struct {
- ClientID string `json:"client_id"`
- }{
- ClientID: c.clientID,
- }
- return c.doCmd(rpc_stop, params)
- }
- // 关闭退出
- func (c *MQTTCoupler) Quit() (jsonrpc2.Response, error) {
- params := struct {
- ClientID string `json:"client_id"`
- }{
- ClientID: c.clientID,
- }
- return c.doCmd(rpc_quit, params)
- }
|