严智鑫
2025-04-22 b1c37cebc8531a256bb9b0f428a4f81f3ad6310b
上片机逻辑修改
3个文件已修改
1个文件已添加
233 ■■■■ 已修改文件
nglib/.idea/encodings.xml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/httpApi.java 116 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/ng/devices/ModbusService.java 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/ng/devices/MysqlService.java 87 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/.idea/encodings.xml
@@ -10,6 +10,7 @@
    <file url="file://$PROJECT_DIR$/src/builder/ThreadHttpServer.java" charset="GBK" />
    <file url="file://$PROJECT_DIR$/src/ng/db/DBSession.java" charset="UTF-8" />
    <file url="file://$PROJECT_DIR$/src/ng/devices/InitUtil.java" charset="UTF-8" />
    <file url="file://$PROJECT_DIR$/src/ng/devices/MysqlService.java" charset="GBK" />
    <file url="file://$PROJECT_DIR$/src/ng/devices/PlcBitInfo.java" charset="UTF-8" />
    <file url="file://$PROJECT_DIR$/src/ng/devices/PlcBitObject.java" charset="UTF-8" />
    <file url="file://$PROJECT_DIR$/src/ng/devices/PlcParameterInfo.java" charset="UTF-8" />
nglib/src/builder/httpApi.java
New file
@@ -0,0 +1,116 @@
package builder;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class httpApi {
    public static void main(String[] args) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("thickness", "0");
        jsonObject.put("width", "0");
        jsonObject.put("height", "0");
        jsonObject.put("sameCount", "0");
        jsonObject.put("glassType", "string");
        sendShelfTask(jsonObject);
    }
    //发送仓储任务
    public static JSONObject sendShelfTask(JSONObject params){
        try {
            String requestURL="http://39.105.110.179:5000/GlassInfo";
            String requestBody = params.toString();
            JSONObject resultJSON= httpApi(requestURL,"POST",requestBody);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo  POST");
        }
        return null;
    }
    //查询上片料架信息
    public static JSONObject selectLoadRack(){
        try {
            String requestURL="http://39.105.110.179:5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo GET");
        }
        return null;
    }
    //上片位片数-1
    public static JSONObject loadRackReduction(){
        try {
            String requestURL="http://39.105.110.179:5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo GET");
        }
        return null;
    }
    //仓储料架信息
    public static JSONObject storageRackInfo(){
        try {
            String requestURL="http://39.105.110.179:5000/GlassInfo";
            JSONObject resultJSON= httpApi(requestURL,"GET",null);
            return resultJSON;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("HTTP ERROR:http://39.105.110.179:5000/GlassInfo GET");
        }
        return null;
    }
    public static JSONObject httpApi(String requestURL,String requestMethod,String requestBody){
        JSONObject jsonObject = null;
        try {
            // 定义 URL
            URL url = new URL(requestURL);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法
            connection.setRequestMethod(requestMethod);
            // 允许输出流,用于发送请求体
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-type", "application/json; charset=UTF-8");
            System.out.println(requestBody);
            // 获取输出流并写入请求体
            try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                if(requestBody!=null){
                    outputStream.writeBytes(requestBody);
                }
                outputStream.flush();
            }
            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 断开连接
            connection.disconnect();
            String resultStr = response.toString();
            if (resultStr!=null&&resultStr.length()>0) {
                jsonObject =JSONObject.parseObject(resultStr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
nglib/src/ng/devices/ModbusService.java
@@ -72,7 +72,6 @@
    }
    
    //一次读取行为
    String once(){
        //按照配置读数据
@@ -112,27 +111,24 @@
            String flag=null;
            String messsage="000000000000000000";
            EPlcType ePlcType=null;
            if(this.machineID==4){
                ePlcType=EPlcType.S1500;
            }else if(this.machineID==6){
            if(this.functionType==10){
                ePlcType=EPlcType.S200_SMART;
            }else{
            }else if(this.functionType==11){
                ePlcType=EPlcType.S1500;
            }else if(this.functionType==12){
                ePlcType=EPlcType.S1200;
            }else{
                return "";
            }
            S7control s7=new S7control(ePlcType, ip, port, 0, 0);
            //List<Integer> word=s7.readWord("DB34.122",  1);
            List<Integer> listWord=s7.readWord(this.remarks,(this.readSize/2));
            if(listWord==null){
                System.out.println("notread");
                return "";
            }
            for(int i=0;i<listWord.size();i++){
                //System.out.println("i  "+listWord.size()+"   "+messsage);
                messsage+=HexUtil.intTo2ByteHex(listWord.get(i));
            }
            //System.out.println(messsage);
            //System.out.println(ip+"  "+s7.readWord("DB34.122",  1));
            //数据库
            DBHelper db=DBHelper.getDBHelper("mes");
            try{
@@ -154,25 +150,12 @@
                    //得到存储过程返回的值,数组第一个为地址,第二个为值
                    int resultsLength=results.length;
                    int resultsSize=resultsLength/2;
                    for(int i=1;i<resultsSize+1;i++){
                        int addressIndex=(i-1)*2;
                        int valueIndex=i*2-1;
                        s7.writeWord(results[addressIndex],Integer.valueOf(results[valueIndex]));//参数组写入
                    }
//                    if(results.length==2){
//                        s7.writeWord(results[0],Integer.valueOf(results[1]) );
//                    }
//                    if(results.length==4){
//                        s7.writeWord(results[0],Integer.valueOf(results[1]));//配方
//                        s7.writeWord(results[2],Integer.valueOf(results[3]));//速度
//                    }
                    List<Integer> listWord2=s7.readWord(this.remarks,(this.readSize/2));
                    System.out.println(listWord2.size());
                }
            }
            catch(Exception e){
                e.printStackTrace();
nglib/src/ng/devices/MysqlService.java
@@ -5,6 +5,8 @@
import builder.Manager;
import builder.S7control;
import builder.httpApi;
import com.alibaba.fastjson.JSONObject;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import ng.db.DBHelper;
import ng.db.DBSession;
@@ -102,9 +104,6 @@
        // TODO Auto-generated method stub
        //读次数+1
        ioCount++;
        //判断是否不调用
    //     if(this.PassInvoke(arg0.Content))
        // return false;
        //转化为mysql输入参数
        String s=this.packToString(arg0);
        //更新最后一条读报文
@@ -112,31 +111,65 @@
        DBSession sn=null;
        String result=null;
        String flag=null;
        //上片
        if(id==2){
            try{
                //JSONObject jSONObject=httpApi.selectLoadRack();
                JSONObject jSONObject=new JSONObject();
                jSONObject.put("thickness", "4");
                jSONObject.put("width", "3660");
                jSONObject.put("height", "2440");
                jSONObject.put("sameCount", "1");
                jSONObject.put("glassType", "string");
                if (jSONObject!=null){
                    //创建连接
                    sn=this.db.createSession(false);
                    Connection con= sn.getConnection();
                    //调用那个存储过程
                    CallableStatement sql=con.prepareCall("{call Total_method_load(?,?,?,?,?,?,?,?)}");
                    sql.registerOutParameter(7, java.sql.Types.VARCHAR);
                    sql.registerOutParameter(8, java.sql.Types.VARCHAR);
                    sql.setString(1, s);
                    sql.setLong(2, id);
                    sql.setDouble(3, Double.valueOf(jSONObject.get("width").toString()) );
                    sql.setDouble(4, Double.valueOf(jSONObject.get("height").toString()) );
                    sql.setDouble(5, Double.valueOf(jSONObject.get("thickness").toString()) );
                    sql.setLong(6, Integer.valueOf(jSONObject.get("sameCount").toString()) );
                    sql.execute();
                    //读取返回参数
                    result= sql.getString(7);
                    flag= sql.getString(8);//1 表示任务完成数量减一
                    if ("1".equals(flag) ){
                        //JSONObject loadRackReductionJson=httpApi.loadRackReduction();
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                sn.close();
            }
        }else{
            try{
                //创建连接
                sn=this.db.createSession(false);
                Connection con= sn.getConnection();
                //调用那个存储过程
                CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}");
                sql.registerOutParameter(3, java.sql.Types.VARCHAR);
                sql.registerOutParameter(4, java.sql.Types.VARCHAR);
                sql.setString(1, s);
                sql.setLong(2, id);
                sql.execute();
                //读取返回参数
                result= sql.getString(3);
                flag= sql.getString(4);
        try{
        //创建连接
            sn=this.db.createSession(false);
            Connection con= sn.getConnection();
            //调用那个存储过程
            CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}");
            sql.registerOutParameter(3, java.sql.Types.VARCHAR);
            sql.registerOutParameter(4, java.sql.Types.VARCHAR);
            sql.setString(1, s);
            sql.setLong(2, id);
            sql.execute();
            //读取返回参数
            result= sql.getString(3);
            flag= sql.getString(4);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            sn.close();
        }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                sn.close();
            }
        }
        if(result!=null){
            if(result.length()>0){
            char c=result.charAt(0);