package com.fzzy.protocol.youxian1.service; import com.fzzy.api.data.GatewayDeviceProtocol; import com.fzzy.api.utils.BytesUtil; import com.fzzy.gateway.api.GatewaySyncGranService; import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.youxian1.client.ClientEngine; import com.ld.io.api.InvokeResult; import io.netty.channel.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; /** * 游仙分库协议-粮情采集入口 */ @Slf4j @Component public class Youxian1GatewayGrainService implements GatewaySyncGranService { @Override public String getGrainProtocol() { return GatewayDeviceProtocol.GRAIN_YOUXIAN1_2023.getCode(); } /** * @param reqData * @return */ @Override public synchronized BaseResp syncGrain(BaseReqData reqData) { BaseResp resp = new BaseResp(); GatewayDevice device = reqData.getDevice(); if (null == device) { resp.setCode(500); resp.setMsg("系统未获取到下行连接设备信息,无法执行"); log.error("----------------系统未获取到下行连接设备信息,无法执行---------"); return resp; } try { //Step 请求信息放入内存 ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); // 判断数据有没有收取完整 String[] attCable = device.getCableRule().split("-"); int cableZ = Integer.valueOf(attCable[0]); int cableY = Integer.valueOf(attCable[1]); int cableX = Integer.valueOf(attCable[2]); // 生成粮情信息 String hexCmd = buildGrainCmd(device, cableX, cableY, cableZ); Channel channel = ClientEngine.getChannel(device.getIp()); if (null == channel) { ClientEngine.start(device.getIp(), device.getPort()); Thread.sleep(500); } InvokeResult message = ClientEngine.send(BytesUtil.hexStrToBytes(hexCmd)); log.error("平台------>>>>主控:发送粮情检测命令-{}---{}", message, hexCmd); // 封装返回信息 if (!InvokeResult.SUCCESS.getCode().equals(message.getCode())) { log.error("平台------>>>>控制柜:发送粮情检测命令-失败{}", message.getMessage()); resp.setCode(500); resp.setMsg("平台------>>>>控制柜:发送粮情检测命令-失败:" + message.getMessage()); } } catch (Exception e) { log.error("粮情检测异常:{}", e); resp.setCode(500); resp.setMsg("平台------>>>>控制柜:发送粮情检测命令:" + e.getMessage()); return resp; } return resp; } /** * (发采集命令): A5 A5 A5 80 8X 41 8Y 8Z 8M * 8 X--分机号(90--16号分机) 8Y---起始间 8Z---本仓间数 8M---每间点数 *

* X--控制器号;Y--起始间;Z--结束间;M--点数 * 生成粮情采集命令 * * @param device * @return */ private String buildGrainCmd(GatewayDevice device, int cableX, int cableY, int cableZ) { String hexCmd = "A5A5A580{id}41{start}{end}{num}"; //分机号,待根据配置进行调整,暂时未知计算规则 TODO String hexId = "90"; hexCmd = hexCmd.replace("{id}", hexId); //开始列,默认从1开始 String hexStart = "81"; hexCmd = hexCmd.replace("{start}", hexStart); //截至列 String hexEnd = "8" + cableX; hexCmd = hexCmd.replace("{end}", hexEnd); //点位数,目前未知具体计算规则 TODO int num = cableX * cableY * cableZ; String hexNum = BytesUtil.intToHexStr1(num); hexCmd = hexCmd.replace("{num}", hexNum); return hexCmd; } @Override public BaseResp syncGrainTh(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp syncConf(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp writeConf(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp initCable(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp disconnect(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp transparent(BaseReqData reqData) { return new BaseResp(); } }