/** * ***************************** 获取需要的协议信息,封装成MyDParam * * @param markValue * @return */ private List<MyDParam> getDParams(List<DParam> dParams) { List<MyDParam> list = new ArrayList<MyDParam>(); if (dParams.size() > 0) { for (int j = 0; j < dParams.size(); j++) { MyDParam myDParam = new MyDParam(); DParam dParam = dParams.get(j); myDParam.setdParam(dParam); Map<Integer, DparamMapValue> map = stringTransformMap(dParam.getDparamIsMap(), dParam.getDparamMapDatas()); myDParam.setDmv(map); list.add(myDParam); } } return list; }
/** * ******************** 获得协议参数 * * @param datas * @param dParam * @return */ public float getParam(byte[] datas, MyDParam myDParam) { DParam dParam = myDParam.getdParam(); int byteBegin = dParam.getDparamByteBegin(); int byteLength = dParam.getDparamByteLength(); int isByte = dParam.getDparamIsByte(); float factory = dParam.getDparamFactor(); // 参数分辨率(倍数) float offset = dParam.getDparamOffset(); // 参数偏移值 String byteDatas = dParam.getDparamByteDatas(); float param = 0; if ((byteBegin + byteLength) <= datas.length) { byte[] dataArray = cutDatas(datas, byteBegin, byteLength); // 截取datas if (isByte == 0) { float tmpFloat = 0; if (dParam.getDparamHighAndLow() == 1) { // 大端法解析 tmpFloat = byteTransformFloat_big(dataArray); } else { // 小端法解析 tmpFloat = byteTransformFloat_small(dataArray); } if (tmpFloat == 0) { param = 0; } else { param = Math.round((tmpFloat * factory + offset) * 10) / 10; } } else { String[] bytedataArray = byteDatas.split(":"); if (bytedataArray.length >= 4) { int bytelen = Integer.parseInt(bytedataArray[0]); int bytebeginbit = Integer.parseInt(bytedataArray[1]); int byteendbit = Integer.parseInt(bytedataArray[2]) + bytebeginbit; int bytefactor = Integer.parseInt(bytedataArray[3]); int tmpInt = 0; for (int i = Math.min(dataArray.length, bytelen) - 1; i >= 0; --i) { tmpInt |= dataArray[i]; tmpInt = tmpInt << 4 * i; } String tmpString = toBinaryString(tmpInt); if (tmpString.length() - byteendbit < 0 || tmpString.length() - bytebeginbit < 0) { return 0; } tmpString = tmpString.substring( tmpString.length() - byteendbit, tmpString.length() - bytebeginbit); param = (float) (new BigInteger(tmpString, 2).intValue() * bytefactor); } } } else { param = 0; } return param; }
private void begin(String vin, List<MyDParam> myDParams) throws Exception { Processing processing = new Processing(); Date endTime = startDate; processing.setEndTime(endTime); processing.setVin(vin); List<Data> car_list = dataService.getAllDatas(vin, startDate); Data data; StringBuilder key = new StringBuilder(" ("); key.append( "SN,VIN,Time,LocateIsValid,LocateTime,LocateLongitude,LocateLatitude,LocateSpeed,LocateDirection,"); int sign = 0; // 批量插入数据的标志,等于0时需要获取key StringBuilder values = new StringBuilder(); MyDParam myDParam; int size = myDParams.size(); if (size == 0) { System.out.println(vin + " has not param to analys"); if (Processer.vintime_map.get(vin) == null) { processingService.add(processing); } else { processingService.update(processing); } Processer.vintime_map.put(vin, endTime); return; } if (car_list.size() == 0) { System.out.println(vin + " has not data to analys"); if (Processer.vintime_map.get(vin) == null) { processingService.add(processing); } else { processingService.update(processing); } Processer.vintime_map.put(vin, endTime); return; } // 获取表的字段 // Map<String , String> single = new HashMap<String, // String>();////防止重复的参数放入key for (int i = 0; i < size; i++) { myDParam = myDParams.get(i); String namePinYin = myDParam.getdParam().getNamePinYin(); if (namePinYin != null && !namePinYin.equals("")) { // //if (single.get(namePinYin) != null ) { // //System.out.println("double key :"+namePinYin+" the name is :" // +myDParam.getdParam().getDparamName()); // //continue; // //} else { // //single.put(namePinYin, ""); key.append(namePinYin + ","); // } } else { key.append(pinyin(myDParam.getdParam().getDparamName()) + ","); // 获取表的字段名 } } key = key.delete(key.length() - 1, key.length()); key.append(") "); // 获取分析后的数据,并插入表中 for (Iterator<Data> iterator = car_list.iterator(); iterator.hasNext(); ) { data = iterator.next(); byte[] bDatas = null; StringBuilder value = new StringBuilder(" ("); value.append( data.getSn() + ",'" + data.getVin() + "','" + data.getTime() + "'," + data.getLocateIsValid() + ",'" + data.getLocateTime() + "'," + data.getLocateLongitude() + "," + data.getLocateLatitude() + "," + data.getLocateSpeed() + "," + data.getLocateDirection() + ","); for (int i = 0; i < size; i++) { // 逐个分析参数 myDParam = myDParams.get(i); bDatas = data.getDatas(); endTime = data.getTime(); processing.setEndTime(endTime); float param = getParam(bDatas, myDParam); // 解析数据 if (myDParam.getdParam().getDparamIsMap() == 1) { int paramInt = (int) param; DparamMapValue dmv = myDParam.getDmv().get(paramInt); if (dmv == null) { value.append("'" + paramInt + "',"); } else { value.append("'" + dmv.getValue() + "',"); } } else { value.append(param + ","); } } value = value.delete(value.length() - 1, value.length()); value.append(") "); values.append(value + ","); sign++; if (sign >= 100) { values = values.delete(values.length() - 1, values.length()); // 插入参数 dParamService.insertDParams(vin, key.toString(), values.toString()); values.delete(0, values.length()); sign = 1; } } if (values.length() == 0) { System.out.println(vin + " has no data at that time"); } else { values = values.delete(values.length() - 1, values.length()); // 插入参数 dParamService.insertDParams(vin, key.toString(), values.toString()); values.delete(0, values.length()); } if (Processer.vintime_map.get(vin) == null) { processingService.add(processing); } else { processingService.update(processing); } Processer.vintime_map.put(vin, endTime); }