|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.core.annotations.ManagerAuth; | 
|---|
|  |  |  | import com.core.common.Arith; | 
|---|
|  |  |  | import com.core.common.BaseRes; | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.R; | 
|---|
|  |  |  | import com.zy.asrs.domain.enums.CrnStatusType; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.zy.asrs.domain.param.SystemSwitchParam; | 
|---|
|  |  |  | import com.zy.asrs.domain.vo.*; | 
|---|
|  |  |  | import com.zy.asrs.entity.BasDevp; | 
|---|
|  |  |  | import com.zy.asrs.entity.BasMap; | 
|---|
|  |  |  | import com.zy.asrs.entity.WrkMast; | 
|---|
|  |  |  | import com.zy.asrs.mapper.LocMastMapper; | 
|---|
|  |  |  | import com.zy.asrs.service.BasDevpService; | 
|---|
|  |  |  | import com.zy.asrs.service.BasMapService; | 
|---|
|  |  |  | import com.zy.asrs.service.WrkMastService; | 
|---|
|  |  |  | import com.zy.common.CodeRes; | 
|---|
|  |  |  | import com.zy.common.model.MapNode; | 
|---|
|  |  |  | import com.zy.common.model.enums.NavigationMapType; | 
|---|
|  |  |  | import com.zy.common.utils.Http; | 
|---|
|  |  |  | import com.zy.common.utils.NavigateMapData; | 
|---|
|  |  |  | import com.zy.common.utils.RedisUtil; | 
|---|
|  |  |  | import com.zy.core.CrnThread; | 
|---|
|  |  |  | import com.zy.core.DevpThread; | 
|---|
|  |  |  | import com.zy.core.Slave; | 
|---|
|  |  |  | import com.zy.core.cache.OutputQueue; | 
|---|
|  |  |  | import com.zy.core.cache.SlaveConnection; | 
|---|
|  |  |  | import com.zy.core.enums.CrnModeType; | 
|---|
|  |  |  | import com.zy.core.enums.RedisKeyType; | 
|---|
|  |  |  | import com.zy.core.enums.SlaveType; | 
|---|
|  |  |  | import com.zy.core.model.CrnSlave; | 
|---|
|  |  |  | import com.zy.core.model.DevpSlave; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.zy.core.properties.SlaveProperties; | 
|---|
|  |  |  | import com.zy.core.properties.SystemProperties; | 
|---|
|  |  |  | import com.zy.core.thread.BarcodeThread; | 
|---|
|  |  |  | import com.zy.core.thread.CrnThread; | 
|---|
|  |  |  | import com.zy.core.thread.DevpThread; | 
|---|
|  |  |  | import com.zy.core.thread.ScaleThread; | 
|---|
|  |  |  | import com.zy.core.thread.SiemensCrnThread; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.io.BufferedReader; | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.io.FileInputStream; | 
|---|
|  |  |  | import java.io.InputStreamReader; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BasDevpService basDevpService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BasMapService basMapService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocMastMapper locMastMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisUtil redisUtil; | 
|---|
|  |  |  | @Value("${super.pwd}") | 
|---|
|  |  |  | private String superPwd; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private NavigateMapData navigateMapData; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/system/running/status") | 
|---|
|  |  |  | @ManagerAuth(memo = "系统运行状态") | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.error("口令错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Thread.sleep(2000L); | 
|---|
|  |  |  | Thread.sleep(200L); | 
|---|
|  |  |  | SystemProperties.WCS_RUNNING_STATUS.set(param.getOperatorType()==1?Boolean.TRUE:Boolean.FALSE); | 
|---|
|  |  |  | return R.ok().add(Cools.add("status", SystemProperties.WCS_RUNNING_STATUS.get())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | // 获取堆垛机信息 | 
|---|
|  |  |  | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId()); | 
|---|
|  |  |  | if (crnThread == null) { | 
|---|
|  |  |  | log.error("{}号堆垛机连接失败", crn.getId()); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CrnProtocol crnProtocol = crnThread.getCrnProtocol(); | 
|---|
|  |  |  | if (crnProtocol == null) { | 
|---|
|  |  |  | log.error("{}号堆垛机连接失败", crn.getId()); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CrnLatestDataVo vo = new CrnLatestDataVo(); | 
|---|
|  |  |  | 
|---|
|  |  |  | //            Short totalBay = locMastMapper.selectTotalBay(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | vo.setOffset((double) new Random().nextInt(560));     //  堆垛机偏移量 | 
|---|
|  |  |  | vo.setBay(crnProtocol.getBay());    //  当前列 | 
|---|
|  |  |  | vo.setBay((short) (crnProtocol.getBay() - crn.getOffset()));    //  当前列 | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 堆垛机状态判断 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | if (crnProtocol.getTaskNo()>0) { | 
|---|
|  |  |  | WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo()); | 
|---|
|  |  |  | if (wrkMast != null) { | 
|---|
|  |  |  | vo.setCrnStatus(CrnStatusType.process(wrkMast.getIoType())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (crn.getId() == 1 && crnProtocol.getAlarm() > 0) { | 
|---|
|  |  |  | vo.setCrnStatus(CrnStatusType.MACHINE_ERROR); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | vo.setCrnStatus(crnProtocol.modeType.equals(CrnModeType.AUTO)? CrnStatusType.MACHINE_AUTO: CrnStatusType.MACHINE_UN_AUTO); | 
|---|
|  |  |  | if (crnProtocol.getTaskNo()>0) { | 
|---|
|  |  |  | WrkMast wrkMast = wrkMastService.selectById(crnProtocol.getTaskNo()); | 
|---|
|  |  |  | if (wrkMast != null) { | 
|---|
|  |  |  | vo.setCrnStatus(CrnStatusType.process(wrkMast.getIoType())); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | vo.setCrnStatus(crnProtocol.modeType.equals(CrnModeType.AUTO)? CrnStatusType.MACHINE_AUTO: CrnStatusType.MACHINE_UN_AUTO); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | vo.setCrnStatus(crnProtocol.modeType.equals(CrnModeType.AUTO)? CrnStatusType.MACHINE_AUTO: CrnStatusType.MACHINE_UN_AUTO); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | vos.add(vo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | List<BarcodeDataVo> list = new ArrayList<>(); | 
|---|
|  |  |  | for (Slave barcode : slaveProperties.getBarcode()) { | 
|---|
|  |  |  | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcode.getId()); | 
|---|
|  |  |  | if (null == barcodeThread) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BarcodeDataVo vo = new BarcodeDataVo(); | 
|---|
|  |  |  | vo.setBarcodeId(barcode.getId()); | 
|---|
|  |  |  | vo.setCodeValue(barcodeThread.getBarcode()); | 
|---|
|  |  |  | list.add(vo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/latest/data/scale") | 
|---|
|  |  |  | @ManagerAuth(memo = "磅秤实时数据") | 
|---|
|  |  |  | public R scaleLatestData(){ | 
|---|
|  |  |  | List<ScaleDataVo> list = new ArrayList<>(); | 
|---|
|  |  |  | for (Slave scale : slaveProperties.getScale()) { | 
|---|
|  |  |  | ScaleThread scaleThread = (ScaleThread) SlaveConnection.get(SlaveType.Scale, scale.getId()); | 
|---|
|  |  |  | if (null == scaleThread) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ScaleDataVo vo = new ScaleDataVo(); | 
|---|
|  |  |  | vo.setScaleId(scale.getId()); | 
|---|
|  |  |  | vo.setValue(String.valueOf(Arith.multiplys(1, scaleThread.getScale(), 1))); | 
|---|
|  |  |  | list.add(vo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(list); | 
|---|
|  |  |  | 
|---|
|  |  |  | CrnDetailVo vo = new CrnDetailVo(); | 
|---|
|  |  |  | for (CrnSlave crnSlave : slaveProperties.getCrn()) { | 
|---|
|  |  |  | if (crnSlave.getId().equals(crnNo)) { | 
|---|
|  |  |  | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnSlave.getId()); | 
|---|
|  |  |  | SiemensCrnThread crnThread = (SiemensCrnThread) SlaveConnection.get(SlaveType.Crn, crnSlave.getId()); | 
|---|
|  |  |  | CrnProtocol crnProtocol = crnThread.getCrnProtocol(); | 
|---|
|  |  |  | vo.setCrnNo(crnNo); | 
|---|
|  |  |  | vo.setWorkNo(crnProtocol.getTaskNo()); | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/barcode/output/site") | 
|---|
|  |  |  | public R crnOutput(){ | 
|---|
|  |  |  | //        StringBuilder str = new StringBuilder(); | 
|---|
|  |  |  | //        String s; | 
|---|
|  |  |  | //        int i = 0; | 
|---|
|  |  |  | //        while((s = OutputQueue.CRN.poll()) != null && i <=32) { | 
|---|
|  |  |  | //            str.append("\n").append(s); | 
|---|
|  |  |  | //            i++; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        return R.ok().add(str.toString()); | 
|---|
|  |  |  | return R.ok().add(JSON.toJSONString(new ArrayList<>(OutputQueue.BARCODE))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/barcode/list") | 
|---|
|  |  |  | public R barcodeList(){ | 
|---|
|  |  |  | ArrayList<HashMap<String, Object>> list = new ArrayList<>(); | 
|---|
|  |  |  | for (Slave slave : slaveProperties.getBarcode()) { | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, slave.getId()); | 
|---|
|  |  |  | map.put("id", slave.getId()); | 
|---|
|  |  |  | map.put("barcode", barcodeThread.getBarcode()); | 
|---|
|  |  |  | list.add(map); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取地图数据 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("/map/{lev}/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R getMapFromRedis(@PathVariable Integer lev) { | 
|---|
|  |  |  | Object data = redisUtil.get(RedisKeyType.MAP.key + lev); | 
|---|
|  |  |  | if (data == null) { | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BasMap basMap = JSON.parseObject(data.toString(), BasMap.class); | 
|---|
|  |  |  | //解析json地图数据 | 
|---|
|  |  |  | ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class); | 
|---|
|  |  |  | navigateMapData.setLev(lev); | 
|---|
|  |  |  | List<List<MapNode>> lists = navigateMapData.filterMap(NavigationMapType.NONE.id, arrayList, lev, null, null);//过滤地图数据 | 
|---|
|  |  |  | return R.ok().add(lists); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取PLC2数据 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("/plc2/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R getMapFromPlc2() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String mapFilename = "plc2.json"; | 
|---|
|  |  |  | String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径 | 
|---|
|  |  |  | File file = new File(fileName); | 
|---|
|  |  |  | StringBuffer stringBuffer = new StringBuffer(); | 
|---|
|  |  |  | if (file.isFile() && file.exists()) { | 
|---|
|  |  |  | InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); | 
|---|
|  |  |  | BufferedReader br = new BufferedReader(isr); | 
|---|
|  |  |  | String lineTxt = null; | 
|---|
|  |  |  | while ((lineTxt = br.readLine()) != null) { | 
|---|
|  |  |  | stringBuffer.append(lineTxt); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | br.close(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //解析json地图数据 | 
|---|
|  |  |  | ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); | 
|---|
|  |  |  | List<List<MapNode>> lists = new ArrayList<>(); | 
|---|
|  |  |  | //重建数据格式 | 
|---|
|  |  |  | for (int i = 0; i < arrayList.size(); i++) { | 
|---|
|  |  |  | Object obj = arrayList.get(i); | 
|---|
|  |  |  | List<MapNode> list = JSON.parseArray(obj.toString(), MapNode.class); | 
|---|
|  |  |  | for (int j = 0; j < list.size(); j++) { | 
|---|
|  |  |  | MapNode mapNode = list.get(j); | 
|---|
|  |  |  | list.set(j, mapNode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | lists.add(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(lists); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 重置redis中的地图,将占用的库位全部解除 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("/map/resetMap/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R resetMapToRedis() { | 
|---|
|  |  |  | for (int i = 1; i <= 10; i++) {//总共10层楼 | 
|---|
|  |  |  | BasMap basMap = basMapService.selectLatestMap(i); | 
|---|
|  |  |  | //载入地图 | 
|---|
|  |  |  | navigateMapData.setLev(i); | 
|---|
|  |  |  | List<List<MapNode>> lists = navigateMapData.getJsonData(-1, null, null);//获取完整地图(包括入库出库) | 
|---|
|  |  |  | if (basMap == null) { | 
|---|
|  |  |  | //数据库中也不存在地图数据,从地图文件中获取 | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | basMap = new BasMap(); | 
|---|
|  |  |  | basMap.setData(JSON.toJSONString(lists)); | 
|---|
|  |  |  | basMap.setCreateTime(new Date()); | 
|---|
|  |  |  | basMap.setUpdateTime(new Date()); | 
|---|
|  |  |  | basMap.setLev(i); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!basMapService.insert(basMap)) { | 
|---|
|  |  |  | log.info("地图数据存储失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | basMap.setData(JSON.toJSONString(lists)); | 
|---|
|  |  |  | basMap.setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!basMapService.updateById(basMap)) { | 
|---|
|  |  |  | log.info("地图数据存储失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将数据库地图数据存入redis | 
|---|
|  |  |  | redisUtil.set(RedisKeyType.MAP.key + i, JSON.toJSONString(basMap)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 重置redis中的地图,将占用的库位全部解除 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("/map/resetMap/{lev}") | 
|---|
|  |  |  | @ManagerAuth(memo = "重置Redis地图") | 
|---|
|  |  |  | public R resetMapToRedisByLev(@PathVariable Integer lev, HttpServletRequest request) { | 
|---|
|  |  |  | // super账号 | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | if (token == null) { | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String deToken = Cools.deTokn(token, superPwd); | 
|---|
|  |  |  | if (deToken == null) { | 
|---|
|  |  |  | return R.error("权限不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!"super".equals(deToken.substring(13))) { | 
|---|
|  |  |  | return R.error("权限不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BasMap basMap = basMapService.selectLatestMap(lev); | 
|---|
|  |  |  | //载入地图 | 
|---|
|  |  |  | navigateMapData.setLev(lev); | 
|---|
|  |  |  | List<List<MapNode>> lists = navigateMapData.getJsonData(-1, null, null);//获取完整地图(包括入库出库) | 
|---|
|  |  |  | if (basMap == null) { | 
|---|
|  |  |  | //数据库中也不存在地图数据,从地图文件中获取 | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | basMap = new BasMap(); | 
|---|
|  |  |  | basMap.setData(JSON.toJSONString(lists)); | 
|---|
|  |  |  | basMap.setCreateTime(new Date()); | 
|---|
|  |  |  | basMap.setUpdateTime(new Date()); | 
|---|
|  |  |  | basMap.setLev(lev); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!basMapService.insert(basMap)) { | 
|---|
|  |  |  | log.info("地图数据存储失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | basMap.setData(JSON.toJSONString(lists)); | 
|---|
|  |  |  | basMap.setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!basMapService.updateById(basMap)) { | 
|---|
|  |  |  | log.info("地图数据存储失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将数据库地图数据存入redis | 
|---|
|  |  |  | redisUtil.set(RedisKeyType.MAP.key + lev, JSON.toJSONString(basMap)); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|