msg.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package msg
  2. import (
  3. "github.com/Masterminds/semver/v3"
  4. )
  5. /** Design notes:
  6. *
  7. * For server -> client: topic is `client_do_action`
  8. * For client -> server: topic is `server_do_notify`
  9. *
  10. * If you want to upgrade a service, you can send a `RequestDeviceUpdateMsg` to the client,
  11. * and the client will respond with a `DeviceActionDoneMsg`. When all clients have responded
  12. * with success, you can send a `ForceDeviceRebootMsg` to the client to reboot the service
  13. * for the upgrade to take effect.
  14. *
  15. */
  16. const ClientDoActionTopic = "client_do_action"
  17. const ServerDoNotifyTopic = "server_do_notify"
  18. // ----- Client -> Server -----
  19. // * Entity: Device
  20. type DeviceHeartbeatMsg struct {
  21. DeviceID string `mapstructure:"device_id"`
  22. LocalTime int64 `mapstructure:"local_time"`
  23. IPAddress string `mapstructure:"ip_address"`
  24. ServiceName string `mapstructure:"service_name"`
  25. ServiceVersion string `mapstructure:"service_version"` // Compile datetime
  26. ProtocolVersion string `mapstructure:"protocol_version"`
  27. }
  28. const DeviceHeartbeatMsgType = "device_heartbeat"
  29. func (m *DeviceHeartbeatMsg) MsgType() string {
  30. return DeviceHeartbeatMsgType
  31. }
  32. // NOTE: Every time a new version of the protocol is released, this function should be updated
  33. const ProtocolVersion = "0.1.0"
  34. func MakeDeviceHeartbeatMsg(deviceID string, localTime int64, ipAddress, serviceName, serviceVersion string) DeviceHeartbeatMsg {
  35. return DeviceHeartbeatMsg{
  36. DeviceID: deviceID,
  37. LocalTime: localTime,
  38. IPAddress: ipAddress,
  39. ServiceName: serviceName,
  40. ServiceVersion: serviceVersion,
  41. ProtocolVersion: ProtocolVersion,
  42. }
  43. }
  44. func (m *DeviceHeartbeatMsg) IsProtocolVersionCompatible() bool {
  45. // Check if the protocol version is compatible
  46. msgVer, err := semver.NewVersion(m.ProtocolVersion)
  47. if err != nil {
  48. return false
  49. }
  50. constraint, err := semver.NewConstraint(ProtocolVersion)
  51. if err != nil {
  52. return false
  53. }
  54. return constraint.Check(msgVer)
  55. }
  56. // * Entity: Device
  57. type DeviceActionDoneMsg struct {
  58. DeviceID string `mapstructure:"device_id"`
  59. ActionType string `mapstructure:"action_type"` // The same as message type
  60. ActionMsgID string `mapstructure:"action_msg_id"`
  61. ActionStartTime int64 `mapstructure:"action_start_time"`
  62. Status int `mapstructure:"status"`
  63. Msg string `mapstructure:"msg"` // Can be used to store errors
  64. }
  65. const DeviceActionDoneMsgType = "device_action_done"
  66. func (m *DeviceActionDoneMsg) MsgType() string {
  67. return DeviceActionDoneMsgType
  68. }
  69. func MakeDeviceActionDoneMsg(deviceID, actionType, actionMsgID string, actionStartTime int64, status int, msg string) DeviceActionDoneMsg {
  70. return DeviceActionDoneMsg{
  71. DeviceID: deviceID,
  72. ActionType: actionType,
  73. ActionMsgID: actionMsgID,
  74. ActionStartTime: actionStartTime,
  75. Status: status,
  76. Msg: msg,
  77. }
  78. }
  79. // * Entity: Device
  80. type DevicePongMsg struct {
  81. DeviceID string `mapstructure:"device_id"`
  82. RecvTime int64 `mapstructure:"recv_time"`
  83. }
  84. const DevicePongMsgType = "device_pong"
  85. func (m *DevicePongMsg) MsgType() string {
  86. return DevicePongMsgType
  87. }
  88. func MakeDevicePongMsg(deviceID string, recvTime int64) DevicePongMsg {
  89. return DevicePongMsg{
  90. DeviceID: deviceID,
  91. RecvTime: recvTime,
  92. }
  93. }
  94. // * Entity: Part
  95. type PartUploadStatusChangedMsg struct {
  96. DeviceID string `mapstructure:"device_id"`
  97. StartTime int64 `mapstructure:"start_time"`
  98. StreamName string `mapstructure:"stream_name"`
  99. PartNumber int64 `mapstructure:"part_number"`
  100. TotalPoints int64 `mapstructure:"total_points"`
  101. Status string `mapstructure:"status"` // "success", "failed", "unchanged"
  102. Msg string `mapstructure:"msg,omitempty"` // Can be used to store errors
  103. }
  104. const PartUploadStatusChangedMsgType = "part_upload_status_changed"
  105. func (m *PartUploadStatusChangedMsg) MsgType() string {
  106. return PartUploadStatusChangedMsgType
  107. }
  108. func MakePartUploadStatusChangedMsg(deviceID string, startTime int64, streamName string, partNumber, totalPoints int64, status, msg string) PartUploadStatusChangedMsg {
  109. return PartUploadStatusChangedMsg{
  110. DeviceID: deviceID,
  111. StartTime: startTime,
  112. StreamName: streamName,
  113. PartNumber: partNumber,
  114. TotalPoints: totalPoints,
  115. Status: status,
  116. Msg: msg,
  117. }
  118. }
  119. // * Entity: Part
  120. type PartInsertToStckStatusChangedMsg struct {
  121. DeviceID string `mapstructure:"device_id"`
  122. StartTime int64 `mapstructure:"start_time"`
  123. StreamName string `mapstructure:"stream_name"`
  124. PartNumber int64 `mapstructure:"part_number"`
  125. TotalPoints int64 `mapstructure:"total_points"`
  126. Status string `mapstructure:"status"` // "success", "failed", "unchanged"
  127. Msg string `mapstructure:"msg,omitempty"` // Can be used to store errors
  128. }
  129. const PartInsertToStckStatusChangedMsgType = "part_insert_to_stck_status_changed"
  130. func (m *PartInsertToStckStatusChangedMsg) MsgType() string {
  131. return PartInsertToStckStatusChangedMsgType
  132. }
  133. func MakePartInsertToStckStatusChangedMsg(deviceID string, startTime int64, streamName string, partNumber, totalPoints int64, status, msg string) PartInsertToStckStatusChangedMsg {
  134. return PartInsertToStckStatusChangedMsg{
  135. DeviceID: deviceID,
  136. StartTime: startTime,
  137. StreamName: streamName,
  138. PartNumber: partNumber,
  139. TotalPoints: totalPoints,
  140. Status: status,
  141. Msg: msg,
  142. }
  143. }
  144. // * Entity: Stream
  145. type StreamUploadStatusChangedMsg struct {
  146. DeviceID string `mapstructure:"device_id"`
  147. StartTime int64 `mapstructure:"start_time"`
  148. StreamName string `mapstructure:"stream_name"`
  149. TotalPointsCount int64 `mapstructure:"total_points_count"`
  150. TotalPartsCount int64 `mapstructure:"total_parts_count"`
  151. SuccessCount int64 `mapstructure:"success_count"`
  152. FailedCount int64 `mapstructure:"failed_count"`
  153. UnchangedCount int64 `mapstructure:"unchanged_count"`
  154. Details interface{} `mapstructure:"details,omitempty"` // Currently unused
  155. }
  156. const StreamUploadStatusChangedMsgType = "stream_upload_status_changed"
  157. func (m *StreamUploadStatusChangedMsg) MsgType() string {
  158. return StreamUploadStatusChangedMsgType
  159. }
  160. func MakeStreamUploadStatusChangedMsg(deviceID string, startTime int64, streamName string, totalPointsCount int64, totalPartsCount, successCount, failedCount, unchangedCount int64) StreamUploadStatusChangedMsg {
  161. return StreamUploadStatusChangedMsg{
  162. DeviceID: deviceID,
  163. StartTime: startTime,
  164. StreamName: streamName,
  165. TotalPointsCount: totalPointsCount,
  166. TotalPartsCount: totalPartsCount,
  167. SuccessCount: successCount,
  168. FailedCount: failedCount,
  169. UnchangedCount: unchangedCount,
  170. }
  171. }
  172. // * Entity: Stream
  173. type StreamInsertToStckStatusChangedMsg struct {
  174. DeviceID string `mapstructure:"device_id"`
  175. StartTime int64 `mapstructure:"start_time"`
  176. StreamName string `mapstructure:"stream_name"`
  177. TotalPointsCount int64 `mapstructure:"total_points_count"`
  178. TotalPartsCount int64 `mapstructure:"total_parts_count"`
  179. SuccessCount int64 `mapstructure:"success_count"`
  180. FailedCount int64 `mapstructure:"failed_count"`
  181. UnchangedCount int64 `mapstructure:"unchanged_count"`
  182. Details interface{} `mapstructure:"details,omitempty"` // Currently unused
  183. }
  184. const StreamInsertToStckStatusChangedMsgType = "stream_insert_to_stck_status_changed"
  185. func (m *StreamInsertToStckStatusChangedMsg) MsgType() string {
  186. return StreamInsertToStckStatusChangedMsgType
  187. }
  188. func MakeStreamInsertToStckStatusChangedMsg(deviceID string, startTime int64, streamName string, totalPointsCount int64, totalPartsCount, successCount, failedCount, unchangedCount int64) StreamInsertToStckStatusChangedMsg {
  189. return StreamInsertToStckStatusChangedMsg{
  190. DeviceID: deviceID,
  191. StartTime: startTime,
  192. StreamName: streamName,
  193. TotalPointsCount: totalPointsCount,
  194. TotalPartsCount: totalPartsCount,
  195. SuccessCount: successCount,
  196. FailedCount: failedCount,
  197. UnchangedCount: unchangedCount,
  198. }
  199. }
  200. // ----- Server -> Client -----
  201. // * Entity: Device
  202. type ForceDeviceRebootMsg struct {
  203. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  204. }
  205. const ForceDeviceRebootMsgType = "force_device_reboot"
  206. func (m *ForceDeviceRebootMsg) MsgType() string {
  207. return ForceDeviceRebootMsgType
  208. }
  209. func MakeForceDeviceRebootMsg(deviceID string) ForceDeviceRebootMsg {
  210. return ForceDeviceRebootMsg{
  211. DeviceID: deviceID,
  212. }
  213. }
  214. // * Entity: Device
  215. type ForceDeviceSendHeartbeatMsg struct {
  216. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  217. }
  218. const ForceDeviceSendHeartbeatMsgType = "force_device_send_heartbeat"
  219. func (m *ForceDeviceSendHeartbeatMsg) MsgType() string {
  220. return ForceDeviceSendHeartbeatMsgType
  221. }
  222. func MakeForceDeviceSendHeartbeatMsg(deviceID string) ForceDeviceSendHeartbeatMsg {
  223. return ForceDeviceSendHeartbeatMsg{
  224. DeviceID: deviceID,
  225. }
  226. }
  227. // * Entity: Device
  228. // * Response: DeviceActionDoneMsg
  229. type RequestDeviceUpdateMsg struct {
  230. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  231. ServiceName string `mapstructure:"service_name"`
  232. ServiceVersion string `mapstructure:"service_version"`
  233. ServiceBinaryURL string `mapstructure:"service_binary_url"`
  234. BinaryHash string `mapstructure:"binary_hash"`
  235. }
  236. const RequestDeviceUpdateMsgType = "request_device_update"
  237. func (m *RequestDeviceUpdateMsg) MsgType() string {
  238. return RequestDeviceUpdateMsgType
  239. }
  240. func MakeRequestDeviceUpdateMsg(deviceID, serviceName, serviceVersion, serviceBinaryURL, binaryHash string) RequestDeviceUpdateMsg {
  241. return RequestDeviceUpdateMsg{
  242. DeviceID: deviceID,
  243. ServiceName: serviceName,
  244. ServiceVersion: serviceVersion,
  245. ServiceBinaryURL: serviceBinaryURL,
  246. BinaryHash: binaryHash,
  247. }
  248. }
  249. // * Entity: Device
  250. // * Response: DeviceActionDoneMsg
  251. type RequestDeviceUploadLogsToMinioMsg struct {
  252. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  253. RemoteBucket string `mapstructure:"remote_bucket"`
  254. RemoteBasePath string `mapstructure:"remote_base_path"`
  255. }
  256. const RequestDeviceUploadLogsToMinioMsgType = "request_device_upload_logs_to_minio"
  257. func (m *RequestDeviceUploadLogsToMinioMsg) MsgType() string {
  258. return RequestDeviceUploadLogsToMinioMsgType
  259. }
  260. func MakeRequestDeviceUploadLogsToMinioMsg(deviceID, remoteBucket, remoteBasePath string) RequestDeviceUploadLogsToMinioMsg {
  261. return RequestDeviceUploadLogsToMinioMsg{
  262. DeviceID: deviceID,
  263. RemoteBucket: remoteBucket,
  264. RemoteBasePath: remoteBasePath,
  265. }
  266. }
  267. // * Entity: Device
  268. // * Response: DevicePongMsg
  269. type DevicePingMsg struct {
  270. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  271. }
  272. const DevicePingMsgType = "device_ping"
  273. func (m *DevicePingMsg) MsgType() string {
  274. return DevicePingMsgType
  275. }
  276. func MakeDevicePingMsg(deviceID string) DevicePingMsg {
  277. return DevicePingMsg{
  278. DeviceID: deviceID,
  279. }
  280. }
  281. // * Entity: Device
  282. // * Response: DeviceActionDoneMsg
  283. type RequestDeviceExecuteShellScriptMsg struct {
  284. DeviceID string `mapstructure:"device_id"` // Specific device ID or `*` for all devices
  285. Script string `mapstructure:"script"`
  286. }
  287. const RequestDeviceExecuteShellScriptMsgType = "request_device_execute_shell_script"
  288. func (m *RequestDeviceExecuteShellScriptMsg) MsgType() string {
  289. return RequestDeviceExecuteShellScriptMsgType
  290. }
  291. func MakeRequestDeviceExecuteShellScriptMsg(deviceID, script string) RequestDeviceExecuteShellScriptMsg {
  292. return RequestDeviceExecuteShellScriptMsg{
  293. DeviceID: deviceID,
  294. Script: script,
  295. }
  296. }