⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readserial.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
字号:
package org.compiere.cti;

import java.io.*;
import org.compiere.apps.AEnv;
import org.compiere.apps.AWindow;
import org.compiere.model.MQuery;
import org.compiere.util.DB;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.compiere.model.MSession;
import org.compiere.util.Env;
import org.compiere.Compiere;

/**
 *
 * This class reads message from the specific serial port and save
 * the message to the serial buffer.
 *
 */
public class ReadSerial extends Thread {
    private InputStream ComPort;

    /**
     *
     * Constructor
     *
     * @param Port The InputStream from the specific serial port.
     **/

    public ReadSerial(InputStream Port) {
        ComPort = Port;
    }

    public void run() {

        Compiere.startupEnvironment(true); //	Load Environment
        MSession.get(Env.getCtx(), true); //	Start Session
        int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());

        int c;
        byte[] readBuffer = new byte[20];
        try {
            while (true) {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}

                int i = 0;

                while (ComPort.available() > 0) {
                    c = ComPort.read(readBuffer);
                    String str = new String(readBuffer).trim();

                    String s = new String();
                    if (str.indexOf("NMBR") > 0) {
                        String str1 = str.substring(str.indexOf("NMBR"));
                        s = s + str1;
                    }

                    if (str.indexOf(s) > 1) {
                        String str2 = str.substring(0, str.indexOf(s));
                        s += str2;
                    }

                    i = i + 1;
                    String s1 = s.trim();
                    if (i == 3) {
                        String tel = s1.substring(s1.indexOf("=") + 1);
                        tel = tel.trim();
                        String oriTel = tel;
                        String C_BPartner_ID = null;

                        // Matched
                        while (tel.length() > 3) {
                            String sql = "SELECT C_BPartner_ID FROM AD_USER WHERE AD_Client_ID=? and PHONE = ? or PHONE2 = ? UNION SELECT C_BPARTNER_ID FROM C_BPARTNER_LOCATION WHERE AD_Client_ID=? and PHONE = ? or PHONE2 = ?";
                            try {
                                PreparedStatement pstmt = DB.prepareStatement(
                                        sql);
                                pstmt.setInt(1, AD_Client_ID);
                                pstmt.setString(2, tel);
                                pstmt.setString(3, tel);
                                pstmt.setInt(4, AD_Client_ID);
                                pstmt.setString(5, tel);
                                pstmt.setString(6, tel);

                                ResultSet rs = pstmt.executeQuery();
                                StringBuffer bpIds = new StringBuffer();
                                int bpCount = 0;
                                while (rs.next()) {
                                    C_BPartner_ID = rs.getString(
                                            "C_BPartner_ID");
                                    bpIds.append(C_BPartner_ID + ",");
                                    bpCount += 1;
                                }
                                if (bpCount > 0) {
                                    this.startWindow(bpIds.substring(0,
                                            bpIds.length() - 1).toString(),
                                            "Tel: " + oriTel + " 匹配 Matching: " +
                                            tel +
                                            " (已匹配 Matched)");
                                    break;
                                } else {
                                    tel = tel.substring(0, tel.length() - 1);
                                }
                                rs.close();
                                pstmt.close();
                                pstmt = null;
                            } catch (Exception e) {
                                System.out.println(e.getMessage());
                            }
                        }

                        // Inaccurately Matched
                        if (C_BPartner_ID == null) {
                            tel = oriTel;
                            while (tel.length() > 3) {
                                String sql =
                                        "SELECT C_BPartner_ID FROM AD_USER WHERE AD_Client_ID = ? and PHONE LIKE ? or PHONE2 LIKE ? UNION SELECT C_BPARTNER_ID FROM C_BPARTNER_LOCATION WHERE AD_Client_ID LIKE ? and PHONE LIKE ? or PHONE2 LIKE ?";
                                try {
                                    PreparedStatement pstmt = DB.
                                            prepareStatement(sql);
                                    pstmt.setInt(1, AD_Client_ID);
                                    pstmt.setString(2, "%" + tel + "%");
                                    pstmt.setString(3, "%" + tel + "%");
                                    pstmt.setInt(4, AD_Client_ID);
                                    pstmt.setString(5, "%" + tel + "%");
                                    pstmt.setString(6, "%" + tel + "%");

                                    ResultSet rs = pstmt.executeQuery();
                                    StringBuffer bpIds = new StringBuffer();
                                    int bpCount = 0;
                                    while (rs.next()) {
                                        C_BPartner_ID = rs.getString(
                                                "C_BPartner_ID");
                                        bpIds.append(C_BPartner_ID + ",");
                                        bpCount += 1;
                                    }
                                    if (bpCount > 0) {
                                        this.startWindow(bpIds.substring(0,
                                                bpIds.length() - 1).
                                                toString(),
                                                "Tel: " + oriTel +
                                                " (模糊匹配 Inaccurately Matched)");
                                        break;
                                    } else {
                                        tel = tel.substring(0,
                                                tel.length() - 1);
                                    }
                                    rs.close();
                                    pstmt.close();
                                    pstmt = null;
                                } catch (Exception e) {
                                    System.out.println(e.getMessage());
                                }
                            }
                        }
                        // Not Matched
                        if (C_BPartner_ID == null) {
                            tel = oriTel;
                            String sql =
                                    "select c_region.name as RegionName,c_city.name as CityName from c_region " +
                                    "left join c_city on (c_city.c_region_id = c_region.c_region_id) " +
                                    "left join c_mobile on (c_mobile.c_city_id = c_city.c_city_id) " +
                                    "where c_city.areacode = ? " +
                                    "or c_city.areacode = ? " +
                                    "or c_mobile.mobilecode = ?";
                            try {
                                PreparedStatement pstmt = DB.prepareStatement(
                                        sql);
                                pstmt.setString(1, tel.substring(0,4));
                                pstmt.setString(2, tel.substring(0,3));
                                pstmt.setString(3, tel.substring(0,7));

                                ResultSet rs = pstmt.executeQuery();
                                String regionName = "";
                                String cityName = "";
                                if (rs.next()) {
                                    regionName = rs.getString("RegionName");
                                    cityName = rs.getString("CityName");
                                    this.startWindow(
                                            "Tel: " + oriTel +
                                            " , " + regionName + " " + cityName);
                                } else {
                                    startWindow("Tel: " + oriTel + " (未匹配 Not Matched)");
                                }
                                rs.close();
                                pstmt.close();
                                pstmt = null;
                            } catch (Exception e) {
                                System.out.println(e.getMessage());
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {}
    }

    public void startWindow(String c_bpartner_id, String StatusLineInfo) {
        MQuery query = new MQuery("C_BPartner");
        query.addRestriction("C_BPartner_ID", MQuery.IN,
                             "(" + c_bpartner_id + ")");
        query.setRecordCount(1);	//	guess
        //	Start Windows
        AWindow frame = new AWindow();
        boolean OK = false;
        OK = frame.initWindow(291, query);
        frame.getPanel().setStatusLine(StatusLineInfo, false);

        if (!OK)
            return;

        frame.pack();

        //	Center the window
        AEnv.showCenterScreen(frame);
        frame = null;
    }

    public void startWindow(String StatusLineInfo) {
        //	Start Windows
        AWindow frame = new AWindow();
        boolean OK = false;
        OK = frame.initWindow(291, null);
        frame.getPanel().setStatusLine(StatusLineInfo, false);

        if (!OK)
            return;

        frame.pack();

        //	Center the window
        AEnv.showCenterScreen(frame);
        frame = null;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -