9 changed files with 233 additions and 25 deletions
-
9src/main/java/com/dreamworks/boditech/controller/TmpController.java
-
7src/main/java/com/dreamworks/boditech/driver/Device.java
-
84src/main/java/com/dreamworks/boditech/driver/DeviceLisService.java
-
19src/main/java/com/dreamworks/boditech/driver/connection/ComLisClient.java
-
7src/main/java/com/dreamworks/boditech/driver/connection/ComLisConnection.java
-
114src/main/java/com/dreamworks/boditech/driver/connection/ComLisSerialPort.java
-
8src/main/java/com/dreamworks/boditech/driver/entity/LisTestInfo.java
-
9src/main/java/com/dreamworks/boditech/service/RuntimeOptionService.java
-
1src/main/java/com/dreamworks/boditech/utils/AppError.java
@ -0,0 +1,84 @@ |
|||
package com.dreamworks.boditech.driver; |
|||
import com.dreamworks.boditech.driver.connection.ClientRequest; |
|||
import com.dreamworks.boditech.driver.connection.ComLisConnection; |
|||
import com.dreamworks.boditech.driver.connection.ComLisSerialPort; |
|||
import com.dreamworks.boditech.driver.entity.LisTestInfo; |
|||
import com.dreamworks.boditech.service.RuntimeOptionService; |
|||
import com.dreamworks.boditech.service.RuntimeVariableService; |
|||
import jakarta.annotation.PostConstruct; |
|||
import jakarta.annotation.Resource; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.nio.ByteBuffer; |
|||
import java.nio.ByteOrder; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@Component |
|||
public class DeviceLisService { |
|||
@Resource |
|||
private RuntimeOptionService options; |
|||
|
|||
// connection |
|||
private ComLisConnection connection; |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
String conType = this.options.getString("app.lis.connectionType"); |
|||
if ("SerialPort".equals(conType)) { |
|||
ComLisSerialPort con = new ComLisSerialPort(); |
|||
con.path = this.options.getString("app.lis.serialport.path"); |
|||
con.baudrate = this.options.getInteger("app.lis.serialport.baudrate"); |
|||
con.connect(); |
|||
this.connection = con; |
|||
} else { |
|||
throw new RuntimeException("not support connection type"); |
|||
} |
|||
} |
|||
|
|||
// request test info |
|||
public LisTestInfo requestTestInfo( String barCode ) { |
|||
List<String> bodyItems = new ArrayList<>(); |
|||
bodyItems.add("Q"); |
|||
bodyItems.add("A8000"); |
|||
bodyItems.add("^" + barCode + "\r"); |
|||
String body = String.join("|", bodyItems); |
|||
byte[] bodyBytes = body.getBytes(); |
|||
int bodySum = 0; |
|||
for ( byte b : bodyBytes ) { |
|||
bodySum += b; |
|||
} |
|||
bodySum = bodySum % 256; |
|||
|
|||
ByteBuffer parameter = ByteBuffer.allocate(body.length() + 7); |
|||
parameter.order(ByteOrder.BIG_ENDIAN); |
|||
parameter.put((byte)0x02); // <STX> |
|||
parameter.put((byte)0x01); // FN |
|||
parameter.put(bodyBytes); // <Record> |
|||
parameter.put((byte)0x03); // <ETX> |
|||
parameter.putShort((short)bodySum); // validate |
|||
parameter.put((byte)0x0D); // <CR> |
|||
parameter.put((byte)0x0A); // <LF> |
|||
ByteBuffer response = this.connection.call(parameter); |
|||
|
|||
short validate = response.getShort(response.limit() - 3); |
|||
// @TODO : check validate |
|||
// @TODO : 这里分割的还不对 ~~~ 后面再写 ~~~ |
|||
byte[] responseBytes = new byte[response.limit() - 7]; |
|||
response.get(responseBytes,2, responseBytes.length - 4); |
|||
String responseBody = new String(responseBytes); |
|||
String[] responseItems = responseBody.split("\\|"); |
|||
LisTestInfo testInfo = new LisTestInfo(); |
|||
testInfo.deviceName = responseItems[1]; |
|||
testInfo.sampleId = responseItems[2]; |
|||
testInfo.sampleType = responseItems[5]; |
|||
testInfo.sampleTime = responseItems[7].trim(); |
|||
testInfo.projectNames = new ArrayList<>(); |
|||
String[] projectNames = responseItems[4].split("\\\\"); |
|||
for ( String projectName : projectNames ) { |
|||
projectName = projectName.replace("^", ""); |
|||
testInfo.projectNames.add(projectName); |
|||
} |
|||
return testInfo; |
|||
} |
|||
} |
@ -1,19 +0,0 @@ |
|||
package com.dreamworks.boditech.driver.connection; |
|||
import com.dreamworks.boditech.driver.entity.LisTestInfo; |
|||
import org.springframework.stereotype.Component; |
|||
import java.util.ArrayList; |
|||
@Component |
|||
public class ComLisClient { |
|||
// request test info |
|||
public LisTestInfo requestTestInfo( String barCode ) { |
|||
if ( barCode.isEmpty() ) { |
|||
return null; |
|||
} |
|||
|
|||
LisTestInfo testInfo = new LisTestInfo(); |
|||
testInfo.sampleType = "WB"; |
|||
testInfo.projectNames = new ArrayList<>(); |
|||
testInfo.projectNames.add("hsCRP"); |
|||
return testInfo; |
|||
} |
|||
} |
@ -0,0 +1,7 @@ |
|||
package com.dreamworks.boditech.driver.connection; |
|||
import java.nio.ByteBuffer; |
|||
public interface ComLisConnection { |
|||
void connect(); |
|||
ByteBuffer call(ByteBuffer request); |
|||
void write(ByteBuffer data); |
|||
} |
@ -0,0 +1,114 @@ |
|||
package com.dreamworks.boditech.driver.connection; |
|||
import com.dreamworks.boditech.utils.AppError; |
|||
import com.dreamworks.boditech.utils.AppRuntimeException; |
|||
import com.fazecast.jSerialComm.SerialPort; |
|||
import com.fazecast.jSerialComm.SerialPortEvent; |
|||
import com.fazecast.jSerialComm.SerialPortMessageListener; |
|||
import java.nio.ByteBuffer; |
|||
import java.util.Timer; |
|||
import java.util.TimerTask; |
|||
public class ComLisSerialPort implements ComLisConnection { |
|||
// serial port path |
|||
public String path; |
|||
// serial port baud rate |
|||
public Integer baudrate; |
|||
// serial port connection |
|||
private SerialPort port; |
|||
// request lock |
|||
private final Object requestLock = new Object(); |
|||
// request |
|||
private ClientRequest request; |
|||
|
|||
@Override |
|||
public void connect() { |
|||
this.port = SerialPort.getCommPort(this.path); |
|||
this.port.openPort(); |
|||
if ( !this.port.isOpen() ) { |
|||
throw new AppRuntimeException(AppError.DEVICE_LIS_SERIAL_PORT_OPEN_FAILED); |
|||
} |
|||
this.port.setBaudRate(this.baudrate); |
|||
|
|||
this.port.addDataListener(new SerialPortMessageListener() { |
|||
@Override |
|||
public byte[] getMessageDelimiter() { |
|||
return new byte[] { (byte)0x0D, (byte)0x0A }; |
|||
} |
|||
|
|||
@Override |
|||
public boolean delimiterIndicatesEndOfMessage() { |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public int getListeningEvents() { |
|||
return SerialPort.LISTENING_EVENT_DATA_RECEIVED; |
|||
} |
|||
|
|||
@Override |
|||
public void serialEvent(SerialPortEvent serialPortEvent) { |
|||
ComLisSerialPort.this.handleOnData(serialPortEvent); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
// handle on data |
|||
public void handleOnData(SerialPortEvent event) { |
|||
byte[] buffer = event.getReceivedData(); |
|||
ByteBuffer data = ByteBuffer.wrap(buffer, 0, buffer.length); |
|||
if ( null != this.request ) { |
|||
this.request.isResponseReceived = true; |
|||
this.request.response = data; |
|||
synchronized (this.requestLock) { |
|||
this.requestLock.notify(); |
|||
} |
|||
} { |
|||
throw new RuntimeException("Received data from lis server but no request"); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ByteBuffer call(ByteBuffer buffer) { |
|||
this.request = new ClientRequest(); |
|||
this.request.parameter = buffer; |
|||
|
|||
// timeout check |
|||
// TimerTask timerTask = new TimerTask() { |
|||
// @Override |
|||
// public void run() { |
|||
// synchronized (ComLisSerialPort.this.requestLock) { |
|||
// if ( ComLisSerialPort.this.request.isResponseReceived ) { |
|||
// return ; |
|||
// } |
|||
// ComLisSerialPort.this.request.response = null; |
|||
// ComLisSerialPort.this.request.errorCode = ClientRequest.ERROR_CODE_TIMEOUT; |
|||
// ComLisSerialPort.this.requestLock.notify(); |
|||
// } |
|||
// } |
|||
// }; |
|||
// Timer timer = new Timer(); |
|||
// timer.schedule(timerTask, 5000); |
|||
|
|||
synchronized (this.requestLock) { |
|||
this.write(request.parameter); |
|||
try { |
|||
this.requestLock.wait(); |
|||
} catch (InterruptedException e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
|
|||
if ( 0 != request.errorCode ) { |
|||
throw new RuntimeException("device error : " + request.errorCode); |
|||
} |
|||
|
|||
ByteBuffer response = request.response; |
|||
this.request = null; |
|||
return response; |
|||
} |
|||
|
|||
@Override |
|||
public void write(ByteBuffer data) { |
|||
byte[] bytes = data.array(); |
|||
this.port.writeBytes(bytes, bytes.length); |
|||
} |
|||
} |
@ -1,6 +1,14 @@ |
|||
package com.dreamworks.boditech.driver.entity; |
|||
import java.util.List; |
|||
public class LisTestInfo { |
|||
// device name |
|||
public String deviceName; |
|||
// sample id |
|||
public String sampleId; |
|||
// sample name |
|||
public List<String> projectNames; |
|||
// sample type |
|||
public String sampleType; |
|||
// sample time |
|||
public String sampleTime; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue