owget/owget.rs
1//! **owget** -- _Rust version_
2//!
3//! ## Read a value or directory from owserver
4//! Combines the functions of **owread** and **owdir**
5//!
6//! **owget** is a tool in the 1-wire file system **OWFS**
7//!
8//! This Rust version of **owdir** is part of **owrust** -- the _Rust language_ OWFS programs
9//! * **OWFS** [documentation](https://owfs.org) and [code](https://github.com/owfs/owfs)
10//! * **owrust** [repository](https://github.com/alfille/owrust)
11//!
12//! ## SYNTAX
13//! ```
14//! owdir [OPTIONS] PATH
15//! ```
16//!
17//! ## PURPOSE
18//! __owget__ combines the functions of __owread__ and __owget__
19//! * Useful for exploring the owfs structure without knowing if a file is a property or direcory
20//! * __owtree__ is another way of visulaizing structure
21//! * using *--dir--* for directory listing will distiguish files from directories
22//!
23//! ## OPTIONS
24//! * `-s IP:port` (default `localhost:4304`)
25//! * `--dir` Add trailing **/** for directory elements
26//! * `--bare` Suppress non-device entries
27//! * `--hex show the value in hexidecimal
28//! * `--size n return only n bytes
29//! * `--offset m start return at byte m
30//! * -h for full list of options
31//!
32//! ## PATH
33//! * 1-wire path to a file
34//! * No Default
35//! * More than one path can be given
36//!
37//! ## USAGE
38//! * owserver must be running in a network-accessible location
39//! * `owget` is a command line program
40//! * output to stdout
41//! * errors to stderr
42//!
43//! ## EXAMPLE
44//! Read a temperature
45//! ```
46//! owget /10.67C6697351FF/temperature
47//! ```
48//! ```text
49//! 85.7961
50//! ```
51//! Get bare root directory
52//! ```
53//! owget --bare
54//! ```
55//! ```text
56//! /10.67C6697351FF
57//! /05.4AEC29CDBAAB
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
67use owrust::console::console_line;
68use owrust::parse_args::{OwGet, Parser};
69
70fn main() {
71 let mut owserver = owrust::new(); // create structure for owserver communication
72 let prog = OwGet;
73
74 // configure and get paths
75 match prog.command_line(&mut owserver) {
76 Ok(paths) => {
77 if paths.is_empty() {
78 // No path -- assume root
79 from_path(&mut owserver, "/".to_string());
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.get(&path) {
96 Ok(value) => {
97 match String::from_utf8(value) {
98 Ok(v) => {
99 console_line(v);
100 }
101 Err(e) => {
102 eprintln!("Unprintable string {}", e);
103 }
104 };
105 }
106 Err(e) => {
107 eprintln!("Trouble with path {} Error {}", path, e);
108 }
109 }
110}