我有一个树莓派,我用它来收集卫星数据。它需要的第一步是从 https://celestrak.org/NORAD/elements/weather.txt 获取卫星轨道信息,但Pi所在的wifi连接不可靠。我一直在使用 httr 的 GET(( 只是作为satellite_data <- GET("https://celestrak.org/NORAD/elements/weather.txt")
但我正在寻找一个更强大的解决方案,如果它失败了,每次不成功的尝试都会从几秒钟到甚至几个小时或一天的时间退缩,因为这是一个远程传感器,我无法实时调试。我已经看过 https://purrr.tidyverse.org/reference/insistently.html 但仍然不知道如何将其与 GET 一起使用。
我做的最好的是
possibly_get_data <- function(url, n_tries, ...){
#Set exponential back-off so that each ith request waits pause_base * 2^i
rate <- rate_backoff(pause_base = 1, max_times = n_tries)
possibly_insistent_get <- insistently(GET, rate, quiet = FALSE) %>% possibly(otherwise = NULL)
possibly_insistent_get(url, ...)
}
所以通过网络连接,我得到了
> library(httr)
> library(purrr)
> library(reprex)
> possibly_get_data <- function(url, n_tries, ...){
+
+ rate <- rate_backoff(pause_base = 1, max_times = n_tries)
+ possibly_insistent_get <- insistently(GET, rate, quiet = FALSE) %>% possibly(otherwise = NULL)
+
+ possibly_insistent_get(url, ...)
+ }
> satellite_data <- possibly_get_data("http://www.celestrak.com/NORAD/elements/weather.txt", 5)
> satellite_data
Response [http://www.celestrak.com/NORAD/elements/weather.txt]
Date: 2019-08-18 12:50
Status: 200
Content-Type: text/plain
Size: 9.07 kB
DMSP 5D-2 F14 (USA 131)
1 24753U 97012A 19228.95808892 .00000036 00000-0 41402-4 0 9995
2 24753 99.0314 236.6619 0008659 358.7096 1.4054 14.15859916154988
NOAA 15
1 25338U 98030A 19228.92040895 .00000033 00000-0 32688-4 0 9997
2 25338 98.7447 250.0738 0011550 73.3037 286.9410 14.25926944105431
DMSP 5D-3 F15 (USA 147)
1 25991U 99067A 19228.94325824 -.00000108 00000-0 -31378-4 0 9993
2 25991 98.9231 193.7199 0008799 249.0096 180.8141 14.16519712 17178
METEOSAT-8 (MSG-1)
...
如果没有连接,我得到
> satellite_data <- possibly_get_data("http://www.celestrak.com/NORAD/elements/weather.txt", 5)
Error: Could not resolve host: www.celestrak.com
Retrying in 1 seconds.
Error: Could not resolve host: www.celestrak.com
Retrying in 3 seconds.
Error: Could not resolve host: www.celestrak.com
Retrying in 7 seconds.
Error: Could not resolve host: www.celestrak.com
Retrying in 1e+01 seconds.
Error: Could not resolve host: www.celestrak.com
> satellite_data
NULL
但它尚未处理来自服务器的错误响应