package com.fzzy.protocol.bhzn.service; import com.fzzy.api.Constant; import com.fzzy.api.data.ApiCommonDevice; 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.bhzn.cmd.CommandBuild; import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerEngine; import com.ld.io.api.InvokeResult; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; /** * 邦海智能粮情协议-网口设备 */ @Slf4j @Component public class Bhzn2GatewayGrainService implements GatewaySyncGranService { @Override public String getGrainProtocol() { return GatewayDeviceProtocol.GRAIN_FZZY_BHZN_WEB.getCode(); } @Override public BaseResp syncGrain(BaseReqData reqData) { BaseResp resp = new BaseResp(); GatewayDevice device = reqData.getDevice(); //获取连接中的设备信息 ApiCommonDevice apiCommonDevice = Constant.getCommonDeviceCacheByIp(device.getIp()); if (null == apiCommonDevice) { resp.setCode(500); resp.setMsg("系统未获取到下行连接设备信息,无法执行"); log.error("----------------系统未获取到下行连接设备信息,无法执行---------"); return resp; } if (Constant.YN_N.equals(apiCommonDevice.getStatus())) { resp.setCode(500); resp.setMsg("下行设备不在线---无法执行----" + device.getDeviceName()); log.error("下行设备不在线---无法执行----" + device.getDeviceName()); return resp; } device.setIp(apiCommonDevice.getIp()); device.setPort(apiCommonDevice.getPort()); reqData.setDevice(device); try { //Step 请求信息放入内存 ProtocolUtils.addSyncReq2Map(device.getDeviceSn(), reqData); // 生成粮情信息 String hexStr = CommandBuild.getInstance().getMsgCheck(device.getDeviceSn(), device.getBhId()); // 发送命令 InvokeResult message = BhznGrainV2ServerEngine.push(device.getIp(), device.getPort(), BytesUtil.hexStrToBytes(hexStr)); log.info("平台------>>>>控制柜:发送粮情检测命令-{}-{}", message,hexStr); // 封装返回信息 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; } @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(); } }