2019-11-05 16:47:55 +01:00
|
|
|
source("packrat/init.R")
|
|
|
|
|
|
|
|
library("tidyverse")
|
|
|
|
|
|
|
|
battery <- read.csv(snakemake@input[[1]])
|
|
|
|
|
2019-11-13 20:53:28 +01:00
|
|
|
if(nrow(battery) > 0){
|
|
|
|
consumption <- battery %>%
|
2019-11-05 16:47:55 +01:00
|
|
|
mutate(group = ifelse(lag(battery_status) != battery_status, 1, 0) %>% coalesce(0),
|
2019-11-13 20:53:28 +01:00
|
|
|
group_id = cumsum(group) + 1) %>%
|
2019-11-05 17:35:01 +01:00
|
|
|
filter(battery_status == 2 || battery_status == 3) %>%
|
2019-11-05 16:47:55 +01:00
|
|
|
group_by(group_id) %>%
|
|
|
|
summarize(battery_diff = first(battery_level) - last(battery_level),
|
2019-11-13 20:53:28 +01:00
|
|
|
time_diff = (last(timestamp) - first(timestamp)) / (1000 * 60 * 60),
|
|
|
|
local_start_date_time = first(local_date_time),
|
|
|
|
local_end_date_time = last(local_date_time),
|
|
|
|
local_start_date = first(local_date),
|
2019-11-21 17:34:30 +01:00
|
|
|
local_end_date = last(local_date),
|
2019-11-21 18:39:52 +01:00
|
|
|
local_start_day_segment = first(local_day_segment),
|
|
|
|
local_end_day_segment = last(local_day_segment)) %>%
|
2019-11-05 16:47:55 +01:00
|
|
|
select(-group_id) %>%
|
2019-11-05 17:35:01 +01:00
|
|
|
filter(time_diff > 0.1) # Avoids including quick cycles
|
2019-11-13 20:53:28 +01:00
|
|
|
} else {
|
|
|
|
consumption <- data.frame(battery_diff = numeric(),
|
|
|
|
time_diff = numeric(),
|
|
|
|
local_start_date_time = character(),
|
|
|
|
local_end_date_time = character(),
|
|
|
|
local_start_date = character(),
|
2019-11-21 17:34:30 +01:00
|
|
|
local_end_date = character(),
|
2019-11-21 18:39:52 +01:00
|
|
|
local_start_day_segment = character(),
|
|
|
|
local_end_day_segment = character())
|
2019-11-13 20:53:28 +01:00
|
|
|
}
|
2019-11-05 16:47:55 +01:00
|
|
|
|
2019-11-08 18:05:41 +01:00
|
|
|
write.csv(consumption, snakemake@output[[1]], row.names = FALSE)
|