socket 服务器

时间:2020-05-29
本文章向大家介绍socket 服务器,主要包括socket 服务器使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
服务器

  

package com.code.modules.roaddiseasecontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.code.modules.utils.JdbcUtils;
import com.code.modules.utils.ZuobiaoHelper;
import org.datacontract.schemas._2004._07.iscope_hb.LocateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.tempuri.ILocateOnRoadService;
import org.tempuri.LocateOnRoadService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ddl
 * @date 2020/04/17 13:46
 */

@Component
public class RoadDiseaseController {

    @Autowired
    JdbcUtils jdbcUtils;

    @Autowired
    ZuobiaoHelper zuobiaoHelper;

    String querySql = "select * from ROAD_DAMAGE_ROLE";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
    private String datetime = sdf.format(new Date());

    /**
     * 加上此注解代码这个异步的方法
     */
    @RequestMapping("/roaddisease")
    public String roadDisease() {
        Object[] param = new Object[]{};

        List results = jdbcUtils.findList(querySql, param);
        System.out.println("=============================");
        System.out.println(results.size());
        System.out.println(results);
        System.out.println("=============================");
        return null;
    }

    /**
     * 心跳入库
     */
    public String GPS(Map map) {
        Object[] params = new Object[]{};

        String devid = map.get("devid") + "";
        String stime = map.get("stime") + "";
        String lgt = map.get("lgt") + "";
        String lat = map.get("lat") + "";

        String cox = "";
        String coy = "";
        boolean flag = true;
        String result = "0";
        if (isNull(devid) || isNull(stime) || isNull(lgt) || isNull(lat)) {
            result = "1";
            flag = false;
        }

        if (stime.indexOf("T") > -1) {
            stime = stime.replace("T", " ");
        }

        System.out.println("校验结果:"+result+"=======flag:"+flag);

        if (flag) {
            String zuobiao = lgt + "," + lat;

            String gps = zuobiaoHelper.getWgs84Zuobiao(zuobiao);

            cox = gps.split(",")[0];
            coy = gps.split(",")[1];

            String sql="  select * from crack_position_table where devid='"+devid+"'";
            Object[] param = new Object[]{};
            List list = jdbcUtils.findList(sql, param);
            if(list.size()>0){
                //crack_position_table
                String updateTable = " update crack_position_table set lgt='%s',LAT='%s',stime='%s',cox='%s',coy='%s' where devid='%s'";
                updateTable = String.format(updateTable,  lgt, lat, stime, cox, coy,devid);
                System.out.println(updateTable);
                jdbcUtils.insert(updateTable,params);
            }else{
                //crack_position_table
                String insertTable = " insert into crack_position_table(devid,lgt,LAT,stime,cox,coy) values('%s','%s','%s','%s','%s','%s')";
                insertTable = String.format(insertTable, devid, lgt, lat, stime, cox, coy);
                System.out.println(insertTable);
                jdbcUtils.insert(insertTable, params);
            }





            String road=getRoadIdByZuobiao(cox,coy);
            String roadid=road.split(",")[0];
            String direction=road.split(",")[1];
            String toMeasureValue=road.split(",")[2];
            //crack_position_year
            String insertYear = " insert into crack_position_year(devid,lgt,LAT,stime,cox,coy,roadid,mileagedb,direction) values('%s','%s','%s',%s,'%s','%s','%s','%s','%s')";
            insertYear = String.format(insertYear, devid, lgt, lat, "TO_DATE('" + stime + "', 'YYYY-MM-DD HH24:MI:SS')", cox, coy,roadid,toMeasureValue,direction);
            System.out.println(insertYear);
            jdbcUtils.insert(insertYear, params);

        }


        Map checkData = new HashMap();
        checkData.put("devid", devid);
        checkData.put("datetime", datetime);
        checkData.put("result", result);
        Map outMap = resultMap(checkData);

        String outstr = JSON.toJSONString(outMap);
        return outstr;
    }

