Browse Source

优化设备数据和设备下发命令查询接口

master
bgy 2 weeks ago
parent
commit
212488a67f
10 changed files with 159 additions and 95 deletions
  1. +5
    -6
      src/main/java/com/topsail/influxdb/controller/DeviceDataController.java
  2. +2
    -2
      src/main/java/com/topsail/influxdb/controller/DeviceLogController.java
  3. +1
    -1
      src/main/java/com/topsail/influxdb/controller/HistoryDataController.java
  4. +12
    -1
      src/main/java/com/topsail/influxdb/mapper/DeviceInfoMapper.java
  5. +15
    -0
      src/main/java/com/topsail/influxdb/mapper/DeviceInfoMapper.xml
  6. +3
    -2
      src/main/java/com/topsail/influxdb/rabbitmq/AmqpListener.java
  7. +43
    -22
      src/main/java/com/topsail/influxdb/service/DeviceDataService.java
  8. +55
    -35
      src/main/java/com/topsail/influxdb/service/DeviceLogService.java
  9. +0
    -9
      src/main/resources/application-rds.properties
  10. +23
    -17
      src/main/resources/application.properties

+ 5
- 6
src/main/java/com/topsail/influxdb/controller/DeviceDataController.java View File

@ -26,13 +26,12 @@ public class DeviceDataController {
/** /**
* 查询单个设备历史数据根据设备号查询设备历史数据 * 查询单个设备历史数据根据设备号查询设备历史数据
* devices/history/{deviceid}
* devices/history
*/ */
@GetMapping(value = "/history/{deviceid}")
public Result<List<DeviceHistoryData>> getHistoryData(String uid, Integer pageNo, Integer pageSize, String startTime, String endTime, @PathVariable("deviceid") String Imei) throws ParseException, BindException {
logger.info("history " + uid);
@GetMapping(value = "/getShengDiLanHistory")
public Result<List<DeviceHistoryData>> getHistoryData(@RequestParam(name = "uid", required = false) String uid, @RequestParam(name = "pageNo", required = false) Integer pageNo, @RequestParam(name = "pageSize", required = false) Integer pageSize, @RequestParam(name = "startTime", required = false) String startTime, @RequestParam(name = "endTime", required = false) String endTime, @RequestParam(name = "imei") String imei) throws ParseException, BindException {
List<DeviceHistoryData> list = new ArrayList<>(); List<DeviceHistoryData> list = new ArrayList<>();
list = deviceDataService.getDeviceHistoryData(uid, pageNo, pageSize, startTime, endTime, Imei);
list = deviceDataService.getDeviceHistoryData(uid, pageNo, pageSize, startTime, endTime, imei);
logger.info("finished request " + uid); logger.info("finished request " + uid);
return Result.success(list); return Result.success(list);
} }
@ -40,7 +39,7 @@ public class DeviceDataController {
/** /**
* 删除所有的设备历史数据 * 删除所有的设备历史数据
*/ */
@RequestMapping(value = "/delete", method = RequestMethod.GET)
@RequestMapping(value = "/shengDiLandelete", method = RequestMethod.GET)
public Result<List<DeviceHistoryData>> deleteDeviceData() throws ParseException, BindException { public Result<List<DeviceHistoryData>> deleteDeviceData() throws ParseException, BindException {
List<DeviceHistoryData> list = new ArrayList<>(); List<DeviceHistoryData> list = new ArrayList<>();
deviceDataService.deleteDeviceData(); deviceDataService.deleteDeviceData();


+ 2
- 2
src/main/java/com/topsail/influxdb/controller/DeviceLogController.java View File

@ -37,7 +37,7 @@ public class DeviceLogController {
* @param houseId 房间id * @param houseId 房间id
* @return * @return
*/ */
@GetMapping(value = "/getPageDeviceLog")
@GetMapping(value = "/getShengDiLanDeviceLog")
public Result getPageDeviceLog(Integer pageNode, Integer pageSize, String startTime, String endTime, String result, String imei, String supplierId, String companyId, String operator, Integer houseId, String bindingInfo) throws ParseException, BindException { public Result getPageDeviceLog(Integer pageNode, Integer pageSize, String startTime, String endTime, String result, String imei, String supplierId, String companyId, String operator, Integer houseId, String bindingInfo) throws ParseException, BindException {
JSONObject jsonObject = deviceLogService.getPageDeviceLog(pageNode, pageSize, startTime, endTime, result, imei, supplierId, companyId, operator, houseId, bindingInfo); JSONObject jsonObject = deviceLogService.getPageDeviceLog(pageNode, pageSize, startTime, endTime, result, imei, supplierId, companyId, operator, houseId, bindingInfo);
return Result.success(jsonObject); return Result.success(jsonObject);
@ -46,7 +46,7 @@ public class DeviceLogController {
/** /**
* 删除所有的设备下发日志 * 删除所有的设备下发日志
*/ */
@RequestMapping(value = "/delete", method = RequestMethod.GET)
@RequestMapping(value = "/shengDiLandelete", method = RequestMethod.GET)
public Result delete() throws ParseException, BindException { public Result delete() throws ParseException, BindException {
deviceLogService.deleteDeviceLog(); deviceLogService.deleteDeviceLog();
return Result.success(new CodeMsg(0, "success")); return Result.success(new CodeMsg(0, "success"));


+ 1
- 1
src/main/java/com/topsail/influxdb/controller/HistoryDataController.java View File

@ -40,7 +40,7 @@ public class HistoryDataController {
*/ */
@RequestMapping(value = "/transferDeviceLogData", method = RequestMethod.GET) @RequestMapping(value = "/transferDeviceLogData", method = RequestMethod.GET)
public Result<List<DeviceHistoryData>> transferDeviceLogData(@RequestParam(name = "imei", required = false) String imei) throws ParseException, BindException { public Result<List<DeviceHistoryData>> transferDeviceLogData(@RequestParam(name = "imei", required = false) String imei) throws ParseException, BindException {
deviceLogService.transferDeviceLogData();
deviceLogService.transferDeviceLogData(imei);
return Result.success(null); return Result.success(null);
} }
/** /**


+ 12
- 1
src/main/java/com/topsail/influxdb/mapper/DeviceInfoMapper.java View File

@ -22,12 +22,23 @@ public interface DeviceInfoMapper {
* @return * @return
*/ */
List<SyncDataFlag> querySyncDeviceFlagInfo();
List<SyncDataFlag> querySyncDeviceFlagInfo(@Param("imei") String imei);
/** /**
* 根据设备编号查询设备命令下发日志数据 * 根据设备编号查询设备命令下发日志数据
*
* @param imei * @param imei
* @return * @return
*/ */
List<DeviceLogData> queryDeviceLogData(@Param("imei") String imei, @Param("companyId") Integer companyId); List<DeviceLogData> queryDeviceLogData(@Param("imei") String imei, @Param("companyId") Integer companyId);
/**
* 更新设备同步标志信息
*
* @param id
* @param flag
*/
void updateSyncDeviceDataFlagInfo(@Param("id") Integer id, @Param("flag") Integer flag);
void updateSyncDeviceLogFlagInfo(@Param("id") Integer id, @Param("flag") Integer flag);
} }

+ 15
- 0
src/main/java/com/topsail/influxdb/mapper/DeviceInfoMapper.xml View File

@ -81,6 +81,16 @@
<sql id="Blob_Column_List"> <sql id="Blob_Column_List">
deviceId deviceId
</sql> </sql>
<update id="updateSyncDeviceDataFlagInfo">
UPDATE shengdilan_sync_data_flag
SET sync_device_data = #{flag}
WHERE id = #{id}
</update>
<update id="updateSyncDeviceLogFlagInfo">
UPDATE shengdilan_sync_data_flag
SET sync_device_log = #{flag}
WHERE id = #{id}
</update>
<select id="queryDeviceBelongInfo" resultType="com.topsail.influxdb.entity.DeviceBelongInfo"> <select id="queryDeviceBelongInfo" resultType="com.topsail.influxdb.entity.DeviceBelongInfo">
SELECT CONCAT(IFNULL(oo.office_name, ''), '-', IFNULL(s.station_name, ''), '-', IFNULL(v.village_name, ''), '-', SELECT CONCAT(IFNULL(oo.office_name, ''), '-', IFNULL(s.station_name, ''), '-', IFNULL(v.village_name, ''), '-',
IFNULL(b.building_name, ''), '-', IFNULL(u.unit_name, ''), '-', IFNULL(h.house_no, ''), '-', IFNULL(b.building_name, ''), '-', IFNULL(u.unit_name, ''), '-', IFNULL(h.house_no, ''), '-',
@ -100,6 +110,11 @@
<select id="querySyncDeviceFlagInfo" resultType="com.topsail.influxdb.entity.SyncDataFlag"> <select id="querySyncDeviceFlagInfo" resultType="com.topsail.influxdb.entity.SyncDataFlag">
SELECT id, imei, sync_device_data, sync_device_log SELECT id, imei, sync_device_data, sync_device_log
FROM shengdilan_sync_data_flag FROM shengdilan_sync_data_flag
WHERE 1=1
<if test="imei != null and imei!='' ">
AND imei = #{imei}
</if>
</select> </select>
<select id="queryDeviceLogData" resultType="com.topsail.influxdb.entity.DeviceLogData"> <select id="queryDeviceLogData" resultType="com.topsail.influxdb.entity.DeviceLogData">
SELECT id, SELECT id,


+ 3
- 2
src/main/java/com/topsail/influxdb/rabbitmq/AmqpListener.java View File

@ -9,6 +9,7 @@ import com.topsail.influxdb.service.DeviceDataService;
import com.topsail.influxdb.service.DeviceLogService; import com.topsail.influxdb.service.DeviceLogService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders; import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.Header;
@ -45,10 +46,10 @@ public class AmqpListener {
try { try {
History history = JSON.parseObject(message, History.class); History history = JSON.parseObject(message, History.class);
deviceDataService.saveDeviceDataToInfluxdb(history); deviceDataService.saveDeviceDataToInfluxdb(history);
amqpService.SendMessage("shengdilandevicedataall", message);
LOG.info("saveDeviceDataToInfluxdb OK:" + history.getImei()); LOG.info("saveDeviceDataToInfluxdb OK:" + history.getImei());
channel.basicAck(deliveryTag, false); channel.basicAck(deliveryTag, false);
} catch (Exception e) { } catch (Exception e) {
amqpService.SendMessage("shengdilandevicedataback", message);
channel.basicNack(deliveryTag, false, true); channel.basicNack(deliveryTag, false, true);
LOG.info("saveDeviceDataToInfluxdb Error:(" + e + ")-" + message); LOG.info("saveDeviceDataToInfluxdb Error:(" + e + ")-" + message);
} }
@ -72,10 +73,10 @@ public class AmqpListener {
try { try {
DeviceLogData deviceLogData = JSON.parseObject(message, DeviceLogData.class); DeviceLogData deviceLogData = JSON.parseObject(message, DeviceLogData.class);
deviceLogService.saveDeviceLogToInfluxdb(deviceLogData); deviceLogService.saveDeviceLogToInfluxdb(deviceLogData);
amqpService.SendMessage("shengdilandevicelogall", message);
LOG.info("saveDeviceLogToInfluxdb OK:" + deviceLogData.getImei()); LOG.info("saveDeviceLogToInfluxdb OK:" + deviceLogData.getImei());
channel.basicAck(deliveryTag, false); channel.basicAck(deliveryTag, false);
} catch (Exception e) { } catch (Exception e) {
amqpService.SendMessage("shengdilandevicelogback", message);
channel.basicNack(deliveryTag, false, true); channel.basicNack(deliveryTag, false, true);
LOG.info("saveDeviceLogToInfluxdb Error:(" + e + ")-" + message); LOG.info("saveDeviceLogToInfluxdb Error:(" + e + ")-" + message);
} }


+ 43
- 22
src/main/java/com/topsail/influxdb/service/DeviceDataService.java View File

@ -2,6 +2,7 @@ package com.topsail.influxdb.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.influxdb.client.DeleteApi;
import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApi; import com.influxdb.client.WriteApi;
@ -14,8 +15,10 @@ import com.topsail.influxdb.entity.DeviceHistoryData;
import com.topsail.influxdb.entity.SyncDataFlag; import com.topsail.influxdb.entity.SyncDataFlag;
import com.topsail.influxdb.mapper.DeviceInfoMapper; import com.topsail.influxdb.mapper.DeviceInfoMapper;
import com.topsail.influxdb.pojo.History; import com.topsail.influxdb.pojo.History;
import com.topsail.influxdb.rabbitmq.service.AmqpService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -24,6 +27,8 @@ import javax.annotation.Resource;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*; import java.util.*;
@Service @Service
@ -47,6 +52,8 @@ public class DeviceDataService {
@Resource @Resource
DeviceInfoMapper deviceInfoMapper; DeviceInfoMapper deviceInfoMapper;
@Autowired
AmqpService amqpService;
/** /**
* 根据设备号查询设备历史数据 * 根据设备号查询设备历史数据
@ -60,7 +67,6 @@ public class DeviceDataService {
* @return * @return
*/ */
public List<DeviceHistoryData> getDeviceHistoryData(String uid, Integer pageNo, Integer pageSize, String startTime, String endTime, String imei) { public List<DeviceHistoryData> getDeviceHistoryData(String uid, Integer pageNo, Integer pageSize, String startTime, String endTime, String imei) {
String org = "shengdilan";
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org); InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org);
StringBuffer query = new StringBuffer(); StringBuffer query = new StringBuffer();
query.append("from(bucket: \"iot\") "); query.append("from(bucket: \"iot\") ");
@ -77,7 +83,7 @@ public class DeviceDataService {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
query.append("|> range(start: -36h)");
query.append("|> range(start: -30d)");
} }
query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"history\") |> filter(fn: (r) => r[\"imei\"] == \"%s\") |> filter(fn: (r) => r[\"_field\"] == \"jsondata\" ) |> sort(columns:[\"_time\"], desc:true) ", imei)); query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"history\") |> filter(fn: (r) => r[\"imei\"] == \"%s\") |> filter(fn: (r) => r[\"_field\"] == \"jsondata\" ) |> sort(columns:[\"_time\"], desc:true) ", imei));
if (pageNo != null && pageSize != null) { if (pageNo != null && pageSize != null) {
@ -178,7 +184,7 @@ public class DeviceDataService {
try (WriteApi writeApi = client.getWriteApi()) { try (WriteApi writeApi = client.getWriteApi()) {
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, deviceDataInfluxData); writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, deviceDataInfluxData);
} }
System.out.println("写入成功==========》" + history.getImei());
System.out.println("设备数据写入成功==========》" + history.getImei());
} }
/** /**
@ -235,24 +241,24 @@ public class DeviceDataService {
* 删除掉所有的Influxdb数据 * 删除掉所有的Influxdb数据
*/ */
public void deleteDeviceData() { public void deleteDeviceData() {
// InfluxDBClient client = InfluxDBClientFactory.create(oldurl, oldtoken.toCharArray());
// StringBuffer query = new StringBuffer();
// query.append("from(bucket: \"iot\") ");
// query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"history\")"));
// query.append("|> range(start: -36d)");
// System.out.println("查询语句==========:" + query);
// DeleteApi deleteApi = client.getDeleteApi();
// OffsetDateTime start = OffsetDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
// OffsetDateTime stop = OffsetDateTime.of(2026, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
// try {
// // Delete data with specific time range
// deleteApi.delete(start, stop, "", DEVICEDATA_BUCKET_NAME, oldorg);
// System.out.println("Data deleted successfully");
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// client.close();
// }
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray());
StringBuffer query = new StringBuffer();
query.append("from(bucket: \"iot\") ");
query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"history\")"));
query.append("|> range(start: -36d)");
System.out.println("查询语句==========:" + query);
DeleteApi deleteApi = client.getDeleteApi();
OffsetDateTime start = OffsetDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime stop = OffsetDateTime.of(2026, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
try {
// Delete data with specific time range
deleteApi.delete(start, stop, "", DEVICEDATA_BUCKET_NAME, org);
System.out.println("Data deleted successfully");
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
}
} }
/** /**
@ -262,10 +268,11 @@ public class DeviceDataService {
*/ */
public void transferDeviceData(String imei) { public void transferDeviceData(String imei) {
//1.查询所有的设备编号 //1.查询所有的设备编号
List<SyncDataFlag> syncDataFlags = deviceInfoMapper.querySyncDeviceFlagInfo();
List<SyncDataFlag> syncDataFlags = deviceInfoMapper.querySyncDeviceFlagInfo(imei);
//2.根据设备编号查询历史Influxdb所有的设备数据 //2.根据设备编号查询历史Influxdb所有的设备数据
if (syncDataFlags != null && syncDataFlags.size() > 0) { if (syncDataFlags != null && syncDataFlags.size() > 0) {
for (SyncDataFlag syncDataFlag : syncDataFlags) { for (SyncDataFlag syncDataFlag : syncDataFlags) {
Boolean syncDeviceData = false;
List<DeviceDataInfluxData> influxdbDataList = getOldInfluxdbData(syncDataFlag.getImei()); List<DeviceDataInfluxData> influxdbDataList = getOldInfluxdbData(syncDataFlag.getImei());
if (influxdbDataList != null && influxdbDataList.size() > 0) { if (influxdbDataList != null && influxdbDataList.size() > 0) {
for (DeviceDataInfluxData influxData : influxdbDataList) { for (DeviceDataInfluxData influxData : influxdbDataList) {
@ -273,11 +280,25 @@ public class DeviceDataService {
//3.将设备数据保存到influxdb中 //3.将设备数据保存到influxdb中
try (WriteApi writeApi = client.getWriteApi()) { try (WriteApi writeApi = client.getWriteApi()) {
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, oldorg, WritePrecision.NS, influxData); writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, oldorg, WritePrecision.NS, influxData);
syncDeviceData = true;
} catch (Exception e) { } catch (Exception e) {
LOG.error("保存设备数据到influxdb失败:{}", e.getMessage()); LOG.error("保存设备数据到influxdb失败:{}", e.getMessage());
syncDeviceData = false;
if (influxData != null && influxData.jsondata != null && !influxData.jsondata.equals("")) {
String message = influxData.jsondata;
History history = JSON.parseObject(message, History.class);
if (history != null && history.getImei() != null && !history.getImei().equals("")) {
amqpService.SendMessage("shengdilandevicedataback", JSON.toJSONString(history));
}
}
} }
} }
} }
if (syncDeviceData) {
//4.更新同步设备数据状态
deviceInfoMapper.updateSyncDeviceDataFlagInfo(syncDataFlag.getId(), 1);
}
} }
} }
} }


+ 55
- 35
src/main/java/com/topsail/influxdb/service/DeviceLogService.java View File

@ -11,11 +11,13 @@ import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable; import com.influxdb.query.FluxTable;
import com.topsail.influxdb.entity.DeviceBelongInfo; import com.topsail.influxdb.entity.DeviceBelongInfo;
import com.topsail.influxdb.entity.DeviceLogData; import com.topsail.influxdb.entity.DeviceLogData;
import com.topsail.influxdb.entity.DeviceLogInfluxData;
import com.topsail.influxdb.entity.SyncDataFlag; import com.topsail.influxdb.entity.SyncDataFlag;
import com.topsail.influxdb.mapper.DeviceInfoMapper; import com.topsail.influxdb.mapper.DeviceInfoMapper;
import com.topsail.influxdb.entity.DeviceLogInfluxData;
import com.topsail.influxdb.rabbitmq.service.AmqpService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -46,7 +48,8 @@ public class DeviceLogService {
@Resource @Resource
DeviceInfoMapper deviceInfoMapper; DeviceInfoMapper deviceInfoMapper;
@Autowired
AmqpService amqpService;
/** /**
* 存储设备数据到influxdb * 存储设备数据到influxdb
* *
@ -85,26 +88,28 @@ public class DeviceLogService {
* 删除掉所有的Influxdb数据 * 删除掉所有的Influxdb数据
*/ */
public void deleteDeviceLog() { public void deleteDeviceLog() {
// InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org);
// StringBuffer query = new StringBuffer();
// query.append("from(bucket: \"devicelog\") ");
// query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"devicelog\")"));
// query.append("|> range(start: -36d)");
// System.out.println("查询语句==========:" + query);
// DeleteApi deleteApi = client.getDeleteApi();
// OffsetDateTime start = OffsetDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
// OffsetDateTime stop = OffsetDateTime.of(2026, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
// try {
// // Delete data with specific time range
// deleteApi.delete(start, stop, "", LOG_BUCKET_NAME, org);
// System.out.println("Data deleted successfully");
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// client.close();
// }
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org);
StringBuffer query = new StringBuffer();
query.append("from(bucket: \"devicelog\") ");
query.append(String.format(" |> filter(fn: (r) => r[\"_measurement\"] == \"devicelog\")"));
query.append("|> range(start: -36d)");
System.out.println("查询语句==========:" + query);
DeleteApi deleteApi = client.getDeleteApi();
OffsetDateTime start = OffsetDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime stop = OffsetDateTime.of(2026, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
try {
// Delete data with specific time range
deleteApi.delete(start, stop, "", LOG_BUCKET_NAME, org);
System.out.println("Data deleted successfully");
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
}
} }
// from(bucket: "devicelog") |> range(start: -3600d) |> filter(fn: (r) => r["_measurement"] == "devicelog") |> filter(fn: (r) => r["_field"] == "logjson") |> filter(fn: (r) => r["deviceBelongInfo"] =~ /.*白志.*/) |> sort(columns:["_time"], desc:true) |> limit(n: 10, offset: 0) |> yield(name: "data")
/** /**
* 查询设备下发命令日志信息 * 查询设备下发命令日志信息
* *
@ -139,7 +144,7 @@ public class DeviceLogService {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
filterFragment.append("|> range(start: -36h)");
filterFragment.append("|> range(start: -3600d)");
} }
// 2. 拼接固定过滤条件_measurement和_field必选 // 2. 拼接固定过滤条件_measurement和_field必选
filterFragment.append(" |> filter(fn: (r) => r[\"_measurement\"] == \"devicelog\")"); filterFragment.append(" |> filter(fn: (r) => r[\"_measurement\"] == \"devicelog\")");
@ -249,23 +254,38 @@ public class DeviceLogService {
/** /**
* 转存设备命令下发日志数据 * 转存设备命令下发日志数据
*/ */
public void transferDeviceLogData() {
public void transferDeviceLogData(String searchImei) {
//1. 查询所有的设备编号 //1. 查询所有的设备编号
List<SyncDataFlag> syncDataFlags = deviceInfoMapper.querySyncDeviceFlagInfo();
//2. 根据设备编号查询设备命令下发日志数据
for (SyncDataFlag syncDataFlag : syncDataFlags) {
String imei = syncDataFlag.getImei();
List<DeviceLogData> deviceLogDataList = deviceInfoMapper.queryDeviceLogData(imei, 10);
if (deviceLogDataList != null && deviceLogDataList.size() > 0) {
DeviceBelongInfo deviceBelongInfo = deviceInfoMapper.queryDeviceBelongInfo(imei);
//3. 批量插入设备命令下发日志数据到influxdb中
for (DeviceLogData deviceLogData : deviceLogDataList) {
deviceLogData.setDeviceBelongInfo(deviceBelongInfo.getDeviceBelongInfo());
deviceLogData.setHouseId(deviceBelongInfo.getHouseId());
saveDeviceLogToInfluxdb(deviceLogData);
List<SyncDataFlag> syncDataFlags = deviceInfoMapper.querySyncDeviceFlagInfo(searchImei);
if (syncDataFlags != null && syncDataFlags.size() > 0) {
//2. 根据设备编号查询设备命令下发日志数据
for (SyncDataFlag syncDataFlag : syncDataFlags) {
Boolean flag = false;
String imei = syncDataFlag.getImei();
List<DeviceLogData> deviceLogDataList = deviceInfoMapper.queryDeviceLogData(imei, 10);
if (deviceLogDataList != null && deviceLogDataList.size() > 0) {
DeviceBelongInfo deviceBelongInfo = deviceInfoMapper.queryDeviceBelongInfo(imei);
//3. 批量插入设备命令下发日志数据到influxdb中
for (DeviceLogData deviceLogData : deviceLogDataList) {
deviceLogData.setDeviceBelongInfo(deviceBelongInfo != null ? deviceBelongInfo.getDeviceBelongInfo() : null);
deviceLogData.setHouseId(deviceBelongInfo != null ? deviceBelongInfo.getHouseId() : null);
try {
saveDeviceLogToInfluxdb(deviceLogData);
flag = true;
} catch (Exception e) {
LOG.error("保存设备命令下发日志数据到influxdb中失败:" + e.getMessage());
flag = false;
if(deviceLogData!=null ){
amqpService.SendMessage("shengdilandevicelogback", JSON.toJSONString(deviceLogData));
}
}
}
}
if (flag) {
//4. 更新同步设备命令下发日志数据状态
deviceInfoMapper.updateSyncDeviceLogFlagInfo(syncDataFlag.getId(), 1);
} }
} }
} }
} }
} }

+ 0
- 9
src/main/resources/application-rds.properties View File

@ -1,9 +0,0 @@
spring.rabbitmq.host=182.92.218.150
#spring.rabbitmq.host=localhost
spring.rabbitmq.username=topsail
spring.rabbitmq.password=topsail
spring.datasource.url=jdbc:mysql://rm-2ze77qng1ddlfur9g4o.mysql.rds.aliyuncs.com:3306/zhrl?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=topsail
spring.datasource.password=Topsail2020

+ 23
- 17
src/main/resources/application.properties View File

@ -1,28 +1,34 @@
spring.profiles.active=prod
server.port=8898 server.port=8898
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
compute.schedule=0
offline.gap=120
spring.rabbitmq.host=182.92.218.150
#spring.rabbitmq.host=localhost
spring.rabbitmq.username=topsail spring.rabbitmq.username=topsail
spring.rabbitmq.password=topsail spring.rabbitmq.password=topsail
spring.rabbitmq.virtualHost=/ spring.rabbitmq.virtualHost=/
spring.rabbitmq.listener.simple.acknowledge-mode=manual spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.prefetch=10 spring.rabbitmq.listener.simple.prefetch=10
spring.datasource.url=jdbc:mysql://47.98.32.177:3306/iot?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=yunmei
spring.datasource.password=yunmei1234
spring.datasource.url=jdbc:mysql://rm-2ze77qng1ddlfur9g4o.mysql.rds.aliyuncs.com:3306/zhrl?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
compute.schedule=0
offline.gap=120
spring.profiles.active=rds
shengdilan.influxdb.token=PHeyRfQX30aLDrY3csu44DgUlF-jgJETi7bro5FMFoUpuADq1PSMWRzCKjNVvtGLCytiyRLEEmbAs_4x5vdR0w==
shengdilan.influxdb.url=http://172.16.1.124:8086
spring.datasource.username=topsail
spring.datasource.password=Topsail2020
##正式环境influxdb配置
shengdilan.influxdb.token=0rg4n4KBC6x65pljf-OzaqvXrRCJGKQxl_ZGSSijdTRKNuVgbeTDMf5UKIHZPYHKjCHVrnKKNOu9hVVVNUCaZw==
shengdilan.influxdb.url=http://113.137.28.150:8086
shengdilan.influxdb.org=shengdilan shengdilan.influxdb.org=shengdilan
/ **
* 旧influxdb配置参数
*/
#shengdilan.influxdb.oldtoken=C2sfXsMC475aTtin7HbRkUXa9tEZTUU0S928ZdPzFktcFW8gZD_zY8-hKhgPxkLLodVS4YcsL3RcwgsJWYlURw==
#shengdilan.influxdb.oldurl=http://192.168.139.128:8086
#shengdilan.influxdb.oldorg=shengdilan
# 旧influxdb配置参数
shengdilan.influxdb.oldtoken=k7dtXN5boSf9gbnUC75ekSJqILCMIcJ_nxxHLgxAYKqEtFSqqIfnELc2xHxuimdRYZgtbReQFs7qq_XWh04Z2w== shengdilan.influxdb.oldtoken=k7dtXN5boSf9gbnUC75ekSJqILCMIcJ_nxxHLgxAYKqEtFSqqIfnELc2xHxuimdRYZgtbReQFs7qq_XWh04Z2w==
shengdilan.influxdb.oldurl=http://182.92.218.150:8086 shengdilan.influxdb.oldurl=http://182.92.218.150:8086
shengdilan.influxdb.oldorg=topsail shengdilan.influxdb.oldorg=topsail
###测试环境influxdb配置
#shengdilan.influxdb.token=PHeyRfQX30aLDrY3csu44DgUlF-jgJETi7bro5FMFoUpuADq1PSMWRzCKjNVvtGLCytiyRLEEmbAs_4x5vdR0w==
#shengdilan.influxdb.url=http://172.16.1.124:8086
#shengdilan.influxdb.org=shengdilan
##旧influxdb配置参数
#shengdilan.influxdb.oldtoken=C2sfXsMC475aTtin7HbRkUXa9tEZTUU0S928ZdPzFktcFW8gZD_zY8-hKhgPxkLLodVS4YcsL3RcwgsJWYlURw==
#shengdilan.influxdb.oldurl=http://192.168.139.128:8086
#shengdilan.influxdb.oldorg=shengdilan

Loading…
Cancel
Save