-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttn_json_file_feed.R
50 lines (39 loc) · 1.84 KB
/
ttn_json_file_feed.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
library(tidyverse)
library(jsonlite)
library(leaflet)
data <- fromJSON("./data/exp-20200127-104632.json") %>% tibble
data <- data[-dim(data)[1],] ## retila el último registro, Danny lo agrega para que formatee bien el json
str(data[[1]]["metadata"])
glimpse(data[[1]]["metadata"][[1]]$gateways)
flat_data <- tibble(
dev_id = data[[1]]$dev_id,
cnt = data[[1]]$counter,
lat = data[[1]]$payload_fields$gps_1$latitude,
lng = data[[1]]$payload_fields$gps_1$longitude,
gw_ids = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 1)[[1]],
gw_time = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 2)[[1]],
gw_lat = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 8)[[1]],
gw_lng = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 4)[[1]],
gw_snr = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 6)[[1]],
gw_rssi = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 9)[[1]],
gw_chnl = map_df(data[[1]]["metadata"][[1]]$gateways, `[`, 10)[[1]]
)
flat_data$color <- (lapply(flat_data$gw_rssi, function(x)(
ifelse(x > -60 , "green",
ifelse(x > -90 && x <= -60 , "gold",
ifelse( x > -120 && x <= -90, "orange",
ifelse( x > -150 && x <= -120, "red",
ifelse( x > -250 && x <= -150, "purple",
"maroon"))))))) %>% enframe %>% unnest)$value
leaflet(flat_data) %>%
addTiles() %>%
addCircleMarkers(~as.numeric(lng), ~as.numeric(lat), color = ~color, popup = ~gw_rssi)
leaflet(flat_data) %>%
addTiles() %>%
addCircleMarkers(~as.numeric(lng), ~as.numeric(lat))
## Algunas estructuras son de 10 columnas y otras de 11 (tienen adicional location_source)
## Hacerlo genérico para que soporte este tipo de estructura, no se puede obtener el gw_rssi si no es así
map(data[[1]]["metadata"][[1]]$gateways, function(r) {
as.tibble(r) ## %>% filter(`location_source` == "registry")
})
flat_data %>% View