SyncHnjzDataJob.java 6.5 KB
package com.skua.modules.quartz.job;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.skua.core.cache.RedisUtil;
import com.skua.core.context.SpringContextUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Random;

/**
 * 同步河南焦作采集数据
 */
@Slf4j
public class SyncHnjzDataJob implements Job {

//	private static final String HNJZ_URL = "https://399w65q886.oicp.vip/parsing/api/RiverMonitoring/RiverwayHXW";//河南焦作数据同步地址-测试
	private static final String HNJZ_URL = "http://123.60.20.81:9876/parsing/api/RiverMonitoring/RiverwayHXW";//河南焦作数据同步地址-正式
	@Autowired
	private RedisUtil redisUtil;
	//格式化时间
	private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	private String parameter;

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	@SneakyThrows
	@Override
	public void execute(JobExecutionContext jobExecutionContext) {
		LocalDateTime now = LocalDateTime.now();
		String cjDateTime = now.format(formatter);
		log.info(String.format(" 同步河南焦作采集数据 !  时间:" + cjDateTime));
		JdbcTemplate hxDB = (JdbcTemplate) SpringContextUtils.getBean("mysql-hx");
		String sql = "SELECT " +
				"  id,\n" +
				"  create_by as createBy,\n" +
				"  create_time as createTime,\n" +
				"  update_by as updateBy,\n" +
				"  update_time as updateTime,\n" +
				"  sys_org_code as sysOrgCode,\n" +
				"  del_flag as delFlag,\n" +
				"  eqm_id as eqmId,\n" +
				"  eqm_no as eqmNo,\n" +
				"  eqm_name as eqmName,\n" +
				"  project_id as projectId,\n" +
				"  area_id as areaId,\n" +
				"  temperature,\n" +
				"  humidity,\n" +
				"  battery,\n" +
				"  rssi,\n" +
				"  sinr,\n" +
				"  batpercent,\n" +
				"  datatype1,\n" +
				"  datavalue1,\n" +
				"  newdatavalue1,\n" +
				"  datatype2,\n" +
				"  datavalue2,\n" +
				"  newdatavalue2,\n" +
				"  datatype3,\n" +
				"  datavalue3,\n" +
				"  newdatavalue3,\n" +
				"  datatype4,\n" +
				"  datavalue4,\n" +
				"  newdatavalue4,\n" +
				"  collect_time as collectTime,\n" +
				"  remark,\n" +
				"  platform_id as platformId,\n" +
				"  tcp_flag as tcpFlag,\n" +
				"  datatype5,\n" +
				"  datavalue5,\n" +
				"  datatype6,\n" +
				"  datavalue6,\n" +
				"  datatype7,\n" +
				"  datavalue7,\n" +
				"  datatype8,\n" +
				"  datavalue8,\n" +
				"  light,\n" +
				"  lat,\n" +
				"  lng,\n" +
				"  x_axis as xAxis,\n" +
				"  y_axis as yAxis,\n" +
				"  hostch_no as hostchNo\n" +
				"  FROM\n" +
				"  dcs_current_info \n" +
				"  WHERE\n" +
				"  platform_id = 'JJ202309250005' \n" +
				"  ORDER BY\n" +
				"  create_time DESC \n" +
				"  LIMIT 1";
		Map<String,Object> map = hxDB.queryForMap(sql);
		if("mn".equals(this.parameter)){
			map.put("id",null);
			map.put("temperature",randomZhengShu(17,19));
			map.put("humidity",randomZhengShu(50,60));
			map.put("battery",randomXiaoShu(7.00,8.00));
			map.put("rssi",randomZhengShu(-113,-100));
			map.put("sinr",randomZhengShu(20,26));
			map.put("batpercent",randomZhengShu(60,100));
			map.put("datatype1",1);
			double datavalue1 = randomXiaoShu(0.03,0.06);
			map.put("datavalue1",datavalue1);
			map.put("newdatavalue1",datavalue1);
			map.put("datatype2",2);
			double datavalue2 = randomXiaoShu(0.01,0.015);
			map.put("datavalue2",datavalue2);
			map.put("newdatavalue2",datavalue2);
			map.put("dataValueSsll",calculateLS( datavalue1,  datavalue2,  1.65));
			map.put("collectTime",cjDateTime);
			map.put("createTime",cjDateTime);
			map.put("updateTime",cjDateTime);
			map.put("light",randomXiaoShu(3.00,4.00));
			map.put("remark",null);
			log.info("河南焦作数据同步请求参数为:"+map);
		}else{
			if(map.size() > 0){
				String collectTime = map.get("collectTime").toString();
				double yw = 0.00;
				double ls = 0.00;
				if(map.get("datavalue1")!=null){
					yw = Double.parseDouble(map.get("datavalue1").toString());
				}
				if(map.get("datavalue2")!=null){
					ls = Double.parseDouble(map.get("datavalue2").toString());
				}
				map.put("dataValueSsll",calculateLS( yw,  ls,  1.65));
				String key = "hnjz-"+collectTime;
				Object code = redisUtil.get(key);
				if(code==null){
					redisUtil.set(key, map);
					log.info("河南焦作数据同步请求参数为("+key+"):"+map);
				}
			}
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(map);
		HttpRequest request = HttpUtil.createPost(HNJZ_URL);
		request.contentType("application/json");
		request.body(json);
		String post = request.execute().body();
		log.info("回复的消息为:"+post);
	}

	public static void main(String[] args) {
		System.out.println("随机数:" + calculateLS( 0.03,  0.015,  1.65));
	}

	public static int randomZhengShu(int min, int max){
		Random random = new Random();
		int randomNumber = random.nextInt(max - min + 1) + min;
		return randomNumber;
	}

	public static double randomXiaoShu(double min,double max){
		Random random = new Random();
		double randomNumber = min + (max - min) * random.nextDouble();
		DecimalFormat decimalFormat = new DecimalFormat("#.###");
		String formattedNumber = decimalFormat.format(randomNumber);
		return Double.parseDouble(formattedNumber);
	}

	/**
	 * 流量计算
	 * @param currentValue
	 * @param speedValue
	 * @param tubedia
	 * @return
	 */
	public static String calculateLS(double currentValue, double speedValue, double tubedia) {
		double H = currentValue;   //水深(液位),计算流量的设备一定会同时上传流速speed(单位m/s)和液位值level(单位m/s)
		double D = tubedia;  //圆管直径diam,单位m
		double R = D / 2;   //管道半径
		double S = 0;       //圆管截面积
		if (H >= D) {
			S = Math.PI * R * R;
		} else if (H <= R) {
			double ang = Math.acos((R - H) / R) * R * R;
			S = ang - Math.sqrt(R * R - (R - H) * (R - H)) * Math.abs(R - H);
		} else if (R < H && H < D) {
			double ang = Math.acos((R - H) / R) * R * R;
			S = ang + Math.sqrt(R * R - (R - H) * (R - H)) * Math.abs(R - H);
		}
		//计算瞬时流量flow,单位m3/s
		DecimalFormat df = new DecimalFormat("#.####");
		return df.format(speedValue * S);
	}
}