package com.bonait.bnframework.HBL.Communication;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class CommBase {
    /**
     * Ping PLC地址是否通畅
     * @param address
     * @param pingTimes  ping的次数
     * @param timeOut  超时时间 10
     * @return
     */
    public boolean NetPing(String address, int pingTimes, int timeOut) {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec( "ping "  + "-c " + pingTimes + " -w " + timeOut+ " "+address);
            InputStreamReader r = new InputStreamReader(process.getInputStream());
            LineNumberReader returnData = new LineNumberReader(r);
            String returnMsg="";
            String line = "";
            while ((line = returnData.readLine()) != null) {
                System.out.println(line);
                returnMsg += line;
            }

            if(returnMsg.indexOf("100% packet loss")!=-1){
                System.out.println("与 " +address +" 连接不畅通.");
                //ToastUtils.info("与 " +address +" 连接不畅通.");
                return  false;
            } else{
                System.out.println("与 " +address +" 连接畅通.");
                //ToastUtils.info("与 " +address +" 连接不畅通.");
                return true;
            }
        } catch (IOException e) {
            // e.printStackTrace();
        }
        return false;

    }

}
