package cn.mrktech.bus.core;
import cn.mrktech.bus.config.KukaConfig;
import cn.mrktech.bus.domain.BusProduce;
import cn.mrktech.bus.models.CoreDataModel;
import cn.mrktech.bus.models.MachineModel;
import cn.mrktech.bus.models.dto.ProduceCellDTO;
import cn.mrktech.bus.service.IBusProduceService;
import cn.mrktech.common.utils.DateUtil;
import cn.mrktech.common.utils.DateUtils;
import cn.mrktech.common.utils.StringUtil;
import cn.mrktech.common.utils.http.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
/**
-
和MES的通讯接口,向MES发送订单完成和状态变更数据
*/
@Service
public class SendToMESService {
private static final Logger log = LoggerFactory.getLogger(SendToMESService.class);@Autowired
private KukaConfig kukaConfig;
@Autowired
private IBusProduceService produceService;
@Autowired
private CoreDataModel coreDataModel;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;/**
- 发送加工完成数据
- 请求参数说明: 字段名称 字段描述 数据类型
- workOrderId 三级任务单号 string
- machineId 设备编号 string
- fileId NC文件 string
- count 加工次数 int
- startTime 开工时间 datetime
- endTime 完工时间 datetime
- 返回参数说明: 字段名称 字段描述 数据类型
- code string
- msg string
- 定时任务读取数据库,发送成功后更新
*/
@Scheduled(initialDelay = 30000, fixedDelay = 5000)
public void sendOrderCompleteMsg(Long id){
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("send_flag", "0");
if(id!=null){
wrapper.gt(false,"id",id);
}
wrapper.last("limit 200");
Listps = produceService.list(wrapper);
if(psnull || ps.size()0){
return ;
}for (BusProduce p:ps ) { try { String requestUrl="http://"+kukaConfig.getMesHost()+":"+kukaConfig.getMesPort()+"/rest/mes/machine/work_order_finish"; Map map=new HashMap(); map.put("workOrderId",p.getWorkOrderId()); map.put("machineId",p.getMachineId()); map.put("fileId",p.getFileId()); map.put("count",p.getCount()); map.put("startTime", DateUtil.dateToString(p.getStartTime(),DateUtil.DEFAULT_DATETIME_FORMAT_SEC)); map.put("endTime",DateUtil.dateToString(p.getEndTime(),DateUtil.DEFAULT_DATETIME_FORMAT_SEC)); String s = JSON.toJSONString(map); String result = HttpUtils.sendJsonPost(requestUrl, s); if(StringUtil.isBlank(result)){ log.info("回传加工完成通讯失败:"+p.getId()); //请求失败 continue; } JSONObject jsonObject = JSONObject.parseObject(result); if(jsonObject.containsKey("code") && "200".equals(jsonObject.getString("code"))){ p.setSendFlag("1"); produceService.updateById(p); }else{ log.info("回传加工完成通讯返回失败,返回信息:"+result); } }catch (Exception e){ log.error("回传加工完成通讯异常:"+e.getMessage()); } }
}
/**
- 发送雕刻机实时状态 定时发送,状态变更时也发送
- 请求参数说明: 字段名称 字段描述
- machineId 设备编码
- runningStatus 设备状态
- type 发送类型
- errorCode 错误代码
- errorMessage 错误信息
- sendTime 发送时间
- 返回参数说明: 字段名称 字段描述
- code
- msg
- @return
*/
@Scheduled(initialDelay = 30000, fixedDelay = 300000)
public void sendStatusSchedule(){
Collectionlist = coreDataModel.getMachineMap().values();
for (MachineModel model:list
) {
sendMachineChangeMsg(model,false);
}
}
public void sendMachineChangeMsg(MachineModel model,boolean isCHange){
taskExecutor.execute(new Runnable() {
@Override
public void run() {
String requestUrl="http://"+kukaConfig.getMesHost()+":"+kukaConfig.getMesPort()+"/rest/mes/machine/machine_status_collect";
Map map=new ConcurrentHashMap();
map.put("machineId",model.getMachineInfo().getMachineId());if(model.isHasWarm() && model.getErrorList().size()>0){ map.put("runningStatus","3"); map.put("errorCode","雕刻机异常"); map.put("errorMessage",model.getErrorList().get(0)); }else{ switch (model.getState()){ case RUNNING: map.put("runningStatus","1"); break; case WAITING: map.put("runningStatus","4"); break; case STOPPED: map.put("runningStatus","2"); break; default: map.put("runningStatus","2"); break; } } map.put("type",isCHange?2:1); map.put("sendTime",DateUtil.getCurrentTime(DateUtil.DEFAULT_DATETIME_FORMAT_SEC)); String s = JSON.toJSONString(map); String result = HttpUtils.sendJsonPost(requestUrl, s); if(StringUtil.isBlank(result)){ log.info("发送设备状态通讯失败:设备id["+model.getKey()+"]设备状态["+model.getState()+"]"); return; } JSONObject jsonObject = JSONObject.parseObject(result); if(!jsonObject.containsKey("code") || !"200".equals(jsonObject.getString("code"))){ log.info("发送设备状态通讯返回失败:设备id["+model.getKey()+"]设备状态["+model.getState()+"];返回信息:"+result); } } });
}
}