Commit d5f851dd sunke

20031接口

1 个父辈 9838bde6
...@@ -6,19 +6,23 @@ import com.neotel.smfcore.common.exception.ApiException; ...@@ -6,19 +6,23 @@ import com.neotel.smfcore.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import lombok.val; import lombok.val;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.util.Strings; import org.apache.logging.log4j.util.Strings;
import org.springframework.util.CollectionUtils;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
...@@ -35,6 +39,12 @@ public class HttpHelper { ...@@ -35,6 +39,12 @@ public class HttpHelper {
// 编码方式 // 编码方式
private static final String CONTENT_CHARSET = "UTF-8"; private static final String CONTENT_CHARSET = "UTF-8";
// 连接超时时间
private static final int CONNECTION_TIMEOUT = 10000;
// 读数据超时时间
private static final int READ_DATA_TIMEOUT = 10000;
public static String postJson(String url, Map<String, Object> params) throws ApiException { public static String postJson(String url, Map<String, Object> params) throws ApiException {
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
...@@ -53,7 +63,6 @@ public class HttpHelper { ...@@ -53,7 +63,6 @@ public class HttpHelper {
try{ try{
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost); CloseableHttpResponse response = httpClient.execute(httpPost);
//System.out.println(response.getStatusLine().getStatusCode() + "\n");
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, CONTENT_CHARSET); String responseContent = EntityUtils.toString(entity, CONTENT_CHARSET);
response.close(); response.close();
...@@ -72,29 +81,23 @@ public class HttpHelper { ...@@ -72,29 +81,23 @@ public class HttpHelper {
* @return 响应结果 * @return 响应结果
*/ */
public static String postParam(String url, Map<String, Object> paramMap) throws ApiException { public static String postParam(String url, Map<String, Object> paramMap) throws ApiException {
// 设置请求参数 String contentType = "application/json;charset=utf-8";
List<NameValuePair> params=new ArrayList<NameValuePair>(); return postParam(url, paramMap, contentType);
if (paramMap != null && !paramMap.isEmpty()) {
//建立一个NameValuePair数组,用于存储欲传送的参数
for (Entry<String, Object> entry : paramMap.entrySet()) {
String value = "";
Object valueObj = entry.getValue();
if(valueObj != null){
if(valueObj instanceof Date){
value = DateUtil.toDateString((Date)valueObj,"yyyyMMdd");
}else{
value = valueObj.toString();
}
}
params.add(new BasicNameValuePair(entry.getKey(),value));
} }
public static String postFormParam(String url, Map<String, Object> paramMap) throws ApiException {
String contentType = "application/x-www-form-urlencoded";
return postParam(url, paramMap, contentType);
} }
try {
public static String postParam(String url, Map<String, Object> paramMap, String contentType) throws ApiException {
// 设置请求参数
try {
List<NameValuePair> params=toNameValuePair(paramMap);
URI uri = new URIBuilder(url).setParameters(params).build(); URI uri = new URIBuilder(url).setParameters(params).build();
log.info("执行MES请求:"+uri.toString()); log.info("执行请求:"+uri.toString());
HttpPost httpPost = new HttpPost(uri); HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); httpPost.addHeader("Content-Type", contentType);
// httpPost.setEntity(new UrlEncodedFormEntity(params, CONTENT_CHARSET)); // httpPost.setEntity(new UrlEncodedFormEntity(params, CONTENT_CHARSET));
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost); CloseableHttpResponse response = httpClient.execute(httpPost);
...@@ -106,7 +109,26 @@ public class HttpHelper { ...@@ -106,7 +109,26 @@ public class HttpHelper {
} catch (Exception e) { } catch (Exception e) {
throw new ApiException("Request params to [" + url + "] failed:" + e.getMessage()); throw new ApiException("Request params to [" + url + "] failed:" + e.getMessage());
} }
}
private static List<NameValuePair> toNameValuePair(Map<String, Object> paramMap){
List<NameValuePair> params=new ArrayList<NameValuePair>();
if (paramMap != null && !paramMap.isEmpty()) {
//建立一个NameValuePair数组,用于存储欲传送的参数
for (Entry<String, Object> entry : paramMap.entrySet()) {
String value = "";
Object valueObj = entry.getValue();
if(valueObj != null){
if(valueObj instanceof Date){
value = DateUtil.toDateString((Date)valueObj,"yyyyMMdd");
}else{
value = valueObj.toString();
}
}
params.add(new BasicNameValuePair(entry.getKey(),value));
}
}
return params;
} }
......
...@@ -95,8 +95,6 @@ public class BarcodeRule { ...@@ -95,8 +95,6 @@ public class BarcodeRule {
} }
public static BarcodeRule newRule(String ruleStr){ public static BarcodeRule newRule(String ruleStr){
BarcodeRule newRule = new BarcodeRule(); BarcodeRule newRule = new BarcodeRule();
newRule.ruleStr = ruleStr; newRule.ruleStr = ruleStr;
...@@ -428,6 +426,33 @@ public class BarcodeRule { ...@@ -428,6 +426,33 @@ public class BarcodeRule {
return -1; return -1;
} }
/**
* 从字符串中获取连续的最大整数, 如7460S54Q3250将返回7460
*/
private int getMaxIntValue(String[] codeArr){
String valueStr = getStrValue(codeArr);
if(!Strings.isNullOrEmpty(valueStr)){
Integer maxInt = 0;
String tempMaxDigitStr = "";
for (int i = 0; i < valueStr.length(); i++) {
char c = valueStr.charAt(i);
if(Character.isDigit(c)){
tempMaxDigitStr = tempMaxDigitStr + c;
if(tempMaxDigitStr.length() >= maxInt.toString().length()){
int tempInt = Integer.valueOf(tempMaxDigitStr);
if(tempInt > maxInt){
maxInt = tempInt;
}
}
}else{
tempMaxDigitStr = "";
}
}
return maxInt;
}
return -1;
}
private Date getDateValue(String[] codeArr){ private Date getDateValue(String[] codeArr){
String value = getStrValue(codeArr); String value = getStrValue(codeArr);
if(!Strings.isNullOrEmpty(value)){ if(!Strings.isNullOrEmpty(value)){
...@@ -615,7 +640,7 @@ public class BarcodeRule { ...@@ -615,7 +640,7 @@ public class BarcodeRule {
} }
int quantity = 1; int quantity = 1;
if(quantity_item.hasThisField()){ if(quantity_item.hasThisField()){
quantity = quantity_item.getIntValue(codeArr); quantity = quantity_item.getMaxIntValue(codeArr);
if(quantity == -1){ if(quantity == -1){
log.info("QTY解析失败"); log.info("QTY解析失败");
codeBean.setError("smfcore.error.barcode.noField",new String[]{"QTY"},"QTY解析失败"); codeBean.setError("smfcore.error.barcode.noField",new String[]{"QTY"},"QTY解析失败");
...@@ -831,11 +856,10 @@ public class BarcodeRule { ...@@ -831,11 +856,10 @@ public class BarcodeRule {
// codeStr = "=1+0x0-13x12=SU100PM0JPR"; // codeStr = "=1+0x0-13x12=SU100PM0JPR";
// rule = "PN[-1_SU:2:-1][RI]"; // rule = "PN[-1_SU:2:-1][RI]";
//镁光 //镁光
codeStr = "P550-502752A|1P3242-10-01-B|1TA201223F|7Q100+EA|SM19976138|21PSIMMTech|2D01-03-2023|4LKR|Q25|1Q4"; codeStr = "P550-502752A|1P3242-10-01-B|1TA201223F|7Q100+EA|SM19976138|21PSIMMTech|2D01-03-2023|4LKR|Q25|1Q4";
rule = "PN[5:0:-1]|BATCH[2:0:-1]|LOT|QTY[2:0:3]|RI|SP[5:0:-1]|EXPDATEdd-MM-yyyy[2:0:-1]|xxx|xxx|xxx"; rule = "PN[5:0:-1]|BATCH[2:0:-1]|LOT|QTY[2:0:-1]|RI|SP[5:0:-1]|EXPDATEdd-MM-yyyy[2:0:-1]|xxx|xxx|xxx";
rule = "PN[5:0:-1]|BATCH[2:0:-1]|LOT|QTY[2_7Q:0:-1]|RI|SP[5:0:-1]|EXPDATEdd-MM-yyyy[2:0:-1]|xxx|xxx|xxx";
BarcodeRule br = BarcodeRule.newRule(rule); BarcodeRule br = BarcodeRule.newRule(rule);
Barcode b = br.toCodeBean(codeStr).getBarcode(); Barcode b = br.toCodeBean(codeStr).getBarcode();
......
package com.neotel.smfcore.custom.micron20031;
import com.neotel.smfcore.common.exception.ApiException;
import com.neotel.smfcore.common.utils.HttpHelper;
import com.neotel.smfcore.common.utils.JsonUtil;
import com.neotel.smfcore.custom.micron20031.bean.MatOrderBean;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author sunke
* @date 2022/12/21 9:11 AM
*/
@Slf4j
public class Micron20031Api {
public static void getToken(){
String url = "https://boapi3testgtwy.micron.com/token";
try {
log.info("get token from MES");
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("grant_type","client_credentials");
String resultStr = HttpHelper.postFormParam(url, dataMap);
Map<String, Object> resultMap = JsonUtil.toMap(resultStr);
Object username1 = resultMap.get("Username");
Object Role = resultMap.get("Role");
Object MaterialType = resultMap.get("MaterialType");
Object Message = resultMap.get("Message");
Object Status = resultMap.get("Status");
} catch (ApiException e) {
log.error("checkUserRights Error", e);
}
}
/**
* Order
* CheckUserRights
* {
* "Username": "KHIENYONGCHE",
* "MaterialType": "SOLDER_PASTE"
* }
* //Input: Username, MaterialType
* //Output: Username, Role, MaterialType, Message, Status
* @param username
*/
public static void checkUserRights(String username){
String checkUserRightsUrl = "";
try {
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("Username",username);
dataMap.put("MaterialType","SOLDER_PASTE");
String resultStr = HttpHelper.postJson(checkUserRightsUrl, dataMap);
Map<String, Object> resultMap = JsonUtil.toMap(resultStr);
Object username1 = resultMap.get("Username");
Object Role = resultMap.get("Role");
Object MaterialType = resultMap.get("MaterialType");
Object Message = resultMap.get("Message");
Object Status = resultMap.get("Status");
} catch (ApiException e) {
log.error("checkUserRights Error", e);
}
}
/**
* CheckStockAvailable
* [
* {
* "MicronPN": "501-10187",
* "MaterialType": "SOLDER_PASTE"
* },
* {
* "MicronPN": "501-10188",
* "MaterialType": "SOLDER_PASTE"
* },
* {
* "MicronPN": "501-10189",
* "MaterialType": "SOLDER_PASTE"
* }
* ]
* //Input: Micron Part Number, MaterialType
* //Output: Micron Part Number, Quantity, UnitOfMeasures, MaterialType, Message, Status
*/
// public static List<MatOrderBean> checkStockAvailable(List<String> pnList){
// String checkStockAvailableUrl = "";
// try {
// List<Map<String,String>> paramList = new ArrayList<>();
// for (String pn : pnList) {
// Map<String,String> dataMap = new HashMap<>();
// dataMap.put("MicronPN",pn);
// dataMap.put("MaterialType","SOLDER_PASTE");
// paramList.add(dataMap);
// }
// String resultStr = HttpHelper.posJsonParams(checkStockAvailableUrl, paramList);
// List<MatOrderBean> orderBeanList = JsonUtil.toList(resultStr,MatOrderBean.class);
// return orderBeanList;
// } catch (ApiException e) {
// log.error("CheckStockAvailable Error", e);
// }
// return new ArrayList<>();
// }
}
package com.neotel.smfcore.custom.micron20031.bean;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
* @author sunke
* @date 2022/12/21 9:33 AM
*/
@Data
public class MatOrderBean {
@JsonProperty("MicronPN")
private String micronPN;
@JsonProperty("UnitOfMeasures")
private String unitOfMeasures;
@JsonProperty("Quantity")
private Integer quantity;
@JsonProperty("MaterialType")
private String materialType;
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!