    /**
     * 心跳批量入库
     */
    public String GPSBatchList(JSONArray array) {
        Object[] params = new Object[]{};


        List reList = new ArrayList();
        for (int i = 0; i < array.size(); i++) {
            Map map= (Map) array.get(i);

            Map out= new HashMap();

            String devid = map.get("devid") + "";
            String stime = map.get("stime") + "";
            String lgt = map.get("lgt") + "";
            String lat = map.get("lat") + "";

            String cox = "";
            String coy = "";
            boolean flag = true;
            String result = "0";
            if (isNull(devid) || isNull(stime) || isNull(lgt) || isNull(lat)) {
                result = "1";
                flag = false;
            }

            if (stime.indexOf("T") > -1) {
                stime = stime.replace("T", " ");
            }

            System.out.println("校验结果:"+result+"=======flag:"+flag);

            if (flag) {
                String zuobiao = lgt + "," + lat;

                String gps = zuobiaoHelper.getWgs84Zuobiao(zuobiao);

                cox = gps.split(",")[0];
                coy = gps.split(",")[1];

                String sql="  select * from crack_position_table where devid='"+devid+"'";
                Object[] param = new Object[]{};
                List list = jdbcUtils.findList(sql, param);
                if(list.size()>0){
                    //crack_position_table
                    String updateTable = " update crack_position_table set lgt='%s',LAT='%s',stime='%s',cox='%s',coy='%s' where devid='%s'";
                    updateTable = String.format(updateTable,  lgt, lat, stime, cox, coy,devid);
                    System.out.println(updateTable);
                    jdbcUtils.insert(updateTable,params);
                }else{
                    //crack_position_table
                    String insertTable = " insert into crack_position_table(devid,lgt,LAT,stime,cox,coy) values('%s','%s','%s','%s','%s','%s')";
                    insertTable = String.format(insertTable, devid, lgt, lat, stime, cox, coy);
                    System.out.println(insertTable);
                    jdbcUtils.insert(insertTable, params);
                }





                String road=getRoadIdByZuobiao(cox,coy);
                String roadid=road.split(",")[0];
                String direction=road.split(",")[1];
                String toMeasureValue=road.split(",")[2];
                //crack_position_year
                String insertYear = " insert into crack_position_year(devid,lgt,LAT,stime,cox,coy,roadid,mileagedb,direction) values('%s','%s','%s',%s,'%s','%s','%s','%s','%s')";
                insertYear = String.format(insertYear, devid, lgt, lat, "TO_DATE('" + stime + "', 'YYYY-MM-DD HH24:MI:SS')", cox, coy,roadid,toMeasureValue,direction);
                System.out.println(insertYear);
                jdbcUtils.insert(insertYear, params);

            }

            out.put("devid", devid);
            out.put("datetime", datetime);
            out.put("result", result);

            reList.add(out);
        }

        Map checkData = new HashMap();
        checkData.put("list", reList);
        Map outMap = resultMap(checkData);

        String outstr = JSON.toJSONString(outMap);
        return outstr;

    }

    /**
     * 道路病害数据
     */
    public String roadDiseaseData(Map map) {
        Object[] params = new Object[]{};

        String sid = map.get("sid") + "";
        String devid = map.get("devid") + "";
        String stime = map.get("stime") + "";
        String lgt = map.get("lgt") + "";
        String lat = map.get("lat") + "";
        String picname = map.get("picName") + "";
        String url = map.get("url") + "";
        String stype = map.get("stype") + "";

        String cox = "";
        String coy = "";
        boolean flag = true;
        String result = "0";
        if (isNull(sid) || isNull(devid) || isNull(stime) || isNull(lgt) || isNull(lat)
                || isNull(picname) || isNull(url) || isNull(stype)) {
            result = "1";
            flag = false;
        }
        System.out.println("校验结果:"+result+"=======flag:"+flag);
        if (flag) {
            String zuobiao = lgt + "," + lat;
            String gps = zuobiaoHelper.getGdToCjZuobiao(zuobiao);

            cox = gps.split(",")[0];
            coy = gps.split(",")[1];

            //crack_position_table
            String insertTable = " insert into crack_data_table(sid,devid,lgt,LAT,stime,cox,coy,picname,url,stype) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')";
            insertTable = String.format(insertTable, sid, devid, lgt, lat, stime, cox, coy, picname, url, stype);
            System.out.println(insertTable);
            jdbcUtils.insert(insertTable, params);

        }

        Map checkData = new HashMap();
        checkData.put("devid", devid);
        checkData.put("datetime", datetime);
        checkData.put("result", result);
        Map outMap = resultMap(checkData);


        String outstr = JSON.toJSONString(outMap);
        return outstr;
    }

