owread/
owread.rs

1//! **owread** -- _Rust version_
2//!
3//! ## Read a value from owserver ( from a 1-wire device )
4//!
5//! **owread** is a tool in the 1-wire file system **OWFS**
6//!
7//! This Rust version of **owread** 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//! owread [OPTIONS] PATH
14//! ```
15//!
16//! ## PURPOSE
17//! Read the value of a device property
18//! * Often a sensor reading like `10.4323424342/temperature`
19//! * can also be informational like `10.4323424342/type`
20//!
21//! ## OPTIONS
22//! * `-s IP:port` (default `localhost:4304`)
23//! * `--hex       show the value in hexidecimal
24//! * `--size n    return only n bytes
25//! * `--offset m  start return at byte m
26//! * -h           for full list of options
27//!
28//! ## PATH
29//! * 1-wire path to a file
30//! * No Default
31//! * More than one path can be given
32//!
33//! **owread** only works on files, not directories. Use **owget** to read both files and directories.
34//!
35//! ## USAGE
36//! * owserver must be running in a network-accessible location
37//! * `owread` is a command line program
38//! * output to stdout
39//! * errors to stderr
40//!
41//! ## EXAMPLE
42//! Read a temperature
43//! ```
44//! owread /10.67C6697351FF/temperature
45//! ```
46//! ```text
47//!     85.7961
48//! ```
49//! Read temperature in hex
50//! ```
51//! owread /10.67C6697351FF/temperature --hex
52//! ```
53//! ```text
54//! 20 20 20 20 20 37 36 2E 31 35 38 35
55//! ```
56//! {c} 2025 Paul H Alfille -- MIT Licence
57
58// owrust project
59// https://github.com/alfille/owrust
60//
61// This is a Rust version of my C owfs code for talking to 1-wire devices via owserver
62// Basically owserver can talk to the physical devices, and provides network access via my "owserver protocol"
63//
64// MIT Licence
65// {c} 2025 Paul H Alfille
66
67use owrust::console::console_line;
68use owrust::parse_args::{OwRead, Parser};
69
70fn main() {
71    let mut owserver = owrust::new(); // create structure for owserver communication
72    let prog = OwRead;
73
74    // configure and get paths
75    match prog.command_line(&mut owserver) {
76        Ok(paths) => {
77            if paths.is_empty() {
78                // No path
79                eprintln!("No 1-wire path, so no readings");
80            } else {
81                // for each pathon command line
82                for path in paths.into_iter() {
83                    from_path(&mut owserver, path);
84                }
85            }
86        }
87        Err(e) => {
88            eprintln!("owread trouble {}", e);
89        }
90    }
91}
92
93// print 1-wire file contents (e.g. a sensor reading)
94fn from_path(owserver: &mut owrust::OwMessage, path: String) {
95    match owserver.read(&path) {
96        Ok(values) => match owserver.show_result(values) {
97            Ok(s) => {
98                console_line(s);
99            }
100            Err(e) => {
101                eprintln!("Reading error {}", e);
102            }
103        },
104        Err(e) => {
105            eprintln!("Trouble with path {} Error {}", path, e);
106        }
107    }
108}