Browse Source

git-svn-id: https://192.168.57.71/svn/qt@91 12fe181a-e57f-b044-8676-16dc139aa63e

ld_zhoutl 8 years ago
parent
commit
9560c825df

+ 22 - 0
jdc/dataChange/trunk/src/main/java/com/jsjty/jdc/data/mappers/WeatherAreaMapper.java

@@ -0,0 +1,22 @@
+package com.jsjty.jdc.data.mappers;
+
+import java.util.List;
+
+import com.jsjty.jdc.data.model.WeatherArea;
+
+/**
+ * 天气区域DAO
+ * 
+ * @author 袁晓冬
+ *
+ */
+public interface WeatherAreaMapper extends BaseMapper<WeatherArea> {
+    
+    /**
+     * 根据数据时间查询需要获取天气区域信息
+     * 
+     * @param sjsj
+     * @return
+     */
+    List<WeatherArea> selectAreas(String sjsj);
+}

+ 145 - 0
jdc/dataChange/trunk/src/main/java/com/jsjty/jdc/data/model/WeatherArea.java

@@ -0,0 +1,145 @@
+package com.jsjty.jdc.data.model;
+
+
+public class WeatherArea {
+    private String id;
+    private String c1;
+    private String c2;
+    private String c3;
+    private String c4;
+    private String c5;
+    private String c6;
+    private String c7;
+    private String c8;
+    private String c9;
+    private String c10;
+    private String c11;
+    private String c12;
+    private String c13;
+    private String c14;
+    private String c15;
+    private String c16;
+    private String c17;
+    private String f0;
+    private String xzqhid;
+    public String getId() {
+        return id;
+    }
+    public void setId(String id) {
+        this.id = id;
+    }
+    public String getC1() {
+        return c1;
+    }
+    public void setC1(String c1) {
+        this.c1 = c1;
+    }
+    public String getC2() {
+        return c2;
+    }
+    public void setC2(String c2) {
+        this.c2 = c2;
+    }
+    public String getC3() {
+        return c3;
+    }
+    public void setC3(String c3) {
+        this.c3 = c3;
+    }
+    public String getC4() {
+        return c4;
+    }
+    public void setC4(String c4) {
+        this.c4 = c4;
+    }
+    public String getC5() {
+        return c5;
+    }
+    public void setC5(String c5) {
+        this.c5 = c5;
+    }
+    public String getC6() {
+        return c6;
+    }
+    public void setC6(String c6) {
+        this.c6 = c6;
+    }
+    public String getC7() {
+        return c7;
+    }
+    public void setC7(String c7) {
+        this.c7 = c7;
+    }
+    public String getC8() {
+        return c8;
+    }
+    public void setC8(String c8) {
+        this.c8 = c8;
+    }
+    public String getC9() {
+        return c9;
+    }
+    public void setC9(String c9) {
+        this.c9 = c9;
+    }
+    public String getC10() {
+        return c10;
+    }
+    public void setC10(String c10) {
+        this.c10 = c10;
+    }
+    public String getC11() {
+        return c11;
+    }
+    public void setC11(String c11) {
+        this.c11 = c11;
+    }
+    public String getC12() {
+        return c12;
+    }
+    public void setC12(String c12) {
+        this.c12 = c12;
+    }
+    public String getC13() {
+        return c13;
+    }
+    public void setC13(String c13) {
+        this.c13 = c13;
+    }
+    public String getC14() {
+        return c14;
+    }
+    public void setC14(String c14) {
+        this.c14 = c14;
+    }
+    public String getC15() {
+        return c15;
+    }
+    public void setC15(String c15) {
+        this.c15 = c15;
+    }
+    public String getC16() {
+        return c16;
+    }
+    public void setC16(String c16) {
+        this.c16 = c16;
+    }
+    public String getC17() {
+        return c17;
+    }
+    public void setC17(String c17) {
+        this.c17 = c17;
+    }
+    public String getF0() {
+        return f0;
+    }
+    public void setF0(String f0) {
+        this.f0 = f0;
+    }
+    public String getXzqhid() {
+        return xzqhid;
+    }
+    public void setXzqhid(String xzqhid) {
+        this.xzqhid = xzqhid;
+    }
+}

+ 298 - 0
jdc/dataChange/trunk/src/main/java/com/jsjty/jdc/data/service/impl/WeatherXZFetchService.java

