Commit 14e06dd6 authored by jlopez's avatar jlopez
Browse files

First draft

parents
# History files
.Rhistory
.Rapp.history
# User-specific files
.Ruserdata
# RStudio files
.Rproj.user/
library(dplyr)
library(broman)
library(randomcoloR)
source("tools.R")
# Le range de départ de la période à extraire
timeS <- "2018-11-26 12:00:00"
# Le range de fin de la période à extraire
timeE <- "2018-11-27 11:59:59"
# Si vide alors pas de range
# timeS <- ""
# timeE <- ""
# Les uuid des mandrill ici 1 mandrill
mandrill_uuid <- c("32-38-31-39-58-36-8e-0c")
# ici avec 2 mandrills
#mandrill_uuid <- c("32-38-31-39-58-36-8e-0c", "32-38-31-39-62-36-8f-0c")
# Si le vecteur est vide cela signifie qu'on prend tout les mandrills
#mandrill_uuid <- c()
# Les chemins des 3 fichiers
path_temperature <- "./2018-11-26au02/logLoRaTemp.csv"
path_acceleration <- "./2018-11-26au02/logLoRaAcc.csv"
path_mouvement <- "./2018-11-26au02/logLoRaMov.csv"
#--------------------------------------------------
# Temperature mandrill
#--------------------------------------------------
data_temperature <- readLines(path_temperature)
mandrill_temperature <- extractTemperature(data_temperature, mandrill_uuid, timeS, timeE)
uuid <- unique(mandrill_temperature$uuid)
mandrillT <- filter(mandrill_temperature, uuid == uuid[1])
color_mandrill <- distinctColorPalette(length(uuid))
if(length(uuid) == 1) {
color_mandrill[1] <- "blue"
}
plot(mandrillT$date,mandrillT$temperature,type="l", col=color_mandrill[1])
if(length(uuid) > 1) {
for(index in c(2:length(uuid))) {
mandrill <- filter(mandrill_temperature, uuid == uuid[index])
lines(mandrill$date,mandrill$temperature, col=color_mandrill[index])
}
}
#--------------------------------------------------
# Mouvement mandrill
#--------------------------------------------------
data_mouvement <- readLines(path_mouvement)
moyenne_mouvement <- FALSE
mandrill_mouvement <- extractMouvement(data_mouvement, mandrill_uuid, timeS, timeE, moyenne_mouvement)
uuid <- unique(mandrill_mouvement$uuid)
mandrill <- filter(mandrill_mouvement, uuid == uuid[1])
color_mandrill <- distinctColorPalette(length(uuid))
if(length(uuid) == 1) {
color_mandrill[1] <- "blue"
}
plot(mandrill$date,mandrill$mouvement,type="l", col=color_mandrill[1])
if(length(uuid) > 1) {
for(index in c(2:length(uuid))) {
mandrill <- filter(mandrill_mouvement, uuid == uuid[index])
lines(mandrill$date,mandrill$mouvement, col=color_mandrill[index])
}
}
#--------------------------------------------------
# Acceleration mandrill
#--------------------------------------------------
data_acceleration <- readLines(path_acceleration)
moyenne_acceleration <- FALSE
mandrill_acceleration <- extractAcceleration(data_acceleration, mandrill_uuid, timeS, timeE, moyenne_acceleration)
uuid <- unique(mandrill_acceleration$uuid)
mandrill <- filter(mandrill_acceleration, uuid == uuid[1])
color_mandrill <- distinctColorPalette(length(uuid))
# Position X
plot(mandrill$date,mandrill$x,type="l", col="blue")
if(length(uuid) > 1) {
for(index in c(2:length(uuid))) {
print(uuid[index])
mandrill <- filter(mandrill_acceleration, uuid == uuid[index])
lines(mandrill$date,mandrill$x, col=color_mandrill[index])
print(nrow(mandrill))
}
}
color_mandrill <- distinctColorPalette(length(uuid))
# Position Y
plot(mandrill$date,mandrill$y,type="l", col="red")
if(length(uuid) > 1) {
for(index in c(2:length(uuid))) {
mandrill <- filter(mandrill_acceleration, uuid == uuid[index])
lines(mandrill$date,mandrill$y, col=color_mandrill[index])
}
}
color_mandrill <- distinctColorPalette(length(uuid))
# Position Z
plot(mandrill$date,mandrill$z,type="l", col="green")
if(length(uuid) > 1) {
for(index in c(2:length(uuid))) {
mandrill <- filter(mandrill_acceleration, uuid == uuid[index])
lines(mandrill$date,mandrill$z, col=color_mandrill[index])
}
}
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
#' L'index des lignes de temperature
INDEX_TEMPERATURE <<- 0
#' L'index des lignes de mouvement
INDEX_MOUVEMENT <<- 0
#' L'index des lignes de acceleration
INDEX_ACCELERATION <<- 0
#' Liste des mois dans le formet 3 lettres
mymonths <- c("Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","Sep", "Oct","Nov","Dec")
#' Permet de transformer une date dans le format POSIXct depuis le format Character
#' @param date A character : La date au format
#' @return A POSIXct
parseDate <- function(data) {
time <- data
time <- strsplit(time, " ")[[1]]
month <- which(mymonths %in% time[2])
other <- paste(time[3:length(time)], collapse = " ")
new_time <- paste(month, other)
date <- as.POSIXct(strptime(new_time, "%m %d %Y %H:%M:%S"), tz = "GMT+0100")
return(date)
}
#' Permet d'extraire les températures des mandrills
#' @param data_temperature A Vector : les données brut de température
#' @param individu A Vector : uuid mandrill vecteur
#' @param timeS A Character : range de départ
#' @param timeE A Character : range de fin
#' @return a data.frame
extractTemperature <- function(data_temperature, individu = c(), timeS = "", timeE = "") {
start_time <- Sys.time()
INDEX_TEMPERATURE <<- 0
if(timeS == "") {
dateS <- as.POSIXct("1111-01-01", tz = "GMT+0100")
} else {
dateS <- as.POSIXct(timeS, tz = "GMT+0100")
}
if(timeE == "") {
dateE <- as.POSIXct("9999-01-01", tz = "GMT+0100")
} else {
dateE <- as.POSIXct(timeE, tz = "GMT+0100")
}
resultTemperature <- suppressWarnings(bind_rows(lapply(data_temperature, parseTemperature, individu, dateS, dateE, length(data_temperature))))
if(nrow(resultTemperature) > 0) {
resultTemperature$date <- as.POSIXct(resultTemperature$date)
}
print(paste0(length(data_temperature), "/", length(data_temperature)))
end_time <- Sys.time()
print(end_time - start_time)
return(resultTemperature)
}
#' Parse une ligne de temperature
#' @param individu A Vector : uuid mandrill vecteur
#' @param dateS A POSIXct Date : range de départ
#' @param dateE A POSIXct Date : range de fin
#' @param totalSize A numeric : la taille total des données de temprérature
#' @return a data.frame
parseTemperature <- function(line, individu = c(), dateS, dateE, totalSize) {
test_temperature <- strsplit(line, "_")[[1]]
date <- parseDate(test_temperature[1])
result <- data.frame(date=as.character(), uuid = as.character(), df1=as.numeric(), df2=as.numeric())
uuid <- test_temperature[4]
INDEX_TEMPERATURE <<- INDEX_TEMPERATURE + 1
if((INDEX_TEMPERATURE %% 5000) == 0) {
print(paste0(INDEX_TEMPERATURE, "/", totalSize))
}
if(length(individu) > 0) {
if(uuid %in% individu) {
if(date >= dateS && date <= dateE) {
df1 <- as.numeric(test_temperature[23])
df2 <- as.numeric(test_temperature[24])
result <- calculTemperature(data.frame(date, uuid, df1, df2))
return(result)
}
}
} else {
if(date >= dateS && date <= dateE) {
df1 <- as.numeric(test_temperature[23])
df2 <- as.numeric(test_temperature[24])
result <- calculTemperature(data.frame(date, uuid, df1, df2))
return(result)
}
}
}
#' Convertion de la temperature
#' @param data a data.frame : un donné de température
#' @return a data.frame
calculTemperature <- function(data) {
df1 <- as.numeric(data[3])
df2 <- as.numeric(data[4])
hdf1 <- dec2hex(df1)
hdf2 <- dec2hex(df2)
C0 <- hdf2
if(df2 < 16) {
C0 <- paste0("0", hdf2)
}
HC <- paste0(hdf1, C0)
dh1 <- hex2dec(HC)
dh2 <- dh1
dh <- dec2hex(dh2)
sizeDH <- nchar(dh)
if(sizeDH < 4) {
dh <- paste0(paste0(rep(0, 4 - sizeDH), collapse = ""), dh)
}
vh1 <- substr(dh, 1, 2)
vh2 <- substr(dh, 3, 4)
hb1 <- R.utils::intToBin(strtoi(vh1, base = 16L))
hb2 <- R.utils::intToBin(strtoi(vh2, base = 16L))
sizeHB1 <- nchar(hb1)
sizeHB2 <- nchar(hb2)
if(sizeHB1 < 8) {
hb1 <- paste0(paste0(rep(0, 8 - sizeHB1), collapse = ""), hb1)
}
if(sizeHB2 < 8) {
hb2 <- paste0(paste0(rep(0, 8 - sizeHB2), collapse = ""), hb2)
}
bv <- substr(paste0(hb1, hb2), 5, 16)
bv2 <- R.utils::intToBin(bitwNot(base::strtoi(bv, base = 2)))
sizeBV <- nchar(bv2)
bv3 <- substr(bv2, sizeBV-11, sizeBV)
vbv <- strsplit(bv3, "")[[1]]
result <- 0.0
if(as.numeric(vbv[1]) == 0) {
b0 <- 1 * as.numeric(vbv[12])
b1 <- 2 * as.numeric(vbv[11])
b2 <- 4 * as.numeric(vbv[10])
b3 <- 8 * as.numeric(vbv[9])
b4 <- 16 * as.numeric(vbv[8])
b5 <- 32 * as.numeric(vbv[7])
b6 <- 64 * as.numeric(vbv[6])
b7 <- 128 * as.numeric(vbv[5])
b8 <- 256 * as.numeric(vbv[4])
b9 <- 512 * as.numeric(vbv[3])
b10 <- 1024 * as.numeric(vbv[2])
b11 <- 2048 * as.numeric(vbv[1])
val <- b0+b1+b2+b3+b4+b5+b6+b7+b8+b9+b10+b11+1
result <- val * -1
} else {
result <- dh1 / 10
}
date <- data[1]
uuid <- data[2]
temperature <- result
return(data.frame(date, uuid, temperature))
}
#' Permet d'extraire les mouvements des mandrills
#' @param data_mouvement A Vector : les données brut de mouvement
#' @param individu A Vector : uuid mandrill vecteur
#' @param timeS A Character : range de départ
#' @param timeE A Character : range de fin
#' @param moyenne A Logical : si on veut une moyenne ou non
#' @return a data.frame
extractMouvement <- function(data_mouvement, individu = c(), timeS = "", timeE = "", moyenne = FALSE) {
start_time <- Sys.time()
INDEX_MOUVEMENT <<- 0
if(timeS == "") {
dateS <- as.POSIXct("1111-01-01", tz = "GMT+0100")
} else {
dateS <- as.POSIXct(timeS, tz = "GMT+0100")
}
if(timeE == "") {
dateE <- as.POSIXct("9999-01-01", tz = "GMT+0100")
} else {
dateE <- as.POSIXct(timeE, tz = "GMT+0100")
}
resultMouvement <- suppressWarnings(bind_rows(lapply(data_mouvement, parseMouvement, individu, dateS, dateE, length(data_mouvement), moyenne)))
if(nrow(resultMouvement) > 0) {
resultMouvement$date <- as.POSIXct(resultMouvement$date)
}
print(paste0(length(data_mouvement), "/", length(data_mouvement)))
end_time <- Sys.time()
print(end_time - start_time)
return(resultMouvement)
}
#' Parse une ligne de mouvement
#' @param individu A Vector : uuid mandrill vecteur
#' @param dateS A POSIXct Date : range de départ
#' @param dateE A POSIXct Date : range de fin
#' @param totalSize A numeric : la taille total des données de temprérature
#' @param moyenne A Logical : si on veut une moyenne ou non
#' @return a data.frame
parseMouvement <- function(line, individu, dateS, dateE, totalSize, moyenne) {
test_mouvement <- strsplit(line, "_")[[1]]
date <- parseDate(test_mouvement[1])
uuid <- test_mouvement[4]
size <- as.numeric(test_mouvement[20])
df <- data.frame(date=as.character(),
uuid=as.character(),
mouvement=as.numeric())
date2 <- as.POSIXct(date)
current_indext <- 1
current_value <- 0
if(date >= dateS && date <= dateE) {
if(!moyenne) {
if((uuid %in% individu) || (length(individu) == 0)) {
for(index in c(23:(size+22))) {
value <- test_mouvement[index]
if(date2 >= dateS && date2 <= dateE) {
df2 <- data.frame(date=date2,
uuid=uuid,
mouvement=as.numeric(value))
date2 <- date2 + 1
df <- rbind(df, df2)
}
}
}
} else {
if((uuid %in% individu) || (length(individu) == 0)) {
if(date >= dateS && date <= dateE) {
for(index in c(23:(size+22))) {
value <- test_mouvement[index]
current_value <- current_value + as.numeric(value)
current_indext <- current_indext + 1
}
}
}
}
}
if(moyenne) {
df <- data.frame(date=date,
uuid=uuid,
mouvement=current_value/current_indext)
}
INDEX_MOUVEMENT <<- INDEX_MOUVEMENT + 1
if((INDEX_MOUVEMENT %% 1000) == 0) {
print(paste0(INDEX_MOUVEMENT, "/", totalSize))
}
if(nrow(df) > 0) {
return(df)
}
}
#' Permet d'extraire les accélérations des mandrills
#' @param data_acceleration A Vector : les données brut d'accélération
#' @param individu A Vector : uuid mandrill vecteur
#' @param timeS A Character : range de départ
#' @param timeE A Character : range de fin
#' @param moyenne A Logical : si on veut une moyenne ou non
#' @return a data.frame
extractAcceleration <- function(data_acceleration, individu = c(), timeS = "", timeE = "", moyenne = FALSE) {
start_time <- Sys.time()
INDEX_ACCELERATION <<- 0
if(timeS == "") {
dateS <- as.POSIXct("1111-01-01", tz = "GMT+0100")
} else {
dateS <- as.POSIXct(timeS, tz = "GMT+0100")
}
if(timeE == "") {
dateE <- as.POSIXct("9999-01-01", tz = "GMT+0100")
} else {
dateE <- as.POSIXct(timeE, tz = "GMT+0100")
}
resultAcceleration <- suppressWarnings(bind_rows(lapply(data_acceleration, parseAcceleration, individu, dateS, dateE, length(data_acceleration), moyenne)))
if(nrow(resultAcceleration) > 0) {
resultAcceleration$date <- as.POSIXct(resultAcceleration$date)
}
print(paste0(length(data_acceleration), "/", length(data_acceleration)))
end_time <- Sys.time()
print(end_time - start_time)
return(resultAcceleration)
}
#' Parse une ligne d'accélération
#' @param individu A Vector : uuid mandrill vecteur
#' @param dateS A POSIXct Date : range de départ
#' @param dateE A POSIXct Date : range de fin
#' @param totalSize A numeric : la taille total des données de temprérature
#' @param moyenne A Logical : si on veut une moyenne ou non
#' @return a data.frame
parseAcceleration <- function(line, individu, dateS, dateE, totalSize, moyenne) {
test_acceleration <- strsplit(line, "_")[[1]]
date <- parseDate(test_acceleration[1])
uuid <- test_acceleration[4]
size <- as.numeric(test_acceleration[20])
timeS <- as.POSIXct(date)
df <- data.frame(date=as.character(),
uuid=as.character(),
x=as.numeric(),
y=as.numeric(),
z=as.numeric())
date2 <- timeS
current_indext <- 1
current_valueX <- 0
current_valueY <- 0
current_valueZ <- 0
groupSize <- (size / 3)
index_element <- 23
if(date >= dateS && date <= dateE) {
if(!moyenne) {
if((uuid %in% individu) || (length(individu) == 0)) {
for(index in c(22:((groupSize+21)))) {
if(date2 >= dateS && date2 <= dateE) {
x <- as.numeric(test_acceleration[index_element])
y <- as.numeric(test_acceleration[index_element+1])
z <- as.numeric(test_acceleration[index_element+2])
index_element <- index_element + 3
df2 <- data.frame(date=date2,
uuid=uuid,
x=x,
y=y,
z=z)
date2 <- date2 + 3
df <- rbind(df, df2)
}
}
}
} else {
if((uuid %in% individu) || (length(individu) == 0)) {
if(date >= dateS && date <= dateE) {
for(index in c(22:((groupSize+21)))) {
current_valueX <- current_valueX + as.numeric(test_acceleration[index_element])
current_valueY <- current_valueY + as.numeric(test_acceleration[index_element+1])
current_valueZ <- current_valueZ + as.numeric(test_acceleration[index_element+2])
index_element <- index_element + 3
current_indext <- current_indext + 1
}
}
}
}
}
if(moyenne) {
df <- data.frame(date=date,
uuid=uuid,
x=round(current_valueX / current_indext),
y=round(current_valueY / current_indext),
z=round(current_valueZ / current_indext))
}
INDEX_ACCELERATION <<- INDEX_ACCELERATION + 1
if((INDEX_ACCELERATION %% 1000) == 0) {
print(paste0(INDEX_ACCELERATION, "/", totalSize))
}