sensor time 传感器时间戳

Need change sensor hal and sensor driver, forNew Android CDD suggest that sensor event should synchronize with SystemClock.elapsedRealtimeNano()clock. 
https://www.codeaurora.org/cgit/quic/la/kernel/msm-3.10/commit/?h=LA.BR.1.2.3_rb1.54&id=ccfee9596acc74c88b757987ae31bf3e32e88aca 

https://www.codeaurora.org/cgit/external/gigabyte/platform/hardware/qcom/sensors/commit/?h=caf/LA.BR.1.2.3_rb1.54&id=10e70b2d8b69faeb750b0e8c21e4f770d418326c 
https://www.codeaurora.org/cgit/external/gigabyte/platform/hardware/qcom/sensors/commit/?h=caf/LA.BR.1.2.3_rb1.54&id=651b18d53a6f1491277525e831e9057bd1208c05 
首先,我们需要定义一个Sensor类来表示传感器数据: ```java public class Sensor { private String id; private long timestamp; private double temperature; public Sensor(String id, long timestamp, double temperature) { this.id = id; this.timestamp = timestamp; this.temperature = temperature; } public String getId() { return id; } public void setId(String id) { this.id = id; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } public double getTemperature() { return temperature; } public void setTemperature(double temperature) { this.temperature = temperature; } @Override public String toString() { return "Sensor{" + "id='" + id + '\'' + ", timestamp=" + new Date(timestamp) + ", temperature=" + temperature + '}'; } } ``` 然后,我们可以编写Flink程序: ```java public class TemperatureJob { public static void main(String[] args) throws Exception { // 创建执行环境 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime); // 定义输入流 DataStream<String> input = env.socketTextStream("localhost", 9999); // 解析输入流并转换为Sensor对象 DataStream<Sensor> sensors = input.map(line -> { String[] fields = line.split(","); String id = fields[0]; long timestamp = Long.parseLong(fields[1]); double temperature = Double.parseDouble(fields[2]); return new Sensor(id, timestamp, temperature); }); // 计算时间窗口内温度超过30度的传感器数据 DataStream<String> result = sensors .filter(sensor -> sensor.getTemperature() > 30) .map(sensor -> sensor.toString()) .windowAll(TumblingProcessingTimeWindows.of(Time.seconds(10))) .reduce((s1, s2) -> s1 + "\n" + s2); // 输出结果到控制台并保存到数据库 result.print(); result.addSink(new JdbcSink<>( // 需要引入 flink-connector-jdbc 依赖 "INSERT INTO temperature (sensor_id, timestamp, temperature) VALUES (?, ?, ?)", new JdbcStatementBuilder<Sensor>() { @Override public void accept(PreparedStatement preparedStatement, Sensor sensor) throws SQLException { preparedStatement.setString(1, sensor.getId()); preparedStatement.setTimestamp(2, new Timestamp(sensor.getTimestamp())); preparedStatement.setDouble(3, sensor.getTemperature()); } }, new JdbcConnectionOptions.JdbcConnectionOptionsBuilder() .withUrl("jdbc:mysql://localhost:3306/test") .withDriverName("com.mysql.jdbc.Driver") .withUsername("root") .withPassword("password") .build())); // 启动执行环境 env.execute("Temperature Job"); } } ``` 上述程序中,我们首先创建了一个执行环境,并设置时间语义为ProcessingTime。然后,我们创建了一个输入流,并将其解析为Sensor对象。接下来,我们使用filter算子过滤出温度超过30度的传感器数据,并使用windowAll算子将其划分到10秒的时间窗口内。最后,我们使用reduce算子将每个时间窗口内的传感器数据拼接为一个字符串,并将其输出到控制台并保存到数据库。需要注意的是,保存到数据库需要引入flink-connector-jdbc依赖。 最后,我们可以使用nc命令启动一个Socket服务器,向该程序发送数据: ``` $ nc -lk 9999 sensor1,1631643170000,25.0 sensor1,1631643180000,28.0 sensor1,1631643190000,32.0 sensor1,1631643200000,35.0 sensor2,1631643170000,26.0 sensor2,1631643180000,29.0 sensor2,1631643190000,33.0 sensor2,1631643200000,36.0 ``` 程序输出结果如下: ``` Sensor{id='sensor1', timestamp=Fri Sep 17 19:46:30 CST 2021, temperature=32.0} Sensor{id='sensor1', timestamp=Fri Sep 17 19:46:40 CST 2021, temperature=35.0} Sensor{id='sensor2', timestamp=Fri Sep 17 19:46:30 CST 2021, temperature=33.0} Sensor{id='sensor2', timestamp=Fri Sep 17 19:46:40 CST 2021, temperature=36.0} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值