feat(data): 添加 MES 系统数据对接功能
- 新增 MES 数据源配置和映射接口- 实现 MES 数据获取和处理的定时任务 - 添加数据保存和缓存功能dev
							parent
							
								
									be933f2a99
								
							
						
					
					
						commit
						4f11b686c5
					
				|  | @ -0,0 +1,66 @@ | |||
| package com.zcloud.config; | ||||
| 
 | ||||
| import com.alibaba.druid.pool.DruidDataSource; | ||||
| import org.apache.ibatis.session.SqlSessionFactory; | ||||
| import org.mybatis.spring.SqlSessionFactoryBean; | ||||
| import org.mybatis.spring.annotation.MapperScan; | ||||
| 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.DefaultResourceLoader; | ||||
| import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||||
| import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||||
| 
 | ||||
| import javax.sql.DataSource; | ||||
| import java.sql.SQLException; | ||||
| 
 | ||||
| /** | ||||
|  * 说明:MES数据源配置 | ||||
|  */ | ||||
| @Configuration | ||||
| @MapperScan(basePackages = MesDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mesSqlSessionFactory") | ||||
| public class MesDataSourceConfig { | ||||
| 
 | ||||
|     static final String PACKAGE = "com.zcloud.mapper.dsno2.monitoring";                          //mapper 目录
 | ||||
|     static final String MAPPER_LOCATION = "classpath:mybatis/dsno2/monitoring/*.xml";            //扫描的 xml 目录
 | ||||
|     static final String CONFIG_LOCATION = "classpath:mybatis/dsno2/mybatis-config.xml";          //自定义的mybatis config 文件位置
 | ||||
|     static final String TYPE_ALIASES_PACKAGE = "com.zcloud.entity";                              //扫描的 实体类 目录
 | ||||
| 
 | ||||
|     @Value("${datasource.mes.url}") | ||||
|     private String url; | ||||
| 
 | ||||
|     @Value("${datasource.mes.username}") | ||||
|     private String user; | ||||
| 
 | ||||
|     @Value("${datasource.mes.password}") | ||||
|     private String password; | ||||
| 
 | ||||
|     @Value("${datasource.mes.driver-class-name}") | ||||
|     private String driverClass; | ||||
| 
 | ||||
|     @Bean(name = "mesDataSource") | ||||
|     public DataSource mesDataSource() throws SQLException { | ||||
|         DruidDataSource dataSource = new DruidDataSource(); | ||||
|         dataSource.setDriverClassName(driverClass); | ||||
|         dataSource.setUrl(url); | ||||
|         dataSource.setUsername(user); | ||||
|         dataSource.setPassword(password); | ||||
|         return dataSource; | ||||
|     } | ||||
| 
 | ||||
|     @Bean(name = "mesTransactionManager") | ||||
|     public DataSourceTransactionManager mesTransactionManager() throws SQLException { | ||||
|         return new DataSourceTransactionManager(mesDataSource()); | ||||
|     } | ||||
| 
 | ||||
|     @Bean(name = "mesSqlSessionFactory") | ||||
|     public SqlSessionFactory mesSqlSessionFactory(@Qualifier("mesDataSource") DataSource mesDataSource) throws Exception { | ||||
|         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); | ||||
|         sessionFactory.setDataSource(mesDataSource); | ||||
|         sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MesDataSourceConfig.MAPPER_LOCATION)); | ||||
|         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MesDataSourceConfig.CONFIG_LOCATION)); | ||||
|         sessionFactory.setTypeAliasesPackage(MesDataSourceConfig.TYPE_ALIASES_PACKAGE); | ||||
|         return sessionFactory.getObject(); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,34 @@ | |||
| package com.zcloud.mapper.dsno2.monitoring; | ||||
| 
 | ||||
| import com.zcloud.entity.PageData; | ||||
| import org.apache.ibatis.annotations.Param; | ||||
| import org.springframework.stereotype.Repository; | ||||
| 
 | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * MES数据访问接口 | ||||
