瀏覽代碼

轨迹同步

huangyan 4 周之前
父節點
當前提交
af93566292
共有 5 個文件被更改,包括 117 次插入4 次删除
  1. 1 1
      conf/app.conf
  2. 22 0
      controllers/Data.go
  3. 1 1
      controllers/User.go
  4. 90 0
      models/Device/DeviceData.go
  5. 3 2
      routers/Data.go

+ 1 - 1
conf/app.conf

@@ -43,7 +43,7 @@ Panel_url = "http://127.0.0.1:6204/Cold_Panel"
 # 不验证登录的接口
 FilterExcludeURL = /Login_verification,/Data/List,/WxPay/Notify,/Data/Company_key_Device_Sensor_List,/docking/Real_Data,/docking/Note_Data,/Company/Transport/List,/DeviceSensor/List_BySN,/Data/GetNewLocus
 # 只验证登录不验证权限的接口
-FilterOnlyLoginCheckURL = /Menu/List,/User/Info,/User/Home,/User/Post,/UpFileToken,/User/WxQRCode,/Company/Get,/DataSource,/Company/Bill_Excel,/WarningSend/List,/WxPay/Get_QRCode,/WxPay/GetOrderState,/WarningHandle/List,/DeviceWarning/List_Count,/Bulletin/GetBulletinById,/Bulletin/IsReadBulletin,/Bulletin/GetBulletinList,/Data/GetNewLocusSSe,/DeviceWarning/BatchEdit,/Notice/WarinType
+FilterOnlyLoginCheckURL = /Menu/List,/User/Info,/User/Home,/User/Post,/UpFileToken,/User/WxQRCode,/Company/Get,/DataSource,/Company/Bill_Excel,/WarningSend/List,/WxPay/Get_QRCode,/WxPay/GetOrderState,/WarningHandle/List,/DeviceWarning/List_Count,/Bulletin/GetBulletinById,/Bulletin/IsReadBulletin,/Bulletin/GetBulletinList,/Data/GetNewLocusSSe,/DeviceWarning/BatchEdit,/Notice/WarinType,/Data/GetSecondaryPositioning
 
 
 MqttIds = tmqttjxit

+ 22 - 0
controllers/Data.go

@@ -321,6 +321,28 @@ func (c *DataController) Device_Sensor_Data_More() {
 	return
 }
 
+// GetSecondaryPositioning 获取APP辅助定位
+func (c *DataController) GetSecondaryPositioning() {
+	T_Sn := c.GetString("T_sn")
+	longitude := c.GetString("longitude")
+	latitude := c.GetString("latitude")
+	T_time := c.GetString("T_time")
+	if len(T_Sn) == 0 || len(longitude) == 0 || len(latitude) == 0 || len(T_time) == 0 {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "缺少必要参数!!!"}
+		c.ServeJSON()
+		return
+	}
+	err := Device.Update_AssistedPositioning(T_Sn, longitude, latitude, T_time)
+	if err != nil {
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "辅助定位更新失败!!!", Data: err.Error()}
+		c.ServeJSON()
+		return
+	}
+	sprintln := fmt.Sprintln("T_Sn:", T_Sn, "longitude:", longitude, "latitude:", latitude, "T_time:", T_time)
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: sprintln}
+	c.ServeJSON()
+}
+
 // -------------------------------------
 
 // 导出传感器数据列表

+ 1 - 1
controllers/User.go

@@ -141,7 +141,7 @@ func (c *UserController) Company_Add() {
 		return
 	}
 
