Bootstrap

N个utils(通信协议)

http

http-get

public static Map<String, Object> getWxLocation(String url) throws Exception {
   // CloseableHttpClient
   CloseableHttpClient client = null;
   // HttpGet
   HttpGet httpGet = null;
   // CloseableHttpResponse
   CloseableHttpResponse response = null;

   try {
      // 获取http客户端
      client = HttpClients.createDefault();
      // 通过httpGet方式来实现我们的get请求
      httpGet = new HttpGet(url);
      // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
      response = client.execute(httpGet);
      System.out.println("response"+response);
      // HttpEntity是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的所有的响应的数据,也全部都是封装在HttpEntity里面
      HttpEntity entity = response.getEntity();
      // 通过EntityUtils来将我们的数据转换成字符串
      String str = EntityUtils.toString(entity, "UTF-8");
      // EntityUtils.toString(entity)

      return ObjectMapper.mapper.readValue(str, Map.class);
   } catch (Exception e) {
      throw new Exception("获取失败,请重试!" + e.getMessage());
   } finally {
      // 关闭资源
      response.close();
      httpGet.clone();
      client.close();
   }
}

http-post

public static Map<String, Object> getPost(String url, Map<String, String> params) throws Exception {
   CloseableHttpClient client = null;
   HttpPost httpPost = null;
   CloseableHttpResponse response = null;

   try {
      // 获取http客户端
      client = HttpClients.createDefault();
      // 通过HttpPost方式来实现我们的get请求
      httpPost = new HttpPost(url);

      // 添加请求参数
      List<NameValuePair> formParams = new ArrayList<>();
      for (Map.Entry<String, String> entry : params.entrySet()) {
         formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
      }

      httpPost.setEntity(new UrlEncodedFormEntity(formParams));

      // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
      response = client.execute(httpPost);
      System.out.println("response" + response);

      // HttpEntity是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEn
      HttpEntity entity = response.getEntity();

      // 通过EntityUtils来将我们的数据转换成字符串
      String str = EntityUtils.toString(entity, "UTF-8");

      return ObjectMapper.mapper.readValue(str, Map.class);
   } catch (Exception e) {
      throw new Exception("获取失败,请重试!" + e.getMessage());
   } finally {
      if (response != null) {
         response.close();
      }
      if (httpPost != null) {
         httpPost.abort();
      }
      if (client != null) {
         client.close();
      }
   }
}

tcp

public static VinInfo sendTcpRequest(String host, Integer post, String vin){
        VinInfo vinInfo = new VinInfo();

        try {
            // 创建一个新的Socket对象,连接到指定的服务器和端口
            Socket socket = new Socket(host, post);
            System.out.println("连接到服务器成功!");
            // 创建输出流来发送数据包
            OutputStream outputStream = socket.getOutputStream();
            String str = vin;
            byte[] byteArray = new byte[31];
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                byteArray[i] = (byte) c;
            }
            outputStream.write(byteArray);
            socket.setSoTimeout(5000);

            //创建输入流来获取数据包
            byte[] buffer = new byte[2048];
            int bytesRead;
            InputStream inputStream = socket.getInputStream();
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byte[] bytes = Arrays.copyOfRange(buffer, 0, bytesRead);
                // 解析GB18030编码的字符
                String vinCar = new String(Arrays.copyOfRange(buffer, 0, 17), "GB18030");
                // 解析时间数据
                ByteBuffer timeBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer, 17, 23));
                int year = timeBuffer.get();
                int month = timeBuffer.get();
                int day = timeBuffer.get();
                int hour = timeBuffer.get();
                int minute = timeBuffer.get();
                int second = timeBuffer.get();
                LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
                Date dateCar = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
//                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
//                            String dateCar = formatter.format(date);

                // 解析经纬度数据
                ByteBuffer locationBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer, 23, bytesRead));
                ByteBuffer allocate = ByteBuffer.allocate(4);
                int longitude_temp = locationBuffer.getInt();
                allocate.putInt(longitude_temp);
                allocate.order(ByteOrder.LITTLE_ENDIAN);
                allocate.flip();
                double longitude = new BigDecimal(allocate.getInt()).divide(new BigDecimal(1000000)).doubleValue();

                ByteBuffer allocate1 = ByteBuffer.allocate(4);
                int latitude_temp = locationBuffer.getInt();
                allocate1.putInt(latitude_temp).order(ByteOrder.LITTLE_ENDIAN).flip();
                double latitude = new BigDecimal(allocate1.getInt()).divide(new BigDecimal(1000000)).doubleValue();
                // 输出解析结果
                vinInfo.setVin(vinCar);
                vinInfo.setDateCar(dateCar);
                vinInfo.setJD(longitude);
                vinInfo.setWD(latitude);
            }
            System.out.println("服务器关闭了连接或没有更多数据可读");


            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vinInfo;
    }

mqtt(语音盒子好像就要弄这个协议,还没弄过)

其他以后遇到更新

;