@@ -0,0 +1,298 @@
+package com.jsjty.jdc.data.service.impl;
+
+import java.net.URLEncoder;
+import java.security.InvalidKeyException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.jsjty.jdc.data.mappers.WeatherAreaMapper;
+import com.jsjty.jdc.data.mappers.WeatherMapper;
+import com.jsjty.jdc.data.model.Weather;
+import com.jsjty.jdc.data.model.WeatherArea;
+import com.jsjty.jdc.data.service.FetchService;
+import com.jsjty.jdc.data.util.HttpProxy;
+import com.jsjty.jdc.data.util.IdGenerator;
+
+/**
+ * 心知天气抓取服务
+ * 
+ * @author 
+ *
+ */
+@Service("weatherXZFetchService")
+public class WeatherXZFetchService implements FetchService {
+    private Logger logger = LoggerFactory.getLogger(WeatherXZFetchService.class);
+    /** 心知天气网APPID密钥 */
+    public static String WEATHER_KEY = "8jjmhrqevzmjxzh0";
+    @Autowired
+    private WeatherMapper mapper;
+    @Autowired
+    private WeatherAreaMapper areaMapper;
+    @Autowired
+    private IdGenerator idGenerator;
+
+    @Override
+    public int doFetch() {
+        List<WeatherArea> areaList = areaMapper.selectAreas(getDeployTime());
+        boolean success = true;
+        for (WeatherArea area : areaList) {
+            if(area.getC2().equals("nanjing")) {
+                System.out.println("sss");
+            }
+            success = fetchWeater(area) && success;
+        }
+        /*if (!success) {
+            // 抓取没有完全成功,60秒后重新抓取
+            throw new TaskRedoException(60, TimeUnit.SECONDS, "抓取失败");
+        }
+        else {
+            return areaList.size();
+        }*/
+        return areaList.size();
+    }
+
+    /**
+     * 抓取区域天气信息
+     * 
+     * @param areaId
+     */
+    private boolean fetchWeater(WeatherArea area) {
+        Date d = new Date();
+        String url = buildUrl(area.getC2());
+        String res = null;
+        try {
+            logger.info("获取天气信息,url:{}", url);
+            res = HttpProxy.http_get_json(url);
+        }
+        catch (Exception e) {
+            logger.error("获取天气信息出错,url:{},错误信息:{}", url, e.getMessage());
+            return false;
+        }
+        if (null == res) {
+            System.err.println("该区域未获取到天气信息:"+area.getC2());
+            return false;
+        }
+        // 生成缓存key,天气每天发布3次,时间分别是08、11、18;
+        JsonParser jp = new JsonParser();
+        JsonElement je = jp.parse(res);
+        JsonObject retJson = je.getAsJsonObject();
+        JsonArray f = retJson.getAsJsonArray("results");
+        JsonArray dayweaters = retJson.getAsJsonArray("results")
+                .get(0).getAsJsonObject().get("daily").getAsJsonArray();
+        Calendar c = Calendar.getInstance();
+        c.setTime(d);
+        JsonObject d1 = dayweaters.get(0).getAsJsonObject();
+        JsonObject d2 = dayweaters.get(1).getAsJsonObject();
+        JsonObject d3 = dayweaters.get(2).getAsJsonObject();
+        saveWeather(area.getC1(), d1, c.getTime());
+        c.add(Calendar.DATE, 1);
+        saveWeather(area.getC1(), d2, c.getTime());
+        c.add(Calendar.DATE, 1);
+        saveWeather(area.getC1(), d3, c.getTime());
+
+        return true;
+    }
+
+    /**
+     * 保存
+     * @param name 城市英文名
+     * @param data 数据
+     * @param time 日期
+     */
+    private void saveWeather(String areaId, JsonObject data, Date time) {
+        DateFormat df = new SimpleDateFormat("yyyyMMdd");
+        String sj = df.format(time);
+        Calendar c = Calendar.getInstance();
+        c.setTime(new Date());
+        // 系统当前日
+        int currDay = c.get(Calendar.DAY_OF_MONTH);
+        c.setTime(time);
+        // 数据所在日
+        int dataDay = c.get(Calendar.DAY_OF_MONTH);
+        // 数据所在时
+        int dataHour = c.get(Calendar.HOUR_OF_DAY);
+        // 查询老记录
+        Weather weather = mapper.selectBySjAndArea(sj, areaId);
+        // 如果已存在记录则判断是否需要更新
+        if (weather != null) {
+            weather.setFa(data.get("code_day").getAsString());
+            weather.setFb(data.get("code_night").getAsString());
+            weather.setFc(data.get("high").getAsString());
+            weather.setFd(data.get("low").getAsString());
+            weather.setFe(data.get("wind_direction").getAsString());
+            weather.setFf(data.get("wind_direction").getAsString());
+            weather.setFg(data.get("wind_scale").getAsString());
+            weather.setFh(data.get("wind_scale").getAsString());
+            weather.setFi("");
+            // 更新夜晚数据
+            if (dataDay == currDay && dataHour >= 18) {
+                mapper.updateNightByPrimaryKey(weather);
+            }
+            else {
+                mapper.updateByPrimaryKey(weather);
+            }
+        }
+        // 不存在记录则插入
+        else {
+            weather = new Weather();
+            weather.setId(idGenerator.generateStringId());
+            weather.setFa(data.get("code_day").getAsString());
+            weather.setFb(data.get("code_night").getAsString());
+            weather.setFc(data.get("high").getAsString());
+            weather.setFd(data.get("low").getAsString());
+            weather.setFe(data.get("wind_direction").getAsString());
+            weather.setFf(data.get("wind_direction").getAsString());
+            weather.setFg(data.get("wind_scale").getAsString());
+            weather.setFh(data.get("wind_scale").getAsString());
+            weather.setSj(Integer.parseInt(sj));
+            weather.setArea(areaId);
+            mapper.insert(weather);
+        }
+        mapper.updateWeatherAreaF0ByC1(getDeployTime(), areaId);
+    }
+
+    /**
+     * 
+     * @param name
+     *            城市英文名
+     * @return
+     */
+    private String buildUrl(String name) {
+        // 客户端日期 按照格式  yyyyMMddHHmm 获取客户端当前时间
+        String url = null;
+        try {
+            StringBuilder urlBuilder = new StringBuilder();
+            //需要加密的数据  
+            urlBuilder.append("https://api.thinkpage.cn/v3/weather/daily.json?key=");
+            urlBuilder.append(WEATHER_KEY);
+            urlBuilder.append("&location=");
+            urlBuilder.append(name);
+            urlBuilder.append("&language=zh-Hans&unit=c&start=0&days=5");
+            url = urlBuilder.toString();
+        }
+        catch (Exception e) {
+            logger.error("创建天气查询出错,错误信息:{}", e.getMessage());
+        }
+        return url;
+    }
+
+    private String standardURLEncoder(String data, String key) {
+        byte[] byteHMAC = null;
+        String urlEncoder = "";
+        try {
+            Mac mac = Mac.getInstance("HmacSHA1");
+            SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
+            mac.init(spec);
+            byteHMAC = mac.doFinal(data.getBytes());
+            if (byteHMAC != null) {
+                String oauth = encode(byteHMAC);
+                if (oauth != null) {
+                    urlEncoder = URLEncoder.encode(oauth, "utf8");
+                }
+            }
+        }
+        catch (InvalidKeyException e1) {
+            e1.printStackTrace();
+        }
+        catch (Exception e2) {
+            e2.printStackTrace();
+        }
+        return urlEncoder;
+    }
+
+    private static final char last2byte = (char) Integer.parseInt("00000011", 2);
+    private static final char last4byte = (char) Integer.parseInt("00001111", 2);
+    private static final char last6byte = (char) Integer.parseInt("00111111", 2);
+    private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
+    private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
+    private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
+    private static final char[] encodeTable = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
+            '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
+
+    private String encode(byte[] from) {
+        StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);
+        int num = 0;
+        char currentByte = 0;
+        for (int i = 0; i < from.length; i++) {
+            num = num % 8;
+            while (num < 8) {
+                switch (num) {
+                case 0:
+                    currentByte = (char) (from[i] & lead6byte);
+                    currentByte = (char) (currentByte >>> 2);
+                    break;
+                case 2:
+                    currentByte = (char) (from[i] & last6byte);
+                    break;
+                case 4:
+                    currentByte = (char) (from[i] & last4byte);
+                    currentByte = (char) (currentByte << 2);
+                    if ((i + 1) < from.length) {
+                        currentByte |= (from[i + 1] & lead2byte) >>> 6;
+                    }
+                    break;
+                case 6:
+                    currentByte = (char) (from[i] & last2byte);
+                    currentByte = (char) (currentByte << 4);
+                    if ((i + 1) < from.length) {
+                        currentByte |= (from[i + 1] & lead4byte) >>> 4;
+                    }
+                    break;
+                }
+                to.append(encodeTable[currentByte]);
+                num += 6;
+            }
+        }
+        if (to.length() % 4 != 0) {
+            for (int i = 4 - to.length() % 4; i > 0; i--) {
+                to.append("=");
+            }
+        }
+        return to.toString();
+    }
+
+    /**
+     * 获取当前应该获取的数据时间
+     * 
+     * @return
+     */
+    private String getDeployTime() {
+        Calendar c = Calendar.getInstance();
+        int h = c.get(Calendar.HOUR_OF_DAY);
+        if (h >= 18) {
+            c.set(Calendar.HOUR_OF_DAY, 18);
+        }
+        else if (h >= 11) {
+            c.set(Calendar.HOUR_OF_DAY, 11);
+        }
+        else if (h >= 8) {
+            c.set(Calendar.HOUR_OF_DAY, 8);
+        }
+        DateFormat df = new SimpleDateFormat("yyyyMMddHH");
+        String dateTime = df.format(c.getTime());
+        dateTime += "00";
+        return dateTime;
+    }
+
+    @Override
+    public String getName() {
+        return "天气抓取服务";
+    }
+}

