lisai17@sina.com 2020-10-21 11:11:37 +08:00
parent 8a53bdfcbf
commit 371583720b
7 changed files with 92 additions and 94 deletions

View File

@ -51,7 +51,7 @@ public class LEDThread extends Device implements Runnable {
} }
}; };
private ReentrantLock lock = new ReentrantLock(); private ReentrantLock lock = new ReentrantLock();
private ReentrantLock connlock = new ReentrantLock(); private ReentrantLock connlock = new ReentrantLock();
protected Socket socket; protected Socket socket;
@ -92,85 +92,83 @@ public class LEDThread extends Device implements Runnable {
} }
public void screen() { public void screen() {
synchronized (lock) { lock.lock();
lock.lock(); try {
try { if (this.license == null) {
if (this.license == null) { return;
}
// 重连一次
if (!isConnected()) {
connect();
// 重连一次后还是连不上,就等下次连上再发送内容
if (!isConnected()) {
log.error("LED %s %s 连接失败", getId(), getIp());
return; return;
} }
}
// 重连一次 String date = sdf.get().format(new Date());
if (!isConnected()) { String dataStr = String.format(this.format, date, this.license, this.text);
connect(); byte[] data = dataStr.getBytes(Charset.forName("gb2312"));
int dataLen = data.length;
// 重连一次后还是连不上,就等下次连上再发送内容 byte[] msg = new byte[frameHead.length + packetHead.length + b.length + dataLen];
if (!isConnected()) { System.arraycopy(frameHead, 0, msg, 0, frameHead.length);
log.error("LED %s %s 连接失败", getId(), getIp());
return;
}
}
String date = sdf.get().format(new Date());
String dataStr = String.format(this.format, date, this.license, this.text);
byte[] data = dataStr.getBytes(Charset.forName("gb2312"));
int dataLen = data.length;
byte[] msg = new byte[frameHead.length + packetHead.length + b.length + dataLen];
System.arraycopy(frameHead, 0, msg, 0, frameHead.length);
// System.out.println(HexDump.toHex(msg)); // System.out.println(HexDump.toHex(msg));
packetHead[packetHead.length - 2] = (byte) (b.length + dataLen); packetHead[packetHead.length - 2] = (byte) (b.length + dataLen);
System.arraycopy(packetHead, 0, msg, frameHead.length, packetHead.length); System.arraycopy(packetHead, 0, msg, frameHead.length, packetHead.length);
// System.out.println(HexDump.toHex(msg)); // System.out.println(HexDump.toHex(msg));
int areaLen = b.length - 9 + dataLen; int areaLen = b.length - 9 + dataLen;
b[7] = (byte) areaLen; b[7] = (byte) areaLen;
b[32] = (byte) dataLen; b[32] = (byte) dataLen;
System.arraycopy(b, 0, msg, packetHead.length + frameHead.length, b.length); System.arraycopy(b, 0, msg, packetHead.length + frameHead.length, b.length);
// System.out.println(HexDump.toHex(msg)); // System.out.println(HexDump.toHex(msg));
System.arraycopy(data, 0, msg, packetHead.length + frameHead.length + b.length, dataLen); System.arraycopy(data, 0, msg, packetHead.length + frameHead.length + b.length, dataLen);
// System.out.println(HexDump.toHex(msg)); // System.out.println(HexDump.toHex(msg));
byte[] toCrc = new byte[msg.length - 8]; byte[] toCrc = new byte[msg.length - 8];
System.arraycopy(msg, 8, toCrc, 0, toCrc.length); System.arraycopy(msg, 8, toCrc, 0, toCrc.length);
int crc = CalcCRC(toCrc, toCrc.length); int crc = CalcCRC(toCrc, toCrc.length);
byte[] msgCrc = new byte[msg.length + 3]; byte[] msgCrc = new byte[msg.length + 3];
System.arraycopy(msg, 0, msgCrc, 0, msg.length); System.arraycopy(msg, 0, msgCrc, 0, msg.length);
msgCrc[msgCrc.length - 2] = (byte) (crc >> 8); msgCrc[msgCrc.length - 2] = (byte) (crc >> 8);
msgCrc[msgCrc.length - 3] = (byte) crc; msgCrc[msgCrc.length - 3] = (byte) crc;
msgCrc[msgCrc.length - 1] = frameTail; msgCrc[msgCrc.length - 1] = frameTail;
// System.out.println(getId() + " LED 发送数据:" + HexDump.toHex(msgCrc)); // System.out.println(getId() + " LED 发送数据:" + HexDump.toHex(msgCrc));
socket.getOutputStream().write(msgCrc); socket.getOutputStream().write(msgCrc);
socket.getOutputStream().flush(); socket.getOutputStream().flush();
InputStream in = socket.getInputStream(); InputStream in = socket.getInputStream();
byte[] buf = new byte[in.available()]; byte[] buf = new byte[in.available()];
in.read(buf); in.read(buf);
// LED 返回 // LED 返回
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] y = new byte[128]; byte[] y = new byte[128];
int read = bis.read(y); int read = bis.read(y);
while (!(read > 0)) { while (!(read > 0)) {
read = bis.read(y); read = bis.read(y);
} }
byte[] z = new byte[read]; byte[] z = new byte[read];
System.arraycopy(y, 0, z, 0, read); System.arraycopy(y, 0, z, 0, read);
// System.out.println(getId() + " LED 返回数据:" + HexDump.toHex(z)); // System.out.println(getId() + " LED 返回数据:" + HexDump.toHex(z));
// socket.close(); // socket.close();
} catch (Exception e) { } catch (Exception e) {
if (e.getMessage().contains("Socket is not connected")) { if (e.getMessage().contains("Socket is not connected")) {
log.error("LED %s %s 连接已断开", getId(), getIp()); log.error("LED %s %s 连接已断开", getId(), getIp());
}
if (e.getMessage().contains("Read timed out")) {
log.error("LED %s %s 连接超时", getId(), getIp());
} else {
log.error(e.getMessage(), e);
}
reconn();
} finally {
lock.unlock();
} }
if (e.getMessage().contains("Read timed out")) {
log.error("LED %s %s 连接超时", getId(), getIp());
} else {
log.error(e.getMessage(), e);
}
reconn();
} finally {
lock.unlock();
} }
} }
@ -289,29 +287,32 @@ public class LEDThread extends Device implements Runnable {
} }
public boolean reconn() { public boolean reconn() {
connlock.lock();
try { try {
log.debug("销毁LED连接"); try {
if (socket != null) { log.debug("销毁LED连接");
socket.close(); if (socket != null) {
} socket.close();
}
socket = null; socket = null;
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e);
}
try {
socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(new InetSocketAddress(getIp(), getPort()), 5000);
} catch (Exception e) {
if (e.getMessage().contains("connect timed out")) {
log.error("LED %s %s 连接超时", getId(), getIp());
} else {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
try {
socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(new InetSocketAddress(getIp(), getPort()), 5000);
} catch (Exception e) {
if (e.getMessage().contains("connect timed out")) {
log.error("LED %s %s 连接超时", getId(), getIp());
} else {
log.error(e.getMessage(), e);
}
}
} finally { } finally {
lock.unlock(); connlock.unlock();
} }
return isConnected(); return isConnected();

View File

@ -92,7 +92,7 @@ public class Config extends JFinalConfig {
public static DeviceThread deviceThread = new DeviceThread(); public static DeviceThread deviceThread = new DeviceThread();
public static SocketIOService socketio = null; public static SocketIOService socketio = null;
private static boolean client_run = true; private static boolean client_run = true;
public static final String CLINET_VERSION = "20201019"; public static final String CLINET_VERSION = "20201021";
public static String getRootPath() { public static String getRootPath() {
return PathKit.getWebRootPath() return PathKit.getWebRootPath()

View File

@ -349,9 +349,13 @@ public class OrderTempSyncService {
order.setProductId(ordercluster.getProductId()); order.setProductId(ordercluster.getProductId());
order.setProductName(ordercluster.getProductName()); order.setProductName(ordercluster.getProductName());
BigDecimal up = null; BigDecimal up = ordercluster.getUnitPrice(); // 集团订单是每天创建的,可以使用集团订单中确定的单价
// 客户信息 if (up == null) {
return Result.failedstr("未查到有效的单价信息");
}
// 客户信息
Customer customer = Customer.dao.findById(ordercluster.getCustomerId()); Customer customer = Customer.dao.findById(ordercluster.getCustomerId());
if (customer != null) { if (customer != null) {
order.setCustomerId(customer.getId()); order.setCustomerId(customer.getId());
@ -362,8 +366,6 @@ public class OrderTempSyncService {
order.setCustomerPhone(customer.getPhone()); order.setCustomerPhone(customer.getPhone());
order.setCustomerTexpayerName(customer.getTexpayerName()); order.setCustomerTexpayerName(customer.getTexpayerName());
order.setCustomerTexpayerNum(customer.getTexpayerNum()); order.setCustomerTexpayerNum(customer.getTexpayerNum());
up = CustomerSupermarketProductService.me.getUnitPrice(customer.getId(), transport.getSupermarketId(), ordercluster.getProductId());
} else { } else {
// 可能是零散购砂客户,没有对应的客户信息记录,但是有客户名称之类的信息 // 可能是零散购砂客户,没有对应的客户信息记录,但是有客户名称之类的信息
order.setCustomerId(ordercluster.getCustomerId()); order.setCustomerId(ordercluster.getCustomerId());
@ -374,12 +376,6 @@ public class OrderTempSyncService {
order.setCustomerPhone(ordercluster.getCustomerPhone()); order.setCustomerPhone(ordercluster.getCustomerPhone());
order.setCustomerTexpayerName(ordercluster.getCustomerTexpayerName()); order.setCustomerTexpayerName(ordercluster.getCustomerTexpayerName());
order.setCustomerTexpayerNum(ordercluster.getCustomerTexpayerNum()); order.setCustomerTexpayerNum(ordercluster.getCustomerTexpayerNum());
up = SupermarketProductService.me.getUnitPrice(transport.getSupermarketId(), ordercluster.getProductId());
}
if (up == null) {
return Result.failedstr("未查到有效的单价信息");
} }
// 支付信息 // 支付信息
@ -677,7 +673,8 @@ public class OrderTempSyncService {
transport.setTransCoTexpayerName(ordercluster.getTransCoTexpayerName()); transport.setTransCoTexpayerName(ordercluster.getTransCoTexpayerName());
transport.setTransCoTexpayerNum(ordercluster.getTransCoTexpayerNum()); transport.setTransCoTexpayerNum(ordercluster.getTransCoTexpayerNum());
BigDecimal up = CustomerSupermarketProductService.me.getUnitPrice(customer.getId(), transport.getSupermarketId(), product.getId()); // BigDecimal up = CustomerSupermarketProductService.me.getUnitPrice(customer.getId(), transport.getSupermarketId(), product.getId());
BigDecimal up = ordercluster.getUnitPrice(); // 集团订单是每天创建的,可以使用集团订单中确定的单价
if (up == null) { if (up == null) {
return Result.failedstr("未查到有效的单价信息"); return Result.failedstr("未查到有效的单价信息");