| 
					
				 | 
			
			
				@@ -3,6 +3,7 @@ package Device 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"Cold_Api/conf" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"Cold_Api/controllers/lib" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	"Cold_Api/logs" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"encoding/json" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"fmt" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"github.com/astaxie/beego/cache" 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -92,6 +93,15 @@ func RedisDeviceData_Get_(key string) (r DeviceData_New, is bool) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return DeviceData_New{}, false 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func Redis_Set_DeviceData_Time(key string) (err error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	err = redisCache_DeviceData.Put(key, "", 40*time.Minute) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		fmt.Println("set key:", key, err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 //创建数据库  Device.CREATE_DeviceData("") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func CREATE_DeviceData(SN string) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -120,8 +130,10 @@ func CREATE_DeviceData(SN string) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func DELETE_DeviceDatar(SN string) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	sql := "DROP TABLE Z_DeviceData_" + SN 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	timeStr := time.Now().Format("2006_01_02_15_04_05") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//sql := "DROP TABLE Z_DeviceData_" + SN 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	sql := "ALTER TABLE Z_DeviceData_" + SN + " RENAME TO " + "Z_DeviceData_" + SN + "_dle_" + timeStr + ";" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	logs.Println("DELETE_DeviceDatar:", sql) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	o := orm.NewOrm() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	_, err := o.Raw(sql).Exec() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if err != nil { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -130,14 +142,79 @@ func DELETE_DeviceDatar(SN string) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return true 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-// ---------------- 特殊方法 ------------------- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-// 获取 ById 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//func Read_List_ById(id int) (r DeviceData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//	o := orm.NewOrm() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//	return r 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// ---------------- 特殊方法 ------------------ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+type DeviceData_T struct { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_name    string    // 标题 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_id      int       // ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_t       float32   // 温度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_rh      float32   // 湿度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Tlower  float32   // 湿度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Tupper  float32   // 湿度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_RHlower float32   // 湿度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_RHupper float32   // 湿度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Site    string    // GPS 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Dattery int       // 电量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_time    time.Time // 采集时间 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 添加 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func Add_DeviceData(SN string, v DeviceData_T) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//if(conf.Test_server){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//	fmt.Println("Add_DeviceData:",SN , t_name , t_id , t_t , t_rh , t_tl , t_tu , t_rhl , t_rhu , t_site , t_battery , t_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//	return true 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	key_time := SN + "|" + strconv.Itoa(v.T_id) + "|" + v.T_time.Format("2006-01-02 15:04:05") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	fmt.Println(key_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if redisCache_DeviceData.IsExist(key_time) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		//println("找到key:",key) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return true 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	o := orm.NewOrm() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 检查 超过时间,查询 数据库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	logs.Println("Add_DeviceData 时间差s:", time.Now().Unix()-v.T_time.Unix()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if time.Now().Unix()-v.T_time.Unix() >= 60*40 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 查看是否 有记录 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		var maps_z []orm2.ParamsList 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		sql_c := "SELECT COUNT(ID) FROM Z_DeviceData_" + SN + " WHERE t_time = '" + v.T_time.Format("2006-01-02 15:04:05") + "' AND t_id = " + strconv.Itoa(v.T_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		logs.Println("检查 超过时间,查询 数据库 SQL:", sql_c) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		_, err := o.Raw(sql_c).ValuesList(&maps_z) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Println(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return false 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		//fmt.Println("maps_z[0][0]:",maps_z[0][0]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if lib.To_int(maps_z[0][0]) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Println("存在记录,跳过!") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return true 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Tlower := strconv.FormatFloat(float64(v.T_Tlower), 'f', -1, 64) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_Tupper := strconv.FormatFloat(float64(v.T_Tupper), 'f', -1, 64) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_RHlower := strconv.FormatFloat(float64(v.T_RHlower), 'f', -1, 64) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	T_RHupper := strconv.FormatFloat(float64(v.T_RHupper), 'f', -1, 64) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 开始插入数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	sql := "INSERT INTO Z_DeviceData_" + SN + " (`t_name`, `t_id`, `t_t`, `t_rh`, `t_tl`,`t_tu`,`t_rhl`,`t_rhu`, `t_site`,`t_battery`, `t_time`) " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		"VALUES ('" + v.T_name + "'," + strconv.Itoa(v.T_id) + ", " + lib.To_string(v.T_t) + ", " + lib.To_string(v.T_rh) + "," + T_Tlower + "," + T_Tupper + "," + T_RHlower + "," + T_RHupper + ", '" + v.T_Site + "'," + strconv.Itoa(v.T_Dattery) + ", '" + v.T_time.Format("2006-01-02 15:04:05") + "')" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//  这里有时间优化  用于一次 prepare 多次 exec,以提高批量执行的速度 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	fmt.Println(sql) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	res, err := o.Raw(sql).Exec() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		fmt.Println(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return false 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	res.RowsAffected() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 存在 写入 redis 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	Redis_Set_DeviceData_Time(key_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return true 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+///--------------- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 type DeviceData_ struct { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	T_name    string  `orm:"column(t_name);size(256);null"`        // 标题 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	T_sn      string  `orm:"column(t_sn);size(256);null"`          // 标题 
			 |