获取电表参数数据

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: springboot 专栏: 物联网项目 标签: 获取电表数据

2024-07-06 16:37:59 129浏览

获取电表参数数据

效果

思路

  1. 先发一个指令给网关进一步转发给电表
  2. 等待netty获取到网关客户端给的数据存到redis中
  3. 从redis中获取到数据返回给前端

注意:由于发指令后,不能立马拿到结果,可能出现redis里还没数据,你这里就要get,那肯定就没有,所以采用两个接口实现,一个接口负责发指令并把数据存redis,一个接口负责从redis里获取数据

核心代码

发送指令

具体代码如下

@Override
    public JSONObject getElectrity1(TManagerDevice device, HttpServletRequest request) {
        TManagerDevice tManagerDeviceVo = baseMapper.selectById(device.getId());
        if (tManagerDeviceVo==null){
            throw new RuntimeException("不存在该设备");
        }
        String deviceCode=tManagerDeviceVo.getDeviceCode();
        String gatewayId = tManagerDeviceVo.getGatewayId();
        WaterGateway ssDeviceGatewa = waterGatewayDao.selectById(gatewayId);
        if (ssDeviceGatewa==null){
            throw new RuntimeException("该设备没有任何网关关联它");
        }
        //拿到处理后的设备编号
        String address = ElectricityMeterUtil.deviceAddressConvert(deviceCode);
        String ip = "/"+ssDeviceGatewa.getGatewayIp()+":"+ssDeviceGatewa.getGatewayPort();
        //获取发送指令
        String instruct =getInstruce(address,device.getOperationType());
        if (instruct==null){
            throw new RuntimeException("获取操作指令失败");
        }
//        String instruct = ElectricityMeterUtil.getCompleteInstruct(address, InstructConstant.ELECTRICITY_METER_PULL_INSTRUCT);

        sendInstructsService.sendInstructs1(instruct,ip,request,deviceCode,device.getOperationType());

       

    }

