Commit e012e7c0 authored by tangwei's avatar tangwei

增加TdEngine

parent 75f985d2
......@@ -41,6 +41,11 @@
<version>1.9.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
<build>
......
package com.yeejoin.amos.boot.module.jxiop.biz.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 从数据源配置
* 若需要配置更多数据源 , 直接在yml中添加数据源配置再增加相应的新的数据源配置类即可
*/
@Configuration
@MapperScan(basePackages = "com.yeejoin.amos.boot.module.jxiop.biz.tdmapper", sqlSessionFactoryRef = "taosSqlSessionFactory")
public class TdEngineConfig {
private Logger logger = LoggerFactory.getLogger(TdEngineConfig.class);
// 精确到 cluster 目录,以便跟其他数据源隔离
private static final String MAPPER_LOCATION = "classpath*:mapper/tdengine/*.xml";
@Value("${spring.db3.datasource.url}")
private String dbUrl;
@Value("${spring.db3.datasource.username}")
private String username;
@Value("${spring.db3.datasource.password}")
private String password;
@Value("${spring.db3.datasource.driver-class-name}")
private String driverClassName;
@Bean(name = "taosDataSource") //声明其为Bean实例
public DataSource clusterDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
return datasource;
}
@Bean(name = "taosTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "taosSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("taosDataSource") DataSource culsterDataSource)
throws Exception {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(culsterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(TdEngineConfig.MAPPER_LOCATION));
sessionFactory.setTypeAliasesPackage("com.yeejoin.amos.boot.module.jxiop.biz.entity");
//mybatis 数据库字段与实体类属性驼峰映射配置
sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sessionFactory.getObject();
}
}
package com.yeejoin.amos.boot.module.jxiop.biz.entity;
import lombok.Data;
import java.util.Date;
@Data
public class IndicatorData {
private String id;
private String address;
private String dataType;
private String equipmentSpecificName;
private String gatewayId;
private String isAlarm;
private Date createdTime;
private String unit;
private String value;
private Float valueF ;
private String valueLabel;
private String equipmentIndexName;
private String equipmentNumber;
private String displayName;
}
......@@ -306,6 +306,7 @@ public class LargeScreenImpl {
ybfbn = new BigDecimal(ybfbn).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
mapdta.put("NJHWC", ybfbn);
try {
System.out.println(JSON.toJSONString(mapdta));
emqKeeper.getMqttClient().publish("qyyxzb/"+s, JSON.toJSONString(mapdta).getBytes(),0,false);
} catch (MqttException e) {
e.printStackTrace();
......@@ -417,13 +418,13 @@ public class LargeScreenImpl {
List<String> value=new ArrayList<>();
value.add(RSD);
Map<String,List<String>> map=new HashMap<>();
map.put("equipmentIndexName",value);
map.put("equipmentIndexName.keyword",value);
List<String> value1=new ArrayList<>();
value1.add(monthy);
map.put("moon",value1);
map.put("moon.keyword",value1);
List<? extends Terms.Bucket> lidate= commonServiceImpl.getgroupsum(map,"value" ,"day", ESDailyPowerGeneration.class);
List<? extends Terms.Bucket> lidate= commonServiceImpl.getgroupsum(map,"value" ,"day.keyword", ESDailyPowerGeneration.class);
Map<String,Double> mapdta=new HashMap<>();
DecimalFormat format2 = new DecimalFormat("#.0000");
......@@ -466,13 +467,13 @@ public class LargeScreenImpl {
List<String> value=new ArrayList<>();
value.add(RSD);
Map<String,List<String>> map=new HashMap<>();
map.put("equipmentIndexName",value);
map.put("gatewayId", gatewayId);
map.put("equipmentIndexName.keyword",value);
map.put("gatewayId.keyword", gatewayId);
List<String> value1=new ArrayList<>();
value1.add(monthy);
map.put("moon",value1);
map.put("moon.keyword",value1);
List<? extends Terms.Bucket> lidate= commonServiceImpl.getgroupsum(map,"value" ,"day",ESDailyPowerGeneration.class);
List<? extends Terms.Bucket> lidate= commonServiceImpl.getgroupsum(map,"value" ,"day.keyword",ESDailyPowerGeneration.class);
Map<String,Double> mapdta=new HashMap<>();
DecimalFormat format2 = new DecimalFormat("#.0000");
......
package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;//package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.boot.module.jxiop.api.dto.IndexDto;
import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESDailyPowerGeneration;
import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESMoonPowerGeneration;
import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESYearPowerGeneration;
import com.yeejoin.amos.boot.module.jxiop.biz.constants.CommonConstans;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.StationCacheInfoDto;
import com.yeejoin.amos.boot.module.jxiop.biz.entity.IndicatorData;
import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESDailyPowerGenerationRepository;
import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESMoonPowerGenerationRepository;
import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESYearPowerGenerationRepository;
import com.yeejoin.amos.boot.module.jxiop.biz.tdmapper.IndicatorDataMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
......@@ -38,7 +42,8 @@ public class PowerGenerationImpl {
ESMoonPowerGenerationRepository moonPowerGenerationRepository;
@Autowired
ESDailyPowerGenerationRepository dailyPowerGenerationRepository;
@Autowired(required=true)
IndicatorDataMapper indicatorDataMapper;
//获取指定指标,当天最后一条数据
public List<IndexDto> getlast(String gatewayId, String value,String daty){
......@@ -67,23 +72,45 @@ public class PowerGenerationImpl {
//@Async("jxiopAsyncExecutor")
public void addPowerGeneration(StationCacheInfoDto stationCacheInfoDto,String gatewayId, String value, String dateFormat, String type, String stationType,String datyvalue){
//获取指标值
List<IndexDto> indexDto=null;
// List<IndexDto> indexDto=null;
List<IndicatorData> indexDto=null;
Date currentDayStartTime = DateUtils.getCurrentDayStartTime(new Date());
Date currentDayEndTime = DateUtils.getCurrentDayEndTime(new Date());
String startTime = DateUtils.convertDateToString(currentDayStartTime, DateUtils.DATE_TIME_PATTERN);
String endTime = DateUtils.convertDateToString(currentDayEndTime, DateUtils.DATE_TIME_PATTERN);
Double flags=null;
if("FDZ".equals(stationType)){
indexDto= this.getlast(gatewayId,value, datyvalue);
//indexDto= this.getlast(gatewayId,value, datyvalue);
indexDto= indicatorDataMapper.selectlastfd(value, gatewayId, startTime, endTime);
}else{
//增加系数转化
//"日发电量"
if("日发电量".equals(value)){
//"日发电量"
flags= CommonConstans.pvGenPoweActor * CommonConstans.pvGenPoweActorDay;
}else if("月发电量".equals(value)){
//"月发电量"
flags= CommonConstans.pvGenPoweActornew;
}else{
indexDto= this.getlastgf(gatewayId,value,datyvalue);
//"年发电量"
flags= CommonConstans.pvGenPoweActorYear;
}
indexDto= indicatorDataMapper.selectlastgf(value, gatewayId, startTime, endTime);
//indexDto= this.getlastgf(gatewayId,value,datyvalue);
}
SimpleDateFormat myFmt2=new SimpleDateFormat(dateFormat);
Date now=new Date();
String daty= myFmt2.format(now);
this.PowerGeneration(stationCacheInfoDto,indexDto,daty,type);
this.PowerGeneration(stationCacheInfoDto,indexDto,daty,type,flags);
}
//更新es
public void PowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndexDto> indexDto,String daty,String type){
public void PowerGeneration(StationCacheInfoDto stationCacheInfoDto, List<IndicatorData> indexDto,String daty,String type,Double flags){
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;
......@@ -100,15 +127,15 @@ public class PowerGenerationImpl {
switch (type) {
case "day":
List<ESDailyPowerGeneration> listd= this.getESDailyPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(day),monthy);
List<ESDailyPowerGeneration> listd= this.getESDailyPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(day),monthy,flags);
dailyPowerGenerationRepository.saveAll(listd);
break;
case "moon":
List<ESMoonPowerGeneration> listm= this.getESMoonPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(month),yeary);
List<ESMoonPowerGeneration> listm= this.getESMoonPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(month),yeary,flags);
moonPowerGenerationRepository.saveAll(listm);
break;
case "year":
List<ESYearPowerGeneration> listy= this.getESYearPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(year));
List<ESYearPowerGeneration> listy= this.getESYearPowerGeneration(stationCacheInfoDto,indexDto,daty,String.valueOf(year),flags);
yearPowerGenerationRepository.saveAll(listy);
break;
default:
......@@ -119,11 +146,16 @@ public class PowerGenerationImpl {
// 组装数据
public List<ESDailyPowerGeneration> getESDailyPowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndexDto> indexDto,String daty,String day,String month){
public List<ESDailyPowerGeneration> getESDailyPowerGeneration(StationCacheInfoDto stationCacheInfoDto, List<IndicatorData> indexDto,String daty,String day,String month,Double flags){
List<ESDailyPowerGeneration> list=new ArrayList<>();
if(indexDto!=null&&!indexDto.isEmpty()){
for (IndexDto dto : indexDto) {
for (IndicatorData dto : indexDto) {
double value=dto.getValueF()!=null?(double)dto.getValueF():0.0;
if(flags!=null){
value=value*flags;
}
ESDailyPowerGeneration dailyPowerGeneration=new ESDailyPowerGeneration(
daty+"_"+dto.getAddress(),
stationCacheInfoDto.getStationId(),
......@@ -134,7 +166,7 @@ public class PowerGenerationImpl {
day,
dto.getGatewayId(),
new Date(),
dto.getValue()!=null?Double.valueOf(dto.getValue()):null,
value,
dto.getEquipmentIndexName(),
dto.getEquipmentNumber(),
dto.getAddress(),
......@@ -145,10 +177,14 @@ public class PowerGenerationImpl {
}
return list;
}
public List<ESMoonPowerGeneration> getESMoonPowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndexDto> indexDto,String daty,String day,String year){
public List<ESMoonPowerGeneration> getESMoonPowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndicatorData> indexDto,String daty,String day,String year,Double flags){
List<ESMoonPowerGeneration> list=new ArrayList<>();
if(indexDto!=null&&!indexDto.isEmpty()){
for (IndexDto dto : indexDto) {
for (IndicatorData dto : indexDto) {
double value=dto.getValueF()!=null?(double)dto.getValueF():0.0;
if(flags!=null){
value=value*flags;
}
ESMoonPowerGeneration moonPowerGeneration=new ESMoonPowerGeneration(
daty+"_"+dto.getAddress(),
stationCacheInfoDto.getStationId(),
......@@ -159,7 +195,7 @@ public class PowerGenerationImpl {
day,
dto.getGatewayId(),
new Date(),
dto.getValue()!=null?Double.valueOf(dto.getValue()):null,
value,
dto.getEquipmentIndexName(),
dto.getEquipmentNumber(),
dto.getAddress(),
......@@ -170,10 +206,14 @@ public class PowerGenerationImpl {
}
return list;
}
public List<ESYearPowerGeneration> getESYearPowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndexDto> indexDto,String daty,String day){
public List<ESYearPowerGeneration> getESYearPowerGeneration(StationCacheInfoDto stationCacheInfoDto,List<IndicatorData> indexDto,String daty,String day,Double flags){
List<ESYearPowerGeneration> list=new ArrayList<>();
if(indexDto!=null&&!indexDto.isEmpty()){
for (IndexDto dto : indexDto) {
for (IndicatorData dto : indexDto) {
double value=dto.getValueF()!=null?(double)dto.getValueF():0.0;
if(flags!=null){
value=value*flags;
}
ESYearPowerGeneration yearPowerGeneration=new ESYearPowerGeneration(
daty+"_"+dto.getAddress(),
stationCacheInfoDto.getStationId(),
......@@ -184,7 +224,7 @@ public class PowerGenerationImpl {
day,
dto.getGatewayId(),
new Date(),
dto.getValue()!=null?Double.valueOf(dto.getValue()):null,
value,
dto.getEquipmentIndexName(),
dto.getEquipmentNumber(),
dto.getAddress()
......
......@@ -12,6 +12,13 @@ spring.db2.datasource.username=root
spring.db2.datasource.password=Yeejoin@2020
spring.db2.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.db3.datasource.url=jdbc:TAOS-RS://139.9.170.47:6041/iot_data?user=root&password=taosdata&timezone=GMT%2b8&allowMultiQueries=true
spring.db3.datasource.username=root
spring.db3.datasource.password=taosdata
spring.db3.datasource.driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
## eureka properties:
eureka.instance.hostname=172.16.10.220
eureka.client.serviceUrl.defaultZone=http://admin:a1234560@${eureka.instance.hostname}:10001/eureka/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment