package org.jeecg.modules.dry.controller; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.jeecg.common.api.vo.Result; import org.jeecg.common.constant.MqttConstant; import org.jeecg.common.util.IpUtils; import org.jeecg.common.util.SpringContextUtils; import org.jeecg.modules.dry.entity.*; import org.jeecg.modules.dry.mqtt.MqttConfig; import org.jeecg.modules.dry.mqtt.MqttUtil; import org.jeecg.modules.dry.service.*; //import org.jeecg.modules.dry.util.HerbUtil; import org.jeecg.modules.dry.vo.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; import java.util.*; @Api(tags = "实时数据处理控制器") @RestController @RequestMapping("/dry/real") @Slf4j public class DryRealTimeDataController { @Autowired private IDryRealTimeDataService dryRealTimeDataService; @Autowired private IDryHerbFormulaService dryHerbFormulaService; @Autowired private IDryHerbInfoService dryHerbInfoService; @Autowired private IDryEqpTypeService dryEqpTypeService; // @Autowired // private HerbUtil herbUtil; @Autowired private MqttUtil mqttUtil; @Autowired private MqttConfig mqttConfig; @Autowired private IDryHerbInfoService herbInfoService; @Autowired private IDryHerbFormulaHisService dryHerbFormulaHisService; @Autowired private IDryEquipmentService dryEquipmentService; @ApiOperation(value="测试", notes="返回Hello") @GetMapping("/hello") public Result sayHello() { return Result.ok("Hello"); } @ApiOperation(value="接收实时数据Json", notes="设备实时数据上传") @PostMapping("/sendRealTimeDataJson") public Result realTimeDataJson(@RequestBody RealTimeDataVo realTimeDataVo) { try { if (mqttConfig.isEnable() && "user".equals(mqttConfig.getRole())){ MqttMessage mqttMessage = new MqttMessage(); mqttMessage.setQos(0); mqttMessage.setPayload(JSONObject.toJSONString(realTimeDataVo).getBytes()); mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA,mqttMessage); //处理故障信息 dryRealTimeDataService.fitFaultRecord(realTimeDataVo); } if ("user".equals(mqttConfig.getRole())){ //处理故障信息 dryRealTimeDataService.fitFaultRecord(realTimeDataVo); } } catch (MqttException e) { e.printStackTrace(); } return dryRealTimeDataService.realTimeDataHandle(realTimeDataVo); } @ApiOperation(value="获取设备实时数据", notes="通过租户ID和设备编码获取实时数据") @GetMapping("/getRealTimeData") public Result queryMachineRealTimeData(RealTimeDataVo realTimeDataVo) { return dryRealTimeDataService.queryMachineRealTImeData(realTimeDataVo); } @ApiOperation(value="获取车间统计数据", notes="通过租户ID获取车间统计数据") @GetMapping("/workshopStatistics") public Result workshopStatistics(RealTimeDataVo realTimeDataVo) { return dryRealTimeDataService.queryWorkshopStatistics(realTimeDataVo); } /** * 1001 风箱升 1002 风箱降 * 1003 滚筒升 1004 滚筒降 * 1005 滚筒正转 1006 滚筒反转 * 1007 设备停止 1010 干燥启动 * 1011 前门开关 1012 后门开关 * 1013 热风启动 1014 开门观察 * 1015 出料按钮 */ @ApiOperation(value="发送控制指令", notes="向服务端发送控制指令,由服务端通过socket转发给控制模块") @PostMapping("/sendCommand") public Result sendCommand(@RequestBody CommandMessageVo msgVo) { return dryRealTimeDataService.sendSocketMsg(msgVo); } // // @ApiOperation(value = "药材识别") // @PostMapping("/identify") // public Result identify(@RequestParam("file") MultipartFile file) throws Exception { // try { // if (file.isEmpty()) { // throw new RuntimeException("上传文件不能为空"); // } // InputStream inputStream = file.getInputStream(); // List predict = herbUtil.predict(inputStream); // // if(predict.size()>0) { // Map collect = predict.stream().collect(Collectors.toMap(Classifications.Classification::getClassName, Classifications.Classification::getProbability)); // // // List voList = new ArrayList<>(); // Set strings = collect.keySet(); // List list = herbInfoService.list(new LambdaQueryWrapper().in(DryHerbInfo::getPinyin, strings)); // list.forEach(item -> { // DryHerbInfoVo dryHerbInfoVo = new DryHerbInfoVo(); // BeanUtil.copyProperties(item, dryHerbInfoVo); // dryHerbInfoVo.setProbabily(collect.get(item.getPinyin())); // voList.add(dryHerbInfoVo); // }); // // List collect1 = voList.stream().sorted(Comparator.comparing(DryHerbInfoVo::getProbabily, Comparator.reverseOrder())). // collect(Collectors.toList()); // // return Result.ok(collect1); // } else { // return Result.error("AI识别服务异常"); // } // // } catch (Exception e) { // e.printStackTrace(); // return Result.error("AI识别服务异常"); // } // } /** * 根据设备和租户查询该设备类型的干燥配方,将配方转成xml格式,以字符串方式返回 * @param tenantId * @param eqpCode * @return * @throws JAXBException */ @ApiOperation(value="干燥配方获取", notes="干燥配方下发") @GetMapping(value = "/queryFormula") public Result queryFormulaByEqpType(Integer tenantId, String eqpCode) throws JAXBException { //获取request HttpServletRequest request = SpringContextUtils.getHttpServletRequest(); // 获取请求主机的IP地址 String ip = IpUtils.getIpAddr(request); DryEquipment dryEquipment = dryEquipmentService.selectByTenantIdEquipmentId(tenantId+ "", eqpCode); if (dryEquipment != null) { if (dryEquipment.getIp().equals(ip)) { } else { return Result.error("设备IP地址未配置:" + ip); } } else { return Result.error("设备不存在"); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.eq(DryHerbFormula::getEqpType, dryEquipment.getType()) .eq(DryHerbFormula::getTenantId, tenantId); List list = dryHerbFormulaService.list(queryWrapper); Formulas formulas = new Formulas(); list.forEach(item -> { DryHerbInfo byId = dryHerbInfoService.getById(item.getHerbId()); if (byId!=null) { item.setPinyin(byId.getPinyin()); item.setName(byId.getName()); } Formula formula = new Formula(); BaseParam baseParam = new BaseParam(); WaterParam waterParam = new WaterParam(); TypeParam typeParam = new TypeParam(); OffsetParam offsetParam = new OffsetParam(); baseParam.setCode(item.getCode()); baseParam.setIndex(item.getCode()); baseParam.setName(item.getName()); baseParam.setAb(item.getName()); baseParam.setTyp(item.getCategory()); waterParam.setDelay(Double.valueOf(item.getDelay())); waterParam.setMoisture3(item.getTarget()); waterParam.setWeight1(Double.valueOf(item.getFeed())); waterParam.setTimes(item.getEt()); waterParam.setTemp1(item.getWindTemp()); waterParam.setTemp2(item.getEnvTemp()); waterParam.setTemp3(item.getEnvHum()); waterParam.setTurntime(item.getTurn()); typeParam.setMtype(Integer.valueOf(item.getCategory())); offsetParam.setMoisoffset(item.getMoisOffset()); offsetParam.setColdwind(Double.valueOf(item.getCoolingDuration())); formula.setBaseParam(baseParam); formula.setWaterParam(waterParam); formula.setTypeParam(typeParam); formula.setOffsetParam(offsetParam); formulas.getDryFormulaList().add(formula); }); // 把vos转换成xml // 创建JAXBContext实例 JAXBContext jaxbContext = JAXBContext.newInstance(Formulas.class); // 创建Marshaller实例 Marshaller marshaller = jaxbContext.createMarshaller(); // 设置是否格式化输出 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); // 将对象序列化到字符串 StringWriter writer = new StringWriter(); marshaller.marshal(formulas, writer); return Result.OK("请求成功",writer.toString()); } @ApiOperation(value="干燥配方上报", notes="干燥配方记录上报") @PostMapping(value = "/sendFormulaHistory") public Result sendFormulaHistory(DryHerbFormulaHisVo hisVo) { //获取request HttpServletRequest request = SpringContextUtils.getHttpServletRequest(); // 获取请求主机的IP地址 String ip = IpUtils.getIpAddr(request); DryHerbFormulaHis his = new DryHerbFormulaHis(); BeanUtil.copyProperties(hisVo, his); his.setFormulaIndex(hisVo.getIndex()); DryEquipment dryEquipment = dryEquipmentService.selectByTenantIdEquipmentId(hisVo.getTenantId() + "", hisVo.getEqp()); if (dryEquipment != null) { if (dryEquipment.getIp().equals(ip)) { his.setEqpId(dryEquipment.getId()); } else { return Result.error("设备IP地址未配置:" + ip); } } else { return Result.error("设备不存在"); } dryHerbFormulaHisService.save(his); return Result.OK("保存成功"); } }