package net.vtstar.cloudmes.business.herc.service; import com.alibaba.fastjson.JSONObject; import net.vtstar.cloudmes.business.herc.domain.Oee; import net.vtstar.cloudmes.business.herc.domain.OeeResult; import net.vtstar.cloudmes.business.herc.domain.SyncProgram; import net.vtstar.cloudmes.business.herc.properties.HercProperties; import net.vtstar.cloudmes.business.mobileterminal.domain.ProductionCompleteVO; import net.vtstar.utils.DateUtils; import net.vtstar.utils.asserts.ParamAssert; import net.vtstar.utils.domain.Return; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import java.io.File; import java.lang.reflect.Field; import java.net.URI; import java.net.URISyntaxException; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class HercService { @Autowired private HercProperties hercProperties; @Autowired private RestTemplate restTemplate; private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** * @param syncProgram 同步程序需要的参数 */ public void syncProgram(SyncProgram syncProgram) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); MultiValueMap<String, Object> body = getMultiValueMap(syncProgram); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers); ResponseEntity<Return> responseEntity = restTemplate.exchange(hercProperties.getSyncProgram(), HttpMethod.POST, httpEntity, Return.class); ParamAssert.state(responseEntity.getStatusCode() == HttpStatus.OK, "同步程序异常"); Return r = responseEntity.getBody(); ParamAssert.state(600 == r.getCode(), r.getMessage()); } /** * 获取herc系统中oee内容 */ public OeeResult getTimePerformanceYieldRateOee(Oee oee) { // url采用了参数解析的方式, 参考herc.url.time-performance-yield-rate-oee ResponseEntity<Return> response = restTemplate.getForEntity(hercProperties.getTimePerformanceYieldRateOee(), Return.class, getValueMap(oee)); ParamAssert.state(response.getStatusCode() == HttpStatus.OK, "获取oee失败"); Return r = response.getBody(); ParamAssert.state(600 == r.getCode(), r.getMessage()); Map<String, Object> map = (Map<String, Object>) r.getData(); return new JSONObject(map).toJavaObject(OeeResult.class); } /** * 将派工单报工同步到herc系统 */ public void syncDispatchListData(ProductionCompleteVO productionCompleteVO) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); RequestEntity<ProductionCompleteVO> requestEntity; try { requestEntity = new RequestEntity<>(productionCompleteVO, httpHeaders, HttpMethod.POST, new URI(hercProperties.getSyncDispatchListData())); } catch (URISyntaxException e) { e.printStackTrace(); throw new IllegalStateException("同步程序url配置错误,请联系管理员"); } ResponseEntity<Return> responseEntity = restTemplate.exchange(requestEntity, Return.class); ParamAssert.state(responseEntity.getStatusCode() == HttpStatus.OK, "同步派工单参数失败"); Return r = responseEntity.getBody(); ParamAssert.state(600 == r.getCode(), r.getMessage()); } /** * 用户同步派工单接口 */ public Map getUserPlanning(String equipNo) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); RequestEntity<MultiValueMap<String, Object>> requestEntity = null; UriComponentsBuilder requestUri = UriComponentsBuilder.fromUri(URI.create(hercProperties.getSyncWorkOrderCount())) .queryParam("empNo", equipNo); ResponseEntity<Return> r = restTemplate.getForEntity(requestUri.toUriString(), Return.class); Map<String, Object> map = (Map<String, Object>) r.getBody().getData(); return map; } /** * 将object转换成HashMap封装的对象 * 用于 HTTP GET请求填充参数时使用 * HTTP GET 请求uri类型 /api/xxxx/{x1}/{x2} , 或 /api/xxxx?x1={x1}&x2={x2} */ private Map<String, Object> getValueMap(Object object) { Class objClass = object.getClass(); Map<String, Object> result = new HashMap<>(); Field[] fields = objClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); try { if (field.getType() == Date.class) { result.put(field.getName(), DateUtils.parseDateToString((Date) field.get(object), DATE_FORMAT)); } else { result.put(field.getName(), field.get(object)); } } catch (IllegalAccessException e) { e.printStackTrace(); } } return result; } /** * 将object转换成MultiValueMap封装的对象 */ private MultiValueMap<String, Object> getMultiValueMap(Object object) { Class objClass = object.getClass(); MultiValueMap<String, Object> result = new LinkedMultiValueMap<>(); Field[] fields = objClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); try { if (field.getType() == File.class) { FileSystemResource resource = new FileSystemResource((File) field.get(object)); result.add(field.getName(), resource); } else if (field.getType() == File[].class) { File[] files = (File[]) field.get(object); for (File file : files) { FileSystemResource fileSystemResources = new FileSystemResource(file); result.add(field.getName(), fileSystemResources); } } else { result.add(field.getName(), field.get(object)); } } catch (IllegalAccessException e) { e.printStackTrace(); } } return result; } }
以上是核心业务代码,使用
restTemplate方式调用远程java接口
package net.vtstar.cloudmes.business.herc.properties; import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "herc.url") public class HercProperties { @Getter @Setter // 程序同步 private String syncProgram; @Getter @Setter // 获取oee、时间稼动率、性能稼动率、良品率、OEE private String timePerformanceYieldRateOee; @Getter @Setter //将提交程序同步到herc系统 private String syncDispatchListData; @Getter @Setter //用户派工单接口 private String syncWorkOrderCount; }
herc.url.sync-program=http://22b1964l16.iok.la/api/CNCSystem/ProgramSynchronization herc.url.time-performance-yield-rate-oee=http://22b1964l16.iok.la/api/CNCSystem/OEE?processDur={processDur}&startTime={startTime}&endTime={endTime}&equipNo={equipNo} herc.url.sync-dispatch-list-data=http://22b1964l16.iok.la/api/CNCSystem/WorkOrderData herc.url.sync-work-order-count=http://22b1964l16.iok.la/api/CNCSystem/WorkOrderCount
以上是配置远程接口