owpresent/owpresent.rs
1//! **owpresent** -- _Rust version_
2//!
3//! ## Does a file exiss (devise exists) on owserver
4//!
5//! **owpresent** is a tool in the 1-wire file system **OWFS**
6//!
7//! This Rust version of **owpresent** is part of **owrust** -- the _Rust language_ OWFS programs
8//! * **OWFS** [documentation](https://owfs.org) and [code](https://github.com/owfs/owfs)
9//! * **owrust** [repository](https://github.com/alfille/owrust)
10//!
11//! ## SYNTAX
12//! ```
13//! owpresent [OPTIONS] PATH
14//! ```
15//! ## PURPOSE
16//! Tell whether a OWFS path is valid
17//!
18//! ## OPTIONS
19//! * `-s IP:port` (default `localhost:4304`)
20//! * -h for full list of options
21//!
22//! ## PATH
23//! * 1-wire path to a file
24//! * No Default
25//! * More than one path can be given
26//!
27//! **owpresent** works on files and directories.
28//!
29//! ## USAGE
30//! * owserver must be running in a network-accessible location
31//! * `owpresent` is a command line program
32//! * output to stdout
33//! * `1` if present
34//! * `0` if not present
35//! * errors to stderr
36//!
37//! ## EXAMPLE
38//! Test presence of a device
39//! ```
40//! owpresent /10.67C6697351FF
41//! ```
42//! ```text
43//! 1
44//! ```
45//! Test a file
46//! ```
47//! owpresent /10.67C6697351FF/temperature
48//! ```
49//! ```text
50//! 1
51//! ```
52//! Test a device that isn't there
53//! ```
54//! owpresent /10.FFFFFFFFFFFF
55//! ```
56//! ```text
57//! 0
58//! ```
59//! {c} 2025 Paul H Alfille -- MIT Licence
60
61// owrust project
62// https://github.com/alfille/owrust
63//
64// This is a Rust version of my C owfs code for talking to 1-wire devices via owserver
65// Basically owserver can talk to the physical devices, and provides network access via my "owserver protocol"
66//
67// MIT Licence
68// {c} 2025 Paul H Alfille
69
70use owrust::console::console_line;
71use owrust::parse_args::{OwDir, Parser};
72
73fn main() {
74 let mut owserver = owrust::new(); // create structure for owserver communication
75 let prog = OwDir;
76
77 // configure and get paths
78 match prog.command_line(&mut owserver) {
79 Ok(paths) => {
80 if paths.is_empty() {
81 // No path -- assume root
82 from_path(&mut owserver, "/".to_string());
83 } else {
84 // for each pathon command line
85 for path in paths.into_iter() {
86 from_path(&mut owserver, path);
87 }
88 }
89 }
90 Err(e) => {
91 eprintln!("owpresent trouble {}", e);
92 }
93 }
94}
95
96// print 1-wire file contents (e.g. a sensor reading)
97fn from_path(owserver: &mut owrust::OwMessage, path: String) {
98 match owserver.present(&path) {
99 Ok(values) => {
100 if values {
101 console_line("1");
102 } else {
103 console_line("0");
104 }
105 }
106 Err(e) => {
107 eprintln!("Trouble with path {} Error {}", path, e);
108 }
109 }
110}