netty客户端获取数据

 //@Override
    public void callbackInstructResultMsg(String ip, String returnStr) {
        log.info(returnStr);

        if (StringUtils.isEmpty(returnStr) || !returnStr.startsWith("FE")) {
            return;
        }
        //获取回调设备编号
        String notFEStr = returnStr.substring(returnStr.indexOf("68") + 2);
        String deviceType = notFEStr.startsWith("10") ? "w" : "e";
        //根据设备号找到数据,并更新数据
        if ("e".equals(deviceType)) {
            String addressNo = notFEStr.substring(0, notFEStr.indexOf("68"));
            //获取设备号
            String deviceNo = ElectricityMeterUtil.addressConvertNo(addressNo, true);
            String operationType = redisUtils.get(InstructConstant.handleInstructs + deviceNo);
            //删除redis中的数据
            redisUtils.delete(InstructConstant.handleInstructs + deviceNo);
            //电表操作
            handleOperation(deviceNo, operationType, notFEStr);
        } else {
            //水表操作
            String deviceCode = handleWaterOperation(notFEStr);
            //删除redis中的数据
            redisUtils.delete(InstructConstant.handleInstructs + deviceCode);
        }

    }
 /**
     * 电表处理操作类型,对不同的类型更新对应设备的信息
     *
     * @param deviceNo
     * @param type
     */
    private void handleOperation(String deviceNo, String type, String notFEStr) {
        //FE FE 68 *** 68 *****
        LambdaQueryWrapper<TManagerDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TManagerDevice::getDeviceCode, deviceNo);
        TManagerDevice vo = new TManagerDevice();
        //合总电闸,拉闸或合闸后修改设备表状态
        if (InstructTypeConstant.E_MAIN_GATE_NO.getKey().equals(type)) {
            vo.setElectricityStatus("1");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //拉总电闸
        if (InstructTypeConstant.E_MAIN_GATE_OFF.getKey().equals(type)) {
            vo.setElectricityStatus("0");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //合A路电闸
        if (InstructTypeConstant.E_A_GATE_NO.getKey().equals(type)) {
            vo.setElectricityAStatus("1");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //拉A路电闸
        if (InstructTypeConstant.E_A_GATE_OFF.getKey().equals(type)) {
            vo.setElectricityAStatus("0");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //合B路电闸
        if (InstructTypeConstant.E_B_GATE_NO.getKey().equals(type)) {
            vo.setElectricityBStatus("1");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //拉B路电闸
        if (InstructTypeConstant.E_B_GATE_OFF.getKey().equals(type)) {
            vo.setElectricityBStatus("0");
            tManagerDeviceMapper.update(vo, wrapper);
        }
        //获取电表参数信息
        if (InstructTypeConstant.E_MAIN_PARSM_INFO.getKey().equals(type)) {
            getElectricityParamInfo(notFEStr, deviceNo);
        }
        //获取电表费控数据
//        if(InstructTypeConstant.E_BALANCE_SETUPA.getKey().equals(type)){
//            getElectricityBalanceInfo(notFEStr,deviceNo);
//        }

    }
/**
     * 获取电表参数信息
     *
     * @param str
     */
    private void getElectricityParamInfo(String str, String deviceCode) {
        //获取数据
        String datastr = str.substring(str.indexOf("B3E9") + 4, str.length() - 4);
        String dataBCD = ElectricityMeterUtil.hexAddcharacter(datastr, " ");
        log.info("getElectricityParamInfo subtract33Data {}", dataBCD);
        Map<String, String> map = new HashMap<>();

        //A路电压
        String aVoltage = datastr.substring(0, 4);
        aVoltage = dataChange(aVoltage, 0);
        log.info("A路电压:" + aVoltage);
        map.put("aVoltage", aVoltage);
        //B路电压
        String bVoltage = datastr.substring(4, 8);
        bVoltage = dataChange(bVoltage, 0);
        log.info("B路电压" + bVoltage);
        map.put("bVoltage", bVoltage);
        //A路电流
        String aCurrent = datastr.substring(8, 12);
        aCurrent = dataChange(aCurrent, 2);
        log.info("A路电流" + aCurrent);
        map.put("aCurrent", aCurrent);
        //B路电流
        String bCurrent = datastr.substring(12, 16);
        bCurrent = dataChange(bCurrent, 2);
        log.info("B路电流" + bCurrent);
        map.put("bCurrent", bCurrent);
        //总功率
        String sumPower = datastr.substring(16, 22);
        sumPower = dataChange(sumPower, 4);
        log.info("总功率:" + sumPower);
        map.put("sumPower", sumPower);
        //A功率
        String aPower = datastr.substring(22, 28);
        aPower = dataChange(aPower, 4);
        log.info("A功率:" + aPower);
        map.put("aPower", aPower);
        //B功率
        String bPower = datastr.substring(28, 34);
        bPower = dataChange(bPower, 4);
        log.info("B功率:" + bPower);
        map.put("bPower", bPower);
        //总视在功率
        String sumApparentPower = datastr.substring(34, 40);
        sumApparentPower = dataChange(sumApparentPower, 4);
        log.info("总视在功率:" + sumApparentPower);
        map.put("sumApparentPower", sumApparentPower);
        //A路视在功率
        String aApparentPower = datastr.substring(40, 46);
        aApparentPower = dataChange(aApparentPower, 4);
        log.info("A路视在功率:" + aApparentPower);
        map.put("aApparentPower", aApparentPower);
        //B路视在功率
        String bApparentPower = datastr.substring(46, 52);
        bApparentPower = dataChange(bApparentPower, 4);
        log.info("B路视在功率:" + bApparentPower);
        map.put("bApparentPower", bApparentPower);
        //A路功率因素
        String aPowerFactor = datastr.substring(52, 56);
        aPowerFactor = dataChange(aPowerFactor, 3);
        log.info("A路功率因素:" + aPowerFactor);
        map.put("aPowerFactor", aPowerFactor);
        //B路功率因素
        String bPowerFactor = datastr.substring(56, 60);
        bPowerFactor = dataChange(bPowerFactor, 3);
        log.info("B路功率因素:" + bPowerFactor);
        map.put("bPowerFactor", bPowerFactor);
        //电网频率
        String frequency = datastr.substring(60);
        frequency = dataChange(frequency, 2);
        log.info("电网频率:" + frequency);
        map.put("frequency", frequency);
        redisUtils.set(InstructConstant.SUMMARY_INSTRUCTS + deviceCode, JSON.toJSONString(map));
    }

重点是把数据存到redis

redis中获取电表参数数据

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695