|  */ | ||||
| @Repository | ||||
| public interface MesDataMapper { | ||||
|      | ||||
|     /** | ||||
|      * 获取MES系统中的所有数据点 | ||||
|      * @return 数据点列表 | ||||
|      */ | ||||
|     List<Map<String, Object>> listAllMesData(); | ||||
|      | ||||
|     /** | ||||
|      * 根据条件查询MES数据 | ||||
|      * @param pd 查询条件 | ||||
|      * @return 数据列表 | ||||
|      */ | ||||
|     List<PageData> listMesDataByCondition(PageData pd); | ||||
|      | ||||
|     /** | ||||
|      * 批量保存从MES获取的数据 | ||||
|      * @param dataList 数据列表 | ||||
|      */ | ||||
|     void saveBatchFromMes(List<Map<String, Object>> dataList); | ||||
| } | ||||
|  | @ -0,0 +1,93 @@ | |||
| package com.zcloud.scheduled.dataDocking; | ||||
| 
 | ||||
| import com.alibaba.fastjson.JSONObject; | ||||
| import com.xxl.job.core.context.XxlJobHelper; | ||||
| import com.xxl.job.core.handler.IJobHandler; | ||||
| import com.xxl.job.core.handler.annotation.XxlJob; | ||||
| import com.zcloud.mapper.dsno2.monitoring.MesDataMapper; | ||||
| import com.zcloud.util.DateUtil; | ||||
| import com.zcloud.util.RedisUtil; | ||||
| import com.zcloud.util.UuidUtil; | ||||
| import org.springframework.scheduling.annotation.Scheduled; | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import javax.annotation.Resource; | ||||
| import java.util.*; | ||||
| 
 | ||||
