| | |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.mina.core.service.IoAcceptor; |
| | | import org.apache.mina.core.service.IoHandler; |
| | | import org.apache.mina.core.session.IoSession; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.jeecg.common.api.CommonAPI; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.config.TenantContext; |
| | | import org.jeecg.common.config.mqtoken.UserTokenContext; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.constant.MqttConstant; |
| | | import org.jeecg.common.system.util.JwtUtil; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.util.RedisUtil; |
| | | import org.jeecg.common.util.SpringContextUtils; |
| | | import org.jeecg.modules.dry.common.CacheConstants; |
| | | import org.jeecg.modules.dry.entity.*; |
| | | import org.jeecg.modules.dry.mqtt.MqttUtil; |
| | | import org.jeecg.modules.dry.service.*; |
| | | import org.jeecg.modules.dry.vo.DryOrderTrendVo; |
| | | import org.jeecg.modules.dry.vo.DryOrderVo; |
| | | import org.jeecg.modules.dry.vo.RealTimeDataVo; |
| | | import org.jeecg.modules.dry.socket.ServerHandler; |
| | | import org.jeecg.modules.dry.socket.SocketServerConfig; |
| | | import org.jeecg.modules.dry.util.DryUtil; |
| | | import org.jeecg.modules.dry.vo.*; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.security.auth.login.LoginContext; |
| | | import java.io.IOException; |
| | | import java.io.ObjectOutputStream; |
| | | import java.net.Socket; |
| | | import java.text.DecimalFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class DryRealTimeDataServiceImpl implements IDryRealTimeDataService { |
| | |
| | | private IDryHerbService herbService; |
| | | |
| | | @Autowired |
| | | private IDryHerbFormulaService dryHerbFormulaService; |
| | | |
| | | @Autowired |
| | | private IDryEquipmentService equipmentService; |
| | | |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private IDryProdRecordService prodRecordService; |
| | | |
| | | |
| | | private String token; |
| | | |
| | | @Value(value = "${jeecg.mqtt.role}") |
| | | private String role; |
| | | |
| | | @Autowired |
| | | private MqttUtil mqttUtil; |
| | | |
| | | public String getTemporaryToken() { |
| | | if (token == null) { |
| | | RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class); |
| | | // 模拟登录生成Token |
| | | token = JwtUtil.sign("admin", "b668043e3ea4bc2d"); |
| | | // 设置Token缓存有效时间为 5 分钟 |
| | | redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); |
| | | redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000); |
| | | } |
| | | |
| | | return token; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Result<?> realTimeDataHandle(RealTimeDataVo realTimeDataVo) { |
| | | TenantContext.setTenant(realTimeDataVo.getTenantid()+""); |
| | | log.info("实时数据:"+realTimeDataVo.toString()); |
| | | // log.info("实时数据:"+realTimeDataVo.toString()); |
| | | |
| | | |
| | | // 1 查询或创建工单 |
| | | // 1.1 从redis取出工单缓存 |
| | |
| | | // 根据租户id和工单号查询数据库是否有记录,有则返回,没有则新增一条再返回 |
| | | orderVo = getOrSaveDryOrderVoDB(realTimeDataVo); |
| | | } |
| | | if (orderVo == null) { |
| | | log.error("工单不存在,工单号:"+realTimeDataVo.getWorkorder()+",设备:" + realTimeDataVo.getMachineid() +",药材:" + realTimeDataVo.getName()); |
| | | return Result.error("工单不存在"); |
| | | } |
| | | |
| | | // 2 更新工单实时数据 |
| | | // 2.1 将工单中的数据替换为最新数据 |
| | | realTimeDataVo.setOrderId(orderVo.getId()); |
| | | |
| | | orderVo.setInitial(realTimeDataVo.getMoisture1()); |
| | | orderVo.setDryTime(realTimeDataVo.getTime3()); |
| | | orderVo.setDelay(realTimeDataVo.getDelay()); |
| | |
| | | orderVo.setState_windbox(realTimeDataVo.getState_windbox()); |
| | | orderVo.setState_valve(realTimeDataVo.getState_valve()); |
| | | orderVo.setOrderStatus(realTimeDataVo.getWorkorder_status()); |
| | | orderVo.setEqp_status(realTimeDataVo.getEqp_status()); |
| | | // orderVo.setEqp_state(realTimeDataVo.getEqp_state()); |
| | | orderVo.setWarning(realTimeDataVo.getEqp_warning()); |
| | | orderVo.setFault(realTimeDataVo.getEqp_fault()); |
| | | orderVo.setLevel(realTimeDataVo.getLevel()); |
| | | |
| | | |
| | | DryOrderTrendVo trendVo = new DryOrderTrendVo(realTimeDataVo); |
| | | // 2.2 保存工单含水率变化 或 重量变化 |
| | | |
| | |
| | | orderVo.getBellowsTemp().put(realTimeDataVo.getTime3(), realTimeDataVo.getTemp2()); |
| | | // 2.3 更新到redis缓存 |
| | | redisUtil.hset(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), |
| | | realTimeDataVo.getTenantid()+"_"+realTimeDataVo.getMachineid(),orderVo, 600000); |
| | | realTimeDataVo.getTenantid()+"_"+realTimeDataVo.getMachineid(),orderVo, 60*60); |
| | | return Result.ok(); |
| | | } |
| | | |
| | |
| | | } |
| | | // 3 数据库没有则新增一条数据 |
| | | } else { |
| | | |
| | | orderVo = saveNewOrder(realTimeDataVo); |
| | | } |
| | | return orderVo; |
| | |
| | | * @param realTimeDataVo |
| | | * @return |
| | | */ |
| | | @NotNull |
| | | private DryOrderVo saveNewOrder(RealTimeDataVo realTimeDataVo) { |
| | | DryOrderVo orderVo; |
| | | // 查询药材 |
| | | DryHerb herb = queryHerbByIndexTenant(realTimeDataVo); |
| | | |
| | | // 查询设备 |
| | | DryEquipment equ = queryEquipmentByCodeTenant(realTimeDataVo); |
| | | if (equ == null) { |
| | | log.error("未找到设备:"+realTimeDataVo.getIndex() +","+realTimeDataVo.getName() +",机台:" + realTimeDataVo.getMachineid()); |
| | | return null; |
| | | } |
| | | // 查询药材 |
| | | DryHerbFormula herbFormula = queryHerbByIndexTenant(realTimeDataVo); |
| | | |
| | | if (herbFormula == null) { |
| | | log.error("未找到药材:"+realTimeDataVo.getIndex() +","+realTimeDataVo.getName() +",机台:" + realTimeDataVo.getMachineid()); |
| | | return null; |
| | | } |
| | | // 创建新工单 |
| | | orderVo = new DryOrderVo(realTimeDataVo); |
| | | orderVo.setHerbId(herb.getId()); |
| | | |
| | | orderVo.setHerbId(herbFormula.getId()); |
| | | orderVo.setEquId(equ.getId()); |
| | | DryOrder dryOrder = BeanUtil.toBean(orderVo, DryOrder.class); |
| | | boolean save = dryOrderService.save(dryOrder); |
| | |
| | | queryWrapper.eq(DryEquipment::getCode, realTimeDataVo.getMachineid()); |
| | | DryEquipment one = equipmentService.getOne(queryWrapper); |
| | | if (one == null) { |
| | | one = new DryEquipment(realTimeDataVo); |
| | | equipmentService.save(one); |
| | | log.error(role+"保存实时数据,未找到设备:"+realTimeDataVo.getMachineid()); |
| | | // one = new DryEquipment(realTimeDataVo); |
| | | // equipmentService.save(one); |
| | | if (MqttConstant.ROLE_ADMIN.equals(role)) { |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(0); |
| | | JSONObject object = new JSONObject(); |
| | | object.put("code", realTimeDataVo.getMachineid()); |
| | | object.put("tenantId", realTimeDataVo.getTenantid()); |
| | | mqttMessage.setPayload(object.toJSONString().getBytes()); |
| | | try { |
| | | mqttUtil.getMqttClient().publish(MqttConstant.SERVICE_REQ_PREFIX ,mqttMessage); |
| | | }catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | return null; |
| | | } |
| | | return one; |
| | | } |
| | |
| | | * @param realTimeDataVo |
| | | * @return |
| | | */ |
| | | private DryHerb queryHerbByIndexTenant(RealTimeDataVo realTimeDataVo) { |
| | | LambdaQueryWrapper<DryHerb> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(DryHerb::getTenantId, realTimeDataVo.getTenantid()); |
| | | queryWrapper.eq(DryHerb::getCode, realTimeDataVo.getIndex()); |
| | | DryHerb one = herbService.getOne(queryWrapper); |
| | | private DryHerbFormula queryHerbByIndexTenant(RealTimeDataVo realTimeDataVo) { |
| | | LambdaQueryWrapper<DryHerbFormula> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(DryHerbFormula::getTenantId, realTimeDataVo.getTenantid()); |
| | | queryWrapper.eq(DryHerbFormula::getName, realTimeDataVo.getName()); |
| | | DryHerbFormula one = dryHerbFormulaService.getOne(queryWrapper); |
| | | if (one == null) { |
| | | one = new DryHerb(realTimeDataVo); |
| | | herbService.save(one); |
| | | one = new DryHerbFormula(realTimeDataVo); |
| | | DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(realTimeDataVo.getTenantid() + "", realTimeDataVo.getMachineid()); |
| | | one.setEqpType(dryEquipment.getType()); |
| | | dryHerbFormulaService.save(one); |
| | | } |
| | | return one; |
| | | } |
| | |
| | | */ |
| | | private void saveOrderTrendVo(DryOrderTrendVo trendVo, DryOrderVo orderVo) { |
| | | //判断 实时含水率 或 实时重量有没有变化,有变化则更新 |
| | | if(orderVo.getTrendVo() == null |
| | | if(orderVo.getTrendVo() == null && trendVo != null && trendVo.getWeight() > 0 |
| | | || trendVo.getWeight() < orderVo.getTrendVo().getWeight() |
| | | ) { |
| | | DryOrder byId = dryOrderService.getById(orderVo.getId()); |
| | |
| | | public Result<?> queryMachineRealTImeData(RealTimeDataVo realTimeDataVo) { |
| | | TenantContext.setTenant(realTimeDataVo.getTenantid()+""); |
| | | |
| | | |
| | | // 查询所有机台,查询语句组装 |
| | | LambdaQueryWrapper<DryEquipment> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(DryEquipment::getTenantId, realTimeDataVo.getTenantid()); |
| | | queryWrapper.eq(DryEquipment::getEnable, "Y"); |
| | | |
| | | queryWrapper.orderByAsc(DryEquipment::getCode); |
| | | // 查询所有机台 |
| | | List<DryEquipment> dryEquipments = equipmentService.list(queryWrapper); |
| | | // 机台列表,用于效率对比 |
| | | List<String> list = new ArrayList<>(); |
| | | |
| | | // 效率列表,用于效率对比 |
| | | List<Double> dList = new ArrayList<>(); |
| | | // 查询当前机台工单 |
| | | DryOrderVo orderVo = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + realTimeDataVo.getMachineid()); |
| | | try { |
| | | try { |
| | | // 如果有可用机台 |
| | | if (dryEquipments != null && dryEquipments.size() > 0) { |
| | | // 查询其它机台工单 |
| | | dryEquipments.stream().forEach(item -> { |
| | | // 获取工单 |
| | | DryOrderVo order = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + item.getCode()); |
| | | list.add(item.getName().substring(0, item.getName().indexOf('#')+1)); |
| | | if (order != null) { |
| | | // 计算干燥效率,用于对比 |
| | | DryOrderTrendVo dryOrderTrendVo = order.getDetailList().get(order.getDetailList().size() - 1); |
| | | double v = order.getOriginWeight() - dryOrderTrendVo.getWeight(); |
| | | |
| | | if (v > 0 && dryOrderTrendVo.getTotalTime()>0) { |
| | | DecimalFormat df = new DecimalFormat("#.00"); |
| | | dList.add(Double.valueOf(df.format(v / dryOrderTrendVo.getTotalTime() * 60))); |
| | | } else { |
| | | dList.add(50d); |
| | | } |
| | | |
| | | } else { |
| | | // 如果没有生产,效率置0 |
| | | dList.add(0d); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | if (orderVo != null) { |
| | | // 将效率对比数据放入当前机台实时数据中返回 |
| | | orderVo.setCompEqpNum(list); |
| | | orderVo.setCompEqpEffic(dList); |
| | | // 查询近十次效率和能能耗平均 |
| | | dryOrderService.queryRecentOrderAvg(orderVo); |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return Result.ok(orderVo); |
| | | } |
| | | |
| | | |
| | | if (dryEquipments != null && dryEquipments.size() > 0) { |
| | | dryEquipments.stream().forEach(item -> { |
| | | DryOrderVo order = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + item.getCode()); |
| | | if (order != null) { |
| | | double v = order.getOriginWeight() - order.getYield(); |
| | | list.add(item.getName().substring(0, item.getName().indexOf('#')+1)); |
| | | if (v > 0) { |
| | | DecimalFormat df = new DecimalFormat("#.00"); |
| | | dList.add(Double.valueOf(df.format(v / order.getDryTime() * 60))); |
| | | } else { |
| | | dList.add(50d); |
| | | @Override |
| | | public Result<?> sendSocketMsg(CommandMessageVo msgVo) { |
| | | |
| | | |
| | | DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(msgVo.getTenantId() + "", msgVo.getMachineId()); |
| | | log.info("获取设备:" + dryEquipment.toString()); |
| | | |
| | | // managedSessions.keySet().forEach(addr -> { |
| | | // ObjectOutputStream oos = null; |
| | | try { |
| | | // Socket socket = SocketServerConfig.clientMap.get(addr); |
| | | IoSession session = ServerHandler.clientSocket.get(dryEquipment.getIp()); |
| | | if (session == null) { |
| | | return Result.error("未获取到session,请检查客户端配置或设备ip配置是否正常"); |
| | | } |
| | | SocketMsgVo smv = new SocketMsgVo(msgVo); |
| | | session.write(JSONObject.toJSONString(smv)); |
| | | // oos = new ObjectOutputStream(socket.getOutputStream()); |
| | | // String s = JSONObject.toJSONString(new SocketMsgVo(msgVo)); |
| | | // oos.writeUTF(s); |
| | | // oos.flush(); |
| | | |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | |
| | | } |
| | | }); |
| | | // }); |
| | | return Result.OK(); |
| | | } |
| | | if (orderVo != null) { |
| | | orderVo.setCompEqpNum(list); |
| | | orderVo.setCompEqpEffic(dList); |
| | | |
| | | |
| | | @Override |
| | | public Result<?> queryWorkshopStatistics(RealTimeDataVo realTimeDataVo) { |
| | | LambdaQueryWrapper<DryEquipment> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(DryEquipment::getTenantId, realTimeDataVo.getTenantid()); |
| | | queryWrapper.eq(DryEquipment::getEnable, "Y"); |
| | | List<DryEquipment> dryEquipments = equipmentService.list(queryWrapper); |
| | | DryOrderVo orderVo = new DryOrderVo(); |
| | | if (dryEquipments != null && dryEquipments.size() > 0) { |
| | | dryEquipments.stream().forEach(item -> { |
| | | DryOrderVo order = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + item.getCode()); |
| | | if (order != null) { |
| | | orderVo.setEnvHum(order.getEnvHum()); |
| | | orderVo.setEnvTemp(order.getEnvTemp()); |
| | | double watt = order.getWatt() - order.getDetailList().get(0).getWatt(); |
| | | orderVo.setWatt(orderVo.getWatt()==null? watt : orderVo.getWatt() + watt); |
| | | double steam = order.getSteam() - order.getDetailList().get(0).getSteam(); |
| | | orderVo.setSteam(orderVo.getSteam()==null? steam : orderVo.getSteam() + steam); |
| | | orderVo.setOriginWeight(orderVo.getOriginWeight()==null? order.getOriginWeight(): orderVo.getOriginWeight() + order.getOriginWeight()); |
| | | |
| | | double yield = order.getOriginWeight()*(1-(order.getInitial()/100))/(1-(order.getTarget()/100)); |
| | | orderVo.setYield(orderVo.getYield()==null? yield: orderVo.getYield() + yield); |
| | | double sub = order.getOriginWeight() - order.getYield(); |
| | | orderVo.setReduce(orderVo.getReduce()==null? sub: orderVo.getReduce() + sub); |
| | | |
| | | |
| | | } |
| | | }); |
| | | } |
| | | //redisUtil.get(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode() |
| | | return Result.OK(orderVo); |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return Result.ok(orderVo); |
| | | |
| | | @Override |
| | | public Result<?> statisticsDataHandle(StatisticsDataVo statsDataVo) { |
| | | return null; |
| | | } |
| | | } |