@Override
 public List<TrackGps> viewTrack(String vehicleId, String startTime, String endTime) {
   if (vehicleId == null || vehicleId.trim().equals("") || startTime == null || endTime == null) {
     return null;
   }
   try {
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     sdf.parse(startTime.trim());
     sdf.parse(endTime.trim());
   } catch (ParseException e) {
     throw new ParamValidationServiceException(
         "The param [startTime,endTime] is error.  [startDate,endDate] need to matche 'yyyy-MM-dd HH:mm:ss' .");
   }
   List<GpsHistory> gpsList =
       dao.getGpsListByHcVehicleId(
           vehicleId, DateUtil.string2DateTime(startTime), DateUtil.string2DateTime(endTime));
   this.calculate(RawDataProtocol.TRUCK_HS, gpsList);
   List<TrackGps> trackList = new ArrayList<TrackGps>();
   for (int i = 0; i < gpsList.size(); i++) {
     GpsHistory gps = gpsList.get(i);
     if (!gps.getEnableGps() || gps.getLatitude() < 0.1 || gps.getLongitude() < 0.1) {
       continue;
     }
     TrackGps trackGps = convertEntityGpsToTrackGps(gps);
     if (trackGps != null) {
       trackList.add(trackGps);
     }
   }
   return trackList;
 }
  @Override
  public void calculateBaseValue(List<GpsHistory> gpsHistoryList) {
    if (gpsHistoryList != null && gpsHistoryList.size() > 0) {
      GpsHistory startGps = gpsHistoryList.get(0);
      GpsHistory endGps = gpsHistoryList.get(gpsHistoryList.size() - 1);

      VehicleRegisterRecord startBase =
          vehicleRegisterDao.getLatestVehicleRegisterRecordBeforeTime(
              startGps.getHcVehicleId(), new Date(startGps.getTime()));
      VehicleRegisterRecord endBase =
          vehicleRegisterDao.getLatestVehicleRegisterRecordBeforeTime(
              startGps.getHcVehicleId(), new Date(endGps.getTime()));

      List<VehicleRegisterRecord> baseList =
          vehicleRegisterDao.getVehicleRegisterRecordListByHcVehicleId(
              startGps.getHcVehicleId(),
              startBase == null ? null : startBase.getMsgTime(),
              endBase == null ? null : endBase.getMsgTime());

      if (baseList != null && baseList.size() != 0) {
        for (GpsHistory gps : gpsHistoryList) {
          for (VehicleRegisterRecord baseValue : baseList) {
            if (baseValue.getMsgTime() < gps.getTime()) {
              gps.setOilBaseValue(
                  baseValue.getBenchmarkOilConsumption() == null
                      ? null
                      : baseValue.getBenchmarkOilConsumption().doubleValue());
              gps.setMileageBaseValue(baseValue.getBenchmarkMileage());
            }
          }
        }
      }
    }
  }
  private TrackGps convertEntityGpsToTrackGps(GpsHistory gps) {
    if (gps == null) {
      return null;
    }
    TrackGps track = new TrackGps();
    track.setId(gps.getId().toString()); // gps id
    track.setTime(
        gps.getTime() != null ? DateUtil.dateTime2String(new Date(gps.getTime())) : null); //
    track.setVehicleId(gps.getHcVehicleId()); //
    if (gps.getCardNo() != null) gps.setCardNo(gps.getCardNo().replaceAll("\\u0000", ""));
    track.setCardNo(gps.getCardNo()); //
    track.setCardBalance(null); //
    track.setPlateNo(gps.getPlateNo()); //
    track.setSpeed(gps.getSpeed()); //

    track.setMileageDayCumulative(gps.getCurrentMileageCumulative()); // 当日累积里程
    track.setOilDayCumulative(gps.getCurrentOilCumulative()); // 当日累积油耗

    track.setAltitude(gps.getAltitude()); //
    if (gps.getCardStatus() != null) {
      switch (gps.getCardStatus()) {
        case OFF_LINE:
          track.setCardStatus(CardStatus.OFFLINE);
          break;
        case ON_LINE:
          track.setCardStatus(CardStatus.ONLINE);
          break;
        default:
          track.setCardStatus(null);
      }
    }
    if (gps.getAccStatus() != null) {
      switch (gps.getAccStatus()) {
        case IGNITION:
          track.setAccStatus(AccStatus.IGNITION);
          break;
        case FLAMEOUT:
          track.setAccStatus(AccStatus.STALL);
          break;
        default:
          track.setAccStatus(null);
      }
    }
    track.setLongitude(gps.getLongitude()); //
    track.setLatitude(gps.getLatitude()); //
    if (gps.getVehicleStatus() != null) {
      track.setVehicleStatus(VehicleStatus.valueOf(gps.getVehicleStatus().name()));
    } else if (gps.getAccStatus() != null) {
      switch (gps.getAccStatus()) {
        case IGNITION:
          track.setVehicleStatus(VehicleStatus.IGNITION);
          break;
        case FLAMEOUT:
          track.setVehicleStatus(VehicleStatus.STALL);
          break;
        default:
          track.setAccStatus(null);
      }
    }
    track.setDeviceId(gps.getDeviceId()); // HSBOX 编号
    if (gps.getEnableGps() == null) {
      track.setTag(gps.getEnableGps() ? GpsTag.VALID : GpsTag.INVALID); // gps 是否有效A有效V无效
    }
    track.setSatelliteNum(gps.getSatelliteNum()); // 卫星数量

    track.setOilCumulative(gps.getOilCumulative());
    track.setMileageCumulative(
        gps.getMileageCumulative() != null ? gps.getMileageCumulative() / 1000D : null);
    return track;
  }
  // TRUCK_HS,//HS货车协议MOBILE_HS,//HS手机协议
  private void calculateHS(List<GpsHistory> gpsList) {
    if (gpsList == null || gpsList.size() == 0) {
      return;
    }
    for (Iterator<GpsHistory> it = gpsList.iterator(); it.hasNext(); ) {
      GpsHistory gps = it.next();
      if (gps == null) {
        it.remove();
        continue;
      }
      if (gps.getTime() == null) {
        it.remove();
      }
    }
    // 排序
    Collections.sort(gpsList);
    // 根据注册基准表计算基准值
    this.calculateBaseValue(gpsList);

    for (int i = 0; i < gpsList.size(); i++) {
      GpsHistory gps = gpsList.get(i);
      GpsHistory lastGps = null;
      gps.setOilCumulative(null);
      gps.setMileageCumulative(null);
      if (i == 0) {
        gps.setOilCumulative(0D);
        gps.setMileageCumulative(0);
        continue;
      } else {
        lastGps = gpsList.get(i - 1);
      }

      // 基准值相同,没有重新注册的情况
      if ((gps.getOilBaseValue() != null
              && lastGps.getOilBaseValue() != null
              && gps.getOilBaseValue().equals(lastGps.getOilBaseValue()))
          || (gps.getOilBaseValue() == null && lastGps.getOilBaseValue() == null)) {
        // 都能采集到油耗值
        if (gps.getCurrentOilCumulative() != null && lastGps.getCurrentOilCumulative() != null) {
          gps.setOilCumulative(gps.getCurrentOilCumulative() - lastGps.getCurrentOilCumulative());
        } else {
          // 当前点没有采集到油耗
          if (gps.getCurrentOilCumulative() == null) {
            gps.setOilCumulative(0D);
          }
          // 上一个点没有采集到油耗值
          if (gps.getCurrentOilCumulative() != null && lastGps.getCurrentOilCumulative() == null) {
            // gps.setOilCumulative(gps.getCurrentOilCumulative()-gps.getOilBaseValue());
            gps.setOilCumulative(0D);
          }
        }
      } else { // 基值不同有重新注册的情况

        // 当前点没有采集到油耗
        if (gps.getCurrentOilCumulative() == null) {
          // 南京说一定会有当前油耗值,所以这种情况基本不会存在
          gps.setOilCumulative(0D);
        } else {
          // 采集到油耗,当前点减去基值
          if (gps.getOilBaseValue() == null) {
            gps.setOilCumulative(0D);
          } else {
            gps.setOilCumulative(gps.getCurrentOilCumulative() - gps.getOilBaseValue());
          }
        }
      }

      // 基准值相同,没有重新注册的情况
      if ((gps.getMileageBaseValue() != null
              && lastGps.getMileageBaseValue() != null
              && gps.getMileageBaseValue().equals(lastGps.getMileageBaseValue()))
          || (gps.getMileageBaseValue() == null && lastGps.getMileageBaseValue() == null)) {
        // 都能采集到里程值
        if (gps.getCurrentMileageCumulative() != null
            && lastGps.getCurrentMileageCumulative() != null) {
          gps.setMileageCumulative(
              gps.getCurrentMileageCumulative() - lastGps.getCurrentMileageCumulative());
          if (!gps.getCurrentMileageCumulative().equals(lastGps.getCurrentMileageCumulative())) {
            gps.setMileageCumulative(
                gps.getCurrentMileageCumulative() - lastGps.getCurrentMileageCumulative());
          }
        } else {
          // 当前点没有采集到里程
          if (gps.getCurrentMileageCumulative() == null) {
            gps.setMileageCumulative(0);
          }
          // 上一个点没有采集到里程值
          if (gps.getCurrentMileageCumulative() != null
              && lastGps.getCurrentMileageCumulative() == null) {
            // gps.setMileageCumulative(gps.getCurrentMileageCumulative()-gps.getMileageBaseValue());
            gps.setMileageCumulative(0);
          }
        }
      } else { // 基值不同有重新注册的情况

        // 当前点没有采集到里程
        if (gps.getCurrentMileageCumulative() == null) {
          // 南京说一定会有当前里程值,所以这种情况基本不会存在
          gps.setMileageCumulative(0);
        } else {
          // 采集到里程,当前点减去基值
          if (gps.getMileageBaseValue() == null) {
            gps.setMileageCumulative(0);
          } else {
            gps.setMileageCumulative(gps.getCurrentMileageCumulative() - gps.getMileageBaseValue());
          }
        }
      }
    }
  }