+ 8 - 2
jdc/dataChange/trunk/src/main/java/com/jsjty/jdc/data/task/QuartzServlet.java

@@ -25,7 +25,7 @@ import com.jsjty.jdc.data.service.impl.HbdtSHFecthServiceImpl;
 import com.jsjty.jdc.data.service.impl.HbdtWxFecthServiceImpl;
 import com.jsjty.jdc.data.service.impl.HbdtXzFecthServiceImpl;
 import com.jsjty.jdc.data.service.impl.PlaneImageFetchService;
-import com.jsjty.jdc.data.service.impl.WeatherFetchService;
+import com.jsjty.jdc.data.service.impl.WeatherXZFetchService;
 
 /**
  * 定时任务Servlet
@@ -75,7 +75,13 @@ public class QuartzServlet extends HttpServlet {
     }
 
     private void initWeatherFetchService() {
-        WeatherFetchService service = beanFactory.getBean(WeatherFetchService.class);
+        /*WeatherFetchService service = beanFactory.getBean(WeatherFetchService.class);
+        long initialDelay = getHourMillis() - System.currentTimeMillis();
+        long oneHour = 60 * 60 * 1000;
+        // 整点执行一次
+        executor.scheduleAtFixedRate(new FetchTask(service, executor, 10), initialDelay, oneHour,
+                TimeUnit.MILLISECONDS);*/
+        WeatherXZFetchService service = beanFactory.getBean(WeatherXZFetchService.class);
         long initialDelay = getHourMillis() - System.currentTimeMillis();
         long oneHour = 60 * 60 * 1000;
         // 整点执行一次