    //服务器检索未反馈的病害,最大一次反馈500个病害状态
    public String crackresult(Map map) {

        String reqid = map.get("reqid") + "";
        String method = map.get("method") + "";
        String stime = map.get("stime") + "";
        String unid = jdbcUtils.getuuid();

        Object[] params = new Object[]{};

        boolean flag = true;
        String result = "0";
        if (isNull(reqid) || isNull(method) || isNull(stime)) {
            result = "1";
            flag = false;
        }
        System.out.println("校验结果:"+result+"=======flag:"+flag);

        List results =new ArrayList();
        if (flag) {
            String sql = diseaseSql("");
            results = jdbcUtils.findList(sql, params);

        }

        //无或0:有后续;1:无后续
        int end=1;
        if(results.size()>0){
            end=0;
        }
        System.out.println("无或0:有后续;1:无后续:-----"+end);

        Map checkData = new HashMap();
        checkData.put("reqid", reqid);
        checkData.put("method", method);
        checkData.put("respid", unid);
        checkData.put("datetime", datetime);
        if (!flag) {
            checkData.put("result", result);
        }else{
            checkData.put("end", end);

            List list = new ArrayList();
            for (int i = 0; i <results.size() ; i++) {
                Map obj=new HashMap();

                Map res= (Map) results.get(i);
                obj.put("sid",res.get("SID"));
                obj.put("result",res.get("NUM"));

                list.add(obj);
            }
            checkData.put("list", list);

        }
        Map outMap = resultMap(checkData);
        String outstr = JSON.toJSONString(outMap);
        return outstr;
    }

    //服务器检索未反馈的病害,最大一次反馈500个病害状态
    public String  crackresultcomfirm(Map map) {

        JSONArray  comfirmed = (JSONArray)map.get("comfirmed");
        StringBuffer sb = new StringBuffer("('");
        for (int i = 0; i < comfirmed.size(); i++) {
            sb.append(comfirmed.get(i)).append("','");
        }
        String id=sb.toString().substring(0,sb.toString().lastIndexOf(","))+")";

        System.out.println(id);
        Object[] params = new Object[]{};

        String sql = "select sid, cnf.TASKNUM from CRACK_DATA_TABLE cdt " +
                " left JOIN COMPLAINUNFINISHEDHISDATA cnf on cnf.TASKNUM=SUBSTR(cdt.sid, 13, 19) " +
                " where 1=1 and cdt.sid in"+id;
        List list= jdbcUtils.findList(sql, params);
        if(list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                Map obj = (Map) list.get(i);
                String tasknum=obj.get("TASKNUM")+"";
                String sid=obj.get("SID")+"";

                //反馈状态
                int status=0;
                //if TASKNUM is null update   FEEDBACK = 1
                if(isNull(tasknum)){
                    status=1;
                }else{
                    //if TASKNUM is not null update   FEEDBACK = 2
                    status=2;
                }
                String update=" UPDATE CRACK_DATA_TABLE SET FEEDBACK ="+status+"  WHERE sid='"+sid+"' ";
                jdbcUtils.insert(update, params);
            }
        }


