You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hub_io.go 4.9 kB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package cdsapi
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strings"
  7. "gitlink.org.cn/cloudream/common/consts/errorcode"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  9. "gitlink.org.cn/cloudream/common/utils/http2"
  10. "gitlink.org.cn/cloudream/common/utils/serder"
  11. )
  12. // TODO2 重新梳理代码
  13. const GetStreamPath = "/hubIO/getStream"
  14. type GetStreamReq struct {
  15. PlanID exec.PlanID `json:"planID"`
  16. VarID exec.VarID `json:"varID"`
  17. SignalID exec.VarID `json:"signalID"`
  18. Signal exec.VarValue `json:"signal"`
  19. }
  20. func (c *Client) GetStream(req GetStreamReq) (io.ReadCloser, error) {
  21. targetUrl, err := url.JoinPath(c.baseURL, GetStreamPath)
  22. if err != nil {
  23. return nil, err
  24. }
  25. body, err := serder.ObjectToJSONEx(req)
  26. if err != nil {
  27. return nil, fmt.Errorf("request to json: %w", err)
  28. }
  29. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  30. Body: body,
  31. })
  32. if err != nil {
  33. return nil, err
  34. }
  35. contType := resp.Header.Get("Content-Type")
  36. if strings.Contains(contType, http2.ContentTypeJSON) {
  37. var codeResp response[any]
  38. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  39. return nil, fmt.Errorf("parsing response: %w", err)
  40. }
  41. return nil, codeResp.ToError()
  42. }
  43. return resp.Body, nil
  44. }
  45. const SendStreamPath = "/hubIO/sendStream"
  46. type SendStreamReq struct {
  47. SendStreamInfo
  48. Stream io.ReadCloser `json:"-"`
  49. }
  50. type SendStreamInfo struct {
  51. PlanID exec.PlanID `json:"planID"`
  52. VarID exec.VarID `json:"varID"`
  53. }
  54. func (c *Client) SendStream(req SendStreamReq) error {
  55. // targetUrl, err := url.JoinPath(c.baseURL, SendStreamPath)
  56. // if err != nil {
  57. // return err
  58. // }
  59. // infoJSON, err := serder.ObjectToJSON(req)
  60. // if err != nil {
  61. // return fmt.Errorf("info to json: %w", err)
  62. // }
  63. // resp, err := http2.PostMultiPart(targetUrl, http2.MultiPartRequestParam{
  64. // Form: map[string]string{"info": string(infoJSON)},
  65. // Files: iterator.Array(&http2.IterMultiPartFile{
  66. // FieldName: "stream",
  67. // FileName: "stream",
  68. // File: req.Stream,
  69. // }),
  70. // })
  71. // if err != nil {
  72. // return err
  73. // }
  74. // contType := resp.Header.Get("Content-Type")
  75. // if strings.Contains(contType, http2.ContentTypeJSON) {
  76. // var err error
  77. // var codeResp response[ObjectUploadResp]
  78. // if codeResp, err = serder.JSONToObjectStreamEx[response[ObjectUploadResp]](resp.Body); err != nil {
  79. // return fmt.Errorf("parsing response: %w", err)
  80. // }
  81. // if codeResp.Code == errorcode.OK {
  82. // return nil
  83. // }
  84. // return codeResp.ToError()
  85. // }
  86. // return fmt.Errorf("unknow response content type: %s", contType)
  87. return fmt.Errorf("not implemented")
  88. }
  89. const ExecuteIOPlanPath = "/hubIO/executeIOPlan"
  90. type ExecuteIOPlanReq struct {
  91. Plan exec.Plan `json:"plan"`
  92. }
  93. func (c *Client) ExecuteIOPlan(req ExecuteIOPlanReq) error {
  94. targetUrl, err := url.JoinPath(c.baseURL, ExecuteIOPlanPath)
  95. if err != nil {
  96. return err
  97. }
  98. body, err := serder.ObjectToJSONEx(req)
  99. if err != nil {
  100. return fmt.Errorf("request to json: %w", err)
  101. }
  102. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  103. Body: body,
  104. })
  105. if err != nil {
  106. return err
  107. }
  108. codeResp, err := ParseJSONResponse[response[any]](resp)
  109. if err != nil {
  110. return err
  111. }
  112. if codeResp.Code == errorcode.OK {
  113. return nil
  114. }
  115. return codeResp.ToError()
  116. }
  117. const SendVarPath = "/hubIO/sendVar"
  118. type SendVarReq struct {
  119. PlanID exec.PlanID `json:"planID"`
  120. VarID exec.VarID `json:"varID"`
  121. VarValue exec.VarValue `json:"varValue"`
  122. }
  123. func (c *Client) SendVar(req SendVarReq) error {
  124. targetUrl, err := url.JoinPath(c.baseURL, SendVarPath)
  125. if err != nil {
  126. return err
  127. }
  128. body, err := serder.ObjectToJSONEx(req)
  129. if err != nil {
  130. return fmt.Errorf("request to json: %w", err)
  131. }
  132. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  133. Body: body,
  134. })
  135. if err != nil {
  136. return err
  137. }
  138. jsonResp, err := ParseJSONResponse[response[any]](resp)
  139. if err != nil {
  140. return err
  141. }
  142. if jsonResp.Code == errorcode.OK {
  143. return nil
  144. }
  145. return jsonResp.ToError()
  146. }
  147. const GetVarPath = "/hubIO/getVar"
  148. type GetVarReq struct {
  149. PlanID exec.PlanID `json:"planID"`
  150. VarID exec.VarID `json:"varID"`
  151. SignalID exec.VarID `json:"signalID"`
  152. Signal exec.VarValue `json:"signal"`
  153. }
  154. type GetVarResp struct {
  155. Value exec.VarValue `json:"value"`
  156. }
  157. func (c *Client) GetVar(req GetVarReq) (*GetVarResp, error) {
  158. targetUrl, err := url.JoinPath(c.baseURL, GetVarPath)
  159. if err != nil {
  160. return nil, err
  161. }
  162. body, err := serder.ObjectToJSONEx(req)
  163. if err != nil {
  164. return nil, fmt.Errorf("request to json: %w", err)
  165. }
  166. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  167. Body: body,
  168. })
  169. if err != nil {
  170. return nil, err
  171. }
  172. jsonResp, err := ParseJSONResponse[response[GetVarResp]](resp)
  173. if err != nil {
  174. return nil, err
  175. }
  176. if jsonResp.Code == errorcode.OK {
  177. return &jsonResp.Data, nil
  178. }
  179. return nil, jsonResp.ToError()
  180. }