+ 29 - 0
jdc/dataChange/trunk/src/main/resources/com/jsjty/jdc/data/mappers/WeatherAreaMapper.xml

@@ -0,0 +1,29 @@
+<?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.jsjty.jdc.data.mappers.WeatherAreaMapper" >
+  <resultMap id="BaseResultMap" type="com.jsjty.jdc.data.model.WeatherArea" >
+    <id column="ID" property="id" jdbcType="VARCHAR" />
+    <result column="C1" property="c1" jdbcType="VARCHAR" />
+    <result column="C2" property="c2" jdbcType="VARCHAR" />
+    <result column="C3" property="c3" jdbcType="VARCHAR" />
+    <result column="C4" property="c4" jdbcType="VARCHAR" />
+    <result column="C5" property="c5" jdbcType="VARCHAR" />
+    <result column="C6" property="c6" jdbcType="VARCHAR" />
+    <result column="C7" property="c7" jdbcType="VARCHAR" />
+    <result column="C8" property="c8" jdbcType="VARCHAR" />
+    <result column="C9" property="c9" jdbcType="VARCHAR" />
+    <result column="C10" property="c10" jdbcType="VARCHAR" />
+    <result column="C11" property="c11" jdbcType="VARCHAR" />
+    <result column="C12" property="c12" jdbcType="VARCHAR" />
+    <result column="C13" property="c13" jdbcType="VARCHAR" />
+    <result column="C14" property="c14" jdbcType="VARCHAR" />
+    <result column="C15" property="c15" jdbcType="VARCHAR" />
+    <result column="C16" property="c16" jdbcType="VARCHAR" />
+    <result column="C17" property="c17" jdbcType="VARCHAR" />
+    <result column="f0" property="f0" jdbcType="VARCHAR" />
+    <result column="XZQHID" property="xzqhid" jdbcType="VARCHAR" />
+  </resultMap>
+  <select id="selectAreas" parameterType="java.lang.String"  resultMap="BaseResultMap" >
+   	select * from t_weather_area where f0 is null or f0 !=#{f0,jdbcType=VARCHAR}
+  </select>
+</mapper>

+ 3 - 0
jdc/dataChange/trunk/src/main/resources/com/jsjty/jdc/data/mappers/WeatherMapper.xml

@@ -67,6 +67,9 @@
   <select id="selectAreaIds" parameterType="java.lang.String"  resultType="java.lang.String" >
    	select c1 from t_weather_area where f0 is null or f0 !=#{f0,jdbcType=VARCHAR}
   </select>
+  <select id="selectCityYwm" parameterType="java.lang.String"  resultType="java.lang.String" >
+   	select c2 from t_weather_area where f0 is null or f0 !=#{f0,jdbcType=VARCHAR}
+  </select>
   <select id="selectBySjAndArea" resultMap="BaseResultMap">
    	select ID, FA, FB, FC, FD, FE, FF, FG, FH, FI, SJ, AREA, UPDTIME
    	from t_weather_day where sj=#{sj,jdbcType=VARCHAR} and area=#{area,jdbcType=VARCHAR}