资讯详情

internship:传感器的编写示例

软件系统的数据交互,其数据源依赖于硬件采集,那么如何实现硬件(传感器)的代码块。 第一步就是 编写实体类(明确实体属性):

package com.hyd.daring.transfer.sensor;  import lombok.Data; import lombok.experimental.Accessors;  import java.util.List;  /** * 传感器实时数据 */ @Data @Accessors(chain = true) public class CurrentSensorDataModel { 
             private List<PositionModel> area;      private DataContent content;      private int alarm;      @Data     @Accessors(chain = true)     public static class PositionModel { 
                /** * 区域位置 */         private String id;         /** * 区域名称 */         private String name;     }      @Data     @Accessors(chain = true)     public static class DataContent { 
                private List<AreaModel> enter_left;         private List<AreaModel> enter_right;         private List<AreaModel> exit_left; private List<AreaModel> exit_right; } @Data @Accessors(chain = true) public static class AreaModel { 
         /** * 名称 */ private String id; /** * 区域名称: */ private String name; /** * 传感器数据列表 */ List<SensorInfo> sensorList; /** * 预警的传感器列表,用sensorList来分组 */ List<SensorInfo> alarmList; /** * 不预警的传感器列表,用sensorList来分组 */ List<SensorInfo> normalList; } @Data @Accessors(chain = true) public static class SensorInfo { 
         private Integer id; /** * 传感器类型 */ private String type; /** * 传感器名称 */ private String name; /** * 传感器值 */ private String value; /** * 传感器单位 */ private String unit; /** * 传感器是否预警 */ private boolean sAlarm; /** * 传感器预警次数 */ private int alarm; /** * 传感器预警等级 */ private int level; /** * 传感器趋势 */ private String trend; } } 

再则值得注意的是,map键值对里的参数类型不仅仅是Integer此类 可以是诸多类型。对于某种map的泛型定义会使用到lambda表达式,如:

Map<String, List<BizSensorConfig>> enterLeftMap = group.getEnterLeft().stream().collect(Collectors.groupingBy(BizSensorConfig::getPosition));
Map<String, List<BizSensorConfig>>此类map的定义

一个string类型数据对应一个数据集合,若用for循环来做则会很复杂,通过lambda表达式更加便捷,仔细看这个聚合操作 其逻辑不难理解。

简单提一下Redis工具类哈希获取数据的方法实现:

   哈希获取数据
   public Object hmGet(String key, Object hashKey) { 
       
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

hmGet方法是把指定的key值传入 封装成HashOperations调用get方法返回

有获取 就有添加:

   public void hmSet(String key, Object hashKey, Object value) { 
       
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

方法示例:

public Map<Integer, SensorDataModel> getSensorData(Integer tunnelId) { 
       
        Map<Object, Object> objectMap = redisUtil.hmGetAll(SENSOR_DATA + "_" + tunnelId);
        Map<Integer, SensorDataModel> res = new HashMap<>();
        for (Map.Entry<Object, Object> entry : objectMap.entrySet()) { 
       
            //传感器id
            Integer sensorId = Integer.valueOf(entry.getKey().toString());
            //传感器数据
            SensorDataModel sensorDataModel = (SensorDataModel) entry.getValue();
            res.put(sensorId, sensorDataModel);
        }
        return res;
    }

参数是对象id,通过redisUtil.hmGetAll()获取信息再遍历分开存储 再封装为map返回。

标签: model1022传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台