|
@@ -1,161 +0,0 @@
|
|
|
-package com.sooka.sponest.dataexchange.monitoringEquipment.inserctpests.service.impl;
|
|
|
-
|
|
|
-import cn.hutool.http.HttpRequest;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ruoyi.common.core.domain.R;
|
|
|
-import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
-import com.ruoyi.system.api.domain.SysFile;
|
|
|
-import com.sooka.sponest.dataexchange.monitoringEquipment.inserctpests.service.PlantDiseasesAndInsectPestsService;
|
|
|
-import com.sooka.sponest.dataexchange.monitoringEquipment.sennor.api.API;
|
|
|
-import com.sooka.sponest.dataexchange.remoteapi.service.center.monitor.RemoteMonitorService;
|
|
|
-import com.sooka.sponest.dataexchange.util.FileUtils;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class PlantDiseasesAndInsectPestsServiceImpl implements PlantDiseasesAndInsectPestsService {
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(PlantDiseasesAndInsectPestsServiceImpl.class);
|
|
|
-
|
|
|
- @Resource
|
|
|
- public RemoteMonitorService remoteMonitorService;
|
|
|
-
|
|
|
- /**病虫害token**/
|
|
|
- @Value("${monitoringEquipment.insect.insectToken:}")
|
|
|
- private String insectToken;
|
|
|
-
|
|
|
- /**病虫害用户名**/
|
|
|
- @Value("${monitoringEquipment.insect.insectName:}")
|
|
|
- private String insectName;
|
|
|
-
|
|
|
- /**精讯物联网病虫害服务地址**/
|
|
|
- @Value("${monitoringEquipment.insect.sennorInsectURL:}")
|
|
|
- private String sennorInsectURL;
|
|
|
-
|
|
|
- /**病虫害传感器图片下载地址**/
|
|
|
- @Value("${monitoringEquipment.insect.insectPestsUrl:}")
|
|
|
- private String insectPestsUrl;
|
|
|
-
|
|
|
- public AjaxResult plantDiseasesAndInsectPestsMessageReceiver(String deviceNumber) {
|
|
|
- //先请求
|
|
|
- StringBuffer URI = new StringBuffer(sennorInsectURL + API.getInsectNewCollectionResult);
|
|
|
- URI.append("?token=").append(insectToken);
|
|
|
- URI.append("&userName=").append(insectName);
|
|
|
- URI.append("&deviceNumber=").append(deviceNumber);
|
|
|
- String result = HttpRequest.post(URI.toString())
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .execute().body();
|
|
|
- log.info("plantDiseasesAndInsectPestsMessageReceiver 收到数据 ==> {}", result);
|
|
|
-
|
|
|
- //后推送
|
|
|
-// String result = deviceNumber;
|
|
|
- JSONObject object;
|
|
|
- try {
|
|
|
- object = JSONObject.parseObject(result);
|
|
|
- JSONObject data = object.getJSONObject("data");
|
|
|
- String oldTaggingImgUrl = object.getJSONObject("data").getString("oldTaggingImgUrl");
|
|
|
- String taggingImgUrl = object.getJSONObject("data").getString("taggingImgUrl");
|
|
|
- if (oldTaggingImgUrl != null) {
|
|
|
- JSONObject img = img(oldTaggingImgUrl);
|
|
|
- if (img != null && img.getJSONObject("data") != null) {
|
|
|
- String imgUrl = img.getJSONObject("data").getString("url");
|
|
|
- if (imgUrl != null) {
|
|
|
- data.put("oldTaggingImgUrl", imgUrl);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (taggingImgUrl != null) {
|
|
|
- JSONObject img2 = img(taggingImgUrl);
|
|
|
- if (img2 != null && img2.getJSONObject("data") != null) {
|
|
|
- String imgUrl2 = img2.getJSONObject("data").getString("url");
|
|
|
- if (imgUrl2 != null) {
|
|
|
- data.put("taggingImgUrl", imgUrl2);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-// JSONObject img = img(oldTaggingImgUrl);
|
|
|
-// object.getJSONObject("data").put("oldTaggingImgUrl", img.getJSONObject("data").getString("url"));
|
|
|
-// JSONObject img2 = img(taggingImgUrl);
|
|
|
-// object.getJSONObject("data").put("taggingImgUrl", img2.getJSONObject("data").getString("url"));
|
|
|
-
|
|
|
- System.out.println("--------------------------图片-----------------------------");
|
|
|
- String updatedOldTaggingImgUrl = data.getString("oldTaggingImgUrl");
|
|
|
- String updatedTaggingImgUrl = data.getString("taggingImgUrl");
|
|
|
- System.out.println("oldTaggingImgUrl:" + updatedOldTaggingImgUrl);
|
|
|
- System.out.println("taggingImgUrl:" + updatedTaggingImgUrl);
|
|
|
- System.out.println("-------------------------------------------------------------");
|
|
|
- if (object.get("state") != null && object.getString("state").equals("success")) {
|
|
|
- AjaxResult ajaxResult = remoteMonitorService.saveInsectPestsDeviceData(object);
|
|
|
- log.info("数据推送成功! data ===> {} , result ===> {}", result, ajaxResult);
|
|
|
- return AjaxResult.success("数据推送成功!", ajaxResult);
|
|
|
- }
|
|
|
- return AjaxResult.error("数据推送失败!", result);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("数据推送失败! data ===> {} , result ===> {}", result, e.getMessage());
|
|
|
- return AjaxResult.error("数据推送失败!", result);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject img(String oldTggingImgUrl) {
|
|
|
- try {
|
|
|
- //定义一个URL对象
|
|
|
- URL url = new URL(insectPestsUrl + oldTggingImgUrl);
|
|
|
- //打开连接
|
|
|
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
- //设置请求方式为"GET"
|
|
|
- conn.setRequestMethod("GET");
|
|
|
- //超时响应时间为10秒
|
|
|
- conn.setConnectTimeout(10 * 1000);
|
|
|
- //通过输入流获取图片数据
|
|
|
- InputStream inStream = conn.getInputStream();
|
|
|
- //得到图片的二进制数据,以二进制封装得到数据,具有通用性
|
|
|
- byte[] data = readInputStream(inStream);
|
|
|
- //new一个文件对象用来保存图片,默认保存当前工程根目录
|
|
|
- File imageFile = new File("InputStreamInfo.jpg");
|
|
|
- //创建输出流
|
|
|
- FileOutputStream outStream = new FileOutputStream(imageFile);
|
|
|
- //写入数据
|
|
|
- outStream.write(data);
|
|
|
- //关闭输出流
|
|
|
- outStream.close();
|
|
|
- MultipartFile multipartFile = FileUtils.getMultipartFile(imageFile);
|
|
|
- R<SysFile> r = remoteMonitorService.upload(multipartFile);
|
|
|
- return JSONObject.parseObject(JSONObject.toJSONString(r));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return new JSONObject();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static byte[] readInputStream(InputStream inStream) throws Exception {
|
|
|
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
- //创建一个Buffer字符串
|
|
|
- byte[] buffer = new byte[6024];
|
|
|
- //每次读取的字符串长度,如果为-1,代表全部读取完毕
|
|
|
- int len = 0;
|
|
|
- //使用一个输入流从buffer里把数据读取出来
|
|
|
- while ((len = inStream.read(buffer)) != -1) {
|
|
|
- //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
|
|
|
- outStream.write(buffer, 0, len);
|
|
|
- }
|
|
|
- //关闭输入流
|
|
|
- inStream.close();
|
|
|
- //把outStream里的数据写入内存
|
|
|
- return outStream.toByteArray();
|
|
|
- }
|
|
|
-
|
|
|
-}
|