IceHDaoGpsClient.java 14.7 KB
package com.glsx.ice.iceClient;

import com.glsx.config.SystemProperty;
import com.glsx.ice.HDaoGps.*;
import com.glsx.model.DTO.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class IceHDaoGpsClient {
    static String url = SystemProperty.getProperty("ice.traceUrl");
    //	static String url = "HDaoGpsSvc:default -h 192.168.3.153 -p 23479";
    static Ice.Communicator ic = Ice.Util.initialize();
    static Ice.ObjectPrx base = ic.stringToProxy(url).ice_timeout(Integer.parseInt(SystemProperty.getProperty("ice.connectTimeout")));
    static DaoGpsServicePrx prx = DaoGpsServicePrxHelper.checkedCast(base);

    public static LatestGpsRecordDto getLatestGpsRecord(Long sn, Integer deviceType, Integer mapType) {
        LatestGpsRecordDto gpsRecord = null;
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }

            // 第2个参数:2: OBD; 4: DDbox(DVD) 5: Tracker 6: rearMirror 7: car recorder
            LatestRgpsGetRequest req = new LatestRgpsGetRequest(sn, deviceType, mapType);
            LatestRgpsGetResponseHolder res = new LatestRgpsGetResponseHolder();
            boolean ret = false;
            ret = prx.getLatestRgpsRecord(req, res);
            if (ret) {
                log.info("===getLatestRGPSRecord:" + res.value.record);
                gpsRecord = new LatestGpsRecordDto();
                gpsRecord.setSn(sn + "");
                gpsRecord.setGlng(res.value.record.gLng / 1000000D);
                gpsRecord.setGlat(res.value.record.gLat / 1000000D);
                gpsRecord.setLng(res.value.record.lng / 1000000D);
                gpsRecord.setLat(res.value.record.lat / 1000000D);
                gpsRecord.setSpeed(res.value.record.speed);
                gpsRecord.setGpstime(res.value.record.gpsTime);
            }
        } catch (Ice.LocalException e) {
            log.error("===getLatestRGPSRecord_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getLatestRGPSRecord_EXCEP:" + e.getMessage(), e);
        }
        return gpsRecord;
    }

    public static LatestBsRecordDto getLatestBsRecord(long sn, Integer deviceType) {
        LatestBsRecordDto bsRecord = null;
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }

            // 第2个参数:2: OBD; 4: DDbox(DVD) 5: Tracker 6: rearMirror 7: car recorder
            LatestBsGetResponseHolder res = new LatestBsGetResponseHolder();
            boolean ret = false;
            LatestBsGetRequest req = new LatestBsGetRequest(sn, deviceType);
            ret = prx.getLatestBsRecord(req, res);
            if (ret) {
                log.info("===LatestBsRecord:" + res.value.record);
                bsRecord = new LatestBsRecordDto();
                bsRecord.setSn(sn + "");
                bsRecord.setGpstime(res.value.record.gpsTime);
                bsRecord.setMcc(res.value.record.mcc);
                bsRecord.setMnc(res.value.record.mnc);

                List<BsInfoDto> bsInfos = Arrays.stream(res.value.record.bsInfos).map(bsInfo -> {
                    BsInfoDto bsRecordDto = new BsInfoDto();
                    bsRecordDto.setCi(bsInfo.cellId);
                    bsRecordDto.setLac(bsInfo.lac);
                    bsRecordDto.setSignal(bsInfo.signal);
                    return bsRecordDto;
                }).collect(Collectors.toList());
                bsRecord.setBsrecord(bsInfos);
            }
        } catch (Ice.LocalException e) {
            log.error("===getLatestBsRecord_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getLatestBsRecord_EXCEP:" + e.getMessage(), e);
        }
        return bsRecord;
    }

    public static LatestGpsBsRecordDto getDevicesGpsInfo(long[] userIds, int extraMapReq, int seqNo) {
        LatestGpsBsRecordDto bsRecord = null;
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }

            // 第2个参数:2: OBD; 4: DDbox(DVD) 5: Tracker 6: rearMirror 7: car recorder
            BatchGpsDevInfoResponseHolder res = new BatchGpsDevInfoResponseHolder();
            boolean ret = false;
            BatchGpsDevInfoRequest req = new BatchGpsDevInfoRequest(userIds, extraMapReq, seqNo);
            ret = prx.getDevicesGpsInfo(req, res);

            if (ret) {
                log.info("===getDevicesGpsInfo:" + res.value.gpsDevInfos);
                GpsDevInfo[] gpsDevInfos = res.value.gpsDevInfos;
                if (null != gpsDevInfos && gpsDevInfos.length > 0) {
                    GpsDevInfo gpsDevInfo = gpsDevInfos[0];
                    bsRecord = new LatestGpsBsRecordDto();
                    bsRecord.setGpstime(gpsDevInfo.record.gpsTime);
                    //bsRecord.setMcc(gpsDevInfo.record.);
                    //bsRecord.setMnc(gpsDevInfo.record.mnc);

                    //List<BsRecordDto> bsInfos = Arrays.stream(res.value.record.bsInfos).map(bsInfo -> {
                    //    BsRecordDto bsRecordDto = new BsRecordDto();
                    //    bsRecordDto.setCi(bsInfo.cellId);
                    //    bsRecordDto.setLac(bsInfo.lac);
                    //    bsRecordDto.setSignal(bsInfo.signal);
                    //    return bsRecordDto;
                    //}).collect(Collectors.toList());
                    //bsRecord.setBsrecord(bsInfos);
                }
            }
        } catch (Ice.LocalException e) {
            log.error("===getDevicesGpsInfo_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getDevicesGpsInfo_EXCEP:" + e.getMessage(), e);
        }
        return bsRecord;
    }

    /**
     * @param apType    2: OBD; 4: DDbox(DVD) 5: Tracker 6: rearMirror  7: car recorder
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static GpsRecordsDto getGpsRecords(int apType, Date startTime, Date endTime, long sn) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            if (startTime.after(endTime)) {
                return null;
            }
            GpsRecordsDto gpsRecordsDto = new GpsRecordsDto();
            GpsGetResponseHolder res = new GpsGetResponseHolder(new GpsGetResponse());
            GpsGetRequest req = new GpsGetRequest();
            req.apType = apType;
            req.startTime = new Long(startTime.getTime() / 1000).intValue();
            req.endTime = new Long(endTime.getTime() / 1000).intValue();
            req.extraMapReq = 1;
            req.userID = sn;
            prx.getGpsRecords(req, res);

            if (res != null && res.value != null && res.value.oprResult == 0) {
                GpsRecord[] r = res.value.records;
                gpsRecordsDto.setGpsrecord(convertGpsRecord(r));
            }

            return gpsRecordsDto;
        } catch (Ice.LocalException e) {
            log.error("===getGpsRecords_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getGpsRecords_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }

    /**
     * @param apType    2: OBD; 4: DDbox(DVD) 5: Tracker 6: rearMirror  7: car recorder
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static BsRecordsDto getBsRecords(int apType, Date startTime, Date endTime, long userId) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            if (startTime.after(endTime)) {
                return null;
            }
            BsRecordsDto bsRecordsDto = new BsRecordsDto();
            BsGetResponseHolder res = new BsGetResponseHolder(new BsGetResponse());
            BsGetRequest req = new BsGetRequest();
            req.apType = apType;
            req.startTime = new Long(startTime.getTime() / 1000).intValue();
            req.endTime = new Long(endTime.getTime() / 1000).intValue();
            req.userID = userId;
            prx.getBsRecords(req, res);

            if (res != null && res.value != null && res.value.oprResult == 0) {
                BsRecord[] r = res.value.records;
                bsRecordsDto.setBsrecord(convertBsRecord(r));
            }
            return bsRecordsDto;
        } catch (Ice.LocalException e) {
            log.error("===getBsRecords_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getBsRecords_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }


    //查询首次GPS点
    public static FirstGpsRecordDto getFirstGpsRecord(int apType, long sn) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            FirstGpsRecordDto firstGpsRecordDto = null;
            GpsGetResponseHolder res = new GpsGetResponseHolder(new GpsGetResponse());
            GpsGetRequest req = new GpsGetRequest();
            req.apType = apType;
            req.reqCount = 0;
            req.userID = sn;
            prx.getFirstGpsRecord(req, res);

            if (res != null && res.value != null && res.value.oprResult == 0) {
                GpsRecord[] r = res.value.records;
                int len = r.length;
                if (len > 0) {
                    GpsRecord gpsRecord = r[0];
                    firstGpsRecordDto = new FirstGpsRecordDto();
                    firstGpsRecordDto.setLat(gpsRecord.lat / 1000000D);
                    firstGpsRecordDto.setLng(gpsRecord.lng / 1000000D);
                    firstGpsRecordDto.setGlat(gpsRecord.gLat / 1000000D);
                    firstGpsRecordDto.setGlng(gpsRecord.gLng / 1000000D);
                    firstGpsRecordDto.setSpeed(gpsRecord.speed);
                    firstGpsRecordDto.setGpstime(gpsRecord.gpsTime);
                }
            }
            return firstGpsRecordDto;
        } catch (Ice.LocalException e) {
            log.error("===getFirstGpsRecord_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getFirstGpsRecord_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }

    public static GpsRecordsDto getRecentGpsRecords(long sn, int apType, int reqCount) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            GpsRecordsDto gpsRecordsDto = new GpsRecordsDto();
            RecentGpsGetResponseHolder res = new RecentGpsGetResponseHolder(new RecentGpsGetResponse());
            RecentGpsGetRequest req = new RecentGpsGetRequest();
            req.userID = sn;
            req.apType = apType;
            req.reqCount = reqCount;
            prx.getRecentGpsRecords(req, res);

            if (res != null && res.value != null && res.value.oprResult == 0) {
                GpsRecord[] r = res.value.records;
                gpsRecordsDto.setGpsrecord(convertGpsRecord(r));
            }
            return gpsRecordsDto;
        } catch (Ice.LocalException e) {
            log.error("===getRecentGpsRecords_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getRecentGpsRecords_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }

    public static BsRecordsDto getRecentBsRecords(long sn, int apType, int reqCount) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            BsRecordsDto bsRecordsDto = new BsRecordsDto();
            RecentBsGetResponseHolder res = new RecentBsGetResponseHolder(new RecentBsGetResponse());
            RecentBsGetRequest req = new RecentBsGetRequest();
            req.apType = apType;
            req.reqCount = reqCount;
            req.userID = sn;
            prx.getRecentBsRecords(req, res);

            if (res != null && res.value != null && res.value.oprResult == 0) {
                BsRecord[] r = res.value.records;
                bsRecordsDto.setBsrecord(convertBsRecord(r));
            }
            return bsRecordsDto;
        } catch (Ice.LocalException e) {
            log.error("===getRecentBsRecords_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getRecentBsRecords_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }

    //todo 地图坐标校偏
    public static BsRecordsDto convGeoGps(long sn, int apType, int reqCount) {
        try {
            if (prx == null) {
                throw new Exception("Invalid proxy");
            }
            BsRecordsDto bsRecordsDto = new BsRecordsDto();
            RecentBsGetResponseHolder res = new RecentBsGetResponseHolder(new RecentBsGetResponse());
            RecentBsGetRequest req = new RecentBsGetRequest();
            req.apType = apType;
            req.reqCount = reqCount;
            req.userID = sn;

            if (res != null && res.value != null && res.value.oprResult == 0) {
                BsRecord[] r = res.value.records;
                bsRecordsDto.setBsrecord(convertBsRecord(r));
            }
            return bsRecordsDto;
        } catch (Ice.LocalException e) {
            log.error("===getRecentBsRecords_ICE_EXCEP:" + e.getMessage(), e);
        } catch (Exception e) {
            log.error("===getRecentBsRecords_EXCEP:" + e.getMessage(), e);
        }
        return null;
    }


    private static List<GpsRecordDto> convertGpsRecord(GpsRecord[] records) {
        return Arrays.stream(records).map(rec -> {
            GpsRecordDto g = new GpsRecordDto();
            g.setGpstime(rec.gpsTime);
            g.setAlt(rec.alt);
            g.setDir(rec.dir);
            g.setSpeed(rec.speed);
            g.setLat(rec.lat / 1000000D);
            g.setLng(rec.lng / 1000000D);
            g.setGlat(rec.gLat / 1000000D);
            g.setGlng(rec.gLng / 1000000D);
            g.setBlat(rec.bLat / 1000000D);
            g.setBlng(rec.bLat / 1000000D);
            return g;
        }).collect(Collectors.toList());
    }

    private static List<BsRecordDto> convertBsRecord(BsRecord[] records) {
        return Arrays.stream(records).map(rec -> {
            BsRecordDto g = new BsRecordDto();
            g.setGpstime(rec.gpsTime);
            g.setMcc(rec.mcc);
            g.setMnc(rec.mnc);
            List<BsInfoDto> bsInfos = Arrays.stream(rec.bsInfos).map(info ->
            {
                BsInfoDto bsRecord = new BsInfoDto();
                bsRecord.setCi(info.cellId);
                bsRecord.setLac(info.lac);
                bsRecord.setSignal(info.signal);
                return bsRecord;
            }).collect(Collectors.toList());
            g.setBsinfo(bsInfos);
            return g;
        }).collect(Collectors.toList());
    }


}