Jelajahi Sumber

first commit air project

wuweihao 5 tahun lalu
induk
melakukan
54b5241440

+ 24 - 0
src/main/java/com/fd/AirApplication.java

@@ -0,0 +1,24 @@
+package com.fd;
+
+
+import lombok.extern.log4j.Log4j2;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
+
+@Log4j2
+@SpringBootApplication
+public class AirApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(AirApplication.class, args);
+
+
+    }
+
+
+
+
+
+}

+ 273 - 0
src/main/java/com/fd/controller/DataController.java

@@ -0,0 +1,273 @@
+package com.fd.controller;
+
+import com.fd.entity.Classroom;
+import com.fd.socket.*;
+import com.fd.util.DataFormatUtils;
+import com.fd.util.PythonUtils;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.PostConstruct;
+import java.io.IOException;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by Owen on 2019/10/22 0022 10:55
+ */
+@Log4j2
+@CrossOrigin(maxAge = 3600)
+@RestController
+@RequestMapping("api")
+public class DataController {
+
+    @Value("${python.exePath}")
+    private String exePath;
+
+    @Value("${python.command}")
+    private String command;
+
+    @Value("${socket.ipAddress}")
+    private String ipAddress;
+
+    @Value("${socket.port}")
+    private Integer port;
+
+    // 监控时间
+    @Value("${socket.listenTime}")
+    private Integer listenTime;
+
+    // 定时拍照时间
+    @Value("${timedTask}")
+    private Integer timedTask;
+
+    private Socket client;
+
+
+    @PostConstruct
+    private Socket init() {
+        // 1 建立连接
+        Socket client = null;
+        try {
+            client = new Socket(ipAddress, port);
+            log.info("========= run client ===========");
+
+            // 监听服务端
+            new Thread(new SocketListenThread(client, listenTime)).start();
+//            new Thread(new ReceiveThread(client)).start();
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            log.error(e);
+        }
+
+        return this.client = client;
+    }
+
+
+    // 返回前端总入口
+    @GetMapping("data")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private ResponseEntity getData() {
+        log.info("run getData");
+//        List connect = PythonUtils.connect(exePath, command);
+        List<Object> list = new ArrayList<>();
+        for (int i = 1; i < 99; i++) {
+            Classroom classroom = new Classroom();
+            classroom.setId(i);
+            classroom.setRegion(DataFormatUtils.randomInt(3, 1));
+            classroom.setStatus(DataFormatUtils.randomInt(2, 0));
+            classroom.setStorey(DataFormatUtils.randomInt(10, 1));
+            classroom.setCount(DataFormatUtils.randomInt(50, 20));
+            list.add(classroom);
+        }
+
+        try {
+            Thread.sleep(9000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        return new ResponseEntity(list, HttpStatus.OK);
+    }
+
+
+    /**
+     * 测试算法
+     */
+    @GetMapping("p")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private ResponseEntity getData1() {
+        log.info("run testPython");
+        List connect = PythonUtils.connect(exePath, command);
+        return new ResponseEntity(connect, HttpStatus.OK);
+    }
+
+
+    /**
+     * 重启socket客户端
+     */
+    @GetMapping("restart")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private ResponseEntity restartSocketClient() {
+        log.info("run restartSocketClient");
+
+        try {
+            // 判断socket是否关闭连接,如果关闭,重启连接
+            boolean closed = client.isClosed();
+            log.info("socket is closed: {}", closed);
+            if (closed) {
+                init();
+                log.info("restart socket ....");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return new ResponseEntity("success", HttpStatus.OK);
+    }
+
+
+    /**
+     * 测试联调
+     */
+
+    @GetMapping("all")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private ResponseEntity all() {
+        long start = System.currentTimeMillis();
+        log.info("run all");
+
+        // 1 通知cpp拍照 #P
+        new Thread(new SendMsgThread(client, "#P")).start();
+
+        // 2 调用算法计算人数
+        List result = PythonUtils.connect(exePath, command);
+
+        // 4 告诉cpp ip+人数
+        new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
+
+        // 5 返回结果集给前端
+        result = DataFormatUtils.frontendFormat(result);
+
+        long end = System.currentTimeMillis();
+        log.info("Total time: {}s", (end - start) / 1000);
+        return new ResponseEntity(result, HttpStatus.OK);
+    }
+
+    // test cpp
+    @GetMapping("6")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private ResponseEntity testListen6() {
+        log.info("run testListen6");
+
+        // 1 通知cpp拍照 #P
+        new Thread(new SendMsgThread(client, "#P")).start();
+
+        List<Object> list = new ArrayList<>();
+        for (int i = 0; i < 5; i++) {
+            String ip = "127.0.0." + i;
+            String s = ip + ":" + i;
+            list.add(s);
+
+        }
+        try {
+            Thread.sleep(2000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+
+        // 2 告诉cpp ip+人数
+        new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
+
+
+        return new ResponseEntity("success", HttpStatus.OK);
+    }
+
+
+    /**
+     * 循环测试服务器版本
+     */
+    @GetMapping("load")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private void load() {
+
+        log.info("run load");
+        boolean isRunning = true;
+
+        // 1 通知cpp拍照 #P
+        int count = 1;
+        while (isRunning) {
+            try {
+                Thread.sleep(timedTask);
+                long start = System.currentTimeMillis();
+
+                new Thread(new SendMsgThread(client, "#P")).start();
+
+                // 2 调用算法计算人数
+                List result = PythonUtils.connect(exePath, command);
+
+                // 3 告诉cpp ip+人数
+                new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
+
+                long end = System.currentTimeMillis();
+                log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
+
+            } catch (InterruptedException e) {
+                isRunning = false;
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * 循环测试本地版本
+     */
+    @GetMapping("1")
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private void load1() {
+
+        boolean isRunning = true;
+
+        // 1 通知cpp拍照 #P
+        int count = 1;
+        while (isRunning) {
+            try {
+                Thread.sleep(timedTask);
+                log.info("run timedTask");
+                long start = System.currentTimeMillis();
+                // 1 通知cpp拍照 #P
+                new Thread(new SendMsgThread(client, "#P")).start();
+
+                List<Object> list = new ArrayList<>();
+                for (int i = 0; i < 5; i++) {
+                    String ip = "127.0.0." + i;
+                    String s = ip + ":" + i;
+                    list.add(s);
+                }
+
+                Thread.sleep(2000);
+
+                // 2 告诉cpp ip+人数
+                new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
+
+                long end = System.currentTimeMillis();
+
+                log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
+
+            } catch (InterruptedException e) {
+                isRunning = false;
+                e.printStackTrace();
+            }
+        }
+
+
+    }
+
+
+}

+ 20 - 0
src/main/java/com/fd/entity/Classroom.java

@@ -0,0 +1,20 @@
+package com.fd.entity;
+
+import lombok.Data;
+
+/**
+ * Created by Owen on 2019/10/23 0023 13:56
+ */
+@Data
+public class Classroom {
+
+    private Integer id; // 教师id
+
+    private Integer region; // 区域
+
+    private Integer storey; // 楼层
+
+    private Integer status; // 状态:1:开启,2:关闭, 0:故障
+
+    private Integer count; // 人数
+}

+ 135 - 0
src/main/java/com/fd/sms/SmsService.java

@@ -0,0 +1,135 @@
+package com.fd.sms;
+
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.exceptions.ServerException;
+import com.aliyuncs.http.MethodType;
+import com.aliyuncs.profile.DefaultProfile;
+import com.aliyuncs.profile.IClientProfile;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+@Log4j2
+@Component
+public class SmsService {
+
+    //初始化ascClient需要的几个参数
+    private static final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
+    private static final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)
+    private static final String accessKeyId = "LTAIUrvuHqj8pvry";//你的accessKeyId,参考本文档步骤2
+    private static final String accessKeySecret = "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4";//你的accessKeySecret,参考本文档步骤2
+    private static final String signName = "四维看看";
+
+    public static boolean sendSms(String phoneNum, String templateParam, String templateCode) {
+        // 可自助调整超时时间
+        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
+        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
+        boolean result = false;
+        try {
+            // 初始化acsClient,暂不支持region化
+            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
+            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
+            IAcsClient acsClient = new DefaultAcsClient(profile);
+
+            // 组装请求对象-具体描述见控制台-文档部分内容
+            SendSmsRequest request = new SendSmsRequest();
+
+            // 必填:待发送手机号
+            request.setPhoneNumbers(phoneNum);
+            // 必填:短信签名-可在短信控制台中找到
+            request.setSignName(signName);
+            // 必填:短信模板-可在短信控制台中找到
+            request.setTemplateCode(templateCode);
+
+            // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
+            request.setTemplateParam(templateParam);
+
+            // 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
+            // request.setSmsUpExtendCode("90997");
+
+            // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
+            // request.setOutId("yourOutId");
+
+            // hint 此处可能会抛出异常,注意catch
+            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
+            if (sendSmsResponse != null
+                    && !StringUtils.isEmpty(sendSmsResponse.getCode())
+                    && "OK".equals(sendSmsResponse.getCode())){
+                log.debug("阿里云短信发送成功");
+                result = true;
+            }
+        }catch (ClientException e){
+            log.error("阿里云短信发送成功:" + e);
+        }
+        return result;
+    }
+
+    public static boolean sendSms(String yzmStr,String userPhone){
+
+        //设置超时时间-可自行调整
+        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
+        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
+        //初始化ascClient需要的几个参数
+        final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
+        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)
+        //替换成你的AK
+        final String accessKeyId = "LTAIUrvuHqj8pvry";//你的accessKeyId,参考本文档步骤2
+        final String accessKeySecret = "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4";//你的accessKeySecret,参考本文档步骤2
+        //初始化ascClient,暂时不支持多region
+        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
+                accessKeySecret);
+        try {
+            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
+        } catch (ClientException e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+        }
+        IAcsClient acsClient = new DefaultAcsClient(profile);
+        //组装请求对象
+        SendSmsRequest request = new SendSmsRequest();
+        //使用post提交
+        request.setMethod(MethodType.POST);
+        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
+        request.setPhoneNumbers(userPhone);
+        //必填:短信签名-可在短信控制台中找到
+        request.setSignName("四维看看");
+        //必填:短信模板-可在短信控制台中找到
+        request.setTemplateCode("SMS_126461520");
+        request.setTemplateParam("{\"code\":"+yzmStr+"}");
+
+        SendSmsResponse sendSmsResponse = null;
+        try {
+            sendSmsResponse = acsClient.getAcsResponse(request);
+        } catch (ServerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ClientException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+            //System.out.println("向阿里请求发短信失败");
+            log.error("sendSms:向阿里请求发短信失败");
+        }
+        if(sendSmsResponse == null)
+        {
+            log.error("sendSms:向阿里请求发短信失败");
+        }
+        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
+            //请求成功
+            //System.out.println("向阿里请求发短信成功");
+            log.info("sendSms:向阿里请求发短信成功");
+            return true;
+
+        }
+        return false;
+    }
+
+    public static void main(String[] args) {
+        String code = String.valueOf((int)((Math.random()*9+1)*100000));
+        System.out.println(code);
+        sendSms("15015980188","{\"code\":\"" + code + "\"}", "SMS_126461520");
+    }
+}

+ 23 - 0
src/main/java/com/fd/socket/CloseUtils.java

@@ -0,0 +1,23 @@
+package com.fd.socket;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * 释放资源工具类
+ */
+public class CloseUtils {
+
+    public static void close(Closeable...targets) {
+        for (Closeable target : targets ) {
+
+            try {
+                if (target != null) {
+                    target.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

+ 116 - 0
src/main/java/com/fd/socket/SendMsgThread.java

@@ -0,0 +1,116 @@
+package com.fd.socket;
+
+
+import lombok.extern.log4j.Log4j2;
+import org.springframework.util.StringUtils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * 多线程封装: 发送端
+ *
+ * 1 发送消息
+ * 2 从控制台获取消息
+ * 3 释放资源
+ * 4 重写run方法
+ */
+@Log4j2
+public class SendMsgThread implements Runnable {
+
+    private Socket client;
+
+    private boolean isRunning;
+
+    private String message;
+
+    // 字节输出流
+    private OutputStream out;
+
+    private BufferedReader buf;
+
+    public SendMsgThread(Socket client, String message) {
+
+        this.client = client;
+        this.message = message;
+        try {
+             out = client.getOutputStream();
+            isRunning = true;
+        } catch (IOException e) {
+            log.error("error SendMsgThread");
+            release();
+        }
+    }
+
+
+
+    @Override
+    public void run() {
+            send();
+
+            while (isRunning) {
+                try {
+                    buf =  new BufferedReader(new InputStreamReader(client.getInputStream()));
+                    String msg = buf.readLine();
+
+                    if (StringUtils.isEmpty(msg)) {
+                        log.error("没有收到服务端消息!");
+                        throw new Exception("没有收到服务端消息!");
+                    }
+
+                    if ("JP".equals(msg)) {
+                        log.info("硬件拍照完成! : {}", msg);
+                    }
+                    if ("JI".equals(msg)) {
+                        log.info("socketServer数据确认成功! : {}", msg);
+                    }
+
+//                     关闭接收信息
+                    isRunning = false;
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    log.error("error SendMsgThread");
+                    release();
+                }
+            }
+
+    }
+
+    //发送消息
+    private void send(){
+        try {
+            // 以字节写入
+            out.write(this.message.getBytes());
+            out.flush();
+            log.info("send msg: {}", this.message);
+
+        } catch (IOException e) {
+            log.error("error SendMsgThread");
+            release();
+        }
+    }
+
+
+    //释放资源
+    private void release(){
+        // 关闭循环
+        this.isRunning = false;
+        CloseUtils.close(out, buf, client);
+    }
+
+
+
+
+
+
+
+
+
+}

+ 95 - 0
src/main/java/com/fd/socket/SocketListenThread.java

@@ -0,0 +1,95 @@
+package com.fd.socket;
+
+import lombok.extern.log4j.Log4j2;
+import org.springframework.util.StringUtils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.Socket;
+
+/**
+ * Created by Owen on 2019/10/23 0023 11:53
+ *
+ * 监听服务端心跳
+ */
+@Log4j2
+public class SocketListenThread implements Runnable {
+
+    private  Integer listenTime;
+
+    private Socket client;
+
+    private boolean isRunning;
+
+
+    // 字节输出流
+    private OutputStream out;
+
+    // 接收服务端消息,要以这种方式才能接受字节输入流
+
+    BufferedReader buf;
+
+    public SocketListenThread(Socket client, Integer listenTime) {
+
+        this.client = client;
+        this.listenTime = listenTime;
+        try {
+            out = client.getOutputStream();
+            isRunning = true;
+        } catch (IOException e) {
+            log.error("error SocketListenThread error");
+            release();
+        }
+    }
+
+    @Override
+    public void run() {
+        // 先发送消息给服务端
+
+        while (isRunning) {
+
+
+            try {
+                // 监听频率
+                Thread.sleep(listenTime);
+                send();
+                buf =  new BufferedReader(new InputStreamReader(client.getInputStream()));
+                String msg = buf.readLine();
+                if (StringUtils.isEmpty(msg)) {
+                    log.error("error heartbeat");
+                    throw new Exception("error heartbeat ");
+                }
+                log.info("Listen heartbeat result msg: {}", msg);
+            } catch (IOException | InterruptedException e) {
+                release();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+
+        }
+    }
+
+    //发送消息
+    private void send(){
+        try {
+            // 以字节写入
+            String heartbeat = "1";
+            out.write(heartbeat.getBytes());
+            out.flush();
+            log.info("heartbeat send: {}", heartbeat);
+        } catch (IOException e) {
+            log.error("error heartbeat");
+            release();
+        }
+    }
+
+    //释放资源
+    private void release(){
+        // 关闭循环
+        this.isRunning = false;
+        CloseUtils.close(out, buf, client);
+    }
+
+}

+ 89 - 0
src/main/java/com/fd/util/DataFormatUtils.java

@@ -0,0 +1,89 @@
+package com.fd.util;
+
+import com.fd.entity.Classroom;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * Created by Owen on 2019/10/23 0023 18:30
+ */
+public class DataFormatUtils {
+
+    // 创建范围随机数
+    public static int randomInt(int max, int min){
+        return new Random().nextInt(max+1 - min) + min;
+    }
+
+    // 处理send server 数据结构
+    public static String ipFormat(List list) {
+
+        List<String> formatData = new ArrayList<>();
+        for (int i = 0; i < list.size(); i++) {
+            String str = (String)list.get(i);
+            String prefix = str.substring(0, str.indexOf(":")+1);
+            str = str.substring(str.indexOf(":")+1);
+            str = format(str, 4);
+            if (i == list.size()-1) {
+                formatData.add(prefix + str + ",");
+            } else {
+                formatData.add(prefix + str);
+            }
+        }
+
+        //处理数据格式给socket server
+        Object[] data = formatData.toArray();
+        String s = Arrays.toString(data);
+        s = s.replace(":", "#");
+        s = s.replaceAll("\\s", "");
+
+        String result = s.substring(1, s.lastIndexOf("]"));
+        return result;
+    }
+
+
+    // 处理返回前端数据
+    public static List frontendFormat(List params) {
+        List<Classroom> list = new ArrayList<>();
+        for (int i = 0; i < params.size(); i++) {
+            String str = (String)params.get(i);
+            str = str.substring(str.lastIndexOf(":")+1);
+
+            Classroom classroom = new Classroom();
+            classroom.setId(i);
+            classroom.setRegion(DataFormatUtils.randomInt(3, 1));
+            classroom.setStatus(DataFormatUtils.randomInt(2, 0));
+            classroom.setStorey(DataFormatUtils.randomInt(10, 1));
+            classroom.setCount(Integer.valueOf(str));
+            list.add(classroom);
+        }
+        return list;
+    }
+
+
+
+
+    // 参数左补0
+    public static String format(String str,int strLength){
+        int strLen = str.length();
+        if (strLen < strLength) {
+            while (strLen < strLength) {
+                StringBuffer sb = new StringBuffer();
+                sb.append("0").append(str);//左补0
+                //    sb.append(str).append("0");//右补0
+                str= sb.toString();
+                strLen= str.length();
+            }
+        }
+        return str;
+
+    }
+
+
+    public static void main(String[] args) {
+        String a = "1231:55";
+        System.out.println(a.substring(a.lastIndexOf(":")+1));
+    }
+}

+ 46 - 0
src/main/java/com/fd/util/FileJsonUtils.java

@@ -0,0 +1,46 @@
+package com.fd.util;
+
+
+import com.alibaba.fastjson.JSON;
+//import com.fd.entity.Person;
+
+import java.util.List;
+
+/**
+ * Created by Owen on 2019/10/17 0017 16:02
+ */
+
+
+
+public class FileJsonUtils {
+
+    public static String readJson(){
+
+        String resource = FileUtils.getResource();
+
+        // 读取文件
+        String param = FileUtils.readFile( resource+"json/data.json");
+
+        return param;
+
+    }
+
+//    public static List<Person> readJsonToObject(){
+//
+//        String resource = FileUtils.getResource();
+//
+//        // 读取文件
+//        String param = FileUtils.readFile( resource+"json/data.json");
+//
+//        // 字符串转对象
+//        List<Person> people = JSON.parseArray(param, Person.class);
+//        return people;
+//    }
+
+
+    public static void main(String[] args) {
+        String s = readJson();
+        System.out.println(s);
+    }
+
+}

+ 111 - 0
src/main/java/com/fd/util/FileUtils.java

@@ -0,0 +1,111 @@
+package com.fd.util;
+
+import lombok.extern.log4j.Log4j2;
+import org.junit.Test;
+import org.springframework.util.ResourceUtils;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.Properties;
+
+/**
+ * Created by dell on 2019/4/1.
+ */
+@Log4j2
+public class FileUtils {
+
+    /**
+     * 读取文件方法
+     * @param Path 文件路径
+     * @return 返回内容
+     */
+    public static String readFile(String Path){
+        BufferedReader reader = null;
+        String laststr = "";
+        try{
+            FileInputStream fileInputStream = new FileInputStream(Path);
+            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
+            reader = new BufferedReader(inputStreamReader);
+            String tempString = null;
+            while((tempString = reader.readLine()) != null){
+                laststr += tempString;
+            }
+            reader.close();
+        }catch(IOException e){
+            e.printStackTrace();
+        }finally{
+            if(reader != null){
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return laststr;
+    }
+
+    /**
+     * 生成文件
+     * @param content 内容
+     * @param path 生成路径
+     * @throws IOException
+     */
+    public static void fileWriter(String content, String path) throws IOException {
+        FileWriter writer = new FileWriter(path);
+        writer.write(content);
+        writer.flush();
+        writer.close();
+    }
+
+
+    /**
+     * 获取资源路径
+     * @return
+     */
+    public static String getResource(){
+        String path = "";
+        try {
+            path = ResourceUtils.getURL("classpath:").getPath();
+            path = URLDecoder.decode(path,"utf-8");
+//            logger.info("classpath path :"+path);
+        } catch (Exception e) {
+//            logger.error(" classpath Error" + e.getMessage(), e);
+        }
+        return path;
+    }
+
+    // 读取配置文件值
+    public static String getValue(String key){
+        Properties prop = new Properties();
+        URL resource = FileUtils.class.getClassLoader().getResource("ip.properties");
+        FileInputStream in = null;
+        try {
+            assert resource != null;
+            log.info(resource.getPath());
+            in = new FileInputStream(resource.getPath());
+            prop.load(in);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return prop.getProperty(key);
+    }
+
+    public static void main(String[] args) {
+        System.out.println(getValue("192.168.1.2"));
+    }
+
+    /**
+     * 测试读取文件
+     */
+    @Test
+    public void readFileTest(){
+        // 项目文件路径
+        String path_2 = "src/main/resources/files/json/shisiado.json";
+        // 读取文件内容
+        String s = readFile(path_2);
+        System.out.println(s);
+
+    }
+}

+ 83 - 0
src/main/java/com/fd/util/PythonUtils.java

@@ -0,0 +1,83 @@
+package com.fd.util;
+
+
+import lombok.extern.log4j.Log4j2;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Created by Owen on 2019/10/22 0022 10:48
+ */
+@Log4j2
+public class PythonUtils {
+
+    // 命令行调用python中间件
+    public static List connect(String exePath, String command) {
+        log.info("run connect");
+        String[] cmdArr = new String[] {exePath, command};
+
+        Process process = null;
+        String line = null;
+        List<Object> list = null;
+
+        try {
+            process = Runtime.getRuntime().exec(cmdArr);
+
+            InputStream is = process.getInputStream();
+            DataInputStream dis = new DataInputStream(is);
+            // 是一行行读的
+            list = new ArrayList<>();
+            while ((line = dis.readLine()) != null) {
+                System.out.println(line);
+                list.add(line);
+            }
+
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            log.error(e);
+        }
+
+
+        try {
+            process.waitFor();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+            log.error(e);
+        }
+        assert list != null;
+        log.info("python data: {}" , Arrays.toString(list.toArray()));
+        return list;
+    }
+
+    public static void main(String[] args) {
+
+//        String exe = "/home/francis/.conda/envs/my_python3.7/bin/python";
+//        String command = "/home/francis/Program/LCFCN/run.py";
+
+        String exe = "python";
+        String command = "D:\\work\\air\\1.py";
+
+//        List connect = connect(exe, command);
+//
+//        for (int i = 10; i < 20; i++) {
+//            connect.add(i);
+//        }
+//
+        List<Object> list = new ArrayList<>();
+        for (int i = 0; i < 10 ; i++) {
+            list.add(i);
+        }
+
+        String string = Arrays.toString(list.toArray());
+        String replace = string.replaceAll("\\s", "");
+        System.out.println("==: "+string);
+        System.out.println("=1=: "+replace);
+        System.out.println(Arrays.toString(list.toArray()));
+    }
+}

+ 61 - 0
src/main/java/com/fd/util/python.java

@@ -0,0 +1,61 @@
+package com.fd.util;
+
+
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Created by Owen on 2019/10/22 0022 10:48
+ */
+public class python {
+
+    // 命令行调用python中间件
+    public static List connect(String exePath, String command) {
+        String[] cmdArr = new String[] {exePath, command};
+
+        Process process = null;
+        String line = null;
+        List<Object> list = null;
+
+        try {
+            process = Runtime.getRuntime().exec(cmdArr);
+
+            InputStream is = process.getInputStream();
+            DataInputStream dis = new DataInputStream(is);
+            // 是一行行读的
+            list = new ArrayList<>();
+            while ((line = dis.readLine()) != null) {
+                list.add(line);
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+
+        try {
+            process.waitFor();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        return list;
+    }
+
+    public static void main(String[] args) {
+
+//        String exe = "/home/francis/.conda/envs/my_python3.7/bin/python";
+//        String command = "/home/francis/Program/LCFCN/run.py";
+
+        String exe = "python";
+        String command = "D:\\work\\air\\run.py";
+
+        List connect = connect(exe, command);
+
+        System.out.println(Arrays.toString(connect.toArray()));
+    }
+}