package com.nanometer.smartlab.api; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.nanometer.smartlab.controller.BaseController; import com.nanometer.smartlab.controller.MenuController; import com.nanometer.smartlab.dao.SysLaboratoryContainerDao; import com.nanometer.smartlab.dao.SysWarehouseContainerDao; import com.nanometer.smartlab.entity.*; import com.nanometer.smartlab.entity.SysWarning; import com.nanometer.smartlab.entity.enumtype.ApiStatus; import com.nanometer.smartlab.entity.enumtype.SeeFlag; import com.nanometer.smartlab.exception.ApiException; import com.nanometer.smartlab.exception.BusinessException; import com.nanometer.smartlab.exception.ExceptionEnumCode; import com.nanometer.smartlab.model.ResponseModel; import com.nanometer.smartlab.service.*; import com.nanometer.smartlab.util.Constants; import com.nanometer.smartlab.util.FacesUtils; import com.nanometer.smartlab.util.IDUtils; import com.nanometer.smartlab.util.MessageUtil; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created by johnny on 17/9/13. */ @Controller @RequestMapping(value = "service") public class ApiAction extends BaseAction { private static Logger logger = Logger.getLogger(ApiAction.class); @Resource private SysUserService sysUserService; @Resource private OpeReagentStatusService opeReagentStatusService; @Resource private SysReagentService sysReagentService; @Resource private SysWarehouseContainerService sysWarehouseContainerService; @Resource private SysLaboratoryContainerService sysLaboratoryContainerService; @Resource private BaseMetaService baseMetaService; @Resource private InterfaceService interfaceService; @Resource private SysWarningService sysWarningService; @Resource private TempSensorsService tempSensorsService; @Resource private SysFileService sysFileService; @Resource private SysLaboratoryContainerDao sysLaboratoryContainerDao; @Resource private SysWarehouseContainerDao sysWarehouseContainerDao; @Value("${institute.url}") String instituteUrl; @ResponseBody @RequestMapping(value = "/registerTestCabinet") public String registerTestCabinet(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("registerTestCabinet" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.registerTestCabinet(userId, JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } // 现在注册流程是,先在web端创建试剂柜,然后当柜子第一次会调用接口,如果货柜条码存在,则注册成功。 // 如果货柜类型等数据发生变更,以柜子发上来的数据为准 @ResponseBody @RequestMapping(value = "/AutheTestCabinet") public String AutheTestCabinet(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("AutheTestCabinet" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.AutheTestCabinet(userId, JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } @ResponseBody @RequestMapping(value = "/logoutTestCabinet") public String logoutTestCabinet(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("logoutTestCabinet" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.logoutTestCabinet(userId, JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } @ResponseBody @RequestMapping(value = "/updateTestCabinet") public String updateTestCabinet(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("updateTestCabinet" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.updateTestCabinet(userId, JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } @ResponseBody @RequestMapping(value = "/openCabinetDoor") public String openCabinetDoor(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("openCabinetDoor" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { String returndata = interfaceService.openCabinetDoor(userId, JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); retJSON.put("userLevel", returndata); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } @ResponseBody @RequestMapping(value = "/closeCabinetDoor") public String closeCabinetDoor(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("closeCabinetDoor" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.closeCabinetDoor(JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } // 报送试剂最新信息接口 @ResponseBody @RequestMapping(value = "/synchAllCabinet") public String synchAllCabinet(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } JSONObject retJSON = new JSONObject(); logger.info("synchAllCabinet" + data); String userId = request.getHeader("userId"); // Body参数 JSONObject bodyJSONObject = new JSONObject(); bodyJSONObject = JSONObject.parseObject(data); // 主控条码 String controllerCode = bodyJSONObject.getString("controllerCode"); try { // 数据 String dataStr = bodyJSONObject.getString("data"); JSONArray dataJSONArray = JSONArray.parseArray(dataStr); logger.info("dataJSONArray: " + dataJSONArray); if( dataJSONArray.size() !=0 ){ interfaceService.synchAllCabinet(dataJSONArray); } retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } // 报警情况接口(warning) @ResponseBody @RequestMapping(value = "/alarm") public String alarm(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("alarm" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.alarm(JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } // 人员违规预警接口 @ResponseBody @RequestMapping(value = "/alarmperson") public String alarmperson(HttpServletRequest request) throws IOException { logger.info("/api/service/alarmperson"); BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } logger.info("alarmperson" + data); String userId = request.getHeader("userId"); JSONObject retJSON = new JSONObject(); try { interfaceService.alarmperson(JSONObject.parseObject(data)); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } // 出入库记录信息接口 @ResponseBody @RequestMapping(value = "/cabinetData") public String cabinetData(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, data = ""; while((str = br.readLine()) != null){ data += str; } JSONObject retJSON = new JSONObject(); logger.info("cabinetData" + data); String userId = request.getHeader("userId"); // Body参数 JSONObject bodyJSONObject = new JSONObject(); bodyJSONObject = JSONObject.parseObject(data); // 主控条码 String controllerCode = bodyJSONObject.getString("controllerCode"); try { // 数据 String dataStr = bodyJSONObject.getString("data"); JSONArray dataJSONArray = JSONArray.parseArray(dataStr); logger.info("dataJSONArray: " + dataJSONArray); if( dataJSONArray.size() !=0 ){ interfaceService.cabinetData(dataJSONArray); } retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } return retJSON.toJSONString(); } //http://[URL]/smartlab/api/service/containerInfo @ResponseBody @RequestMapping(value = "/containerInfo") public Object getContainerInfo(@RequestParam String startTime, @RequestParam String endTime) { logger.info("getContainerInfo start..."); List warehouseList = sysWarehouseContainerService.getSysWarehouseContainerInfoList(startTime, endTime); List laboratoryList = sysLaboratoryContainerService.getSysLaboratoryContainerInfoList(startTime, endTime); List datalist = Lists.newArrayListWithCapacity(warehouseList.size() + laboratoryList.size()); for (SysWarehouseContainer container : warehouseList) { Map map = Maps.newHashMapWithExpectedSize(3); datalist.add(map); map.put("containerCode", container.getContainerCode()); map.put("containerName", container.getName()); map.put("validFlag", container.getValidFlag().getKey()); map.put("type", this.baseMetaService.getBaseMetaKey(container.getType())); map.put("place", "0"); map.put("left", this.baseMetaService.getBaseMetaKey(container.getCharacterLeft())); map.put("right", this.baseMetaService.getBaseMetaKey(container.getCharacterRight())); } for (SysLaboratoryContainer container : laboratoryList) { Map map = Maps.newHashMapWithExpectedSize(3); datalist.add(map); map.put("containerCode", container.getContainerCode()); map.put("containerName", container.getName()); map.put("validFlag", container.getValidFlag().getKey()); map.put("type", this.baseMetaService.getBaseMetaKey(container.getType())); map.put("place", "1"); map.put("left", this.baseMetaService.getBaseMetaKey(container.getCharacterLeft())); map.put("right", this.baseMetaService.getBaseMetaKey(container.getCharacterRight())); } logger.info("getContainerInfo end..."); return ResponseModel.getOkInstence(datalist); } @ResponseBody @RequestMapping(value = "/reagentInfo") public Object getReagentInfo(@RequestParam String startTime, @RequestParam String endTime, @RequestParam String containerCode, @RequestParam String status) { logger.info("getReagentInfo start..."); String containerid = null; SysLaboratoryContainer laboratoryContainer = sysLaboratoryContainerDao .getSysLaboratoryContainerByContainerCode(containerCode); if (Objects.isNull(laboratoryContainer)) { SysWarehouseContainer warehouseContainer = sysWarehouseContainerDao .getSysWarehouseContainerByContainerCode(containerCode); if (Objects.isNull(warehouseContainer)) { JSONObject retJSON = new JSONObject(); logger.error("The containerCode:" + containerCode + " has not been exists!"); retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + "The containerCode:" + containerCode + " has not been exists!"); return retJSON.toJSONString(); } else { containerid = warehouseContainer.getId(); } } else { containerid = laboratoryContainer.getId(); } List reagetList = opeReagentStatusService.getOpeReagentStatusInfoList(startTime, endTime, containerid, status); List datalist = Lists.newArrayListWithCapacity(reagetList.size()); for (OpeReagentStatus opeReagentStatus : reagetList) { Map map = Maps.newHashMapWithExpectedSize(4); datalist.add(map); map.put("reagentCode", opeReagentStatus.getReagentCode()); map.put("reagentName", opeReagentStatus.getReagent().getName()); map.put("cas", opeReagentStatus.getReagent().getCas()); map.put("reagentType", opeReagentStatus.getReagent().getReagentType()); map.put("deadline", opeReagentStatus.getReagent().getDeadline()); map.put("specification", opeReagentStatus.getReagent().getReagentFormat()); map.put("masterMetering", opeReagentStatus.getReagent().getMainMetering()); map.put("itemNumber", opeReagentStatus.getReagent().getReagentType()); map.put("producer", opeReagentStatus.getReagent().getProductHome()); } logger.info("getReagentInfo end..."); return ResponseModel.getOkInstence(datalist); } @ResponseBody @RequestMapping(value = "/userInfo") public Object getUserInfo(@RequestParam String startTime, @RequestParam String endTime) { logger.info("getUserInfo start..."); List userList = sysUserService.getSysUserInfoList(startTime, endTime); List allLaboratoryContainerList = sysLaboratoryContainerService.getSysLaboratoryContainerList(null,null,null,null,null); List allWarehouseContainerList = sysWarehouseContainerService.getSysWarehouseContainerList(null,null,null,null,null); List containerCodeList = new ArrayList(); List wareContainerCodeList = new ArrayList(); for (SysLaboratoryContainer laboratoryContainer : allLaboratoryContainerList) { containerCodeList.add(laboratoryContainer.getContainerCode()); } for (SysWarehouseContainer warehouseContainer : allWarehouseContainerList) { containerCodeList.add(warehouseContainer.getContainerCode()); wareContainerCodeList.add(warehouseContainer.getContainerCode()); } List datalist = Lists.newArrayListWithCapacity(userList.size()); for (SysUser user : userList) { Map map = Maps.newHashMapWithExpectedSize(5); datalist.add(map); map.put("name", user.getName()); map.put("role", user.getSeeFlag().getKey()); map.put("validFlag", user.getValidFlag().getKey()); map.put("idcard", user.getIdCard()); map.put("point", user.getPoint()); map.put("unit", user.getCompany()); map.put("Arp", user.getArp()); map.put("department", user.getDepartment()); if (StringUtils.isNotBlank(user.getContainerCodeList())) { map.put("containerCodeList", ArrayUtils.addAll(user.getContainerCodeList().split(","), (String[])wareContainerCodeList.toArray(new String[0]))); } if(user.getSeeFlag().ordinal() == SeeFlag.MANAGE.ordinal()){ map.put("containerCodeList", containerCodeList); } } logger.info("getUserInfo end..."); return ResponseModel.getOkInstence(datalist); } @RequestMapping(value = "/putInLaboratory", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST) @ResponseBody public String putInLaboratory(HttpServletRequest request, String reagentCode, String barCode, String containerCode) { try { logger.info("PutInLaboratory start..."); logger.info("Params : reagentCode[" + reagentCode + "], barCode[" + barCode + "], containerCode[" + containerCode + "]"); // 检查用户 String idCard = this.getIdCardFromHeader(request); if (StringUtils.isBlank(idCard)) { throw new BusinessException(ExceptionEnumCode.PARAM_NULL, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NULL.getCode(), "用户卡号")); } SysUser sysUser = this.sysUserService.getSysUserByIdCard(idCard); if (sysUser == null) { if(idCard.length()==20){//截取长度为8的卡号再次查询 idCard=idCard.substring(10,18); sysUser = this.sysUserService.getSysUserByIdCard(idCard); if(sysUser==null){ throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "用户卡号", idCard)); } }else { throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "用户卡号", idCard)); } } // 检查试剂条码 if (StringUtils.isBlank(reagentCode)) { throw new BusinessException(ExceptionEnumCode.PARAM_NULL, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NULL.getCode(), "试剂条码")); } // 检查地点条码 if (StringUtils.isBlank(barCode)) { throw new BusinessException(ExceptionEnumCode.PARAM_NULL, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NULL.getCode(), "地点条码")); } // 检查货柜条码 if (StringUtils.isBlank(containerCode)) { throw new BusinessException(ExceptionEnumCode.PARAM_NULL, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NULL.getCode(), "货柜条码")); } this.opeReagentStatusService.putReagentInLaboratory(reagentCode, barCode, containerCode, sysUser); return this.getOkResponseString(); } catch (BusinessException e) { logger.error("试剂放入实验室柜失败", e); return this.getResponseString(e.getErrorCode().getCode(), e.getMessage()); } catch (Exception e) { logger.error("试剂放入实验室柜失败", e); return this.getSysErrResponseString(); } finally { logger.info("PutInLaboratory end..."); } } // // // @RequestMapping(value = "/setWarning", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST) // @ResponseBody // public String setWarning(HttpServletRequest request) throws ParseException, IOException, ApiException { // BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); // String str = ""; // String inputLine; // try { // while ((inputLine = reader.readLine()) != null) { // str += inputLine; // } // reader.close(); // } catch (IOException e) { // System.out.println("IOException: " + e); // } // JSONObject jsonObject = JSON.parseObject(str); // SysWarning sysWarning = new SysWarning(); // if (!jsonObject.containsKey("controllerCode") // || ! jsonObject.containsKey("data")) { // throw new ApiException(ApiStatus.PARAM_NO_EXIST); // } // // String controllerCode = jsonObject.getString("controllerCode"); // // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // String dataStr = jsonObject.getString("data"); // // // JSONObject dataJson = JSON.parseObject(dataStr); // // if (!dataJson.containsKey("warningId") // || !dataJson.containsKey("userId") // || !dataJson.containsKey("warningTime") // || !dataJson.containsKey("warningLevel") // || !dataJson.containsKey("containerCode") // || !dataJson.containsKey("warningPoint")) { // throw new ApiException(ApiStatus.PARAM_NO_EXIST); // } // // String containerCode = dataJson.getString("containerCode"); // String warningId = dataJson.getString("warningId"); // String userId = dataJson.getString("userId"); // String warningTimeStr = dataJson.getString("warningTime"); // Integer warningLevelInt = dataJson.getInteger("warningLevel"); // Integer warningPoint = dataJson.getInteger("warningPoint"); // // sysWarning.setUserId(userId); // sysWarning.setWarningTime(sdf.parse(warningTimeStr)); // sysWarning.setWarningPoint(warningPoint); // sysWarning.setWarningLevel(warningLevelInt); // sysWarning.setWarningId(warningId); // sysWarning.setId(IDUtils.uuid()); // // // SysLaboratoryContainer laboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode(containerCode); // if (Objects.isNull(laboratoryContainer)) { // SysWarehouseContainer warehouseContainer = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode(containerCode); // if (Objects.isNull(warehouseContainer)) { // logger.error("The containerCode:"+containerCode+" has not been exists!"); // } else { // sysWarning.setContainerId(warehouseContainer.getId()); // } // } else { // sysWarning.setContainerId(laboratoryContainer.getId()); // } // JSONObject retJSON = new JSONObject(); // try { // sysWarningService.insert(sysWarning); // retJSON.put("code", ApiStatus.OK.getRetCode()); // retJSON.put("message", ApiStatus.OK.getRetMsg()); // } catch (Exception e) { // retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); // StackTraceElement[] stackTraceElements = e.getStackTrace(); // // StringBuilder sb = new StringBuilder(); // sb.append(e.toString()); // for (StackTraceElement stackTraceElement : stackTraceElements) { // sb.append("\n"); // sb.append(stackTraceElement.toString()); // } // retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); // } // return retJSON.toJSONString(); // } // 智能柜温度,湿度,气体浓度数据接口 @RequestMapping(value = "/tempSensors") @ResponseBody public String tempSensors(HttpServletRequest request) throws ParseException, IOException, ApiException { logger.info("tempSensors start..."); JSONObject retJSON = new JSONObject(); BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); String str = ""; String inputLine; try { while ((inputLine = reader.readLine()) != null) { str += inputLine; } reader.close(); } catch (IOException e) { System.out.println("IOException: " + e); } JSONObject jsonObject = JSON.parseObject(str); if (!jsonObject.containsKey("data")) { logger.error("data has not been exists!"); retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + "data has not been exists!"); return retJSON.toJSONString(); } // 2.2 Body参数: // 货柜条码 containerCode String ○ // 数据 data JSONArray SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dataStr = jsonObject.getString("data"); String containerCode = jsonObject.getString("containerCode"); logger.info("dataStr:" + dataStr); JSONArray dataJsonArray = null; Iterator dataIt = null; JSONObject dataJson = null; try{ dataJsonArray = JSONArray.parseArray(dataStr); logger.info("dataJsonArray:" + dataJsonArray.size()); dataIt = dataJsonArray.iterator(); } catch (Exception e){ e.printStackTrace(); } int type = 0; String containerId = null; logger.info("The containerCode:" + containerCode + " finding!"); // 查找货柜条码对应货柜信息 SysLaboratoryContainer laboratoryContainer = sysLaboratoryContainerDao .getSysLaboratoryContainerByContainerCode(containerCode); if (Objects.isNull(laboratoryContainer)) { SysWarehouseContainer warehouseContainer = sysWarehouseContainerDao .getSysWarehouseContainerByContainerCode(containerCode); if (Objects.isNull(warehouseContainer)) { logger.error("The containerCode:" + containerCode + " has not been exists!"); retJSON.put("code", ApiStatus.PARAM_NO_EXIST.getRetCode()); retJSON.put("message", ApiStatus.PARAM_NO_EXIST.getRetMsg() + "--" + "The containerCode:" + containerCode + " has not been exists!"); return retJSON.toJSONString(); } else { type = TempSensors.TYPE_WAREHOUSE_CONTAINER; containerId = warehouseContainer.getId(); } } else { type = TempSensors.TYPE_LABORATORY_CONTAINER; containerId = laboratoryContainer.getId(); } while (dataIt.hasNext()) { logger.info("dataIt.hasNext()" ); dataJson = JSONObject.parseObject(dataIt.next().toString()); logger.info("dataJson" +dataJson.toJSONString() ); // // 2.2.1.数据JSON格式 // 温度值 Temp Float ○ // 湿度值 Humidity Float ○ // 气体浓度 voc1 Float ○ // 报警信息 Flag Integer // 更新时间 updateTime String ○ if (!dataJson.containsKey("Temp") || !dataJson.containsKey("Humidity") || !dataJson.containsKey("voc1") || !dataJson.containsKey("Flag") || !dataJson.containsKey("updateTime")) { retJSON.put("code", ApiStatus.PARAM_NO_EXIST.getRetCode()); retJSON.put("message", ApiStatus.PARAM_NO_EXIST.getRetMsg() + "--" + "data has not been exists!"); return retJSON.toJSONString(); } Float temp = dataJson.getFloat("Temp"); Float humidity = dataJson.getFloat("Humidity"); Float voc1 = dataJson.getFloat("voc1"); String flag = dataJson.getString("Flag"); if (flag == null || flag.isEmpty() || flag.length() < 5) { continue; } String updateTime = dataJson.getString("updateTime"); SysContainerSensors sysContainerSensors = new SysContainerSensors(); sysContainerSensors.setContainerId(containerCode); TempSensors tempSensors = new TempSensors(); tempSensors.setContainerId(containerCode); /* tempSensors.setFlag(flag); */ tempSensors.setTemp(temp); tempSensors.setHumidity(humidity); tempSensors.setVoc1(voc1); tempSensors.setFlag(flag); tempSensors.setUpdateTime(sdf.parse(updateTime)); tempSensors.setId(IDUtils.uuid()); tempSensors.setType(type); tempSensors.setContainerId(containerId); try { // 将温度、湿度、气体浓度保存到数据库 tempSensorsService.insert(tempSensors); sysLaboratoryContainerService.updateInfo(temp,humidity,voc1,containerId,flag); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); } catch (Exception e) { retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); StackTraceElement[] stackTraceElements = e.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append(e.toString()); for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append("\n"); sb.append(stackTraceElement.toString()); } retJSON.put("message", ApiStatus.SYS_ERR.getRetMsg() + "--" + sb.toString()); } } return retJSON.toJSONString(); } /** * 获取试剂柜试剂最新状态接口 * * @param containerCode * @return */ //http://[URL]/smartlab/api/service/getReagentStatusByContainerCode @ResponseBody @RequestMapping(value = "/getReagentStatusByContainerCode") public Object getReagentStatusByContainerCode(@RequestParam String containerCode) { logger.info("getReagentStatusByContainerCode start..."); JSONObject retJSON = new JSONObject(); try { if (StringUtils.isBlank(containerCode)) { throw new ApiException(ApiStatus.PARAM_NO_EXIST); } List list = opeReagentStatusService.getReagentStatusByContainerCode(containerCode); list.stream().forEach(info->info.setContainerCode(containerCode)); return ResponseModel.getOkInstence(list); } catch (ApiException e) { retJSON.put("code", e.getApiStatus().getRetCode()); retJSON.put("message", e.getErrorMessage()); } return retJSON; } /** * 获取试剂柜APP版本 * * @param containerCode * @return */ //http://[URL]/smartlab/api/service/getContainerVersion @ResponseBody @RequestMapping(value = "/getContainerVersion") public Object getContainerVersion(HttpServletRequest request) { logger.info("getContainerVersion start..."); JSONObject retJSON = new JSONObject(); List versionList = this.baseMetaService.getBaseMetaList("containerversion", "", 0, 1); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); retJSON.put("version", versionList.get(0).getMetaValue()); return retJSON; } @ResponseBody @RequestMapping(value = "/getAppVersion") public Object getAppVersion(HttpServletRequest request) { logger.info("getAppVersion start..."); JSONObject retJSON = new JSONObject(); try { List list = this.sysFileService.getSysFileList(null, null, null); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); if(list==null&&list.size()==0){ retJSON.put("data", null); }else { SysFile sysFile=list.get(0); Map map=new HashMap(); map.put("newOrOld",sysFile.getNewOrOld()); map.put("version",sysFile.getVersion()); String urlPrefix = instituteUrl.replace("http://", ""); map.put("downLoadUrl",urlPrefix+"/smartlab/api/service/downloadFile?id="+sysFile.getId()); retJSON.put("data",map); } }catch (Exception e){ retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); retJSON.put("message", e.getMessage()); } return retJSON; } @ResponseBody @RequestMapping(value = "/getAllFile") public Object getAllFile(HttpServletRequest request) { logger.info("getAllFile start..."); JSONObject retJSON = new JSONObject(); try { List list = this.sysFileService.getSysFileList(null, null, null); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); retJSON.put("data", list); }catch (Exception e){ retJSON.put("code", ApiStatus.SYS_ERR.getRetCode()); retJSON.put("message", e.getMessage()); } return retJSON; } @ResponseBody @RequestMapping(value = "/downloadFile") public HttpServletResponse downloadFile(HttpServletRequest request, String id, HttpServletResponse response){ SysFile sysFile=this.sysFileService.getSysFileById(id); try { // path是指欲下载的文件的路径。 File file = new File(sysFile.getUrl()); // 取得文件名。 String filename = file.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(sysFile.getUrl())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); String name=new String(filename.getBytes("UTF-8"),"ISO-8859-1"); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + name); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; } @RequestMapping(value = "/dataLogin") public ModelAndView login(HttpServletRequest request){ UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456789"); token.setRememberMe(true); BaseController baseController=new BaseController(); Subject currentUser = baseController.getSubject(); System.out.println(1); try { System.out.println(2); currentUser.login(token); } catch (UnknownAccountException e) { System.out.println("用户名不存在"); FacesUtils.warn("用户名不存在"); return null; } catch (Exception e) { System.out.println("登陆失败"); logger.error("登陆失败", e); } if (currentUser.isAuthenticated()) {//登录成功进入主页 System.out.println(3); SysUser user = this.sysUserService.getSysUserByAccount("admin"); currentUser.getSession(true).setAttribute(Constants.SESSION_USER, user); currentUser.getSession(true).getId(); MenuController menuController=new MenuController(); menuController.initPage(); System.out.println(4); //return "redirect:http://192.168.1.195:8888/smartlab_war_exploded/index.xhtml"; return new ModelAndView("redirect:/index.xhtml"); } else { System.out.println("用户名与密码不匹配"); FacesUtils.warn("用户名与密码不匹配"); } return null; /*JSONObject retJSON = new JSONObject(); retJSON.put("code", ApiStatus.OK.getRetCode()); retJSON.put("message", ApiStatus.OK.getRetMsg()); System.out.println(5); return retJSON;*/ } //从柜子的设备码根据设备的所在实验室的课题组的课题组下的所有用户,没有课题组则返回用部门搜索 @ResponseBody @RequestMapping(value = "/userListByContainerCode") public Object userListByContainerCodeNew(@RequestParam String containerCode,@RequestParam String startTime, @RequestParam String endTime) throws ApiException { logger.info("userListByContainerCodeNew start...containerCode= " + containerCode + ",startTime=" + startTime + ",endTime=" + endTime); //1.课题组下的用户 List userList= interfaceService.getUserListByContainerCodeNew(containerCode, startTime, endTime); if (userList == null) { //2.否则部门下用户 userList = interfaceService.getUserListByContainerCode(containerCode, startTime, endTime); } List datalist = Lists.newArrayListWithCapacity(userList.size()); for (SysUser user : userList) { Map map = Maps.newHashMapWithExpectedSize(5); datalist.add(map); map.put("name", user.getName()); map.put("role", user.getSeeFlag().getKey()); map.put("validFlag", user.getValidFlag().getKey()); map.put("idcard", user.getIdCard()); map.put("point", user.getPoint()); map.put("unit", user.getCompany()); map.put("Arp", user.getArp()); map.put("departmentName", user.getDepartment()); map.put("project", user.getProject()); } logger.info("getUserInfo end..."); return ResponseModel.getOkInstence(datalist); } }