|
|
@ -20,6 +20,8 @@ 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; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
import javax.annotation.PreDestroy; |
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
import java.text.ParseException; |
|
|
import java.text.ParseException; |
|
|
import java.text.SimpleDateFormat; |
|
|
import java.text.SimpleDateFormat; |
|
|
@ -52,7 +54,24 @@ public class DeviceDataService { |
|
|
DeviceInfoMapper deviceInfoMapper; |
|
|
DeviceInfoMapper deviceInfoMapper; |
|
|
@Autowired |
|
|
@Autowired |
|
|
AmqpService amqpService; |
|
|
AmqpService amqpService; |
|
|
|
|
|
private InfluxDBClient influxDBClient; |
|
|
|
|
|
private InfluxDBClient oldInfluxDBClient; |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
|
public void init() { |
|
|
|
|
|
this.influxDBClient = InfluxDBClientFactory.create(url, token.toCharArray(), org); |
|
|
|
|
|
this.oldInfluxDBClient = InfluxDBClientFactory.create(oldurl, oldtoken.toCharArray(), oldorg); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PreDestroy |
|
|
|
|
|
public void destroy() { |
|
|
|
|
|
if (influxDBClient != null) { |
|
|
|
|
|
influxDBClient.close(); |
|
|
|
|
|
} |
|
|
|
|
|
if (oldInfluxDBClient != null) { |
|
|
|
|
|
oldInfluxDBClient.close(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
/** |
|
|
/** |
|
|
* 根据设备号查询设备历史数据 |
|
|
* 根据设备号查询设备历史数据 |
|
|
* |
|
|
* |
|
|
@ -165,7 +184,7 @@ public class DeviceDataService { |
|
|
*/ |
|
|
*/ |
|
|
public List<DeviceDataInfluxData> getOldInfluxdbData(String imei) { |
|
|
public List<DeviceDataInfluxData> getOldInfluxdbData(String imei) { |
|
|
DeviceBelongInfo deviceBelongInfo = deviceInfoMapper.queryDeviceBelongInfo(imei); |
|
|
DeviceBelongInfo deviceBelongInfo = deviceInfoMapper.queryDeviceBelongInfo(imei); |
|
|
InfluxDBClient client = InfluxDBClientFactory.create(oldurl, oldtoken.toCharArray(), oldorg); |
|
|
|
|
|
|
|
|
// InfluxDBClient client = InfluxDBClientFactory.create(oldurl, oldtoken.toCharArray(), oldorg); |
|
|
StringBuffer query = new StringBuffer(); |
|
|
StringBuffer query = new StringBuffer(); |
|
|
query.append("from(bucket: \"iot\") "); |
|
|
query.append("from(bucket: \"iot\") "); |
|
|
query.append("|> range(start: -1y)"); |
|
|
query.append("|> range(start: -1y)"); |
|
|
@ -175,7 +194,10 @@ public class DeviceDataService { |
|
|
// } |
|
|
// } |
|
|
query.append(" |> yield(name: \"last\")"); |
|
|
query.append(" |> yield(name: \"last\")"); |
|
|
System.out.println("查询语句==========:" + query); |
|
|
System.out.println("查询语句==========:" + query); |
|
|
List<FluxTable> tables = client.getQueryApi().query(query.toString()); |
|
|
|
|
|
|
|
|
if(oldInfluxDBClient==null){ |
|
|
|
|
|
oldInfluxDBClient = InfluxDBClientFactory.create(oldurl, oldtoken.toCharArray(), oldorg); |
|
|
|
|
|
} |
|
|
|
|
|
List<FluxTable> tables = oldInfluxDBClient.getQueryApi().query(query.toString()); |
|
|
List<DeviceDataInfluxData> rerurnList = new ArrayList<>(); |
|
|
List<DeviceDataInfluxData> rerurnList = new ArrayList<>(); |
|
|
for (FluxTable fluxTable : tables) { |
|
|
for (FluxTable fluxTable : tables) { |
|
|
List<FluxRecord> records = fluxTable.getRecords(); |
|
|
List<FluxRecord> records = fluxTable.getRecords(); |
|
|
@ -198,7 +220,7 @@ public class DeviceDataService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
client.close(); |
|
|
|
|
|
|
|
|
// client.close(); |
|
|
Collections.reverse(rerurnList); |
|
|
Collections.reverse(rerurnList); |
|
|
return rerurnList; |
|
|
return rerurnList; |
|
|
} |
|
|
} |
|
|
@ -230,9 +252,9 @@ public class DeviceDataService { |
|
|
if (createtime != null) { |
|
|
if (createtime != null) { |
|
|
time = createtime.toInstant(); |
|
|
time = createtime.toInstant(); |
|
|
} |
|
|
} |
|
|
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray()); |
|
|
|
|
|
|
|
|
// 复用已创建的客户端实例 |
|
|
DeviceDataInfluxData deviceDataInfluxData = rebuildDeviceDataInfluxData(history, time); |
|
|
DeviceDataInfluxData deviceDataInfluxData = rebuildDeviceDataInfluxData(history, time); |
|
|
try (WriteApi writeApi = client.getWriteApi()) { |
|
|
|
|
|
|
|
|
try (WriteApi writeApi = influxDBClient.getWriteApi()) { |
|
|
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, deviceDataInfluxData); |
|
|
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, deviceDataInfluxData); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -337,9 +359,12 @@ public class DeviceDataService { |
|
|
List<DeviceDataInfluxData> influxdbDataList = getOldInfluxdbData(syncDataFlag.getImei().toLowerCase(Locale.ROOT)); |
|
|
List<DeviceDataInfluxData> influxdbDataList = getOldInfluxdbData(syncDataFlag.getImei().toLowerCase(Locale.ROOT)); |
|
|
if (influxdbDataList != null && influxdbDataList.size() > 0) { |
|
|
if (influxdbDataList != null && influxdbDataList.size() > 0) { |
|
|
for (DeviceDataInfluxData influxData : influxdbDataList) { |
|
|
for (DeviceDataInfluxData influxData : influxdbDataList) { |
|
|
InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray()); |
|
|
|
|
|
|
|
|
// InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray()); |
|
|
//3.将设备数据保存到influxdb中 |
|
|
//3.将设备数据保存到influxdb中 |
|
|
try (WriteApi writeApi = client.getWriteApi()) { |
|
|
|
|
|
|
|
|
if(influxDBClient== null){ |
|
|
|
|
|
influxDBClient = InfluxDBClientFactory.create(url, token.toCharArray()); |
|
|
|
|
|
} |
|
|
|
|
|
try (WriteApi writeApi = influxDBClient.getWriteApi()) { |
|
|
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, influxData); |
|
|
writeApi.writeMeasurement(DEVICEDATA_BUCKET_NAME, org, WritePrecision.NS, influxData); |
|
|
syncDeviceData = true; |
|
|
syncDeviceData = true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
|