| /** | ||||
|  * MES系统数据对接定时任务 | ||||
|  * 从MES系统的Oracle数据库视图中获取数据 | ||||
|  * | ||||
|  * @author | ||||
|  */ | ||||
| @Component | ||||
| public class MesDataScheduled extends IJobHandler { | ||||
| 
 | ||||
|     @Resource | ||||
|     private MesDataMapper mesDataMapper; | ||||
| 
 | ||||
|     @Resource | ||||
|     private RedisUtil redisUtil; | ||||
| 
 | ||||
|     @Override | ||||
|     @XxlJob(value = "mesDataJob") | ||||
|     public void execute() throws Exception { | ||||
|         XxlJobHelper.log("开始执行MES系统数据对接任务"); | ||||
| 
 | ||||
|         // 处理批次 ID
 | ||||
|         String processingBatchId = UuidUtil.get32UUID(); | ||||
|         // 批处理时间
 | ||||
|         String processingTime = DateUtil.date2Str(new Date()); | ||||
| 
 | ||||
|         try { | ||||
|             // 从MES系统获取数据
 | ||||
|             List<Map<String, Object>> mesDataList = mesDataMapper.listAllMesData(); | ||||
|             XxlJobHelper.log("从MES系统获取到{}条数据", mesDataList.size()); | ||||
| 
 | ||||
|             // 处理数据
 | ||||
|             for (Map<String, Object> item : mesDataList) { | ||||
|                 // 添加处理时间和批次ID
 | ||||
|                 item.put("PROCESSING_TIME", processingTime); | ||||
|                 item.put("PROCESSING_BATCH_ID", processingBatchId); | ||||
| 
 | ||||
|                 // 处理空值
 | ||||
|                 item.replaceAll((k, v) -> v == null ? "" : v); | ||||
|             } | ||||
| 
 | ||||
|             // 保存数据到数据库
 | ||||
| //            if (!mesDataList.isEmpty()) {
 | ||||
| //                mesDataMapper.saveBatchFromMes(mesDataList);
 | ||||
| //                XxlJobHelper.log("成功保存{}条MES数据到本地数据库", mesDataList.size());
 | ||||
| //
 | ||||
| //                // 将数据存入Redis缓存
 | ||||
| //                redisUtil.set("MES_DATA_LIST", JSONObject.toJSONString(mesDataList));
 | ||||
| //                XxlJobHelper.log("已将MES数据存入Redis缓存");
 | ||||
| //            }
 | ||||
| 
 | ||||
|             XxlJobHelper.log("MES系统数据对接任务执行完成"); | ||||
|         } catch (Exception e) { | ||||
|             XxlJobHelper.log("MES系统数据对接任务执行异常: {}", e.getMessage()); | ||||
|             throw e; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void init() { | ||||
|         // 初始化操作
 | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void destroy() { | ||||
|         // 销毁操作
 | ||||
|     } | ||||
| 
 | ||||
|     @Scheduled(cron ="*/5 * * * * ?") | ||||
| 	public void scheduled(){ | ||||
| 		try { | ||||
| 			System.out.println( "============定时查询MES数据=========="); | ||||
|             List<Map<String, Object>> mesDataList = mesDataMapper.listAllMesData(); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | @ -0,0 +1,62 @@ | |||
| datasource.no1.driver-class-name: com.mysql.cj.jdbc.Driver | ||||
| datasource.no1.url=jdbc:mysql://127.0.0.1:3306/integrated_yjb?allowPublicKeyRetrieval=true&serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 | ||||
| datasource.no1.username=root | ||||
| datasource.no1.password=Mysql@zcloud33080 | ||||
| # | ||||
| datasource.no2.driver-class-name:org.postgresql.Driver | ||||
| datasource.no2.url=jdbc:postgresql://127.0.0.1:3306/postgres?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=UTC | ||||
| datasource.no2.username=postgres | ||||
| datasource.no2.password=DjIGYM8k0h | ||||
| #MES系统数据库配置 | ||||
| datasource.mes.driver-class-name=oracle.jdbc.OracleDriver | ||||
| datasource.mes.url=jdbc:oracle:thin:@172.16.90.252:1521/jymes | ||||
| datasource.mes.username=drzf | ||||
| datasource.mes.password=drzf | ||||
| #redis | ||||
| spring.redis.host=10.199.64.30 | ||||
| spring.redis.password=5.5pYdZqHxpR#9%W. | ||||
| spring.redis.database=4 | ||||
| spring.redis.port=16379 | ||||
| 
 | ||||
| #druidè¿æ¥æ±  | ||||
| spring.datasource.type:com.alibaba.druid.pool.DruidDataSource | ||||
| #æå¤§æ´»è·æ° | ||||
| spring.datasource.maxActive:20 | ||||
| #åå§åæ°é | ||||
| spring.datasource.initialSize:1 | ||||
| #æå¤§è¿æ¥çå¾
è¶
æ¶æ¶é´ | ||||
| spring.datasource.maxWait:60000 | ||||
| #æå¼PSCacheï¼å¹¶ä¸æå®æ¯ä¸ªè¿æ¥PSCacheçå¤§å° | ||||
| spring.datasource.poolPreparedStatements:true | ||||
| spring.datasource.maxPoolPreparedStatementPerConnectionSize:20 | ||||
| #éè¿connectionProperties屿§æ¥æå¼mergeSqlåè½ï¼æ
¢SQLè®°å½ | ||||
| #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 | ||||
| spring.datasource.minIdle:1 | ||||
| spring.datasource.timeBetweenEvictionRunsMillis:60000 | ||||
| spring.datasource.minEvictableIdleTimeMillis:300000 | ||||
| spring.datasource.validationQuery:select 1 from dual | ||||
| spring.datasource.testWhileIdle:true | ||||
| spring.datasource.testOnBorrow:false | ||||
| spring.datasource.testOnReturn:false | ||||
| #é
ç½®çæ§ç»è®¡æ¦æªçfiltersï¼å»æåçæ§çé¢sqlå°æ æ³ç»è®¡,'wall'ç¨äºé²ç«å¢ | ||||
| filters:stat, wall, log4j | ||||
| #ç¼åé
ç½®æä»¶ä½ç½® | ||||
| spring.cache.ehcache.cofnig=ehcache.xml | ||||
| #é
ç½®è¿å¥è¯,æ§å¶å°è¾åºsqlè¯å¥ | ||||
| logging.level.com.zcloud.mapper=debug | ||||
| #ä¸ä¼ æä»¶å¤§å°éå¶ | ||||
| spring.servlet.multipart.max-file-size=500MB | ||||
| spring.servlet.multipart.max-request-size=500MB | ||||
| #activitiæ¨¡åæ£æµ | ||||
| spring.activiti.check-process-definitions=false | ||||
| #å符 | ||||
| server.servlet.encoding.charset=UTF-8 | ||||
| server.servlet.encoding.force=true | ||||
| server.servlet.encoding.enabled=true | ||||
| spring.main.banner-mode=off | ||||
| 
 | ||||
| xxl.job.admin.addresses=http://192.168.20.230:28080/xxl-job-admin | ||||
| xxl.job.accessToken=yjb.prod | ||||
| xxl.job.port=1211 | ||||
| xxl.job.executor.appname=yjb-job-normal-task | ||||
| xxl.job.executor.logretentiondays=30 | ||||
|  | @ -1,5 +1,5 @@ | |||
| spring.application.name=integrated_whb_scheduled | ||||
| server.port=19099 | ||||
| spring.profiles.active=master | ||||
| spring.profiles.active=local | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,46 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||||
| <mapper namespace="com.zcloud.mapper.dsno2.monitoring.MesDataMapper"> | ||||
| 
 | ||||
|     <!-- 查询所有MES数据,使用MyBatis自动映射功能 --> | ||||
|     <select id="listAllMesData" resultType="java.util.HashMap"> | ||||
|         select * from drzf_view | ||||
|     </select> | ||||
| 
 | ||||
|     <!-- 根据条件查询MES数据 --> | ||||
|     <select id="listMesDataByCondition" parameterType="com.zcloud.entity.PageData" resultType="com.zcloud.entity.PageData"> | ||||
|         SELECT * FROM mes_data_view | ||||
|         WHERE 1=1 | ||||
|         <if test="deviceId != null and deviceId != ''"> | ||||
|             AND DEVICE_ID = #{deviceId} | ||||
|         </if> | ||||
|         <if test="deviceName != null and deviceName != ''"> | ||||
|             AND DEVICE_NAME LIKE CONCAT('%', #{deviceName}, '%') | ||||
|         </if> | ||||
|     </select> | ||||
| 
 | ||||
|     <!-- 批量保存从MES获取的数据 --> | ||||
|     <insert id="saveBatchFromMes" parameterType="java.util.List"> | ||||
|         INSERT INTO target_table ( | ||||
|             ID, | ||||
|             DEVICE_ID, | ||||
|             DEVICE_NAME, | ||||
|             CURRENT_VALUE, | ||||
|             GATHER_TIME, | ||||
|             PROCESSING_TIME, | ||||
|             PROCESSING_BATCH_ID | ||||
|         ) VALUES | ||||
|         <foreach collection="list" item="item" separator=","> | ||||
|             ( | ||||
|                 #{item.ID, jdbcType=VARCHAR}, | ||||
|                 #{item.DEVICE_ID, jdbcType=VARCHAR}, | ||||
|                 #{item.DEVICE_NAME, jdbcType=VARCHAR}, | ||||
|                 #{item.CURRENT_VALUE, jdbcType=VARCHAR}, | ||||
|                 #{item.GATHER_TIME, jdbcType=TIMESTAMP}, | ||||
|                 #{item.PROCESSING_TIME, jdbcType=TIMESTAMP}, | ||||
|                 #{item.PROCESSING_BATCH_ID, jdbcType=VARCHAR} | ||||
|             ) | ||||
|         </foreach> | ||||
|     </insert> | ||||
| 
 | ||||
| </mapper> | ||||
		Loading…
	
		Reference in New Issue