|
|
@ -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); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|