From a547865c1388b7edccf6c426547d5ca0313fb34c Mon Sep 17 00:00:00 2001 From: "lisai17@sina.com" Date: Sun, 20 Dec 2020 18:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E8=BF=9B=E5=90=8C=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devicectrl/common/DeviceThread.java | 51 ++- .../devicectrl/controllers/Controller.java | 218 +++------ .../devicectrl/controllers/InController.java | 407 +++++++++-------- .../devicectrl/controllers/OutController.java | 424 ++++++++++-------- .../devicectrl/device/DingsongScale.java | 138 ++++++ .../ssjygl/devicectrl/device/LEDThread.java | 2 +- .../local/ssjygl/devicectrl/device/PLC.java | 4 +- .../com/cowr/local/ssjygl/main/Config.java | 2 +- .../transport/TransportDeviceService.java | 6 +- .../src/main/resources/dev/config.properties | 2 +- .../src/main/resources/prod/config.properties | 2 +- 11 files changed, 699 insertions(+), 557 deletions(-) create mode 100644 ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/DingsongScale.java diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/common/DeviceThread.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/common/DeviceThread.java index f5cf995..aa3348a 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/common/DeviceThread.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/common/DeviceThread.java @@ -17,6 +17,7 @@ import com.jfinal.plugin.activerecord.Record; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.concurrent.locks.ReentrantLock; public class DeviceThread extends Thread { private static Log log = Log.getLog(DeviceThread.class); @@ -52,6 +53,7 @@ public class DeviceThread extends Thread { private Map plcMap = new HashMap<>(); private List controllers = new ArrayList<>(); private List threads = new ArrayList<>(); + private ReentrantLock ctrllock = new ReentrantLock(true); private int supermarket_id; @@ -63,13 +65,21 @@ public class DeviceThread extends Thread { "scale": { "ip": "192.168.0.189", "port": 10003 }, "which": "R01", "scale_wait_time":5000, - "camera": { "ip": "192.168.0.100" } + "camera": { "ip": "192.168.0.100" }, + + "plcRod1Up": "rodIn1Up", + "plcSensor1Reset": "sensorIn1Reset", + "plcSensor1": "sensorIn1" }, { "led": { "ip": "192.168.0.197", "port": 5005 }, "plc": { "ip": "192.168.0.220", "port": 502 }, "scale": { "ip": "192.168.0.189", "port": 10001 }, "which": "C01", - "camera": { "ip": "192.168.0.102" } + "camera": { "ip": "192.168.0.102" }, + + "plcRod1Up": "rodOut1Up", + "plcSensor1Reset": "sensorOut1Reset", + "plcSensor1": "sensorOut1" } ], "printer": [{ @@ -145,6 +155,8 @@ public class DeviceThread extends Thread { printerMap.put(printerconf.getString("id"), new Printer(printerconf.getString("ip"), printerconf.getIntValue("port"))); } + Map checkcommctrl = new HashMap<>(); + // 加载称重流程控制相关硬件配置 for (int i = 0; i < flow.size(); i++) { //TODO debug 只连一个摄像头测试 @@ -155,6 +167,9 @@ public class DeviceThread extends Thread { JSONObject device = flow.getJSONObject(i); String which = device.getString("which"); + String plcRod1Up = device.getString("plcRod1Up"); + String plcSensor1Reset = device.getString("plcSensor1Reset"); + String plcSensor1 = device.getString("plcSensor1"); Integer scale_wait_time = device.getInteger("scale_wait_time"); JSONObject cameraconf = device.getJSONObject("camera"); JSONObject scaleconf = device.getJSONObject("scale"); @@ -180,6 +195,8 @@ public class DeviceThread extends Thread { } } else if (supermarket_id == 7) { scale = new KeLiScale(which + "_scale", scaleconf.getString("ip"), scaleconf.getIntValue("port")); + } else if (supermarket_id == 9) { + scale = new DingsongScale(which + "_scale", scaleconf.getString("ip"), scaleconf.getIntValue("port")); } else { scale = new XiShuiScale(which + "_scale", scaleconf.getString("ip"), scaleconf.getIntValue("port")); } @@ -206,14 +223,23 @@ public class DeviceThread extends Thread { } if (which.startsWith(Enums.CtrlFlowEnum.R.name())) { - controller = new InController(which, camera, scale, led, plc, scale_wait_time); + controller = new InController(which, camera, scale, led, plc, scale_wait_time, plcRod1Up, plcSensor1Reset, plcSensor1); } else if (which.startsWith(Enums.CtrlFlowEnum.C.name())) { - controller = new OutController(which, camera, scale, led, plc, scale_wait_time); + controller = new OutController(which, camera, scale, led, plc, scale_wait_time, plcRod1Up, plcSensor1Reset, plcSensor1); } else { log.error("which 值设置错误 %s ", config); return; } + // 使用同一个磅的,认为是同进同出 + String scalekey = scaleconf.getString("ip") + "_" + scaleconf.getIntValue("port"); + if (checkcommctrl.containsKey(scalekey)) { + checkcommctrl.get(scalekey).setCtrllock(ctrllock); // 把已经存在从找出来,传 lock 进去 + controller.setCtrllock(ctrllock); // 当前的 ctrl 也传同一个 lock 进去 + } else { + checkcommctrl.put(scalekey, controller); + } + camera.setController(controller); cameras.put(camera.getIp(), camera); @@ -239,7 +265,7 @@ public class DeviceThread extends Thread { }); } - public void reconnScale(String scaleId){ + public void reconnScale(String scaleId) { if (!this.scaleMap.containsKey(scaleId)) { throw new IllegalArgumentException(scaleId + " does not exist"); } @@ -250,11 +276,11 @@ public class DeviceThread extends Thread { return; } - try{ + try { scale.disconnect(); Thread.sleep(500); scale.connect(); - }catch (Exception e){ + } catch (Exception e) { log.error(e.getMessage(), e); } } @@ -330,6 +356,17 @@ public class DeviceThread extends Thread { return; } + // 永安砂站只有一个磅,同进同出,出入口共用道闸 + if (CliCacheData.SUP.getId() == 9 && Enums.WhichEnum.C01.name().equals(which)) { + which = Enums.WhichEnum.R01.name(); + + if (num == 1) { + num = 2; + } else if (num == 2) { + num = 1; + } + } + String cmd = "rod"; if (which.startsWith(Enums.CtrlFlowEnum.R.name())) { diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/Controller.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/Controller.java index b550fdd..e385dc5 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/Controller.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/Controller.java @@ -7,6 +7,7 @@ import com.cowr.local.ssjygl.main.CliCacheData; import com.cowr.local.ssjygl.transport.TransportDeviceService; import com.cowr.local.ssjygl.transport.TransportSyncService; import com.cowr.model.PostLicenseResult; +import com.jfinal.kit.StrKit; import com.jfinal.log.Log; import java.io.File; @@ -34,6 +35,13 @@ public abstract class Controller implements Runnable { private PLC plc; // 道闸控制 private int scaleWaitTime; // 落杆后,上榜等待时间 + // plc 指令 + private String rod1Up; // 抬前闸 + private String sensor1Reset; // 重置前地感状态 + private String sensor1; // 读地感状态 + + private ReentrantLock ctrllock; // 同进同出互斥锁 + private String currentLicense; // 当前流程车牌 private BlockingQueue licenseQueue = new LinkedBlockingQueue<>(); // 车识识别结果队列 private ReentrantLock licenseResultLock = new ReentrantLock(); @@ -52,6 +60,27 @@ public abstract class Controller implements Runnable { this.scaleWaitTime = scaleWaitTime; } + public Controller(String which, Camera camera, AbsScale scale, LEDThread led, PLC plc, int scaleWaitTime, String rod1Up, String sensor1Reset, String sensor1) { + log.info("%s 流程上磅等待时间 %s", which, scaleWaitTime); + + this.which = which; + this.camera = camera; + this.scale = scale; + this.led = led; + this.plc = plc; + this.scaleWaitTime = scaleWaitTime; + + if (StrKit.notBlank(rod1Up)) { + this.rod1Up = rod1Up; + } + if (StrKit.notBlank(sensor1Reset)) { + this.sensor1Reset = sensor1Reset; + } + if (StrKit.notBlank(sensor1)) { + this.sensor1 = sensor1; + } + } + public void onLicenseResult(String license, File file, boolean head, boolean delete) { if (license == null || license.isEmpty()) { return; @@ -246,12 +275,6 @@ public abstract class Controller implements Runnable { this.running = running; } - public abstract String plcRod1Up(); // 抬闸 1 - - public abstract String plcSensor1Reset(); // 重置地感 1 - - public abstract String plcSensor1(); // 读地感 1 - public int getScaleWaitTime() { return scaleWaitTime; } @@ -260,6 +283,38 @@ public abstract class Controller implements Runnable { this.scaleWaitTime = scaleWaitTime; } + public String getRod1Up() { + return rod1Up; + } + + public void setRod1Up(String rod1Up) { + this.rod1Up = rod1Up; + } + + public String getSensor1Reset() { + return sensor1Reset; + } + + public void setSensor1Reset(String sensor1Reset) { + this.sensor1Reset = sensor1Reset; + } + + public String getSensor1() { + return sensor1; + } + + public void setSensor1(String sensor1) { + this.sensor1 = sensor1; + } + + public ReentrantLock getCtrllock() { + return ctrllock; + } + + public void setCtrllock(ReentrantLock ctrllock) { + this.ctrllock = ctrllock; + } + public void deviceInit() { // 后续加入摄像头连不上的时候,可以手动加入 // 摄像头没有连上 getLicenseQueue() 不为空,表示有手动加入车牌 @@ -308,155 +363,4 @@ public abstract class Controller implements Runnable { log.error(e.getMessage(), e); } } - -// public Boolean weightFlow(LicenseJPGPair pair) { -// try { -// // 将识别的到的车牌加入服务端队列 -// postLicenseQueue(getWhich(), getLicenseQueue()); -// -// PostLicenseResult postLicenseResult = postLicense(CliCacheData.SUP.getId(), pair.license, pair.file, getWhich()); -// -// if (postLicenseResult.isUploaded()) { -// if (postLicenseResult.getLicense() == null) { -// log.error("%s 车牌号数据错误", getWhich()); -// return null; -// } -// -// // 上传成功,但是服务端认为无权限 -// if (!postLicenseResult.isAccess()) { -// log.debug("%s 【%s】未授权或者有未处理完的记录", getWhich(), pair.license); -//// onLicenseResult(pair.license, pair.file, true, false); -// Thread.sleep(10000); -// return null; -// } -// } else { -// log.debug("%s 【%s】上传失败", getWhich(), pair.license); -// return null; -// } -// -// log.debug("%s 【%s】开始流程", getWhich(), pair.license); -// -// setCurrentLicense(pair.license); // 设定当前流程中的车辆 -// -// log.debug("%s 【%s】上磅前,控制LED显示", getWhich(), pair.license); -// try { -// getLed().screen(pair.license, "等待上磅", LEDInfoFormat); -// } catch (Exception e) { -// log.error(e.getMessage(), e); -// } -// -// log.debug("%s 【%s】上磅,抬前闸", getWhich(), pair.license); -// try { -// getPlc().write(plcRod1Up()); -// } catch (Exception e) { -// log.error(e.getMessage(), e); -// } -// -// try { -// getPlc().write(plcSensor1Reset()); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// -// log.debug("%s 【%s】上磅,等待地感线圈状态", getWhich(), pair.license); -// try { -// while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 -// if (getPlc().read(plcSensor1())) { -// break; -// } -// } -// } catch (IOException e) { -// log.debug("%s 【%s】上磅,PLC连接失败", getWhich(), pair.license); -// log.error(e.getMessage(), e); -// } -// -// // 重置流程,这个位置要重置地感线圈状态 -// if (getResetFlow()) { -// try { -// getPlc().write(plcSensor1Reset()); -// } catch (IOException e) { -// log.error(e.getMessage(), e); -// } -// setResetFlow(false); -// resetCurrentLicense(); -// log.debug("%s %s 重置流程", getWhich(), pair.license); -// return null; -// } -// -// log.debug("%s 【%s】上磅后,控制LED显示", getWhich(), pair.license); -// try { -// getLed().screen(pair.license, "开始称重", LEDInfoFormat); -// } catch (Exception e) { -// log.error(e.getMessage(), e); -// } -// -// Thread.sleep(5000); // 获取到地感线圈状态后,等待 5 秒后开始称重 -// -// double weight = 0; -// int tryCount = 0; -// while (tryCount < 10 && !getResetFlow()) { -// try { -// if (!getScale().isConnected() && !getScale().connect()) { -// log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); -// } else { -// weight = getScale().weigh30(); -// if (weight > 0) { -// log.debug("%s 【%s】称重结果【%s】", getWhich(), pair.license, weight); -// break; -// } -// } -// } catch (Exception e) { -// getScale().disconnect(); -// -// log.error(e.getMessage(), e); -// log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); -// } -// tryCount++; -// } -// -// // 重置流程 -// if (getResetFlow()) { -// setResetFlow(false); -// resetCurrentLicense(); -// log.debug("%s %s 重置流程", getWhich(), pair.license); -// return null; -// } -// -// if (weight <= 0) { -// log.debug("%s 【%s】称重失败【%s】", getWhich(), pair.license, weight); -// } -// -// log.debug("%s 【%s】上磅后,控制LED显示称重结果【%s】", getWhich(), pair.license, weight); -// try { -// String weightStr; -// if (weight >= 100) { -// weightStr = String.format("%.1f", weight); -// } else { -// weightStr = String.format("%.2f", weight); -// } -// -// getLed().screen(pair.license, weightStr, LEDWeightFormat); -// } catch (IOException e) { -// log.error(e.getMessage(), e); -// log.debug("%s 【%s】控制LED显示称重结果失败【%s】", getWhich(), pair.license, weight); -// } -// -// log.debug("%s 【%s】称重后,上传重量【%s】", getWhich(), pair.license, weight); -// tryCount = 0; -// Boolean weightPosted = null; -// while (tryCount < 10) { -// if (weightPosted != null && weightPosted) { -// break; -// } -// weightPosted = postWeight(CliCacheData.SUP.getId(), pair.license, weight, getWhich()); -// tryCount++; -// Thread.sleep(500); -// } -// -// return weightPosted; -// } catch (Exception e) { -// log.error(e.getMessage(), e); -// return false; -// } -// } } diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/InController.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/InController.java index aeac262..d6aa39a 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/InController.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/InController.java @@ -5,6 +5,7 @@ import com.cowr.local.ssjygl.devicectrl.utils.LicenseJPGPair; import com.cowr.local.ssjygl.main.CliCacheData; import com.cowr.local.ssjygl.main.Config; import com.cowr.model.PostLicenseResult; +import com.jfinal.kit.StrKit; import com.jfinal.log.Log; import java.io.IOException; @@ -14,21 +15,24 @@ public class InController extends Controller implements Runnable { public InController(String which, Camera camera, AbsScale scale, LEDThread led, PLC plc, int scaleWaitTime) { super(which, camera, scale, led, plc, scaleWaitTime); + + setRod1Up("rodIn1Up"); + setSensor1Reset("sensorIn1Reset"); + setSensor1("sensorIn1"); } - @Override - public String plcRod1Up() { - return "rodIn1Up"; - } + public InController(String which, Camera camera, AbsScale scale, LEDThread led, PLC plc, int scaleWaitTime, String rod1Up, String sensor1Reset, String sensor1) { + super(which, camera, scale, led, plc, scaleWaitTime, rod1Up, sensor1Reset, sensor1); - @Override - public String plcSensor1Reset() { - return "sensorIn1Reset"; - } - - @Override - public String plcSensor1() { - return "sensorIn1"; + if (StrKit.isBlank(rod1Up)) { + setRod1Up("rodIn1Up"); + } + if (StrKit.isBlank(sensor1Reset)) { + setRod1Up("sensorIn1Reset"); + } + if (StrKit.isBlank(sensor1)) { + setRod1Up("sensorIn1"); + } } public void run() { @@ -40,7 +44,7 @@ public class InController extends Controller implements Runnable { // 出现错位等情况时,需要重置流程,重新开始 if (getResetFlow()) { try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 + getPlc().write(getSensor1Reset()); // 重置地感状态 } catch (IOException e) { log.error(e.getMessage(), e); } @@ -66,7 +70,7 @@ public class InController extends Controller implements Runnable { log.debug("%s 【%s】 重置流程", getWhich(), getCurrentLicense()); try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 + getPlc().write(getSensor1Reset()); // 重置地感状态 } catch (IOException e) { log.error(e.getMessage(), e); } @@ -81,217 +85,244 @@ public class InController extends Controller implements Runnable { // 将识别的到的车牌加入服务端队列 postLicenseQueue(getWhich(), getLicenseQueue()); - PostLicenseResult postLicenseResult = postLicense(CliCacheData.SUP.getId(), pair.license, pair.file, getWhich()); + // 识别结果加入队列后,在上传之前,需要判断是否有同进同出锁 + if (getCtrllock() != null) { + if (getCtrllock().isLocked()) { + try { + while (getCtrllock().isLocked()) { + log.debug("出场流程已经开始,等待释放资源"); - if (postLicenseResult.isUploaded()) { - if (postLicenseResult.getLicense() == null) { - log.error("%s 车牌号数据错误", getWhich()); - continue; - } - - // 上传成功,但是服务端认为无权限 - if (!postLicenseResult.isAccess()) { - log.debug("%s 【%s】未授权或者有未处理完的记录", getWhich(), pair.license); - continue; - } - } else { - log.debug("%s 【%s】上传失败", getWhich(), pair.license); - continue; - } - - log.debug("%s 【%s】开始流程", getWhich(), pair.license); - - setCurrentLicense(pair.license); // 设定当前流程中的车辆 - - log.debug("%s 【%s】上磅前,控制LED显示", getWhich(), pair.license); - try { - getLed().setInfo(pair.license, "等待上磅"); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,抬前闸", getWhich(), pair.license); - - try { - getPlc().write(plcRod1Up()); // 抬闸 - } catch (Exception e) { - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,重置地感线圈状态", getWhich(), pair.license); - - try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,等待地感线圈状态", getWhich(), pair.license); - try { - while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 - if (getPlc().read(plcSensor1())) { // 读取地感状态 - break; + Thread.sleep(10000); + } + } catch (Exception e) { + log.error(e.getMessage(), e); } } - } catch (IOException e) { - log.debug("%s 【%s】上磅,PLC连接失败", getWhich(), pair.license); - log.error(e.getMessage(), e); + + getCtrllock().lock(); + log.debug("进入 %s 流程,锁定资源", getWhich()); } - // 重置流程,这个位置要重置地感线圈状态 - if (getResetFlow()) { - try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { - log.error(e.getMessage(), e); - } - setResetFlow(false); - resetCurrentLicense(); - log.debug("%s %s 重置流程", getWhich(), pair.license); - continue; - } - - log.debug("%s 【%s】上磅后,控制LED显示", getWhich(), pair.license); try { - getLed().setInfo(pair.license, "开始称重"); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - // 盐港的雷达离地磅比较远,要多等一会儿 - Thread.sleep(getScaleWaitTime()); + PostLicenseResult postLicenseResult = postLicense(CliCacheData.SUP.getId(), pair.license, pair.file, getWhich()); - double weight = 0; - int tryCount = 0; - while (tryCount < 10 && !getResetFlow()) { - log.debug("第 %s 次,重 %s", tryCount, weight); - - try { - if (!getScale().isConnected()) { - log.debug("%s 【%s】磅秤连接断开,重连", getWhich(), pair.license); - getScale().connect(); + if (postLicenseResult.isUploaded()) { + if (postLicenseResult.getLicense() == null) { + log.error("%s 车牌号数据错误", getWhich()); + continue; } - weight = getScale().weigh15(); - if (weight > 0) { - log.debug("%s 【%s】称重结果【%s】", getWhich(), pair.license, weight); - break; - } else { - // 读数错误,断开连接,下次读取时重连 - getScale().disconnect(); + // 上传成功,但是服务端认为无权限 + if (!postLicenseResult.isAccess()) { + log.debug("%s 【%s】未授权或者有未处理完的记录", getWhich(), pair.license); + continue; } - } catch (Exception e) { - getScale().disconnect(); - - log.error(e.getMessage(), e); - log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); - } - - tryCount++; - } - - // 重置流程 - if (getResetFlow()) { - try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { - log.error(e.getMessage(), e); - } - setResetFlow(false); - resetCurrentLicense(); - log.debug("%s %s 重置流程", getWhich(), pair.license); - continue; - } - - if (weight <= 0) { - log.debug("%s 【%s】称重失败【%s】", getWhich(), pair.license, weight); - } - - log.debug("%s 【%s】上磅后,控制LED显示称重结果【%s】", getWhich(), pair.license, weight); - try { - String weightStr; - if (weight >= 100) { - weightStr = String.format("%.1f", weight); } else { - weightStr = String.format("%.2f", weight); + log.debug("%s 【%s】上传失败", getWhich(), pair.license); + continue; } - getLed().setWeightInfo(pair.license, weightStr); - } catch (Exception e) { - log.error(e.getMessage(), e); - log.debug("%s 【%s】控制LED显示称重结果失败【%s】", getWhich(), pair.license, weight); - } + log.debug("%s 【%s】开始流程", getWhich(), pair.license); - log.debug("%s 【%s】称重后,上传重量【%s】", getWhich(), pair.license, weight); - tryCount = 0; - Boolean weightPosted = null; - while (tryCount < 10) { - tryCount++; - weightPosted = postWeight(CliCacheData.SUP.getId(), pair.license, weight, getWhich()); - if (weightPosted != null && weightPosted) { - break; - } - Thread.sleep(500); - } + setCurrentLicense(pair.license); // 设定当前流程中的车辆 - if (weightPosted != null && weightPosted) { - log.debug("%s 【%s】抬入场后闸", getWhich(), pair.license); + log.debug("%s 【%s】上磅前,控制LED显示", getWhich(), pair.license); try { - getPlc().write("rodIn2Up"); - } catch (IOException e) { + getLed().setInfo(pair.license, "等待上磅"); + } catch (Exception e) { log.error(e.getMessage(), e); - log.debug("%s 【%s】入场后闸,PLC连接失败", getWhich(), pair.license); } - } else { - log.debug("%s 【%s】称重流程中断,跳出流程", getWhich(), pair.license); - continue; - } - try { - getPlc().write("sensorIn2Reset"); - } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } + log.debug("%s 【%s】上磅,抬前闸", getWhich(), pair.license); - log.debug("%s 【%s】下磅,等待地感线圈状态", getWhich(), pair.license); - try { - while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 - if (getPlc().read("sensorIn2")) { + try { + getPlc().write(getRod1Up()); // 抬闸 + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + log.debug("%s 【%s】上磅,重置地感线圈状态", getWhich(), pair.license); + + try { + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + + log.debug("%s 【%s】上磅,等待地感线圈状态", getWhich(), pair.license); + try { + while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 + if (getPlc().read(getSensor1())) { // 读取地感状态 + break; + } + } + } catch (IOException e) { + log.debug("%s 【%s】上磅,PLC连接失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + + // 重置流程,这个位置要重置地感线圈状态 + if (getResetFlow()) { + try { + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + log.debug("%s %s 重置流程", getWhich(), pair.license); + continue; + } + + log.debug("%s 【%s】上磅后,控制LED显示", getWhich(), pair.license); + try { + getLed().setInfo(pair.license, "开始称重"); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + // 盐港的雷达离地磅比较远,要多等一会儿 + Thread.sleep(getScaleWaitTime()); + + double weight = 0; + int tryCount = 0; + while (tryCount < 10 && !getResetFlow()) { + log.debug("第 %s 次,重 %s", tryCount, weight); + + try { + if (!getScale().isConnected()) { + log.debug("%s 【%s】磅秤连接断开,重连", getWhich(), pair.license); + getScale().connect(); + } + + weight = getScale().weigh15(); + if (weight > 0) { + log.debug("%s 【%s】称重结果【%s】", getWhich(), pair.license, weight); + break; + } else { + // 读数错误,断开连接,下次读取时重连 + getScale().disconnect(); + } + } catch (Exception e) { + getScale().disconnect(); + + log.error(e.getMessage(), e); + log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); + } + + tryCount++; + } + + // 重置流程 + if (getResetFlow()) { + try { + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + log.debug("%s %s 重置流程", getWhich(), pair.license); + continue; + } + + if (weight <= 0) { + log.debug("%s 【%s】称重失败【%s】", getWhich(), pair.license, weight); + } + + log.debug("%s 【%s】上磅后,控制LED显示称重结果【%s】", getWhich(), pair.license, weight); + try { + String weightStr; + if (weight >= 100) { + weightStr = String.format("%.1f", weight); + } else { + weightStr = String.format("%.2f", weight); + } + + getLed().setWeightInfo(pair.license, weightStr); + } catch (Exception e) { + log.error(e.getMessage(), e); + log.debug("%s 【%s】控制LED显示称重结果失败【%s】", getWhich(), pair.license, weight); + } + + log.debug("%s 【%s】称重后,上传重量【%s】", getWhich(), pair.license, weight); + tryCount = 0; + Boolean weightPosted = null; + while (tryCount < 10) { + tryCount++; + weightPosted = postWeight(CliCacheData.SUP.getId(), pair.license, weight, getWhich()); + if (weightPosted != null && weightPosted) { break; } + Thread.sleep(500); + } + + if (weightPosted != null && weightPosted) { + log.debug("%s 【%s】抬入场后闸", getWhich(), pair.license); + try { + getPlc().write("rodIn2Up"); + } catch (IOException e) { + log.error(e.getMessage(), e); + log.debug("%s 【%s】入场后闸,PLC连接失败", getWhich(), pair.license); + } + } else { + log.debug("%s 【%s】称重流程中断,跳出流程", getWhich(), pair.license); + continue; } - } catch (IOException e) { - log.debug("%s 【%s】下磅,PLC连接失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } - if (getResetFlow()) { try { getPlc().write("sensorIn2Reset"); } catch (IOException e) { log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); log.error(e.getMessage(), e); } - setResetFlow(false); - resetCurrentLicense(); - continue; - } - resetCurrentLicense(); - log.debug("%s 【%s】流程处理完毕", getWhich(), pair.license); - - if (getResetFlow()) { + log.debug("%s 【%s】下磅,等待地感线圈状态", getWhich(), pair.license); try { - getPlc().write("sensorIn2Reset"); + while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 + if (getPlc().read("sensorIn2")) { + break; + } + } } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.debug("%s 【%s】下磅,PLC连接失败", getWhich(), pair.license); log.error(e.getMessage(), e); } - setResetFlow(false); + + if (getResetFlow()) { + try { + getPlc().write("sensorIn2Reset"); + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + continue; + } + resetCurrentLicense(); - continue; + log.debug("%s 【%s】流程处理完毕", getWhich(), pair.license); + + if (getResetFlow()) { + try { + getPlc().write("sensorIn2Reset"); + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + continue; + } + } finally { + if (getCtrllock() != null) { + getCtrllock().unlock(); + + log.debug("完成 %s 流程,释放资源", getWhich()); + } } } catch (Exception e) { log.error(e.getMessage(), e); diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/OutController.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/OutController.java index b77f314..07f2654 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/OutController.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/controllers/OutController.java @@ -5,6 +5,7 @@ import com.cowr.local.ssjygl.devicectrl.utils.LicenseJPGPair; import com.cowr.local.ssjygl.main.CliCacheData; import com.cowr.model.PostLicenseResult; import com.cowr.ssjygl.order.ordercluster.OrderclusterService; +import com.jfinal.kit.StrKit; import com.jfinal.log.Log; import java.io.IOException; @@ -15,21 +16,24 @@ public class OutController extends Controller implements Runnable { public OutController(String which, Camera camera, AbsScale scale, LEDThread led, PLC plc, int scaleWaitTime) { super(which, camera, scale, led, plc, scaleWaitTime); + + setRod1Up("rodOut1Up"); + setSensor1Reset("sensorOut1Reset"); + setSensor1("sensorOut1"); } - @Override - public String plcRod1Up() { - return "rodOut1Up"; - } + public OutController(String which, Camera camera, AbsScale scale, LEDThread led, PLC plc, int scaleWaitTime, String rod1Up, String sensor1Reset, String sensor1) { + super(which, camera, scale, led, plc, scaleWaitTime, rod1Up, sensor1Reset, sensor1); - @Override - public String plcSensor1Reset() { - return "sensorOut1Reset"; - } - - @Override - public String plcSensor1() { - return "sensorOut1"; + if (StrKit.isBlank(rod1Up)) { + setRod1Up("rodOut1Up"); + } + if (StrKit.isBlank(sensor1Reset)) { + setRod1Up("sensorOut1Reset"); + } + if (StrKit.isBlank(sensor1)) { + setRod1Up("sensorOut1"); + } } public void run() { @@ -41,7 +45,7 @@ public class OutController extends Controller implements Runnable { // 出现错位等情况时,需要重置流程,重新开始 if (getResetFlow()) { try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 + getPlc().write(getSensor1Reset()); // 重置地感状态 } catch (IOException e) { log.error(e.getMessage(), e); } @@ -67,7 +71,7 @@ public class OutController extends Controller implements Runnable { log.debug("%s 【%s】 重置流程", getWhich(), getCurrentLicense()); try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 + getPlc().write(getSensor1Reset()); // 重置地感状态 } catch (IOException e) { log.error(e.getMessage(), e); } @@ -82,225 +86,253 @@ public class OutController extends Controller implements Runnable { // 将识别的到的车牌加入服务端队列 postLicenseQueue(getWhich(), getLicenseQueue()); - PostLicenseResult postLicenseResult = postLicense(CliCacheData.SUP.getId(), pair.license, pair.file, getWhich()); - if (postLicenseResult.isUploaded()) { - if (postLicenseResult.getLicense() == null) { - log.error("%s 车牌号数据错误", getWhich()); - continue; - } + // 识别结果加入队列后,在上传之前,需要判断是否有同进同出锁 + if (getCtrllock() != null) { + if (getCtrllock().isLocked()) { + try { + while (getCtrllock().isLocked()) { + log.debug("入场流程已经开始,等待释放资源"); - // 上传成功,但是服务端认为无权限 - if (!postLicenseResult.isAccess()) { - log.debug("%s 【%s】未授权或者有未处理完的记录", getWhich(), pair.license); - Thread.sleep(10000); - continue; - } - } else { - log.debug("%s 【%s】上传失败", getWhich(), pair.license); - continue; - } - - log.debug("%s 【%s】开始流程", getWhich(), pair.license); - - setCurrentLicense(pair.license); // 设定当前流程中的车辆 - - log.debug("%s 【%s】上磅前,控制LED显示", getWhich(), pair.license); - try { - getLed().setInfo(pair.license, "等待上磅"); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,抬前闸", getWhich(), pair.license); - try { - getPlc().write(plcRod1Up()); // 抬闸 - } catch (Exception e) { - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,重置地感线圈状态", getWhich(), pair.license); - - try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】上磅,等待地感线圈状态", getWhich(), pair.license); - try { - while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 - if (getPlc().read(plcSensor1())) { // 读地感状态 - break; + Thread.sleep(10000); + } + } catch (Exception e) { + log.error(e.getMessage(), e); } } - } catch (IOException e) { - log.debug("%s 【%s】上磅,PLC连接失败", getWhich(), pair.license); - log.error(e.getMessage(), e); + + getCtrllock().lock(); + log.debug("进入 %s 流程,锁定资源", getWhich()); } - // 重置流程,这个位置要重置地感线圈状态 - if (getResetFlow()) { + try { + + PostLicenseResult postLicenseResult = postLicense(CliCacheData.SUP.getId(), pair.license, pair.file, getWhich()); + + if (postLicenseResult.isUploaded()) { + if (postLicenseResult.getLicense() == null) { + log.error("%s 车牌号数据错误", getWhich()); + continue; + } + + // 上传成功,但是服务端认为无权限 + if (!postLicenseResult.isAccess()) { + log.debug("%s 【%s】未授权或者有未处理完的记录", getWhich(), pair.license); + Thread.sleep(10000); + continue; + } + } else { + log.debug("%s 【%s】上传失败", getWhich(), pair.license); + continue; + } + + log.debug("%s 【%s】开始流程", getWhich(), pair.license); + + setCurrentLicense(pair.license); // 设定当前流程中的车辆 + + log.debug("%s 【%s】上磅前,控制LED显示", getWhich(), pair.license); try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { + getLed().setInfo(pair.license, "等待上磅"); + } catch (Exception e) { log.error(e.getMessage(), e); } - setResetFlow(false); - resetCurrentLicense(); - log.debug("%s %s 重置流程", getWhich(), pair.license); - continue; - } - log.debug("%s 【%s】上磅后,控制LED显示", getWhich(), pair.license); - try { - getLed().setInfo(pair.license, "开始称重"); - } catch (Exception e) { - log.error(e.getMessage(), e); - } + log.debug("%s 【%s】上磅,抬前闸", getWhich(), pair.license); + try { + getPlc().write(getRod1Up()); // 抬闸 + } catch (Exception e) { + log.error(e.getMessage(), e); + } - // 获取到地感线圈状态后,等待 5 秒后开始称重 - Thread.sleep(getScaleWaitTime()); - - double weight = 0; - int tryCount = 0; - while (tryCount < 10 && !getResetFlow()) { - log.debug("第 %s 次,重 %s", tryCount, weight); + log.debug("%s 【%s】上磅,重置地感线圈状态", getWhich(), pair.license); try { - if (!getScale().isConnected()) { - log.debug("%s 【%s】磅秤连接断开,重连", getWhich(), pair.license); - getScale().connect(); + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + + log.debug("%s 【%s】上磅,等待地感线圈状态", getWhich(), pair.license); + try { + while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 + if (getPlc().read(getSensor1())) { // 读地感状态 + break; + } + } + } catch (IOException e) { + log.debug("%s 【%s】上磅,PLC连接失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + + // 重置流程,这个位置要重置地感线圈状态 + if (getResetFlow()) { + try { + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + log.debug("%s %s 重置流程", getWhich(), pair.license); + continue; + } + + log.debug("%s 【%s】上磅后,控制LED显示", getWhich(), pair.license); + try { + getLed().setInfo(pair.license, "开始称重"); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + // 获取到地感线圈状态后,等待 5 秒后开始称重 + Thread.sleep(getScaleWaitTime()); + + double weight = 0; + int tryCount = 0; + while (tryCount < 10 && !getResetFlow()) { + log.debug("第 %s 次,重 %s", tryCount, weight); + + try { + if (!getScale().isConnected()) { + log.debug("%s 【%s】磅秤连接断开,重连", getWhich(), pair.license); + getScale().connect(); + } + + weight = getScale().weigh15(); + if (weight > 0) { + log.debug("%s 【%s】称重结果【%s】", getWhich(), pair.license, weight); + break; + } else { + // 读数错误,断开连接,下次读取时重连 + getScale().disconnect(); + } + } catch (Exception e) { + getScale().disconnect(); + + log.error(e.getMessage(), e); + log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); } - weight = getScale().weigh15(); - if (weight > 0) { - log.debug("%s 【%s】称重结果【%s】", getWhich(), pair.license, weight); - break; + tryCount++; + } + + // 重置流程 + if (getResetFlow()) { + try { + getPlc().write(getSensor1Reset()); // 重置地感状态 + } catch (IOException e) { + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + log.debug("%s %s 重置流程", getWhich(), pair.license); + continue; + } + + if (weight <= 0) { + log.debug("%s 【%s】称重失败【%s】", getWhich(), pair.license, weight); + } + + log.debug("%s 【%s】上磅后,控制LED显示称重结果【%s】", getWhich(), pair.license, weight); + try { + BigDecimal total_price = OrderclusterService.me.getTotalPriceByLicense(CliCacheData.SUP.getId(), pair.license, weight); + + log.debug("%s 【%s】总价 %s 元", getWhich(), pair.license, total_price); + + String weightStr; + if (weight >= 100) { + weightStr = String.format("%.1f", weight); } else { - // 读数错误,断开连接,下次读取时重连 - getScale().disconnect(); + weightStr = String.format("%.2f", weight); + } + + if (total_price == null) { + getLed().setWeightInfo(pair.license, weightStr); + } else { + // 显示总价 + getLed().setPriceInfo(pair.license, weightStr, String.format("%.2f", total_price)); } } catch (Exception e) { - getScale().disconnect(); - log.error(e.getMessage(), e); - log.debug("%s 【%s】磅秤连接失败", getWhich(), pair.license); + log.debug("%s 【%s】控制LED显示称重结果失败【%s】", getWhich(), pair.license, weight); } - tryCount++; - } - - // 重置流程 - if (getResetFlow()) { - try { - getPlc().write(plcSensor1Reset()); // 重置地感状态 - } catch (IOException e) { - log.error(e.getMessage(), e); - } - setResetFlow(false); - resetCurrentLicense(); - log.debug("%s %s 重置流程", getWhich(), pair.license); - continue; - } - - if (weight <= 0) { - log.debug("%s 【%s】称重失败【%s】", getWhich(), pair.license, weight); - } - - log.debug("%s 【%s】上磅后,控制LED显示称重结果【%s】", getWhich(), pair.license, weight); - try { - BigDecimal total_price = OrderclusterService.me.getTotalPriceByLicense(CliCacheData.SUP.getId(), pair.license, weight); - - log.debug("%s 【%s】总价 %s 元", getWhich(), pair.license, total_price); - - String weightStr; - if (weight >= 100) { - weightStr = String.format("%.1f", weight); - } else { - weightStr = String.format("%.2f", weight); - } - - if (total_price == null) { - getLed().setWeightInfo(pair.license, weightStr); - } else { - // 显示总价 - getLed().setPriceInfo(pair.license, weightStr, String.format("%.2f", total_price)); - } - } catch (Exception e) { - log.error(e.getMessage(), e); - log.debug("%s 【%s】控制LED显示称重结果失败【%s】", getWhich(), pair.license, weight); - } - - log.debug("%s 【%s】称重后,上传重量【%s】", getWhich(), pair.license, weight); - tryCount = 0; - Boolean weightPosted = null; - while (tryCount < 10) { - tryCount++; - weightPosted = postWeight(CliCacheData.SUP.getId(), pair.license, weight, getWhich()); - if (weightPosted != null && weightPosted) { - break; - } - Thread.sleep(500); - } - - if (weightPosted != null && weightPosted) { - // 出场时,称完重量后,就等待用户结算,然后发送抬杆指令,这里不自动抬杆 - log.debug("%s 【%s】等待结算", getWhich(), pair.license); - } else { - if (weightPosted == null) { - log.debug("%s 【%s】上传车重失败", getWhich(), pair.license); - continue; - } else { - log.debug("%s 【%s】上传车重数据不正确", getWhich(), pair.license); - } - } - - try { - getPlc().write("sensorOut2Reset"); - } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } - - log.debug("%s 【%s】下磅,等待地感线圈状态", getWhich(), pair.license); - try { - while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 - if (getPlc().read("sensorOut2")) { + log.debug("%s 【%s】称重后,上传重量【%s】", getWhich(), pair.license, weight); + tryCount = 0; + Boolean weightPosted = null; + while (tryCount < 10) { + tryCount++; + weightPosted = postWeight(CliCacheData.SUP.getId(), pair.license, weight, getWhich()); + if (weightPosted != null && weightPosted) { break; } + Thread.sleep(500); + } + + if (weightPosted != null && weightPosted) { + // 出场时,称完重量后,就等待用户结算,然后发送抬杆指令,这里不自动抬杆 + log.debug("%s 【%s】等待结算", getWhich(), pair.license); + } else { + if (weightPosted == null) { + log.debug("%s 【%s】上传车重失败", getWhich(), pair.license); + continue; + } else { + log.debug("%s 【%s】上传车重数据不正确", getWhich(), pair.license); + } } - } catch (IOException e) { - log.debug("%s 【%s】下磅,PLC连接失败", getWhich(), pair.license); - log.error(e.getMessage(), e); - } - if (getResetFlow()) { try { getPlc().write("sensorOut2Reset"); } catch (IOException e) { log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); log.error(e.getMessage(), e); } - setResetFlow(false); - resetCurrentLicense(); - continue; - } - resetCurrentLicense(); - log.debug("%s 【%s】流程处理完毕", getWhich(), pair.license); - - if (getResetFlow()) { + log.debug("%s 【%s】下磅,等待地感线圈状态", getWhich(), pair.license); try { - getPlc().write("sensorOut2Reset"); + while (!getResetFlow()) { // 流程没有被重置,就一直等待地感线圈状态 + if (getPlc().read("sensorOut2")) { + break; + } + } } catch (IOException e) { - log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.debug("%s 【%s】下磅,PLC连接失败", getWhich(), pair.license); log.error(e.getMessage(), e); } - setResetFlow(false); + + if (getResetFlow()) { + try { + getPlc().write("sensorOut2Reset"); + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + continue; + } + resetCurrentLicense(); - continue; + log.debug("%s 【%s】流程处理完毕", getWhich(), pair.license); + + if (getResetFlow()) { + try { + getPlc().write("sensorOut2Reset"); + } catch (IOException e) { + log.debug("%s 【%s】 重置地感线圈状态失败", getWhich(), pair.license); + log.error(e.getMessage(), e); + } + setResetFlow(false); + resetCurrentLicense(); + continue; + } + } finally { + if (getCtrllock() != null) { + getCtrllock().unlock(); + + log.debug("完成 %s 流程,释放资源", getWhich()); + } } } catch (Exception e) { log.error(e.getMessage(), e); diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/DingsongScale.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/DingsongScale.java new file mode 100644 index 0000000..e42b681 --- /dev/null +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/DingsongScale.java @@ -0,0 +1,138 @@ +package com.cowr.local.ssjygl.devicectrl.device; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; + +/** + * 柯力称重显示器 + */ +public class DingsongScale extends AbsScale { + private static final int DATA_LENGTH = 24; + private static final int DATA_FROM = 4; + private static final int DATA_TO = 10; + + public DingsongScale(String id, String ip, int port) { + super(id, ip, port); + } + + @Override + public double weigh() throws IOException { + InputStream inputStream = socket.getInputStream(); + byte[] clearBuf = new byte[inputStream.available()]; + inputStream.read(clearBuf); + int errCount = 0; + + String weight = "0.0"; + while (true) { + if (errCount >= 10) { + break; + } + + errCount++; + + byte[] buf = new byte[DATA_LENGTH]; + int read = inputStream.read(buf); + + if (read != DATA_LENGTH) { + continue; + } + + weight = new String(Arrays.copyOfRange(buf, DATA_FROM, DATA_TO)); + break; + } + + return Double.parseDouble(weight) / 1000d; + } + + @Override + public double weigh15() throws IOException { + ArrayList list = new ArrayList<>(); + AtomicBoolean run = new AtomicBoolean(true); + + InputStream inputStream = socket.getInputStream(); + byte[] clearBuf = new byte[inputStream.available()]; + inputStream.read(clearBuf); + int errCount = 0; + + while (run.get() && list.size() < 15) { + long st = System.currentTimeMillis(); + + byte[] buf = new byte[DATA_LENGTH]; + int read = inputStream.read(buf); + + if (read != DATA_LENGTH && errCount < 10) { + errCount++; + continue; + } + + String weight = new String(Arrays.copyOfRange(buf, DATA_FROM, DATA_TO)); + + if (!pattern.matcher(weight).matches() || weight.equals("000000")) { +// System.out.println("error:" + weight); + + if (errCount == 100) { + break; + } + errCount++; + continue; + } + + try { + if (Long.parseLong(weight) > 100000) { + errCount++; + continue; + } + } catch (Exception e) { + e.printStackTrace(); + } + +// System.out.println("读一次耗时:" + (System.currentTimeMillis() - st) + ", " + Double.parseDouble(flip(weight))); + list.add(weight); + } + + String max = "0"; + if (list.size() > 0) { + Map collect = list.stream().collect( + Collectors.groupingBy(num -> num, Collectors.counting())); + max = collect.entrySet().stream().max(Comparator.comparingLong(Map.Entry::getValue)).get().getKey(); + } + + return Double.parseDouble(max) / 1000d; + } + + + public static void main(String[] args) { + DingsongScale s = new DingsongScale("scale", "192.168.20.30", 10001); + + try { + boolean connect = s.connect(); + System.out.println("connect " + connect); + } catch (IOException e) { + e.printStackTrace(); + } + + long st = System.currentTimeMillis(); + + for (int i = 0; i < 100; i++) { + try { + st = System.currentTimeMillis(); + double weigh = s.weigh15(); + System.out.println("end:" + weigh + ", tm: " + (System.currentTimeMillis() - st)); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } +// break; + } + } +} diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/LEDThread.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/LEDThread.java index f945635..6888911 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/LEDThread.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/LEDThread.java @@ -346,7 +346,7 @@ public class LEDThread extends Device implements Runnable { public static void main(String[] args) { try { - LEDThread led = new LEDThread("", "192.168.20.50", 5005); + LEDThread led = new LEDThread("", "192.168.20.52", 5005); Thread thread = new Thread(led); thread.setDaemon(true); thread.start(); diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/PLC.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/PLC.java index 6ec2d4e..80a8947 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/PLC.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/devicectrl/device/PLC.java @@ -275,11 +275,11 @@ public class PLC extends Device { public static void main(String[] args) { try { - PLC plc = new PLC("_plc", "192.168.20.21", 502, com.cowr.local.ssjygl.devicectrl.common.DeviceThread.defaultAddressTable); + PLC plc = new PLC("_plc", "192.168.20.20", 502, com.cowr.local.ssjygl.devicectrl.common.DeviceThread.defaultAddressTable); plc.connect(); plc.isConnected(); // plc.write("rodIn1Down"); - String cmd = "rodOut2Down"; + String cmd = "rodIn2Down"; plc.write(cmd); // plc.write("sensorIn2Reset"); diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/main/Config.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/main/Config.java index 6574d9a..664d035 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/main/Config.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/main/Config.java @@ -95,7 +95,7 @@ public class Config extends JFinalConfig { public static DeviceThread deviceThread = new DeviceThread(); public static SocketIOService socketio = null; private static boolean client_run = true; - public static final String CLINET_VERSION = "20201207"; + public static final String CLINET_VERSION = "20201218"; public static String getRootPath() { return PathKit.getWebRootPath() diff --git a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/transport/TransportDeviceService.java b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/transport/TransportDeviceService.java index 007862a..8003ec9 100644 --- a/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/transport/TransportDeviceService.java +++ b/ssjygl-xsx-local/src/main/java/com/cowr/local/ssjygl/transport/TransportDeviceService.java @@ -286,7 +286,7 @@ public class TransportDeviceService { return Result.failed("记录已完成或者已取消"); } - Double weight = null; + Double weight = null; int tryCount = 0; // 称重失败,在称一次 @@ -301,9 +301,9 @@ public class TransportDeviceService { Config.deviceThread.reconnScale(which); Thread.sleep(500); - }catch (Exception e){ + } catch (Exception e) { log.error(e.getMessage(), e); - }finally { + } finally { tryCount++; log.debug("%s 第 %s 次,重 %s", transport.getTruckLicense(), tryCount, weight); diff --git a/ssjygl-xsx-local/src/main/resources/dev/config.properties b/ssjygl-xsx-local/src/main/resources/dev/config.properties index 1012b37..b123ade 100644 --- a/ssjygl-xsx-local/src/main/resources/dev/config.properties +++ b/ssjygl-xsx-local/src/main/resources/dev/config.properties @@ -15,7 +15,7 @@ socketserver.port=21002 socketio.port=12002 #当前部署本地程序的砂站id -current.supermarket_id=7 +current.supermarket_id=9 #落杆后,等待上磅的时间 default_scale_wait_time=8000 diff --git a/ssjygl-xsx-local/src/main/resources/prod/config.properties b/ssjygl-xsx-local/src/main/resources/prod/config.properties index f79c500..a9f3b45 100644 --- a/ssjygl-xsx-local/src/main/resources/prod/config.properties +++ b/ssjygl-xsx-local/src/main/resources/prod/config.properties @@ -15,7 +15,7 @@ socketserver.port=21002 socketio.port=12002 #当前部署本地程序的砂站id -current.supermarket_id=1 +current.supermarket_id=9 #落杆后,等待上磅的时间 default_scale_wait_time=8000