|  |  | 
 |  |  | package com.zy.asrs.controller; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
 |  |  | import com.core.annotations.ManagerAuth; | 
 |  |  | import com.core.common.Cools; | 
 |  |  | import com.core.common.R; | 
 |  |  | import com.zy.asrs.domain.vo.PlcErrorTableVo; | 
 |  |  | import com.zy.asrs.domain.vo.SiteTableVo; | 
 |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RestController; | 
 |  |  | import com.zy.asrs.entity.BasDevp; | 
 |  |  | import com.zy.asrs.entity.BasSte; | 
 |  |  | import com.zy.asrs.entity.WrkCharge; | 
 |  |  | import com.zy.asrs.service.BasDevpService; | 
 |  |  | import com.zy.asrs.service.BasSteService; | 
 |  |  | import com.zy.asrs.service.WrkChargeService; | 
 |  |  | import com.zy.common.model.enums.WrkChargeType; | 
 |  |  | import com.zy.common.service.CommonService; | 
 |  |  | import com.zy.core.DevpThread; | 
 |  |  | import com.zy.core.News; | 
 |  |  | import com.zy.core.cache.MessageQueue; | 
 |  |  | import com.zy.core.cache.OutputQueue; | 
 |  |  | import com.zy.core.cache.SlaveConnection; | 
 |  |  | import com.zy.core.enums.SlaveType; | 
 |  |  | import com.zy.core.enums.SteChargeType; | 
 |  |  | import com.zy.core.enums.SteStatusType; | 
 |  |  | import com.zy.core.model.DevpSlave; | 
 |  |  | import com.zy.core.model.Task; | 
 |  |  | import com.zy.core.model.protocol.StaProtocol; | 
 |  |  | import com.zy.core.model.protocol.SteProtocol; | 
 |  |  | import com.zy.core.properties.SlaveProperties; | 
 |  |  | import com.zy.core.thread.SiemensDevpThread; | 
 |  |  | import com.zy.core.thread.SteThread; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Random; | 
 |  |  | import java.util.concurrent.atomic.AtomicInteger; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 输送设备接口 | 
 |  |  |  * Created by vincent on 2020-06-01 | 
 |  |  |  */ | 
 |  |  | @Slf4j | 
 |  |  | @RestController | 
 |  |  | @RequestMapping("/site") | 
 |  |  | public class SiteController { | 
 |  |  |  | 
 |  |  |     private AtomicInteger integer = new AtomicInteger(); | 
 |  |  |     @Autowired | 
 |  |  |     private SlaveProperties slaveProperties; | 
 |  |  |     @Autowired | 
 |  |  |     private BasDevpService basDevpService; | 
 |  |  |     @Autowired | 
 |  |  |     private WrkChargeService wrkChargeService; | 
 |  |  |     @Autowired | 
 |  |  |     private BasSteService basSteService; | 
 |  |  |     @Autowired | 
 |  |  |     private CommonService commonService; | 
 |  |  |  | 
 |  |  |     @GetMapping("/io/mode/info/site") | 
 |  |  |     @ManagerAuth(memo = "充电模式") | 
 |  |  |     public R ioMode(){ | 
 |  |  |         List<Map<String, Object>> res = new ArrayList<>(); | 
 |  |  |         for (DevpSlave devp : slaveProperties.getDevp()) { | 
 |  |  |             SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); | 
 |  |  |             Map<String, Object> map1 = new HashMap<>(); | 
 |  |  |             map1.put("floor", 1); | 
 |  |  |             map1.put("modeVal", !devpThread.charge0); | 
 |  |  |             map1.put("modeDesc", !devpThread.charge0?"充电中":"关闭"); | 
 |  |  |             res.add(map1); | 
 |  |  |             Map<String, Object> map2 = new HashMap<>(); | 
 |  |  |             map2.put("floor", 2); | 
 |  |  |             map2.put("modeVal", devpThread.charge1); | 
 |  |  |             map2.put("modeDesc", devpThread.charge1?"充电中":"关闭"); | 
 |  |  |             res.add(map2); | 
 |  |  |         } | 
 |  |  |         return R.ok().add(res); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping("/table/site") | 
 |  |  |     @ManagerAuth(memo = "站点信息表") | 
 |  |  |     public R siteTable(){ | 
 |  |  |         List<SiteTableVo> list = new ArrayList<>(); | 
 |  |  |         for (int i=0;i<32;i++){ | 
 |  |  |             SiteTableVo vo = new SiteTableVo(); | 
 |  |  |             vo.setDevNo(String.valueOf(i)); | 
 |  |  |             vo.setWorkNo("1204"); | 
 |  |  |             vo.setAutoing("Y"); | 
 |  |  |             vo.setCanining("Y"); | 
 |  |  |             vo.setCanouting("N"); | 
 |  |  |             vo.setHighLow("1"); | 
 |  |  |             vo.setInreqIn("Y"); | 
 |  |  |             vo.setInreqOut("N"); | 
 |  |  |             vo.setLoading("Y"); | 
 |  |  |             if (integer.get()%5 == 0) { | 
 |  |  |                 vo.setStaNo("0000"); | 
 |  |  |             } else { | 
 |  |  |                 vo.setStaNo("9998"); | 
 |  |  |             } | 
 |  |  |             list.add(vo); | 
 |  |  |         // 内存数据 | 
 |  |  |         Map<Integer, StaProtocol> station = new HashMap<>(); | 
 |  |  |         for (DevpSlave devp : slaveProperties.getDevp()) { | 
 |  |  |             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); | 
 |  |  |             station.putAll(devpThread.getStation()); | 
 |  |  |         } | 
 |  |  |         integer.getAndIncrement(); | 
 |  |  |         // 持久数据 | 
 |  |  |         List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>().orderBy("dev_no")); | 
 |  |  |         for (BasDevp devp : basDevps) { | 
 |  |  |             SiteTableVo vo = new SiteTableVo(); | 
 |  |  |             vo.setDevNo(devp.getDevNo());    // 站点编号 | 
 |  |  |             list.add(vo); | 
 |  |  |             StaProtocol staProtocol = station.get(devp.getDevNo()); | 
 |  |  |             if (null == staProtocol) { continue; } | 
 |  |  |             vo.setWorkNo(staProtocol.getWorkNo());   //  工作号 | 
 |  |  |             vo.setAutoing(staProtocol.isAutoing()?"Y":"N");     //  自动 | 
 |  |  |             vo.setLoading(staProtocol.isLoading()?"Y":"N");     // 有物 | 
 |  |  |             vo.setInEnable(staProtocol.isInEnable()?"Y":"N");   // 可入 | 
 |  |  |             vo.setOutEnable(staProtocol.isOutEnable()?"Y":"N"); // 可出 | 
 |  |  |             vo.setLiftArrival(staProtocol.isLiftArrival()?"Y":"N"); // 提升机到位信号 | 
 |  |  |             vo.setShuttleTakeEnable(staProtocol.isShuttleTakeEnable()?"Y":"N"); // 穿梭车可取信号 | 
 |  |  |             vo.setPakMk(staProtocol.isPakMk()?"Y":"N");       // 入库标记 | 
 |  |  |             vo.setEmptyMk(staProtocol.isEmptyMk()?"Y":"N");     // 空板信号 | 
 |  |  |             vo.setStaNo(staProtocol.getStaNo());                // 目标站 | 
 |  |  |             vo.setLocType1(staProtocol.getLocType1()==null?"-":staProtocol.getLocType1().toString());                // 高低信号 | 
 |  |  |         } | 
 |  |  |         return R.ok().add(list); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |     @ManagerAuth(memo = "输送设备plc异常信息表") | 
 |  |  |     public R plcErrorTable(){ | 
 |  |  |         List<PlcErrorTableVo> list = new ArrayList<>(); | 
 |  |  |         for (int i = 0; i<new Random().nextInt(13); i++){ | 
 |  |  |             PlcErrorTableVo table = new PlcErrorTableVo(); | 
 |  |  |             table.setNo(String.valueOf(i)); | 
 |  |  |             table.setError("异常信息"); | 
 |  |  |             table.setPlcDesc("plc异常描述"); | 
 |  |  |             list.add(table); | 
 |  |  |         for (DevpSlave devp : slaveProperties.getDevp()) { | 
 |  |  |             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); | 
 |  |  |             Map<Integer, StaProtocol> station = devpThread.getStation(); | 
 |  |  |  | 
 |  |  |             for(Map.Entry<Integer, StaProtocol> entry : station.entrySet()) { | 
 |  |  |                 StaProtocol staProtocol = entry.getValue(); | 
 |  |  | //                if (staProtocol) { | 
 |  |  | //                    PlcErrorTableVo vo = new PlcErrorTableVo(); | 
 |  |  | //                    vo.setNo(entry.getKey());   //  序号 | 
 |  |  | // | 
 |  |  | //                    vo.setPlcDesc("");  //  todo:luxiaotao plc错误描述 | 
 |  |  | //                    vo.setError("");    //  todo:luxiaotao 异常信息 | 
 |  |  | //                    list.add(vo); | 
 |  |  | //                } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         list.sort((o1, o2) -> { | 
 |  |  |             if (o1.getNo().compareTo(o2.getNo()) > 0){ | 
 |  |  |                 return 1; | 
 |  |  | 
 |  |  |     @PostMapping("/output/site") | 
 |  |  |     @ManagerAuth(memo = "站点设备报文日志输出") | 
 |  |  |     public R siteOutput(){ | 
 |  |  |         String str = "\n" +new Date().toLocaleString() + "【2020-5-29 13:14:22】扫描plcA 目标站--27328372372832763643234323432342"; | 
 |  |  |         return R.ok().add(str); | 
 |  |  |         StringBuilder str = new StringBuilder(); | 
 |  |  |         String s; | 
 |  |  |         int i = 0; | 
 |  |  |         while((s = OutputQueue.DEVP.poll()) != null && i <=10) { | 
 |  |  |             str.append("\n").append(s); | 
 |  |  |             i++; | 
 |  |  |         } | 
 |  |  |         return R.ok().add(str.toString()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /****************************************************************/ | 
 |  |  |     /************************** 详情操作 ******************************/ | 
 |  |  |     /****************************************************************/ | 
 |  |  |  | 
 |  |  |     @GetMapping("/detl/{siteId}") | 
 |  |  |     public R siteDetl(@PathVariable("siteId") Integer siteId){ | 
 |  |  |         SiteTableVo vo = new SiteTableVo(); | 
 |  |  |         for (DevpSlave devp : slaveProperties.getDevp()) { | 
 |  |  |             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); | 
 |  |  |             Map<Integer, StaProtocol> station = devpThread.getStation(); | 
 |  |  |             for (Map.Entry<Integer, StaProtocol> entry : station.entrySet()) { | 
 |  |  |                 if (siteId.equals(entry.getKey())) { | 
 |  |  |                     StaProtocol staProtocol = entry.getValue(); | 
 |  |  |                     vo.setDevNo(entry.getKey());    // 站点编号 | 
 |  |  |                     vo.setWorkNo(staProtocol.getWorkNo());   //  工作号 | 
 |  |  |                     vo.setAutoing(staProtocol.isAutoing()?"Y":"N");     //  自动 | 
 |  |  |                     vo.setLoading(staProtocol.isLoading()?"Y":"N");     // 有物 | 
 |  |  |                     vo.setInEnable(staProtocol.isInEnable()?"Y":"N");   // 可入 | 
 |  |  |                     vo.setOutEnable(staProtocol.isOutEnable()?"Y":"N"); // 可出 | 
 |  |  |                     vo.setPakMk(staProtocol.isPakMk()?"Y":"N");       // 需求1 | 
 |  |  |                     vo.setEmptyMk(staProtocol.isEmptyMk()?"Y":"N");     // 空板信号 | 
 |  |  |                     vo.setStaNo(staProtocol.getStaNo());                // 目标站 | 
 |  |  |                     return R.ok().add(vo); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return R.error("plc已掉线"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping("/detl/update") | 
 |  |  |     @ManagerAuth(memo = "修改站点数据") | 
 |  |  |     public R siteDetlUpdate(@RequestParam Integer siteId, | 
 |  |  |                             @RequestParam Short workNo, | 
 |  |  |                             @RequestParam Short staNo, | 
 |  |  |                             @RequestParam String pakMk){ | 
 |  |  |         for (DevpSlave devp : slaveProperties.getDevp()) { | 
 |  |  |             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId()); | 
 |  |  |             Map<Integer, StaProtocol> station = devpThread.getStation(); | 
 |  |  |             for (Map.Entry<Integer, StaProtocol> entry : station.entrySet()) { | 
 |  |  |                 if (siteId.equals(entry.getKey())) { | 
 |  |  |                     StaProtocol staProtocol = entry.getValue(); | 
 |  |  |                     staProtocol = staProtocol.clone(); | 
 |  |  |                     if (workNo != null) { | 
 |  |  |                         staProtocol.setWorkNo(workNo); | 
 |  |  |                     } | 
 |  |  |                     if (staNo != null) { | 
 |  |  |                         staProtocol.setStaNo(staNo); | 
 |  |  |                     } | 
 |  |  |                     if (pakMk != null) { | 
 |  |  |                         staProtocol.setPakMk(pakMk.equals("Y")); | 
 |  |  |                     } | 
 |  |  |                     boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol)); | 
 |  |  |                     if (result) { | 
 |  |  |                         return R.ok(); | 
 |  |  |                     } else { | 
 |  |  |                         return R.error("下发命令失败"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return R.error("plc已掉线"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |