123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- package MqttServer
- import (
- "Cold_mqtt/conf"
- "Cold_mqtt/lib"
- "Cold_mqtt/logs"
- "Cold_mqtt/models/Account"
- "Cold_mqtt/models/Device"
- "Cold_mqtt/models/Product"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "math/rand"
- "strconv"
- "time"
- )
- var redis_V3MqttControllers cache.Cache
- func init() {
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_V3MqttControllers", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redis_V3MqttControllers, err = cache.NewCache("redis", config)
- if err != nil || redis_V3MqttControllers == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- func Read_DeviceParameter3(T_sn string) {
- logs.Println("Read_DeviceParameter:", T_sn)
- Rt_parameter := Ms2_Project{
- Type: 3,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
- logs.Println("回复失败 [Rt_Parameter],err=", err)
- }
- logs.Println(string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: T_sn, T_data: string(jsonStu), T_num: 10})
- Mqtt_publish(T_sn, string(jsonStu))
- //lib.CountryMsidMap[(Msid*10)+1] = T_uuid // Mqtt 消息透传
- //go func() {
- // time.Sleep(3 * time.Second)
- // Read_DeviceSensorParameter3(T_sn)
- //}()
- return
- }
- func Read_DeviceSensorParameter3(T_sn string) {
- time.Sleep(1 * time.Second)
- logs.Println("Read_DeviceSensorParameter:", T_sn)
- // base 读取基本参数
- Rt_parameter := Ms_Read_DeviceSensorParameter{
- Type: 5,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- Data: []int{},
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "sensor")
- logs.Println("回复失败 [Rt_Parameter],err=", err)
- }
- logs.Println(string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: T_sn, T_data: string(jsonStu), T_num: 10})
- Mqtt_publish(T_sn, string(jsonStu))
- //lib.CountryMsidMap[(Msid*10)+2] = T_uuid // Mqtt 消息透传
- return
- }
- // 修改设备参数
- func Pu_DeviceParameter3(Rt_DeviceParameter Device.DeviceParameter) {
- Rt_parameter := Ms_Project2_Device_Parameter{
- //Sn: Rt_DeviceParameter.T_sn,
- Type: 4,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- }
- T_prt := "未知公司"
- r_Device, err := Device.Read_Device_ByT_sn(Rt_DeviceParameter.T_sn)
- if err == nil {
- _, Company_r := Account.Read_Company_id(r_Device.T_pid)
- T_prt = Company_r.T_name
- }
- //T_prt, err = iconvx.ConvertString(T_prt, "utf-8", "GBK")
- //T_prt = hex.EncodeToString( []byte(T_prt))
- //
- T_prt_b, err := lib.Utf8ToGbk([]byte(T_prt))
- //T_prt = string(T_prt_b)
- T_prt = hex.EncodeToString(T_prt_b)
- Rt_parameter.Data = Ms_Project_Device_Parameter_Data{
- T_name: Rt_DeviceParameter.T_name,
- T_saveT: Rt_DeviceParameter.T_saveT,
- T_overA: Rt_DeviceParameter.T_overA,
- T_outA: Rt_DeviceParameter.T_outA,
- T_lostA: Rt_DeviceParameter.T_lostA,
- T_scan: Rt_DeviceParameter.T_scan,
- T_bat: Rt_DeviceParameter.T_bat,
- T_warn: Rt_DeviceParameter.T_warn,
- T_warnD: Rt_DeviceParameter.T_warnD,
- T_dormancy: Rt_DeviceParameter.T_dormancy,
- T_snum: Rt_DeviceParameter.T_snum,
- T_prt: T_prt,
- T_btname: Rt_DeviceParameter.T_btname,
- T_btsid: Rt_DeviceParameter.T_btsid,
- T_btchar: Rt_DeviceParameter.T_btchar,
- }
- // 更新 消息ID
- Rt_DeviceParameter.T_Msid = Rt_parameter.Msid
- Device.Update_DeviceParameter(Rt_DeviceParameter, "T_Msid")
- // 发布数据
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("设备配置", "MQTT回复失败 [Pu_DeviceParameter]", string(jsonStu))
- logs.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter],err=", err)
- return
- }
- logs.Println(string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(Rt_DeviceParameter.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: Rt_DeviceParameter.T_sn, T_data: string(jsonStu), T_num: 10})
- Mqtt_publish(Rt_DeviceParameter.T_sn, string(jsonStu))
- return
- }
- // 修改传感器参数
- func Pu_DeviceParameter_Sensor3(Rt_parameter_sensor Device.DeviceSensorParameter) {
- Rt_parameter := Ms_Project2_Sensor_Parameter{
- //Sn: Rt_parameter_sensor.T_sn,
- Type: 6,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- }
- // 强制修改 是否启用预警记录数据 1:启用 0:关闭 默认:1
- if Rt_parameter_sensor.T_enprelnote != 0 {
- Rt_parameter_sensor.T_enprelnote = 1
- }
- Rt_parameter.Data = append(Rt_parameter.Data, Ms_Project_Sensor_Parameter_Data{
- T_id: Rt_parameter_sensor.T_id,
- T_name: Rt_parameter_sensor.T_name,
- T_Tlower: Rt_parameter_sensor.T_Tlower,
- T_Tupper: Rt_parameter_sensor.T_Tupper,
- T_RHlower: Rt_parameter_sensor.T_RHlower,
- T_RHupper: Rt_parameter_sensor.T_RHupper,
- T_en: Rt_parameter_sensor.T_en,
- T_free: Rt_parameter_sensor.T_free,
- T_enprel: Rt_parameter_sensor.T_enprel,
- T_tprel: Rt_parameter_sensor.T_tprel,
- T_tpreu: Rt_parameter_sensor.T_tpreu,
- T_hprel: Rt_parameter_sensor.T_hprel,
- T_hpreu: Rt_parameter_sensor.T_hpreu,
- T_enprelnote: Rt_parameter_sensor.T_enprelnote,
- //T_speed: Rt_parameter_sensor.T_speed,
- //T_sense: Rt_parameter_sensor.T_sense,
- })
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("设备配置", "MQTT回复失败 [Pu_DeviceParameter_Sensor]", string(jsonStu))
- logs.Println("设备配置 MQTT回复失败 [Pu_DeviceParameter_Sensor2],err=", err)
- }
- // 更新 消息ID
- Rt_parameter_sensor.T_Msid = Rt_parameter.Msid
- // 缓存数据
- DeviceMqttMapAdd(Rt_parameter_sensor.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: Rt_parameter_sensor.T_sn, T_data: string(jsonStu), T_num: 10})
- Device.Update_DeviceSensorParameter(Rt_parameter_sensor, "T_Msid")
- logs.Println(string(jsonStu))
- Mqtt_publish(Rt_parameter_sensor.T_sn, string(jsonStu))
- return
- }
- func Set_DeviceTask3(v Device.Device_task) {
- Rt_parameter := Ms2_Project{
- Type: 11,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- Data: map[string]interface{}{},
- }
- if v.T_task == "start" {
- Rt_parameter.Data["task"] = 1
- } else {
- Rt_parameter.Data["task"] = 0
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
- logs.Println("回复失败 [Rt_Parameter],err=", err)
- }
- logs.Println(v.T_sn, "==>", string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
- Mqtt_publish(v.T_sn, string(jsonStu))
- return
- }
- func Set_DeviceRestart3(v Device.Device_task) {
- Rt_parameter := Ms2_Project{
- Type: 97,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- Data: map[string]interface{}{},
- }
- if v.T_task == "1" {
- Rt_parameter.Data["task"] = 1
- } else {
- Rt_parameter.Data["task"] = 0
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
- logs.Println("回复失败 [Rt_Parameter],err=", err)
- }
- logs.Println(v.T_sn, "==>", string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
- Mqtt_publish(v.T_sn, string(jsonStu))
- return
- }
- func Get_Device_Realtime3(T_sn string) {
- Rt_parameter := Ms2_Project{
- Type: 0,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- }
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "实时数据 [Rt_realtime]", T_sn)
- logs.Println("回复失败 [Rt_realtime],err=", err)
- }
- logs.Println(string(jsonStu))
- Mqtt_publish(T_sn, string(jsonStu))
- return
- }
- func Up_DeviceProductUpgrade(v Product.ProductUpgrade_T) {
- Rt_parameter := Ms2_Project{
- Type: 14,
- Msid: ((time.Now().Unix() - 1600000000) * 10) + int64(rand.Intn(10)),
- Data: map[string]interface{}{},
- }
- Rt_parameter.Data["version"] = v.T_version
- Rt_parameter.Data["bin"] = v.T_file
- // 回复
- jsonStu, err := json.Marshal(Rt_parameter)
- if err != nil {
- logs.Println("MqttServer", "参数请求 [Rt_Parameter]", "base")
- logs.Println("回复失败 [Rt_Parameter],err=", err)
- }
- logs.Println(v.T_sn, "==>", string(jsonStu))
- // 缓存数据
- DeviceMqttMapAdd(v.T_sn+"-"+strconv.FormatInt(Rt_parameter.Msid, 10), DeviceMqttMap_Struct{T_sn: v.T_sn, T_data: string(jsonStu), T_num: 30})
- Mqtt_publish(v.T_sn, string(jsonStu))
- return
- }
- func RTC_TimeStr(T_sn string, Msid int64, Type int) {
- if Type == 99 {
- if redis_V3MqttControllers.IsExist(T_sn + "RTC_TimeStr") {
- return
- }
- redis_V3MqttControllers.Put(T_sn+"RTC_TimeStr", "1", 2*time.Minute)
- }
- Time := time.Now()
- Rt_Project_Data_r := Ms2_Project{
- Type: Type,
- Msid: Msid,
- }
- var Data map[string]interface{}
- Data = make(map[string]interface{})
- Data["ut"] = Time.Unix()
- Data["date"] = Time.Format("2006-01-02 15:04:05")
- Rt_Project_Data_r.Data = Data
- // 回复
- jsonStu, err := json.Marshal(Rt_Project_Data_r)
- if err != nil {
- return
- }
- Mqtt_publish(T_sn, string(jsonStu))
- return
- }
|