|
@@ -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_
|