10 个版本
0.1.9 | 2021 年 6 月 6 日 |
---|---|
0.1.8 | 2021 年 6 月 5 日 |
0.1.7 | 2021 年 5 月 27 日 |
#957 in 网页编程
在 i3owm 中使用
20KB
259 行
open_notify 
从 open-notify.org 获取关于发现国际空间站的信息。
open_notify
...是一个 rust crate,它允许您轻松访问来自 open-notify.org 的当前发现信息。这是我为了学习 rust 而制作的一个 非官方 扩展,但希望您会喜欢它。
内容
如何使用
首先将此 crate 添加到您的 Cargo.toml
文件中的依赖项
[dependencies]
open_notify = "0.1.7"
获取连续的 ISS 更新
然后在您的 rust 源文件中通过调用 open_notify::init()
使用该 crate,它返回一个接收器对象。然后您可以使用此接收器对象调用 open_notify::update()
来获取 ISS 发现更新,如以下示例所示
extern crate open_notify;
use open_notify::{find_current, init, update};
fn main() {
// start our observatory via OWM
let receiver = &init(52.520008, 13.404954, 0.0, 90);
loop {
match update(receiver) {
Some(response) => match response {
Ok(spots) => println!(
"ISS is {}",
match find_current(spots,None) {
Some(_s) => "visible",
None => "invisible",
}
),
Err(e) => println!("Could not fetch ISS spotting info because: {}", e),
},
None => (),
}
}
}
首先:开始轮询
init()
启动一个线程,然后定期轮询 api.open-notify.org 获取当前 ISS 位置。然后您可以使用 update()
来请求它。
然后:获取 ISS 发现更新
从 update()
调用中可能获得三种结果,您必须面对这些结果
没有新内容:None
update()
返回 None
,如果当前没有可用的更新。这意味着:您不会两次获得相同的更新!换句话说:update()
不会为您缓存最新的更新。
ISS 发现更新:Vec<Spot>
如果轮询线程下载了新的更新,update()
返回一个 Vec<Spot>
对象。其中 Vec<Spot>
包含一系列的观测事件。
出现一些错误:Err
在发生错误时,update()
返回一个包含简短错误描述的 String
对象。
可能会发生错误...
- 最初,当 还没有更新 时,您将获得一个
Err
,它包含确切的字符串"loading..."
(在open_notify::LOADING
中预定义)。 - 如果接收到 服务器错误 响应(例如,如果使用了 无效的API密钥,则返回
500 Internal Server Error
)。 - 在解析来自 api.open_notify.org 的响应时发生 json错误。
仅获取一次 ISS 发现
如果您只需要一次获取当前的ISS观测事件,可以使用将 init()
和 update()
封装到一个单一同步或异步调用的方法 spot()
。在第一次成功的观测更新后,生成的线程将立即停止,并返回结果。
extern crate open_notify;
use open_notify::blocking::spot;
fn main() {
// start our observatory via OWM
match &spot(52.520008, 13.404954, 0.0) {
Ok(spots) => println!(
"ISS is {}",
match find_current(spots,None) {
Some(_s) => "visible",
None => "invisible",
}
),
Err(e) => println!("Could not fetch ISS spotting info because: {}", e),
}
}
spot()
有一个 阻塞 和一个 非阻塞 变体。
- 上面的示例使用了同步(阻塞)变体
open_notify::blocking::spot
,它不会返回,直到有新的更新。 - 如果您想自己处理返回的 future,请使用
open_notify::spot
并异步等待结果,直到有结果为止。
参考文档
除了这个介绍之外,还有一个参考文档,可以在 这里 找到。
链接
网站
在 open_notify.thats-software.com 上,这份README看起来更棒。
github 仓库
有关源代码,请参阅 github.com 上的 此存储库。
在 crates.io
在 crates.io 上发布。
许可证
open_notify 在 MIT许可证 下许可(http://opensource.org/licenses/MIT 或 LICENSE-MIT)。
依赖关系
~5–20MB
~267K SLoC