当前位置:网站首页>internship:传感器的编写示例
internship:传感器的编写示例
2022-07-21 14:38:00 【ahyo】
软件系统的数据交互,其数据来源依托于硬件的采集,那么硬件(传感器)的代码块怎么实现。
第一步就是 实体类的编写(明确实体属性):
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);
}
传感器所得数据存储在Redis中,如何获取传感器数据?
方法示例:
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返回。
边栏推荐
- Use of Seaborn
- The company that opens an account for futures has low handling charges and is safe
- Xshell远程连接服务器
- streamlit TypeError: Plain typing. NoReturn is not valid as type argument
- vector容器成员函数——reserve()及迭代器失效问题
- leetcode 931.下降路径最小和
- leetcode 1380. Lucky number in matrix
- Leetcode 539. Minimum time difference
- 运营商AI机遇:以大模型拓展全新赛道
- leetcode 938. 二叉搜索树的范围和
猜你喜欢
随机推荐
2022-07-21日报:吴恩达撰文:如何建立适应AI职业生涯的项目
固收类的理财产品收益是确定的吗?
Exploration of running applet of domestic Tongxin UOS system
3D coordinate system of 3D conversion, perspective rotation and other basic knowledge
Preparation method of polyether / polyacrylamide monomethyl / Polyacrylamide / granular poly (N-isopropylacrylamide) chitosan hydrogel
Length unit, color unit, text style and font style
Goldfish rhca memoirs: cl210 performs image operation -- compare image format + build custom image
IDEA快捷键(第十六天)
Leetcode skimming: middle order traversal of binary trees
聚醚/聚丙烯酰胺-竣甲基/聚丙烯酰胺/粒状聚N-异丙基丙烯酰胺壳聚糖水凝胶的制备方法
leetcode 376.摆动序列
是时候考虑对自己的 App 进行瘦身
DOM event proxy (2)
第1集 vmware虛擬機安裝最牛B教程(12天)
Animation, and basic use of animation
Importance of data center operation and maintenance management skills
64. Minimum path and
Western Agricultural University C plus
Western Agricultural University C plus
while和do-while循环