|  | @@ -0,0 +1,978 @@
 | 
	
		
			
				|  |  | +package MqttServer
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import (
 | 
	
		
			
				|  |  | +	"Cold_mqtt/MqttServer/WarningNotice"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/Nats/NatsServer"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/lib"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/logs"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/models/Device"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/models/Product"
 | 
	
		
			
				|  |  | +	"Cold_mqtt/models/Warning"
 | 
	
		
			
				|  |  | +	"encoding/json"
 | 
	
		
			
				|  |  | +	"fmt"
 | 
	
		
			
				|  |  | +	"io/ioutil"
 | 
	
		
			
				|  |  | +	"math"
 | 
	
		
			
				|  |  | +	"net/http"
 | 
	
		
			
				|  |  | +	"strconv"
 | 
	
		
			
				|  |  | +	"strings"
 | 
	
		
			
				|  |  | +	"time"
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func AsyncFuncV3(r_Device *Device.Device, Ms_project Ms2m_Project, msg []byte) {
 | 
	
		
			
				|  |  | +	ms2_Return := Ms2_Return{
 | 
	
		
			
				|  |  | +		Type:   Ms_project.Type,
 | 
	
		
			
				|  |  | +		Msid:   Ms_project.Msid,
 | 
	
		
			
				|  |  | +		Status: 1,
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	switch Ms_project.Type {
 | 
	
		
			
				|  |  | +	case 0: // 0:实时数据
 | 
	
		
			
				|  |  | +		var ms2_Project_list Ms2_Project_list
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project_list)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_project_0]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_project_0],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		for _, v := range ms2_Project_list.Data {
 | 
	
		
			
				|  |  | +			// 是否存在传感器  不存在 跳过
 | 
	
		
			
				|  |  | +			DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", "实时数据 传感器不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			// lost 字段为传感器离线状态(1代表离线,在线不传此字段),如果离线 温湿度时间数据为空
 | 
	
		
			
				|  |  | +			if _, lost_is := v["lost"]; lost_is {
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_link = 0
 | 
	
		
			
				|  |  | +				Device.Update_DeviceSensor(DeviceSensor_r, "T_link")
 | 
	
		
			
				|  |  | +				Warning.Add_DeviceLogs(103, *r_Device, fmt.Sprintf("[%d]%s: 传感器异常断开!", DeviceSensor_r.T_id, DeviceSensor_r.T_name))
 | 
	
		
			
				|  |  | +				//  发现 lost 此字段 代表 传感器异常!!!!!
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
 | 
	
		
			
				|  |  | +			UT := time.Unix(int64_, 0)
 | 
	
		
			
				|  |  | +			logs.Println(UT.Format("2006-01-02 15:04:05"))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(r_Device.T_sn, DeviceSensor_r.T_id)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", "记录数据 传感器参数不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//// 更新记录 - 缓存
 | 
	
		
			
				|  |  | +			DeviceData_t := Device.DeviceData_R{
 | 
	
		
			
				|  |  | +				T_t:    float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10),
 | 
	
		
			
				|  |  | +				T_rh:   float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10),
 | 
	
		
			
				|  |  | +				T_Site: lib.To_string(v["s"]),
 | 
	
		
			
				|  |  | +				T_time: UT,
 | 
	
		
			
				|  |  | +				T_sp:   DeviceSensorParameter_r.Id,
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			Device.RedisDeviceData_Set(r_Device.T_sn, DeviceSensor_r.T_id, DeviceData_t)
 | 
	
		
			
				|  |  | +			r_Device.T_Site = DeviceData_t.T_Site
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 传感器属于 连接状态
 | 
	
		
			
				|  |  | +			if DeviceSensor_r.T_link != 1 {
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_link = 1
 | 
	
		
			
				|  |  | +				Device.Update_DeviceSensor(DeviceSensor_r, "T_link")
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_Site")
 | 
	
		
			
				|  |  | +		// 反馈透传
 | 
	
		
			
				|  |  | +		NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 1: // 1:记录数据
 | 
	
		
			
				|  |  | +		var ms2_Project_list Ms2_Project_list
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project_list)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_project_1]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_project_1],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 监控状态 数据有效
 | 
	
		
			
				|  |  | +		if math.Abs(float64(time.Now().Unix()-ms2_Project_list.Dut)) < 30 {
 | 
	
		
			
				|  |  | +			r_Device.T_monitor = 1
 | 
	
		
			
				|  |  | +			Device.Update_Device(*r_Device, "T_monitor")
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 回复
 | 
	
		
			
				|  |  | +		ms2_Return.Status = 1
 | 
	
		
			
				|  |  | +		for _, v := range ms2_Project_list.Data {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
 | 
	
		
			
				|  |  | +			UT := time.Unix(int64_, 0)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//时间异常 大于10s,直接丢掉数据
 | 
	
		
			
				|  |  | +			if (UT.Unix() - time.Now().Unix()) > 60 {
 | 
	
		
			
				|  |  | +				ms2_Return.Status = 3
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 是否存在传感器  不存在 跳过
 | 
	
		
			
				|  |  | +			DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", "记录数据 传感器不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 获取传感器参数
 | 
	
		
			
				|  |  | +			DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(r_Device.T_sn, DeviceSensor_r.T_id)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", "记录数据 传感器参数不存在 跳过处理", r_Device.T_sn+lib.To_string(v["id"]))
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//// 更新记录 - 缓存
 | 
	
		
			
				|  |  | +			DeviceData_t := Device.DeviceData_R{
 | 
	
		
			
				|  |  | +				T_t:    float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10),
 | 
	
		
			
				|  |  | +				T_rh:   float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10),
 | 
	
		
			
				|  |  | +				T_Site: lib.To_string(v["s"]),
 | 
	
		
			
				|  |  | +				T_time: UT,
 | 
	
		
			
				|  |  | +				T_sp:   DeviceSensorParameter_r.Id,
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 验证 数据是否安装规定上传间隔
 | 
	
		
			
				|  |  | +			//DeviceData_Previous_r, is := Device.RedisDeviceData_Get(r_Device.T_sn+"_Node", DeviceSensor_r.T_id)
 | 
	
		
			
				|  |  | +			//if is {
 | 
	
		
			
				|  |  | +			//	//最新的时间 要大
 | 
	
		
			
				|  |  | +			//	if DeviceData_t.T_time.Unix() > DeviceData_Previous_r.T_time.Unix() {
 | 
	
		
			
				|  |  | +			//		DeviceParameter_r, is := Device.Read_DeviceParameter(r_Device.T_sn)
 | 
	
		
			
				|  |  | +			//		if is {
 | 
	
		
			
				|  |  | +			//			// 计算时间差
 | 
	
		
			
				|  |  | +			//			duration := DeviceData_Previous_r.T_time.Sub(DeviceData_t.T_time)
 | 
	
		
			
				|  |  | +			//			// 将时间差表示为秒
 | 
	
		
			
				|  |  | +			//			seconds := int(math.Abs(duration.Seconds()))
 | 
	
		
			
				|  |  | +			//			// 记录间隔 超过 保存间隔(+-10s)范围
 | 
	
		
			
				|  |  | +			//			//difference := int(math.Abs(float64(seconds - DeviceParameter_r.T_overA)))
 | 
	
		
			
				|  |  | +			//			if seconds < (DeviceParameter_r.T_saveT - 10) {
 | 
	
		
			
				|  |  | +			//				logs.Println(r_Device.T_sn, " 没有达到数据保存间隔,间隔:", seconds, " < ", DeviceParameter_r.T_saveT, "- 10", " 最新记录数据时间:", DeviceData_Previous_r.T_time)
 | 
	
		
			
				|  |  | +			//				if F_T_Exceeding(r_Device, DeviceData_t, DeviceSensorParameter_r) && F_H_Exceeding(r_Device, DeviceData_t, DeviceSensorParameter_r) {
 | 
	
		
			
				|  |  | +			//					logs.Println("!!! 跳过记录数据", "["+r_Device.T_sn+"]", string(msg))
 | 
	
		
			
				|  |  | +			//					continue
 | 
	
		
			
				|  |  | +			//				}
 | 
	
		
			
				|  |  | +			//			}
 | 
	
		
			
				|  |  | +			//		}
 | 
	
		
			
				|  |  | +			//	}
 | 
	
		
			
				|  |  | +			//}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			DeviceParameter_r, is := Device.Read_DeviceParameter(r_Device.T_sn)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				DeviceParameter_r.T_saveT = 1
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 判断是否超标 fsfa
 | 
	
		
			
				|  |  | +			//if F_T_Exceeding(r_Device, DeviceData_t, DeviceSensorParameter_r) && F_H_Exceeding(r_Device, DeviceData_t, DeviceSensorParameter_r) {
 | 
	
		
			
				|  |  | +			//	// 数据超标
 | 
	
		
			
				|  |  | +			//	logs.Println("数据超标")
 | 
	
		
			
				|  |  | +			//
 | 
	
		
			
				|  |  | +			//}else {
 | 
	
		
			
				|  |  | +			//
 | 
	
		
			
				|  |  | +			//}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 补丁,  判断 开机时间 ,根据  上电推送信息
 | 
	
		
			
				|  |  | +			_, is = Device.RedisDeviceData_Get(r_Device.T_sn+"_Redis", DeviceSensor_r.T_id)
 | 
	
		
			
				|  |  | +			Device.RedisDeviceData_Set(r_Device.T_sn+"_Redis", DeviceSensor_r.T_id, DeviceData_t) // 写缓存
 | 
	
		
			
				|  |  | +			if is {
 | 
	
		
			
				|  |  | +				// 计算周期时间
 | 
	
		
			
				|  |  | +				DeviceData_t.T_time = lib.PeriodTime(DeviceData_t.T_time, DeviceParameter_r.T_saveT/60)
 | 
	
		
			
				|  |  | +				logs.Println("计算周期时间:", UT.Format("2006-01-02 15:04:05"), "=>", DeviceData_t.T_time.Format("2006-01-02 15:04:05"), "保存间隔:", DeviceParameter_r.T_saveT)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if r_, _ := Device.Add_DeviceData(r_Device.T_sn, DeviceSensor_r.T_id, DeviceData_t, true); r_ {
 | 
	
		
			
				|  |  | +				// 被替换
 | 
	
		
			
				|  |  | +				Device.Add_DeviceDataOld(Device.DeviceDataOld{
 | 
	
		
			
				|  |  | +					T_sn:        r_Device.T_sn,
 | 
	
		
			
				|  |  | +					T_id:        DeviceSensor_r.T_id,
 | 
	
		
			
				|  |  | +					T_t:         DeviceData_t.T_t,
 | 
	
		
			
				|  |  | +					T_rh:        DeviceData_t.T_rh,
 | 
	
		
			
				|  |  | +					T_Site:      DeviceData_t.T_Site,
 | 
	
		
			
				|  |  | +					T_time:      UT,
 | 
	
		
			
				|  |  | +					T_operation: 4, // 4:忽略
 | 
	
		
			
				|  |  | +					T_uuid:      "",
 | 
	
		
			
				|  |  | +				})
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			r_Device.T_Site = DeviceData_t.T_Site // GPS
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 传感器属于 连接状态
 | 
	
		
			
				|  |  | +			if DeviceSensor_r.T_link != 1 {
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_link = 1
 | 
	
		
			
				|  |  | +				Device.Update_DeviceSensor(DeviceSensor_r, "T_link")
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_Site")
 | 
	
		
			
				|  |  | +		NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		break
 | 
	
		
			
				|  |  | +	case 2: // 2:设备报警
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project_list
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "设备报警", string(msg))
 | 
	
		
			
				|  |  | +			break
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		// 缓存报警ID,防止重复发送
 | 
	
		
			
				|  |  | +		if Warning.Redis_Warning_Repeat_T_sn_Msid(r_Device.T_sn + "|" + strconv.FormatInt(ms2_Project.Msid, 10)) {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", " 存在报警 跳过:", string(msg))
 | 
	
		
			
				|  |  | +			break
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		for _, v := range ms2_Project.Data {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			var Warning_r Warning.Warning
 | 
	
		
			
				|  |  | +			Warning_r.T_pid = r_Device.T_pid
 | 
	
		
			
				|  |  | +			Warning_r.T_tp = lib.To_int(v["tp"]) // 报警类型
 | 
	
		
			
				|  |  | +			Warning_r.T_sn = r_Device.T_sn
 | 
	
		
			
				|  |  | +			Warning_r.T_id = lib.To_int(v["id"])
 | 
	
		
			
				|  |  | +			Warning_r.T_Msid = Ms_project.Msid
 | 
	
		
			
				|  |  | +			Warning_r.T_D_name = r_Device.T_devName
 | 
	
		
			
				|  |  | +			Warning_r.T_State = 1
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 获取 传感器 参数
 | 
	
		
			
				|  |  | +			DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, Warning_r.T_id)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", " 传感器ID 不存在", string(msg))
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			Warning_r.T_DS_name = DeviceSensor_r.T_name
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 获取 传感器 配置参数
 | 
	
		
			
				|  |  | +			DeviceSensorParameter_r, is := Device.Read_DeviceSensorParameter(Warning_r.T_sn, Warning_r.T_id)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", " 传感器ID 配置参数不存在", string(msg))
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 温度湿度 封装
 | 
	
		
			
				|  |  | +			T := float32(math.Ceil(float64(lib.To_float32(v["t"])*10)) / 10)
 | 
	
		
			
				|  |  | +			RH := float32(math.Ceil(float64(lib.To_float32(v["h"])*10)) / 10)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 持续时间计算
 | 
	
		
			
				|  |  | +			time_int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
 | 
	
		
			
				|  |  | +			time_int64_f, _ := strconv.ParseInt(lib.To_string(v["fut"]), 10, 64)
 | 
	
		
			
				|  |  | +			if time_int64_f == 0 {
 | 
	
		
			
				|  |  | +				time_int64_f = time_int64_
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			Warning_r.T_fUt = time.Unix(time_int64_f, 0)
 | 
	
		
			
				|  |  | +			Warning_r.T_Ut = time.Unix(time_int64_, 0)
 | 
	
		
			
				|  |  | +			// 如果记录时间大于当前时间
 | 
	
		
			
				|  |  | +			//if time.Now().Unix() < time_int64_  {
 | 
	
		
			
				|  |  | +			//	Warning_r.T_Ut = time.Now()
 | 
	
		
			
				|  |  | +			//	Warning_r.T_Remark += "设备时间异常!"
 | 
	
		
			
				|  |  | +			//	// 推送同步 时间
 | 
	
		
			
				|  |  | +			//	go func() {
 | 
	
		
			
				|  |  | +			//		time.Sleep(time.Second * 3)
 | 
	
		
			
				|  |  | +			//		logs.Println(fmt.Sprintf("%s  时间相差:%d", r_Device.T_sn, int(math.Abs(float64(time_int64_-time.Now().Unix())))))
 | 
	
		
			
				|  |  | +			//		Warning.Add_DeviceLogs(103, *r_Device, fmt.Sprintf("时间相差:%d", int(math.Abs(float64(time_int64_-time.Now().Unix())))))
 | 
	
		
			
				|  |  | +			//		RTC_TimeStr(r_Device.T_sn, time.Now().Unix(), 99)
 | 
	
		
			
				|  |  | +			//	}()
 | 
	
		
			
				|  |  | +			//}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//时间异常 大于10s,直接丢掉数据
 | 
	
		
			
				|  |  | +			//if (time_int64_ - time.Now().Unix()) > 10 {
 | 
	
		
			
				|  |  | +			//	ms2_Return.Status = 3
 | 
	
		
			
				|  |  | +			//	logs.Println("时间异常 大于10s,直接丢掉数据", Warning_r.T_sn)
 | 
	
		
			
				|  |  | +			//	continue
 | 
	
		
			
				|  |  | +			//}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 查看 sn 是否有归属
 | 
	
		
			
				|  |  | +			if Warning_r.T_pid == 0 { //  寻找报警归属
 | 
	
		
			
				|  |  | +				if len(Warning_r.T_sn) > 10 {
 | 
	
		
			
				|  |  | +					//r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
 | 
	
		
			
				|  |  | +					//if err != nil {
 | 
	
		
			
				|  |  | +					//	logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn)
 | 
	
		
			
				|  |  | +					//}
 | 
	
		
			
				|  |  | +					Warning_r.T_pid = r_Device.T_pid
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 报警数据没有按照 设置间隔推送  (!!! 临时解决问题)
 | 
	
		
			
				|  |  | +			//if Warning_r.T_tp == 1 || Warning_r.T_tp == 2 || Warning_r.T_tp == 4 || Warning_r.T_tp == 5 {
 | 
	
		
			
				|  |  | +			//	DeviceParameter_r, is := Device.Read_DeviceParameter(r_Device.T_sn)
 | 
	
		
			
				|  |  | +			//	if !is {
 | 
	
		
			
				|  |  | +			//		logs.Println(r_Device.T_sn + " 没有设备参数,跳过")
 | 
	
		
			
				|  |  | +			//		continue
 | 
	
		
			
				|  |  | +			//	}
 | 
	
		
			
				|  |  | +			//	Warningr := Warning.Read_T_snT_idT_tp(Warning_r.T_sn, Warning_r.T_id, Warning_r.T_tp)
 | 
	
		
			
				|  |  | +			//	if Warningr.Id != 0 {
 | 
	
		
			
				|  |  | +			//		// 计算时间差
 | 
	
		
			
				|  |  | +			//		duration := time.Now().Sub(Warningr.T_Ut)
 | 
	
		
			
				|  |  | +			//
 | 
	
		
			
				|  |  | +			//		// 将时间差表示为秒
 | 
	
		
			
				|  |  | +			//		seconds := int(duration.Seconds())
 | 
	
		
			
				|  |  | +			//		if seconds < (DeviceParameter_r.T_overA - 3) {
 | 
	
		
			
				|  |  | +			//			logs.Println(r_Device.T_sn+" 没有达到设置报警间隔,跳过,间隔:", DeviceParameter_r.T_overA, " 时间差", seconds, "  New Warningr.Id ", Warningr.Id)
 | 
	
		
			
				|  |  | +			//			logs.Println("!!! 跳过没有达到设置报警间隔", "["+r_Device.T_sn+"]", string(msg))
 | 
	
		
			
				|  |  | +			//			continue
 | 
	
		
			
				|  |  | +			//		}
 | 
	
		
			
				|  |  | +			//	}
 | 
	
		
			
				|  |  | +			//
 | 
	
		
			
				|  |  | +			//}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 时间周期补丁 --------------------
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//tp	报警类型
 | 
	
		
			
				|  |  | +			// 1	温度超上限报警
 | 
	
		
			
				|  |  | +			// 2	温度超下限报警
 | 
	
		
			
				|  |  | +			// 3	温度恢复正常
 | 
	
		
			
				|  |  | +			// 4	湿度超上限报警
 | 
	
		
			
				|  |  | +			// 5	湿度超下限报警
 | 
	
		
			
				|  |  | +			// 6	湿度恢复正常
 | 
	
		
			
				|  |  | +			// 7	温度超上限预警
 | 
	
		
			
				|  |  | +			// 8	温度超下限预警
 | 
	
		
			
				|  |  | +			// 9	温度预警恢复正常
 | 
	
		
			
				|  |  | +			// 10	湿度超上限预警
 | 
	
		
			
				|  |  | +			// 11	湿度超下限预警
 | 
	
		
			
				|  |  | +			// 12	湿度预警恢复正常
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 首次时间缓存,防止乱传数据
 | 
	
		
			
				|  |  | +			Warning_r.T_fUt = Warning.Redis_WarningStart(Warning_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 补丁
 | 
	
		
			
				|  |  | +			if Warning_r.T_tp == 3 || Warning_r.T_tp == 6 || Warning_r.T_tp == 9 || Warning_r.T_tp == 12 { // 恢复
 | 
	
		
			
				|  |  | +				Ws_time,Ws_is := Warning.Redis_WarningStart_DelK(Warning_r)
 | 
	
		
			
				|  |  | +				if !Ws_is {
 | 
	
		
			
				|  |  | +					logs.Println("之前没有报警信息,跳过保存报警!")
 | 
	
		
			
				|  |  | +					continue
 | 
	
		
			
				|  |  | +				}else {
 | 
	
		
			
				|  |  | +					logs.Println("之前发送报警信息,首次报警时间:",Ws_time.Format("2006-01-02 15:04:05"))
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			} else {
 | 
	
		
			
				|  |  | +				logs_s := "报警时间首次时间:" + Warning_r.T_fUt.Format("2006-01-02 15:04:05") + ",报警时间:" + Warning_r.T_Ut.Format("2006-01-02 15:04:05")
 | 
	
		
			
				|  |  | +				Warning_r.T_Ut = lib.PeriodWTime(Warning_r.T_fUt,Warning_r.T_Ut,2)
 | 
	
		
			
				|  |  | +				logs.Println("报警时间计算处理:", logs_s,"=>",Warning_r.T_Ut.Format("2006-01-02 15:04:05"))
 | 
	
		
			
				|  |  | +				if Warning.Redis_WarningTimeListIs(Warning_r) {
 | 
	
		
			
				|  |  | +					logs.Println("报警时间已经存在,跳过!:",Warning_r.T_Ut.Format("2006-01-02 15:04:05"))
 | 
	
		
			
				|  |  | +					continue
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//当期湿度:88.0%(↓23.0~↑73.0),已持续1小时33秒
 | 
	
		
			
				|  |  | +			time_int64_f_x := (int(Warning_r.T_Ut.Unix()) - int(Warning_r.T_fUt.Unix()))
 | 
	
		
			
				|  |  | +			logs.Println("持续时间:", time_int64_f_x)
 | 
	
		
			
				|  |  | +			if time_int64_f_x > 0 {
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark += "已持续 " + lib.MinuteToDataTime(time_int64_f_x)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//// 报警处理
 | 
	
		
			
				|  |  | +			WarningNotice.WarningCompanyNotice(&Warning_r, T, RH)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//封装具体信息
 | 
	
		
			
				|  |  | +			switch {
 | 
	
		
			
				|  |  | +			case Warning_r.T_tp == 1, Warning_r.T_tp == 2, Warning_r.T_tp == 3:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "当前温度:" + strconv.FormatFloat(float64(T), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_Tlower), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_Tupper), 'f', -1, 32) + ")," + Warning_r.T_Remark
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			case Warning_r.T_tp == 4, Warning_r.T_tp == 5, Warning_r.T_tp == 6:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "当前湿度:" + strconv.FormatFloat(float64(RH), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_RHlower), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_RHupper), 'f', -1, 32) + ")," + Warning_r.T_Remark
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			case Warning_r.T_tp == 7, Warning_r.T_tp == 8, Warning_r.T_tp == 9:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "当前温度:" + strconv.FormatFloat(float64(T), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_tprel), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_tpreu), 'f', -1, 32) + ")," + Warning_r.T_Remark
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			case Warning_r.T_tp == 10, Warning_r.T_tp == 11, Warning_r.T_tp == 12:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "当前湿度:" + strconv.FormatFloat(float64(RH), 'f', -1, 32) + " (↓" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_hprel), 'f', -1, 32) + "~↑" + strconv.FormatFloat(float64(DeviceSensorParameter_r.T_hpreu), 'f', -1, 32) + ")," + Warning_r.T_Remark
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			//记录温湿度
 | 
	
		
			
				|  |  | +			Warning_r.T_t = T
 | 
	
		
			
				|  |  | +			Warning_r.T_rh = RH
 | 
	
		
			
				|  |  | +			Warning_r.T_sp = DeviceSensorParameter_r.Id
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 添加报警
 | 
	
		
			
				|  |  | +			_, err = Warning.Add_Warning(Warning_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//// 不能 同时增加 记录数据,因为缺失GPS数据
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		break
 | 
	
		
			
				|  |  | +	case 3: // 3:读取主机参数
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		T_name := lib.To_string(ms2_Project.Data["name"])
 | 
	
		
			
				|  |  | +		if len(T_name) == 0 {
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if T_name == "*" {
 | 
	
		
			
				|  |  | +			T_name = r_Device.T_devName
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if r_Device.T_devName == "未知类型" {
 | 
	
		
			
				|  |  | +			T_name = Product.Read_ProductType_Get(r_Device.T_model)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if T_name == r_Device.T_sn {
 | 
	
		
			
				|  |  | +			T_name = Product.Read_ProductType_Get(r_Device.T_model)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		r_Device.T_devName = T_name
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Deviceparameter := Device.DeviceParameter{
 | 
	
		
			
				|  |  | +			T_sn:       r_Device.T_sn,
 | 
	
		
			
				|  |  | +			T_name:     T_name,
 | 
	
		
			
				|  |  | +			T_saveT:    lib.To_int(ms2_Project.Data["saveT"]),     //数据保存间隔 (s)*最小值60
 | 
	
		
			
				|  |  | +			T_overA:    lib.To_int(ms2_Project.Data["overA"]),     //超限报警触发间隔 (s)*最小值60
 | 
	
		
			
				|  |  | +			T_outA:     lib.To_int(ms2_Project.Data["outA"]),      //断电报警触发间隔  (s)*最小值60
 | 
	
		
			
				|  |  | +			T_lostA:    lib.To_int(ms2_Project.Data["lostA"]),     //传感器掉线报警触发间隔 (s)*最小值60
 | 
	
		
			
				|  |  | +			T_warnD:    lib.To_int(ms2_Project.Data["warnD"]),     // 超限预警延时  (s)*
 | 
	
		
			
				|  |  | +			T_scan:     lib.To_int(ms2_Project.Data["scan"]),      // 显示轮播间隔   s(1~240) 默认:5 *
 | 
	
		
			
				|  |  | +			T_warn:     lib.To_int(ms2_Project.Data["warn"]),      // 超限预警触发间隔 (s)*为0时预警只触发一次 *最小值60
 | 
	
		
			
				|  |  | +			T_bat:      lib.To_int(ms2_Project.Data["bat"]),       //电池电量下限   (%)*
 | 
	
		
			
				|  |  | +			T_dormancy: lib.To_int(ms2_Project.Data["dormancy"]),  //是否进入休眠  0:关闭 1:开启    默认:0
 | 
	
		
			
				|  |  | +			T_snum:     lib.To_int(ms2_Project.Data["snum"]),      // 【管理主机】 - 传感器数量  (范围0~255)
 | 
	
		
			
				|  |  | +			T_btname:   lib.To_string(ms2_Project.Data["btname"]), //--蓝牙打印机名称 用于移动端连接蓝牙打印机
 | 
	
		
			
				|  |  | +			T_btsid:    lib.To_int(ms2_Project.Data["btsid"]),     //--打印机服务号 用于移动端连接蓝牙打印机
 | 
	
		
			
				|  |  | +			T_btchar:   lib.To_int(ms2_Project.Data["btchar"]),    //--蓝牙特征码	用于移动端连接蓝牙打印机
 | 
	
		
			
				|  |  | +			//T_speed:     lib.To_int(ms2_Project.Data["speed"]),     //--蓝牙特征码	用于移动端连接蓝牙打印机
 | 
	
		
			
				|  |  | +			T_State:     1,
 | 
	
		
			
				|  |  | +			T_SendState: 1,
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if Device.Add_DeviceParameter_fun(Deviceparameter) {
 | 
	
		
			
				|  |  | +			Warning.Add_DeviceLogs(102, *r_Device, "修改主机参数")
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_devName")
 | 
	
		
			
				|  |  | +		// 获取所有传感器
 | 
	
		
			
				|  |  | +		//Read_DeviceSensorParameter(r_Device.T_sn)
 | 
	
		
			
				|  |  | +		// 不用反馈
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 4: // 4:设置主机参数 - 反馈
 | 
	
		
			
				|  |  | +		var ms2_Return_r Ms2_Return
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Return_r)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "JSON反序列化失败[4:设置主机参数]", string(msg), err.Error())
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		DeviceParameter_r, err := Device.Read_DeviceParameter_SN_Msid(r_Device.T_sn, ms2_Return_r.Msid)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", " SN_Msid Err![4:设置主机参数]", string(msg), err.Error())
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		DeviceParameter_r.T_SendState = 2
 | 
	
		
			
				|  |  | +		// 判断参数   //1设置成功,0设置失败
 | 
	
		
			
				|  |  | +		if ms2_Return_r.Status == 1 {
 | 
	
		
			
				|  |  | +			DeviceParameter_r.T_SendState = 1
 | 
	
		
			
				|  |  | +			r_Device.T_devName = DeviceParameter_r.T_name
 | 
	
		
			
				|  |  | +			Device.Update_Device(*r_Device, "T_devName")
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 状态更新
 | 
	
		
			
				|  |  | +		Device.Update_DeviceParameter_(DeviceParameter_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 同步参数
 | 
	
		
			
				|  |  | +		DeviceParameter_r.T_State = 1
 | 
	
		
			
				|  |  | +		Device.Add_DeviceParameter_fun(DeviceParameter_r)
 | 
	
		
			
				|  |  | +		Warning.Add_DeviceLogs(102, *r_Device, "修改主机参数-反馈成功")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 取消掉 其他参数
 | 
	
		
			
				|  |  | +		Device.UPDATE_DeviceParameter_SN_T_SendState_2_sql(r_Device.T_sn)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 5: // 读取传感器参数
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project_list
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		for _, v := range ms2_Project.Data {
 | 
	
		
			
				|  |  | +			DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, lib.To_int(v["id"]))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			T_name := lib.To_string(v["name"])
 | 
	
		
			
				|  |  | +			if len(T_name) == 0 {
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			if T_name == "*" {
 | 
	
		
			
				|  |  | +				T_name = r_Device.T_sn + "-" + lib.To_string(v["id"])
 | 
	
		
			
				|  |  | +				if is {
 | 
	
		
			
				|  |  | +					T_name = DeviceSensor_r.T_name
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 不存在
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_sn = r_Device.T_sn
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_pid = r_Device.T_pid
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_id = lib.To_int(v["id"])
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_name = T_name
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_datashow = 1
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_sort = 1
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_monitor = r_Device.T_monitor
 | 
	
		
			
				|  |  | +				// 设备同步参数
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_Dattery = r_Device.T_Dattery
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_Site = r_Device.T_Site
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_State = r_Device.T_State
 | 
	
		
			
				|  |  | +				DeviceSensor_r.T_online = r_Device.T_online
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				DeviceSensor_r.Id = Device.Add_DeviceSensor(*r_Device, DeviceSensor_r)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 记录传感器
 | 
	
		
			
				|  |  | +			Devicesensorparameter := Device.DeviceSensorParameter{
 | 
	
		
			
				|  |  | +				T_sn:   r_Device.T_sn,
 | 
	
		
			
				|  |  | +				T_id:   lib.To_int(v["id"]),
 | 
	
		
			
				|  |  | +				T_name: T_name,
 | 
	
		
			
				|  |  | +				// 报警
 | 
	
		
			
				|  |  | +				T_Tlower:  lib.To_float32(v["tl"]),
 | 
	
		
			
				|  |  | +				T_Tupper:  lib.To_float32(v["tu"]),
 | 
	
		
			
				|  |  | +				T_RHlower: lib.To_float32(v["hl"]),
 | 
	
		
			
				|  |  | +				T_RHupper: lib.To_float32(v["hu"]),
 | 
	
		
			
				|  |  | +				// 预警
 | 
	
		
			
				|  |  | +				T_enprel:     lib.To_int(v["enprel"]),
 | 
	
		
			
				|  |  | +				T_tprel:      lib.To_float32(v["tprel"]),
 | 
	
		
			
				|  |  | +				T_tpreu:      lib.To_float32(v["tpreu"]),
 | 
	
		
			
				|  |  | +				T_hprel:      lib.To_float32(v["hprel"]),
 | 
	
		
			
				|  |  | +				T_hpreu:      lib.To_float32(v["hpreu"]),
 | 
	
		
			
				|  |  | +				T_enprelnote: lib.To_int(v["enprelnote"]),
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				//T_speed: lib.To_int(v["speed"]),
 | 
	
		
			
				|  |  | +				//T_sense: lib.To_int(v["sense"]),
 | 
	
		
			
				|  |  | +				T_en:   lib.To_int(v["en"]),
 | 
	
		
			
				|  |  | +				T_free: lib.To_int(v["free"]),
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				T_Msid:      ms2_Project.Msid, // 消息识别ID
 | 
	
		
			
				|  |  | +				T_SendState: 1,                // 发送状态  0 待发送   1 发送成功  2 失败  3 覆盖
 | 
	
		
			
				|  |  | +				T_State:     1,                //  1 系统获取   2 用户提交
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			// 强制修改 是否启用预警记录数据  1:启用  0:关闭  默认:1
 | 
	
		
			
				|  |  | +			if Devicesensorparameter.T_enprelnote != 0 {
 | 
	
		
			
				|  |  | +				Devicesensorparameter.T_enprelnote = 1
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 过滤参数 相同
 | 
	
		
			
				|  |  | +			id := Device.Add_DeviceSensorParameter_fun(Devicesensorparameter)
 | 
	
		
			
				|  |  | +			Warning.Add_DeviceLogs(102, *r_Device, "修改设备参数")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			logs.Println("DeviceSensorParameter ID:", id)
 | 
	
		
			
				|  |  | +			// 对 传感器 的状态更新
 | 
	
		
			
				|  |  | +			DeviceSensor_r.T_name = T_name
 | 
	
		
			
				|  |  | +			DeviceSensor_r.T_sp = id
 | 
	
		
			
				|  |  | +			Device.Update_DeviceSensor(DeviceSensor_r, "T_name", "T_sp")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		go func() {
 | 
	
		
			
				|  |  | +			time.Sleep(time.Second * 3)
 | 
	
		
			
				|  |  | +			Get_Device_Realtime(r_Device.T_sn)
 | 
	
		
			
				|  |  | +		}()
 | 
	
		
			
				|  |  | +		// 不用反馈
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 6: // 设置传感器参数
 | 
	
		
			
				|  |  | +		var ms2_Return_r Ms2_Return
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Return_r)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		r, is := Device.Read_DeviceSensorParameter_SN_T_id_Msid(r_Device.T_sn, ms2_Return_r.Msid)
 | 
	
		
			
				|  |  | +		if !is {
 | 
	
		
			
				|  |  | +			logs.Println("设备配置", "设置设备参数  SN_Msid Err! [Ms_Parameter_r]", string(msg))
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		r.T_SendState = 2
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 判断参数   //1设置成功,0设置失败
 | 
	
		
			
				|  |  | +		if ms2_Return_r.Status == 1 {
 | 
	
		
			
				|  |  | +			r.T_SendState = 1
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r.T_sn, r.T_id)
 | 
	
		
			
				|  |  | +			if !is {
 | 
	
		
			
				|  |  | +				logs.Println("设备配置", "设置传感器参数  r_DeviceSensor Err! [Ms_Parameter_r]", r.T_sn+"|"+strconv.Itoa(r.T_id))
 | 
	
		
			
				|  |  | +				logs.Println("设置传感器参数  r_DeviceSensor Err![Ms_Parameter_r],err=", r.T_sn+"|"+strconv.Itoa(r.T_id))
 | 
	
		
			
				|  |  | +				logs.Println("MqttServer", r_Device.T_sn+"["+lib.To_string(r.T_id)+"] 没有找到传感器:", string(msg))
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 添加一份最新的
 | 
	
		
			
				|  |  | +			r.T_State = 0
 | 
	
		
			
				|  |  | +			id := Device.Add_DeviceSensorParameter_fun(r)
 | 
	
		
			
				|  |  | +			Warning.Add_DeviceLogs(102, *r_Device, "修改设备参数")
 | 
	
		
			
				|  |  | +			logs.Println("DeviceSensorParameter ID:", id)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 对 传感器 的状态更新
 | 
	
		
			
				|  |  | +			DeviceSensor_r.T_name = r.T_name
 | 
	
		
			
				|  |  | +			DeviceSensor_r.T_sp = id
 | 
	
		
			
				|  |  | +			Device.Update_DeviceSensor(DeviceSensor_r, "T_name", "T_sp")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 状态更新
 | 
	
		
			
				|  |  | +		Device.Update_DeviceSensorParameter(r, "T_SendState")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 取消掉 其他参数
 | 
	
		
			
				|  |  | +		Device.UPDATE_DeviceSensorParamete_SN_T_SendState_2_sql(r_Device.T_sn, strconv.Itoa(r.T_id))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		//// 更新主机参数
 | 
	
		
			
				|  |  | +		//go func() {
 | 
	
		
			
				|  |  | +		//	time.Sleep(time.Second * 1)
 | 
	
		
			
				|  |  | +		//	Read_DeviceSensorParameter3(r_Device.T_sn)
 | 
	
		
			
				|  |  | +		//}()
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 12:
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project_list
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_project_2]", string(msg), err.Error())
 | 
	
		
			
				|  |  | +			break
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 缓存报警ID,防止重复发送
 | 
	
		
			
				|  |  | +		if Warning.Redis_Warning_Repeat_T_sn_Msid(r_Device.T_sn + "|" + strconv.FormatInt(ms2_Project.Msid, 10)) {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", " 存在报警 跳过:", string(msg))
 | 
	
		
			
				|  |  | +			break
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		//	{
 | 
	
		
			
				|  |  | +		//		"sn": "YD0123456789",
 | 
	
		
			
				|  |  | +		//		"type": 12,
 | 
	
		
			
				|  |  | +		//		"msid": 125456001,
 | 
	
		
			
				|  |  | +		//		"data": [
 | 
	
		
			
				|  |  | +		//	{
 | 
	
		
			
				|  |  | +		//	"tp":7, // 对照 报警类型对应表
 | 
	
		
			
				|  |  | +		//	"msg": "报警信息或故障信息", //如:1号传感器故障 (非必填,但是 要带字段)
 | 
	
		
			
				|  |  | +		//	"ut": 1626857518 //产生时间
 | 
	
		
			
				|  |  | +		//	},
 | 
	
		
			
				|  |  | +		//	..........
 | 
	
		
			
				|  |  | +		//]
 | 
	
		
			
				|  |  | +		//}
 | 
	
		
			
				|  |  | +		for _, v := range ms2_Project.Data {
 | 
	
		
			
				|  |  | +			var Warning_r Warning.Warning
 | 
	
		
			
				|  |  | +			Warning_r.T_tp = lib.To_int(v["tp"]) + 100 // 报警类型
 | 
	
		
			
				|  |  | +			Warning_r.T_sn = r_Device.T_sn
 | 
	
		
			
				|  |  | +			Warning_r.T_id = -1
 | 
	
		
			
				|  |  | +			Warning_r.T_Msid = Ms_project.Msid
 | 
	
		
			
				|  |  | +			Warning_r.T_D_name = r_Device.T_devName
 | 
	
		
			
				|  |  | +			Warning_r.T_DS_name = r_Device.T_devName
 | 
	
		
			
				|  |  | +			time_int64_, _ := strconv.ParseInt(lib.To_string(v["ut"]), 10, 64)
 | 
	
		
			
				|  |  | +			Warning_r.T_Ut = time.Unix(time_int64_, 0)
 | 
	
		
			
				|  |  | +			Warning_r.T_Remark = lib.To_string(v["msg"])
 | 
	
		
			
				|  |  | +			Warning_r.T_State = 1
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			//时间异常 大于10s,直接丢掉数据
 | 
	
		
			
				|  |  | +			if (time_int64_ - time.Now().Unix()) > 10 {
 | 
	
		
			
				|  |  | +				ms2_Return.Status = 3
 | 
	
		
			
				|  |  | +				continue
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 持续时间计算
 | 
	
		
			
				|  |  | +			time_int64_f, _ := strconv.ParseInt(lib.To_string(v["fut"]), 10, 64)
 | 
	
		
			
				|  |  | +			if time_int64_f == 0 {
 | 
	
		
			
				|  |  | +				time_int64_f = time_int64_
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			Warning_r.T_fUt = time.Unix(time_int64_f, 0)
 | 
	
		
			
				|  |  | +			time_int64_f_x := (int(time_int64_) - int(time_int64_f))
 | 
	
		
			
				|  |  | +			logs.Println(strconv.FormatInt(Warning_r.T_Msid, 10)+"持续时间:", time_int64_f_x)
 | 
	
		
			
				|  |  | +			if time_int64_f_x > 0 {
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark += " 已持续" + lib.MinuteToDataTime(time_int64_f_x)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 查看 sn 是否有归属
 | 
	
		
			
				|  |  | +			if Warning_r.T_pid == 0 { //  寻找报警归属
 | 
	
		
			
				|  |  | +				if len(Warning_r.T_sn) > 10 {
 | 
	
		
			
				|  |  | +					r_Device, err := Device.Read_Device_ByT_sn(Warning_r.T_sn)
 | 
	
		
			
				|  |  | +					if err != nil {
 | 
	
		
			
				|  |  | +						logs.Println("MessageDisconnected 没有该设备:", Warning_r.T_sn)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +					Warning_r.T_pid = r_Device.T_pid
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			switch Warning_r.T_tp {
 | 
	
		
			
				|  |  | +			case 101: // 主机报警
 | 
	
		
			
				|  |  | +				Warning_r.T_State = 3
 | 
	
		
			
				|  |  | +				WarningNotice.WarningToAdminId(&Warning_r, []int{}) // 发送给 绑定公司管理员
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			case 115:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "开始监测任务"
 | 
	
		
			
				|  |  | +				//  1   start:开始监测任务,
 | 
	
		
			
				|  |  | +				// 防止没有结束命令
 | 
	
		
			
				|  |  | +				DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
 | 
	
		
			
				|  |  | +				logs.Println("DeviceTask_r.id:", DeviceTask_r.Id)
 | 
	
		
			
				|  |  | +				if ok {
 | 
	
		
			
				|  |  | +					if DeviceTask_r.T_Ut_end.IsZero() {
 | 
	
		
			
				|  |  | +						logs.Println("没有结束 强制 更新上一条")
 | 
	
		
			
				|  |  | +						DeviceTask_r.T_Ut_end = Warning_r.T_Ut
 | 
	
		
			
				|  |  | +						Device.Update_DeviceTask(DeviceTask_r)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				logs.Println("插入新任务")
 | 
	
		
			
				|  |  | +				// 插入开始 任务
 | 
	
		
			
				|  |  | +				dt := Device.DeviceTask{
 | 
	
		
			
				|  |  | +					T_sn:       r_Device.T_sn,
 | 
	
		
			
				|  |  | +					T_Ut_start: Warning_r.T_Ut,
 | 
	
		
			
				|  |  | +					T_Ut:       Warning_r.T_Ut,
 | 
	
		
			
				|  |  | +					T_State:    1,
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				Device.Add_DeviceTask(dt)
 | 
	
		
			
				|  |  | +				r_Device.T_monitor = 1
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			case 116:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "中途打印任务"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
 | 
	
		
			
				|  |  | +				if ok {
 | 
	
		
			
				|  |  | +					if DeviceTask_r.T_Ut_end.IsZero() {
 | 
	
		
			
				|  |  | +						DeviceTask_r.T_Ut_end = Warning_r.T_Ut
 | 
	
		
			
				|  |  | +						DeviceTask_r.T_State = 3
 | 
	
		
			
				|  |  | +						Device.Update_DeviceTask(DeviceTask_r)
 | 
	
		
			
				|  |  | +						// 插入开始 任务
 | 
	
		
			
				|  |  | +						dt := Device.DeviceTask{
 | 
	
		
			
				|  |  | +							T_sn:       r_Device.T_sn,
 | 
	
		
			
				|  |  | +							T_Ut_start: DeviceTask_r.T_Ut_start,
 | 
	
		
			
				|  |  | +							T_Ut:       Warning_r.T_Ut,
 | 
	
		
			
				|  |  | +							T_State:    1,
 | 
	
		
			
				|  |  | +						}
 | 
	
		
			
				|  |  | +						logs.Println("插入新任务")
 | 
	
		
			
				|  |  | +						Device.Add_DeviceTask(dt)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				r_Device.T_monitor = 1
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			case 117:
 | 
	
		
			
				|  |  | +				Warning_r.T_Remark = "结束监测任务"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				// 防止没有结束命令
 | 
	
		
			
				|  |  | +				DeviceTask_r, ok := Device.Read_DeviceTask_All_Limit_1(r_Device.T_sn)
 | 
	
		
			
				|  |  | +				if ok {
 | 
	
		
			
				|  |  | +					if DeviceTask_r.T_Ut_end.IsZero() {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +						DeviceTask_r.T_Ut_end = Warning_r.T_Ut
 | 
	
		
			
				|  |  | +						DeviceTask_r.T_State = 2
 | 
	
		
			
				|  |  | +						Device.Update_DeviceTask(DeviceTask_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				// 遍历所有探头,停止记录后 将最后一条数据写入关机数据
 | 
	
		
			
				|  |  | +				DeviceSensorList := Device.Read_DeviceSensor_ByTsn(r_Device.T_sn)
 | 
	
		
			
				|  |  | +				for _, vd := range DeviceSensorList {
 | 
	
		
			
				|  |  | +					DeviceData_t, is := Device.RedisDeviceData_Get(r_Device.T_sn+"_Redis", vd.T_id)
 | 
	
		
			
				|  |  | +					if is {
 | 
	
		
			
				|  |  | +						Device.Add_DeviceData(r_Device.T_sn, vd.T_id, DeviceData_t, true)
 | 
	
		
			
				|  |  | +						Device.RedisDeviceData_Del(r_Device.T_sn+"_Redis", vd.T_id) // 清除缓存标志
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				r_Device.T_monitor = 0
 | 
	
		
			
				|  |  | +				break
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			Device.Update_Device(*r_Device, "T_monitor")
 | 
	
		
			
				|  |  | +			//// 推送运维信息
 | 
	
		
			
				|  |  | +			WarningNotice.WarningCompanyNotice(&Warning_r, 0, 0)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			// 添加报警
 | 
	
		
			
				|  |  | +			_, err = Warning.Add_Warning(Warning_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		break
 | 
	
		
			
				|  |  | +	case 13: // 上电推送信息
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_Parameter],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		var Warning_r Warning.Warning
 | 
	
		
			
				|  |  | +		Warning_r.T_tp = 106 // 报警类型
 | 
	
		
			
				|  |  | +		Warning_r.T_sn = r_Device.T_sn
 | 
	
		
			
				|  |  | +		Warning_r.T_D_name = r_Device.T_devName
 | 
	
		
			
				|  |  | +		Warning_r.T_id = 0
 | 
	
		
			
				|  |  | +		Warning_r.T_DS_name = ""
 | 
	
		
			
				|  |  | +		Warning_r.T_Msid = Ms_project.Msid
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		r_Device.T_model = lib.To_string(ms2_Project.Data["model"])
 | 
	
		
			
				|  |  | +		r_Device.T_sver = lib.To_string(ms2_Project.Data["sver"])
 | 
	
		
			
				|  |  | +		r_Device.T_hver = lib.To_string(ms2_Project.Data["hver"])
 | 
	
		
			
				|  |  | +		r_Device.T_imei = lib.To_string(ms2_Project.Data["imei"])
 | 
	
		
			
				|  |  | +		r_Device.T_iccid = lib.To_string(ms2_Project.Data["iccid"])
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_model", "T_sver", "T_hver", "T_imei", "T_iccid")
 | 
	
		
			
				|  |  | +		//  加入日志
 | 
	
		
			
				|  |  | +		//	  "rtc" : 1671173184,//设备时间
 | 
	
		
			
				|  |  | +		//    "rtl" : 3,//设备开机累计运行秒数
 | 
	
		
			
				|  |  | +		//    "st" : 1671173177,//设备开机时间
 | 
	
		
			
				|  |  | +		//    "lst" : 0//设备上次关机时间
 | 
	
		
			
				|  |  | +		UT_rtc := time.Unix(int64(lib.To_int64(ms2_Project.Data["rtc"])), 0).Format("2006-01-02 15:04:05")
 | 
	
		
			
				|  |  | +		UT_st := time.Unix(int64(lib.To_int64(ms2_Project.Data["st"])), 0).Format("2006-01-02 15:04:05")
 | 
	
		
			
				|  |  | +		UT_lst := time.Unix(int64(lib.To_int64(ms2_Project.Data["lst"])), 0).Format("2006-01-02 15:04:05")
 | 
	
		
			
				|  |  | +		UT_rtl := lib.To_string(ms2_Project.Data["rtl"])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		str := "----- 设备信息 -----\n"
 | 
	
		
			
				|  |  | +		str += "设备型号:" + r_Device.T_model + " |\n"
 | 
	
		
			
				|  |  | +		str += "软件版本:" + r_Device.T_sver + " |\n"
 | 
	
		
			
				|  |  | +		str += "硬件版本:" + r_Device.T_hver + " |\n"
 | 
	
		
			
				|  |  | +		str += "模组imei:" + r_Device.T_imei + " |\n"
 | 
	
		
			
				|  |  | +		str += "sim卡号:" + r_Device.T_iccid + " |\n"
 | 
	
		
			
				|  |  | +		str += "设备时间:" + UT_rtc + " |\n"
 | 
	
		
			
				|  |  | +		str += "设备开机时间:" + UT_st + " |\n"
 | 
	
		
			
				|  |  | +		str += "设备上次关机时间:" + UT_lst + " |\n"
 | 
	
		
			
				|  |  | +		str += "设备开机累计运行秒数:" + UT_rtl + " |\n"
 | 
	
		
			
				|  |  | +		Warning_r.T_Remark = str
 | 
	
		
			
				|  |  | +		Warning_r.T_Ut = time.Now()
 | 
	
		
			
				|  |  | +		Warning_r.T_State = 1
 | 
	
		
			
				|  |  | +		// 添加报警
 | 
	
		
			
				|  |  | +		_, err = Warning.Add_Warning(Warning_r)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 15: // 网络延时测试
 | 
	
		
			
				|  |  | +		Rt_Project_Data_r := Ms2_Project{
 | 
	
		
			
				|  |  | +			Type: Ms_project.Type,
 | 
	
		
			
				|  |  | +			Msid: Ms_project.Msid,
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		var Data map[string]interface{}
 | 
	
		
			
				|  |  | +		Data = make(map[string]interface{})
 | 
	
		
			
				|  |  | +		Data["ut"] = Ms_project.Dut
 | 
	
		
			
				|  |  | +		Rt_Project_Data_r.Data = Data
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 回复
 | 
	
		
			
				|  |  | +		jsonStu, err := json.Marshal(Rt_Project_Data_r)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Mqtt_publish(r_Device.T_sn, string(jsonStu))
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 88: // 临时授权
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Parameter]", string(msg))
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		url := "192.168.0.33:3991/CreateAuth"
 | 
	
		
			
				|  |  | +		method := "POST"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		payload := strings.NewReader("model=" + lib.To_string(ms2_Project.Data["model"]) + "&sver=" + lib.To_string(ms2_Project.Data["sver"]) + "&hver=" + lib.To_string(ms2_Project.Data["hver"]) + "&imei=" + lib.To_string(ms2_Project.Data["imei"]) + "&iccid=" + lib.To_string(ms2_Project.Data["iccid"]) + "&sn=" + lib.To_string(ms2_Project.Data["sn"]))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		client := &http.Client{}
 | 
	
		
			
				|  |  | +		req, err := http.NewRequest(method, url, payload)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			fmt.Println(err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		res, err := client.Do(req)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			fmt.Println(err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		defer res.Body.Close()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		body, err := ioutil.ReadAll(res.Body)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			fmt.Println(err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		fmt.Println(string(body))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 实体类
 | 
	
		
			
				|  |  | +		type Ms2_Projectccc struct {
 | 
	
		
			
				|  |  | +			Pass   string `json:"psaa"`
 | 
	
		
			
				|  |  | +			Status string `json:"status"`
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		var ms2_Projectccc Ms2_Projectccc
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err = json.Unmarshal(body, &ms2_Projectccc)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms2_Projectccc]", string(msg))
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Rt_Project_Data_r := Ms2_Project{
 | 
	
		
			
				|  |  | +			Type: Ms_project.Type,
 | 
	
		
			
				|  |  | +			Msid: Ms_project.Msid,
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		var Data map[string]interface{}
 | 
	
		
			
				|  |  | +		Data = make(map[string]interface{})
 | 
	
		
			
				|  |  | +		Data["Pass"] = ms2_Projectccc.Pass
 | 
	
		
			
				|  |  | +		Data["Status"] = ms2_Projectccc.Status
 | 
	
		
			
				|  |  | +		Rt_Project_Data_r.Data = Data
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 回复
 | 
	
		
			
				|  |  | +		jsonStu, err := json.Marshal(Rt_Project_Data_r)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Mqtt_publish(r_Device.T_sn, string(jsonStu))
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 98: // 时间同步
 | 
	
		
			
				|  |  | +		RTC_TimeStr(r_Device.T_sn, Ms_project.Msid, 98)
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	case 100: // 设备实时状态推送
 | 
	
		
			
				|  |  | +		var ms2_Project Ms2_Project
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		err := json.Unmarshal(msg, &ms2_Project)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			logs.Println("MqttServer", "JSON反序列化失败[Ms_project_]", string(msg))
 | 
	
		
			
				|  |  | +			logs.Println("JSON反序列化失败[Ms_project],err=", err)
 | 
	
		
			
				|  |  | +			return
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		r_Device.T_Dattery = lib.To_int(ms2_Project.Data["power"]) //#设备剩余电量百分比
 | 
	
		
			
				|  |  | +		r_Device.T_rssi = lib.To_string(ms2_Project.Data["rssi"])  //#信号强度
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_Dattery", "T_rssi")
 | 
	
		
			
				|  |  | +		// 反馈透传
 | 
	
		
			
				|  |  | +		NatsServer.Mqtt_DeviceReal(r_Device.T_sn, msg)
 | 
	
		
			
				|  |  | +		Device.Update_Device(*r_Device, "T_Dattery")
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	default:
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 回复
 | 
	
		
			
				|  |  | +	jsonStu, err := json.Marshal(ms2_Return)
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		logs.PrintlnError("MqttServer", "json.Marshal ", string(msg))
 | 
	
		
			
				|  |  | +		logs.Println("回复失败 [Ms_project_0],err=", err)
 | 
	
		
			
				|  |  | +		return
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	Mqtt_publish(r_Device.T_sn, string(jsonStu))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func F_T_Exceeding(r_Device *Device.Device, DeviceData_t Device.DeviceData_R, DeviceSensorParameter_r Device.DeviceSensorParameter) bool {
 | 
	
		
			
				|  |  | +	if r_Device.T_ist == 1 { // 温度   1开启   2关闭
 | 
	
		
			
				|  |  | +		if !(DeviceSensorParameter_r.T_Tupper < DeviceData_t.T_t || DeviceData_t.T_t < DeviceSensorParameter_r.T_Tlower) {
 | 
	
		
			
				|  |  | +			logs.Println(r_Device.T_sn+" 温度没有超标,且不在保存间隔范围 :", DeviceSensorParameter_r.T_Tupper, DeviceData_t.T_t, DeviceSensorParameter_r.T_Tlower)
 | 
	
		
			
				|  |  | +			return true
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return false
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +func F_H_Exceeding(r_Device *Device.Device, DeviceData_t Device.DeviceData_R, DeviceSensorParameter_r Device.DeviceSensorParameter) bool {
 | 
	
		
			
				|  |  | +	if r_Device.T_ish == 1 { // 湿度   1开启   2关闭
 | 
	
		
			
				|  |  | +		if !(DeviceSensorParameter_r.T_RHupper < DeviceData_t.T_rh || DeviceData_t.T_rh < DeviceSensorParameter_r.T_RHlower) {
 | 
	
		
			
				|  |  | +			logs.Println(r_Device.T_sn+" 湿度没有超标,且不在保存间隔范围 :", DeviceSensorParameter_r.T_RHupper, DeviceData_t.T_rh, DeviceSensorParameter_r.T_RHlower)
 | 
	
		
			
				|  |  | +			return true
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return false
 | 
	
		
			
				|  |  | +}
 |