-	if c.Admin_r.T_pids != "*" {
+	if c.Admin_r.T_pids != "*" && c.Admin_r.T_pid == 0 {
 		T_pids := []int{int(id)}
 		if err = Account.Add_Company_bind(c.Admin_r, T_pids); err != nil {
 			c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}

+ 90 - 0
models/Device/DeviceData.go

@@ -40,6 +40,17 @@ type DeviceData_ struct {
 	T_time time.Time // 采集时间
 }
 
+// DeviceData_R1 辅助定位更新redis 结构体
+type DeviceData_R1 struct {
+	T_t    float32   // 温度
+	T_rh   float32   // 湿度
+	T_Site string    // GPS
+	T_time time.Time // 采集时间
+	T_sp   int       // 传感器参数ID
+	T_tp   int       // 传感器参数ID
+	//create_time
+}
+
 // 模板
 type DeviceData_R struct {
 	T_sn     string  // sn
@@ -196,6 +207,34 @@ func RedisDeviceData_Set(key string, r DeviceData_) (err error) {
 	return
 }
 
+// 辅助定位设置redis数据
+func RedisDeviceData_SetAssistedPositioning(T_sn string, T_id int, r DeviceData_R1) (err error) {
+	key := T_sn + "|" + strconv.Itoa(T_id)
+
+	if redis_DeviceData.IsExist(key) {
+		var t DeviceData_R1
+		v := redis_DeviceData.Get(key)
+		json.Unmarshal(v.([]byte), &t)
+		// 防止时间溢出
+		if time.Now().Unix() <= r.T_time.Unix() {
+			r.T_time = time.Now()
+		}
+
+	}
+
+	//json序列化
+	str, err := json.Marshal(r)
+	if err != nil {
+		logs.Error("RedisDeviceData_Set", err)
+		return
+	}
+	err = redis_DeviceData.Put(key, str, 1*time.Hour)
+	if err != nil {
+		logs.Error("set key:", key, ",value:", str, err)
+	}
+	return
+}
+
 func RedisDeviceData_Get(key string) (r DeviceData_, is bool) {
 	if redis_DeviceData.IsExist(key) {
 		v := redis_DeviceData.Get(key)
@@ -210,6 +249,17 @@ func RedisDeviceData_Get(key string) (r DeviceData_, is bool) {
 	return DeviceData_{}, false
 }
 
+// 辅助定位获取redis数据
+func AssistedPositioning_Get(T_sn string, T_id int) (r DeviceData_R1, is bool) {
+	key := T_sn + "|" + strconv.Itoa(T_id)
+	if redis_DeviceData.IsExist(key) {
+		v := redis_DeviceData.Get(key)
+		json.Unmarshal(v.([]byte), &r)
+		return r, true
+	}
+	return DeviceData_R1{}, false
+}
+
 // -------------------------------------------------------
 // 创建数据库  Device.CREATE_DeviceData("")
 func CREATE_DeviceData(SN string) bool {
@@ -295,6 +345,46 @@ func Add_DeviceData(SN string, v DeviceData_T) bool {
 	return true
 }
 
+// 辅助定位更新缓存
+func Update_AssistedPositioning(T_sn, longitude, latitude, T_time string) error {
+	o := orm.NewOrm()
+	tableName := "z_device_data_" + T_sn
+	var deviceData []DeviceData_
+	tsn, i := Read_DeviceSensor_ByTsn(T_sn)
+	if i == 0 {
+		return errors.New("获取设备失败")
+	}
+	for _, v := range tsn {
+		r, is := AssistedPositioning_Get(v.T_sn, v.T_id)
+		if is {
+			r.T_Site = longitude + "," + latitude
+			err := RedisDeviceData_SetAssistedPositioning(v.T_sn, v.T_id, r)
+			if err != nil {
+				return err
+			}
+			sql := "SELECT * FROM " + tableName + " ORDER BY t_time DESC LIMIT 1"
+			fmt.Println(sql)
+			rows, err := o.Raw(sql).QueryRows(&deviceData)
+			if err != nil {
+				logs.Error(lib.FuncName(), err)
+				return err
+			}
+			if rows > 0 {
+				for _, data := range deviceData {
+					format := data.T_time.Format("2006-01-02 15:04:05")
+					sql = "UPDATE " + tableName + " SET t_site = '" + longitude + "," + latitude + "' WHERE t_id = " + strconv.Itoa(v.T_id) + " AND t_time = '" + format + "'"
+					_, err := o.Raw(sql).Exec()
+					if err != nil {
+						logs.Error(lib.FuncName(), err)
+						return err
+					}
+				}
+			}
+		}
+	}
+	return nil
+}
+
 func Read_DeviceData_ById_List(SN string, T_id int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int) {
 	o := orm.NewOrm()
 	var maps []DeviceData_

+ 3 - 2
routers/Data.go

@@ -11,8 +11,9 @@ func init() {
 		beego.NSNamespace("/Data",
 			beego.NSBefore(T_pidFilter),
 			//数据实时轨迹
-			beego.NSRouter("/GetNewLocusSSe", &controllers.DataController{}, "*:GetNewLocus"), // SSE获取实时轨迹
-			beego.NSRouter("/GetNewLocus", &controllers.DataController{}, "*:GetNewLocus_"),   // websocket获取实时轨迹
+			beego.NSRouter("/GetNewLocusSSe", &controllers.DataController{}, "*:GetNewLocus"),                      // SSE获取实时轨迹
+			beego.NSRouter("/GetNewLocus", &controllers.DataController{}, "*:GetNewLocus_"),                        // websocket获取实时轨迹
+			beego.NSRouter("/GetSecondaryPositioning", &controllers.DataController{}, "*:GetSecondaryPositioning"), // 获取辅助定位
 			// 获取实时轨迹
 			beego.NSRouter("/Device_Sensor", &controllers.DataController{}, "*:Device_Sensor_Get"),                                 // 获取传感器
 			beego.NSRouter("/Device_Sensor_Data_Real", &controllers.DataController{}, "*:Device_Sensor_Data_Real"),                 // 获取传感器