package com.ld.igds.protocol.bhzn.verb.analysis; import com.alibaba.fastjson.JSONObject; import com.ld.igds.common.CoreDeviceService; import com.ld.igds.constant.DeviceStatus; import com.ld.igds.io.constant.OrderRespEnum; import com.ld.igds.io.notify.NotifyWebInvoker; import com.ld.igds.io.request.ExeDevice; import com.ld.igds.models.Device; import com.ld.igds.models.DeviceSer; import com.ld.igds.protocol.bhzn.verb.command.BaseRemoteImpl; import com.ld.igds.protocol.bhzn.verb.dto.IoMessage; import com.ld.igds.protocol.bhzn.verb.dto.Res247; import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerUtils; import com.ld.igds.temp.dto.TempParam; import com.ld.igds.util.BytesUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * 设备操作类 */ @Slf4j @Component(AnalysisDevice.BEAN_ID) public class AnalysisDevice extends BaseRemoteImpl { public static final String BEAN_ID = "bhzn.analysisDevice"; @Autowired private CoreDeviceService deviceService; @Autowired private NotifyWebInvoker notifyInvoker; /** * 设备状态 * * @param reMessage * @param ser */ public void analysis(IoMessage reMessage, DeviceSer ser) { Res247 res247 = JSONObject.parseObject(reMessage.getContentStr(),Res247.class); AnalysisWin(ser,res247.getWin_Str()); AnalysisFan(ser,res247.getFan_Str()); AnalysisAir(ser,res247.getAir_Str()); AnalysisLight(ser,res247.getLight_Str()); // BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),0, ""); // } deviceService.updateStatus(ser.getCompanyId(), ser.getId(), BhznVerbServerUtils.getStatusMap()); // notifyInvoker.notifyAnalysisStatusSuccess(ser.getCompanyId(), ser.getId(), OrderRespEnum.MSG_SUCCESS, "设备状态查询成功并完成解析!"); } /** * 解析窗户状态 * @param ser * @param winStr */ private void AnalysisWin(DeviceSer ser,Integer[] winStr){ if(winStr == null || winStr.length == 0)return; for (int i=1;i<= winStr.length;i++) { BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i, getWinStatus(winStr[i-1])); } } /** * 解析风机状态 * @param ser * @param fanStr */ private void AnalysisFan(DeviceSer ser,Integer[] fanStr){ if(fanStr == null || fanStr.length == 0)return; for (int i=1;i<= fanStr.length;i++) { BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+20, getFanStatus(fanStr[i-1])); } } /** * 解析空调状态 * @param ser * @param airStr */ private void AnalysisAir(DeviceSer ser,Integer[] airStr){ if(airStr == null || airStr.length == 0)return; Integer s= 0; for (int i=1;i<= airStr.length;i++) { List devices = deviceService.getCacheDeviceBySerId(ser.getCompanyId(),ser.getId()); // 调整缓存 if (null == devices || devices.isEmpty()) return; // 更新缓存 Device temp; s = airStr[i-1]; for (int j = 0; j < devices.size(); j++) { if(i == (devices.get(j).getPassCode() - 40)){ // 4608 -12 00 String hexStr = BytesUtil.intToHexStr(s); for (int k =0;k<4;k++){ if(hexStr.length() < 16){ hexStr = "0" + hexStr; } } // 12 String l = hexStr.substring(4,6); //00 String h = hexStr.substring(6,8); String hb = BytesUtil.toBinary8String(BytesUtil.hexToInt(h)); //00000000 0000 0 000 Integer status = Integer.parseInt(hb.substring(4,5)); Integer t = BytesUtil.hexToInt(l); BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(), devices.get(j).getId(), getLightStatus(status)); TempParam param = new TempParam(); param.setId( devices.get(j).getId()); param.setTargetTemp(t+""); param.setTargetStatus(getLightStatus(status)); param.setTargetModel("01"); List exeDeviceList = new ArrayList<>(); ExeDevice dev = new ExeDevice(); dev.setSerId(ser.getId()); exeDeviceList.add(dev); param.setDeviceList(exeDeviceList); param.setCompanyId(ser.getCompanyId()); deviceService.updateTempControlInfo(param); log.info("空调状态解析完成:"+param.toString()); //BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+40, getWinStatus(airStr[i])); } } } } /** * 解析照明状态 * @param ser * @param lightStr */ private void AnalysisLight(DeviceSer ser,Integer[] lightStr){ if(lightStr == null || lightStr.length == 0)return; for (int i=1;i<= lightStr.length;i++) { BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+60, getLightStatus(lightStr[i-1])); } } private String getWinStatus(Integer s){ if(s == null)return DeviceStatus.ZERO.getCode(); String hexStr = BytesUtil.intToHexStr(s); for (int i =0;i<4;i++){ if(hexStr.length() < 4){ hexStr = "0" + hexStr; } } String h = hexStr.substring(0,2); String l = hexStr.substring(2,4); h = BytesUtil.toBinary8String(BytesUtil.hexToInt(h)); int kaidu = (BytesUtil.hexToInt(l)); String temp = h.substring(6,8); if("00".equals(temp ) && kaidu == 100){ return DeviceStatus.OPEN.getCode(); } if("00".equals(temp ) && kaidu == 0){ return DeviceStatus.CLOSE.getCode(); } if("01".equals(temp )){ return DeviceStatus.OPEN.getCode(); } if("10".equals(temp )){ return DeviceStatus.CLOSE.getCode(); } return DeviceStatus.ZERO.getCode(); } private String getFanStatus(Integer s){ if(0 == s){ return DeviceStatus.CLOSE.getCode(); } if(1 == s){ return DeviceStatus.OPEN.getCode(); } return DeviceStatus.ZERO.getCode(); } private String getLightStatus(Integer s){ if(0 == s){ return DeviceStatus.CLOSE.getCode(); } if(1 == s){ return DeviceStatus.OPEN.getCode(); } return DeviceStatus.ZERO.getCode(); } }