|
|
@@ -0,0 +1,72 @@
|
|
|
+package reporter
|
|
|
+
|
|
|
+import "C"
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "sync/atomic"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
+ "hnyfkj.com.cn/rtu/linux/baseapp"
|
|
|
+ "hnyfkj.com.cn/rtu/linux/utils/singletask"
|
|
|
+)
|
|
|
+
|
|
|
+const MODULE_NAME = "Reporter"
|
|
|
+
|
|
|
+var (
|
|
|
+ Reporter *MQTTReporter
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ MqttQos1 byte = 1 //// 消息至少送达一次
|
|
|
+ FastInterval = 1 * time.Second //// 快速检测时间间隔
|
|
|
+ SlowInterval = 5 * time.Second //// 慢速检测时间间隔
|
|
|
+)
|
|
|
+
|
|
|
+type MQTTReporter struct {
|
|
|
+ client mqtt.Client
|
|
|
+
|
|
|
+ ctx context.Context
|
|
|
+ cancel context.CancelFunc
|
|
|
+
|
|
|
+ isLogin atomic.Bool // 标记是否已成功登录MQTT后端服务器
|
|
|
+ dui string // 登录成功后服务端返回的设备唯一ID
|
|
|
+ inheritDUI string // 继承的历史ID, 可选可为空(换板时)
|
|
|
+
|
|
|
+ // 主动上报的后台任务, 登录成功时用于照片补录和上报通知类消息
|
|
|
+ reuploadHistTask *singletask.OnceTask // 补录数据, 单实例
|
|
|
+ reportMcuCfgTask *singletask.OnceTask // 上报配置, 单实例
|
|
|
+ reportRtuPosTask *singletask.OnceTask // 上报位置, 单实例
|
|
|
+
|
|
|
+ // 注册本地的远程方法, 登录成功后用于让服务端能够主动下发指令
|
|
|
+ registerRpcMeths *singletask.OnceTask // 注册方法, 单实例
|
|
|
+}
|
|
|
+
|
|
|
+func ModuleInit() bool {
|
|
|
+ err := loadCfgServers()
|
|
|
+ if err != nil {
|
|
|
+ baseapp.Logger.Errorf("[%s] 加载服务器配置项失败: %v!!", MODULE_NAME, err)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+func ModuleExit() {
|
|
|
+ if Reporter != nil {
|
|
|
+ Reporter.cancel()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (r *MQTTReporter) IsLogin() bool {
|
|
|
+ return r.isLogin.Load()
|
|
|
+}
|
|
|
+
|
|
|
+//export RTU_IsLoginMqServer
|
|
|
+func RTU_IsLoginMqServer() C.int {
|
|
|
+ if Reporter != nil && Reporter.IsLogin() {
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+}
|