invoker.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // 串行执行
  13. func (c *MQTTCoupler) needSerialize(method string) bool {
  14. switch method {
  15. case rpc_ping, rpc_stop:
  16. return false
  17. case rpc_exec, rpc_quit:
  18. return true
  19. default:
  20. return true
  21. }
  22. }
  23. // 超时结束
  24. func (c *MQTTCoupler) needTimeoutEnd(method string) bool {
  25. switch method {
  26. case rpc_exec:
  27. return false
  28. case rpc_ping, rpc_stop, rpc_quit:
  29. return true
  30. default:
  31. return true
  32. }
  33. }
  34. // 心跳检测
  35. func (c *MQTTCoupler) Ping() (shell.ExecuteResult, error) {
  36. params := struct {
  37. ClientID string `json:"client_id"`
  38. }{
  39. ClientID: c.clientID,
  40. }
  41. return c.doCmd(rpc_ping, params)
  42. }
  43. // 执行命令
  44. func (c *MQTTCoupler) Exec(
  45. cmd string) (shell.ExecuteResult, error) {
  46. params := struct {
  47. ClientID string `json:"client_id"`
  48. shell.ExecuteParams
  49. }{
  50. ClientID: c.clientID,
  51. ExecuteParams: shell.ExecuteParams{
  52. Cmd: cmd,
  53. Timeout: int(shell.DefaultTimeout / time.Second),
  54. },
  55. }
  56. timeout := getCmdTimeoutByPrefix(cmd)
  57. if timeout > 0 {
  58. params.Timeout = timeout
  59. }
  60. return c.doCmd(rpc_exec, params)
  61. }
  62. // 中断执行
  63. func (c *MQTTCoupler) Stop() (shell.ExecuteResult, error) {
  64. params := struct {
  65. ClientID string `json:"client_id"`
  66. }{
  67. ClientID: c.clientID,
  68. }
  69. return c.doCmd(rpc_stop, params)
  70. }
  71. // 关闭退出
  72. func (c *MQTTCoupler) Quit() (shell.ExecuteResult, error) {
  73. params := struct {
  74. ClientID string `json:"client_id"`
  75. }{
  76. ClientID: c.clientID,
  77. }
  78. return c.doCmd(rpc_quit, params)
  79. }