        //再次查询反馈
        String outstr= crackresult(map);
        return outstr;

    }


    //将经纬度(坐标)转换详细结构化道路信息,及周边其他信息。
    public String geocoder(Map map) {

        String reqid = map.get("reqid") + "";
        String method = map.get("method") + "";
        String stime = map.get("stime") + "";
        String location = map.get("location") + "";
        String coordsys = map.get("coordsys") + "";
        String management = map.get("management") + "";
        String unid = jdbcUtils.getuuid();

        Object[] params = new Object[]{};

        boolean flag = true;
        String result = "0";
        if (isNull(reqid) || isNull(method) || isNull(stime)
            || isNull(location) || isNull(coordsys) || isNull(management)) {
            result = "1";
            flag = false;
        }
        System.out.println("校验结果:"+result+"=======flag:"+flag);

        List results =new ArrayList();


        Double zhuanghao=0.0;
        if("pudong-gl".equals(management)){
            //获取传进来的gps坐标  装换成 城建坐标
            String gps = zuobiaoHelper.getWgs84Zuobiao(location);
            if (isNull(gps)) {
                result = "1";
                flag = false;
            }else{

                String cox = gps.split(",")[0];
                String coy = gps.split(",")[1];

                //拿到城建坐标  根据坐标查询是在那条道路上
            String roadGps=getRoadIdByZuobiao(cox,coy);
//                String roadGps="";
                if (isNull(roadGps)) {
                    result = "1";
                    flag = false;
                }else{
                String roadid=roadGps.split(",")[0];
                String direction=roadGps.split(",")[1];
                //位置(最近)桩号
                String toMeasureValue=roadGps.split(",")[2];
                zhuanghao = Double.parseDouble(toMeasureValue) / 1000;
//                    String roadid="X618310115";
//                    roadid="X581310115";

                    if (flag) {
                        String sql="SELECT " +
                                " info.roadid as id, " +
                                " info.roadname as name, " +
                                " round(info.STARTLOCATION,4) as startloc, " +
                                " round(info.ENDLOCATION,4) as endloc, " +
                                " 0 as distance, " +
                                " be.DEPARTID as depart, " +
                                " be.unitid as unit, " +
                                " be.projectid as project " +
                                "FROM " +
                                " YHROADBASICINFO info " +
                                " LEFT JOIN YHROADBELONGUNIT be ON be.ROADID = info.roadid " +
//                                " and be.STARTLOCATION = info.STARTLOCATION " +
//                                " and be.ENDLOCATION = info.ENDLOCATION" +
                                " where 1=1 " +
                                " and info.roadid='"+roadid+"'" +
                                " and (info.STARTLOCATION>="+zhuanghao+" and "+zhuanghao+" <=info.ENDLOCATION)";
                        results = jdbcUtils.findList(sql, params);
                    }
                }

            }
        }else{
            result = "2";
            flag = false;

        }


        Map checkData = new HashMap();
        checkData.put("reqid", reqid);
        checkData.put("method", method);
        checkData.put("respid", unid);
        checkData.put("datetime", datetime);

        checkData.put("result", result);
        if (!flag) {
            if (result=="2") {
                checkData.put("msg", "请求失败 请使用  management: pudong-gl");
            }
        }else{
            List<Map<String,Object>> roads = new ArrayList<>();
            if(results.size()>0){

                Double value= Double.parseDouble(String.format("%.4f",zhuanghao));

                for (int i = 0; i < results.size(); i++) {
                    Map road = (Map) results.get(i);

                    Map r=new HashMap();
                    Set<String> keySet = road.keySet();
                    for (String key : keySet)
                    {
                        String newKey = key.toLowerCase();
                        if(isNull(road.get(key)+"")){
                            r.put(newKey, "");
                        }else{
                            r.put(newKey, road.get(key));
                        }

                    }
                    r.put("location",value);
                    roads.add(r);
                }
            }
            checkData.put("roads", roads);

        }
        Map outMap = resultMap(checkData);
        String outstr = JSON.toJSONString(outMap);
        return outstr;
    }

    private String diseaseSql(String sid){
        String sql="select * from ( " +
                "  select cdt.sid,'完成' as tyle,'1' as num,cdt.feedback from CRACK_DATA_TABLE cdt " +
                "  inner JOIN COMPLAINFINISHEDHISDATA cf on cf.TASKNUM=SUBSTR(cdt.sid, 13, 19) " +
                "  where 1=1 and (cdt.FEEDBACK is  null or cdt.FEEDBACK = 2) " +
                "  and  cf.FINISHFLAG='1' and cf.CURSTATUS='5' " +
                "  union all " +
                "  select cdt.sid,'退单' as tyle,'2' as num,cdt.feedback  from CRACK_DATA_TABLE cdt " +
                "  inner JOIN COMPLAINFINISHEDHISDATA cf on cf.TASKNUM=SUBSTR(cdt.sid, 13, 19) " +
                "  where 1=1 and (cdt.FEEDBACK is  null or cdt.FEEDBACK = 2) " +
                "  and  cf.FINISHFLAG='1' and cf.CURSTATUS='4' " +
                "  union all " +
                "   " +
                "  select cdt.sid,'未完成' as tyle,'3' as num,cdt.feedback from CRACK_DATA_TABLE cdt " +
                "  inner JOIN COMPLAINUNFINISHEDHISDATA cnf on cnf.TASKNUM=SUBSTR(cdt.sid, 13, 19) " +
                "  where 1=1 and (cdt.FEEDBACK is  null)" +
                ") t ";
        if(!isNull(sid)){
            sql+=" t.sid='"+sid+"' ";
        }else{

        }
        return sql;
    }

    //新增设备运行日志
    public String crackDeviceOnlineData(Map map) {

        String reqid = map.get("reqid") + "";
        String method = map.get("method") + "";
        String stime = map.get("stime") + "";
        String devid = map.get("devid") + "";
        String logDate = map.get("logDate") + "";
        String url = map.get("url") + "";
        String mileage = map.get("mileage") + "";
        String runtime = map.get("runtime") + "";

        String unid = jdbcUtils.getuuid();


        if (stime.indexOf("T") > -1) {
            stime = stime.replace("T", " ");

        }
        Object[] params = new Object[]{
//				unid,"TO_DATE('"+stime+"', 'SYYYY-MM-DD HH24:MI:SS')",devid,"TO_DATE('"+logDate+"', 'SYYYY-MM-DD HH24:MI:SS')",url,mileage,runtime
        };

        String cox = "";
        String coy = "";
        boolean flag = true;
        String result = "0";
        if (isNull(reqid) || isNull(method) || isNull(stime) || isNull(devid) || isNull(logDate)
                || isNull(url) || isNull(mileage) || isNull(runtime)) {
            result = "1";
            flag = false;
        }
        System.out.println("校验结果:"+result+"=======flag:"+flag);
        if (flag) {

            //crack_device_online
            String insertTable = " insert into crack_device_online(unid,stime,devid,logdate,url,mileage,runtime) values('%s',%s,'%s',%s,'%s','%s','%s')";
            insertTable = String.format(insertTable, unid, "TO_DATE('" + stime + "', 'YYYY-MM-DD HH24:MI:SS')", devid, "TO_DATE('" + logDate + "', 'YYYY-MM-DD HH24:MI:SS')", url, mileage, runtime);
            System.out.println(insertTable);
            jdbcUtils.insert(insertTable, params);

        }

        Map checkData = new HashMap();
        checkData.put("reqid", reqid);
        checkData.put("method", method);
        checkData.put("respid", unid);
        checkData.put("datetime", datetime);
        checkData.put("result", result);
        Map outMap = resultMap(checkData);

        String outstr = JSON.toJSONString(outMap);
        return outstr;
    }



    private  String getRoadIdByZuobiao(String cox,String coy) {
        String rest="";
        LocateOnRoadService service = new LocateOnRoadService();
        ILocateOnRoadService client = service.getBasicHttpBindingILocateOnRoadService();
        LocateResult locateResult = client.locateOnRoadWithResolution(Double.parseDouble(cox), Double.parseDouble(coy), 5d);
        String id = locateResult.getID().getValue();
        String direction = locateResult.getDirection().getValue();
        Double toMeasureValue = locateResult.getToMeasureValue();
        rest=id+","+direction+","+toMeasureValue;
        return rest;
    }

    //返回的信息
    public static Map resultMap(Map checkData) {
        Map outMap = new HashMap();
        Map uploadResult = new HashMap();
        uploadResult.put("checkData", checkData);
        outMap.put("uploadResult", uploadResult);
        return outMap;
    }


    //判断是否为空
    public static Boolean isNull(String str) {
        Boolean flag = false;
        System.out.println("========数据校验=============");
        System.out.println(str);
        if (str == null || "".equals(str) || "null".equals(str)) {
            flag = true;
        }
        return flag;

    }


    public static void main(String[] args) {
//        String insertTable = " ('%s',%s,'%s')";
//        insertTable = String.format(insertTable, 123, "TO_DATE('" + 123123 + "', YYYY-MM-DD HH24:MI:SS)", "1234");
//        System.out.println(insertTable);




        Double zhuanghao = Double.parseDouble("222.2246554465") / 1000;
        Double value= Double.parseDouble(String.format("%.4f",zhuanghao));
        System.out.println(value);
    }


}

  

原文地址:https://www.cnblogs.com/wcnwcn/p/12987504.html