invoker.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "time"
  4. "hnyfkj.com.cn/rtu/linux/utils/shell"
  5. )
  6. var (
  7. rpc_ping = "executor.ping"
  8. rpc_exec = "executor.exec"
  9. rpc_stop = "executor.interrupt"
  10. rpc_quit = "executor.close"
  11. )
  12. func (c *MQTTCoupler) needSerialize(method string) bool {
  13. if method == rpc_ping || method == rpc_stop {
  14. return false
  15. }
  16. return true
  17. }
  18. func (c *MQTTCoupler) needTimeoutEnd(method string) bool {
  19. if method == rpc_ping || method == rpc_stop || method == rpc_quit {
  20. return true
  21. }
  22. return false
  23. }
  24. // 心跳检测
  25. func (c *MQTTCoupler) Ping() (shell.ExecuteResult, error) {
  26. params := struct {
  27. ClientID string `json:"client_id"`
  28. }{
  29. ClientID: c.clientID,
  30. }
  31. return c.doCmd(rpc_ping, params)
  32. }
  33. // 执行命令
  34. func (c *MQTTCoupler) Exec(
  35. cmd string) (shell.ExecuteResult, error) {
  36. params := struct {
  37. ClientID string `json:"client_id"`
  38. shell.ExecuteParams
  39. }{
  40. ClientID: c.clientID,
  41. ExecuteParams: shell.ExecuteParams{
  42. Cmd: cmd,
  43. Timeout: int(shell.DefaultTimeout / time.Second),
  44. },
  45. }
  46. timeout := getCmdTimeoutByPrefix(cmd)
  47. if timeout > 0 {
  48. params.Timeout = timeout
  49. }
  50. return c.doCmd(rpc_exec, params)
  51. }
  52. // 中断执行
  53. func (c *MQTTCoupler) Stop() (shell.ExecuteResult, error) {
  54. params := struct {
  55. ClientID string `json:"client_id"`
  56. }{
  57. ClientID: c.clientID,
  58. }
  59. return c.doCmd(rpc_stop, params)
  60. }
  61. // 关闭退出
  62. func (c *MQTTCoupler) Quit() (shell.ExecuteResult, error) {
  63. params := struct {
  64. ClientID string `json:"client_id"`
  65. }{
  66. ClientID: c.clientID,
  67. }
  68. return c.doCmd(rpc_quit, params)
  69. }