Commit 6c7c05aa authored by Michael Watzko's avatar Michael Watzko
Browse files

Add munin script, auto select local port

parent a1992af4
#!/bin/bash
if [ "$1" = "config" ]; then
echo "graph_data_size custom 1d, 1m for 1w, 1m for 1t, 1m for 1y"
echo "update_rate 30"
fi
case $1 in
config)
cat <<'EOM'
graph_title Temperaturen DG
graph_vlabel Temperatur
graph_category haus
temp0.label Bad Raumtemperatur
temp1.label Zulauf
temp2.label Rücklauf Bad
temp3.label Rücklauf Schlafzimmer 1
temp4.label Rücklauf Schlafzimmer 2
temp5.label Rücklauf Wohnzimmer 1
temp6.label Rücklauf Wohnzimmer 2
temp7.label Rücklauf Küche
EOM
exit 0;;
esac
# load.label load
# load.warning 1.35
# load.critical 1.4
IFS=$'\n'
VALUES=($(cargo run "192.168.3.222:51" \
"28:ff:d8:61:c1:17:05:72" \
"28:ff:f3:54:c1:17:05:33" \
"28:ff:fe:35:c1:17:05:c0" \
"28:ff:71:14:c0:17:04:6e" \
"28:ff:fe:55:c1:17:05:50" \
"28:ff:4b:19:c1:17:04:61" \
"28:ff:0d:13:c0:17:04:a4" \
"28:ff:f7:08:c1:17:04:b8" \
))
unset IFS
echo "temp0.value ${VALUES[0]}"
echo "temp1.value ${VALUES[1]}"
echo "temp2.value ${VALUES[2]}"
echo "temp3.value ${VALUES[3]}"
echo "temp4.value ${VALUES[4]}"
echo "temp5.value ${VALUES[5]}"
echo "temp6.value ${VALUES[6]}"
echo "temp7.value ${VALUES[7]}"
\ No newline at end of file
......@@ -27,10 +27,10 @@ enum Value {
}
fn main() {
if std::env::args().len() > 1 {
if std::env::args().len() > 2 {
let mut devices = Vec::new();
let mut args = std::env::args().collect::<Vec<String>>();
for i in 1..std::env::args().len() {
for i in 2..std::env::args().len() {
let arg = &args[i];
if arg.len() == 23 {
devices.push(Device {
......@@ -48,14 +48,15 @@ fn main() {
}
}
let mut socket = UdpSocket::bind("0.0.0.0:5354").unwrap();
let mut socket = UdpSocket::bind("0.0.0.0:0").unwrap();
socket.set_read_timeout(Some(Duration::from_millis(5000)));
socket.set_read_timeout(Some(Duration::from_millis(1000)));
let mut random = random::default();
let mut buffer = [0; 2048];
let mut error_code = 1;
loop {
'main: for _ in 0..5 {
let size = Request::ReadSpecified(random.read::<u8>(), Bus::OneWire).write(&mut &mut buffer[..]).unwrap();
let size = {
let mut pos = size;
......@@ -67,34 +68,34 @@ fn main() {
pos
};
println!("Requesting");
let request_time = Instant::now();
socket.send_to(&buffer[..size], "192.168.3.222:51").expect("Failed to send");
socket.send_to(&buffer[..size], &args[1]).expect("Failed to send");
if let Ok((amt, src)) = socket.recv_from(&mut buffer) {
let mut reader = &mut &buffer[..amt];
let response = Response::read(reader);
let duration = Instant::now().duration_since(request_time);
println!(" Received from {}: {}bytes, {:?}, {}ms", src, amt, response, duration.as_secs() * 1000 + duration.subsec_millis() as u64);
// println!(" Received from {}: {}bytes, {:?}, {}ms", src, amt, response, duration.as_secs() * 1000 + duration.subsec_millis() as u64);
match response {
Ok(response) => {
if let Err(e) = handle_response(response, reader) {
if let Err(e) = handle_response(response, reader, true) {
println!(" Handling failed: {:?}", e);
}
error_code = 0;
break 'main;
},
_ => {},
};
}
}
println!("{:?}", devices);
std::process::exit(error_code);
} else {
default();
}
}
fn default() {
let mut socket = UdpSocket::bind("0.0.0.0:5354").unwrap();
let mut socket = UdpSocket::bind("0.0.0.0:0").unwrap();
// Receives a single datagram message on the socket. If `buf` is too small to hold
// the message, it will be cut off.
......@@ -115,15 +116,17 @@ fn default() {
println!("Requesting");
let request_time = Instant::now();
socket.send_to(&buffer[..size], "192.168.3.222:51").expect("Failed to send");
if let Ok((amt, src)) = socket.recv_from(&mut buffer) {
let mut reader = &mut &buffer[..amt];
let response = Response::read(reader);
println!(" Received from {}: {}, {:?}", src, amt, response);
let duration = Instant::now().duration_since(request_time);
println!(" Received from {}: {}bytes, {:?}, {}ms", src, amt, response, duration.as_secs() * 1000 + duration.subsec_millis() as u64);
match response {
Ok(response) => {
if let Err(e) = handle_response(response, reader) {
if let Err(e) = handle_response(response, reader, false) {
println!(" Handling failed: {:?}", e);
}
},
......@@ -134,7 +137,7 @@ fn default() {
}
fn handle_response(response: Response, reader: &mut Read) -> Result<Value, Error> {
fn handle_response(response: Response, reader: &mut Read, silent: bool) -> Result<Value, Error> {
Ok(match response {
Response::Ok(id, format) if format == Format::AddressValuePairs(Type::Array(8), Type::F32) => {
let mut devices = Vec::new();
......@@ -151,12 +154,17 @@ fn handle_response(response: Response, reader: &mut Read) -> Result<Value, Error
reader.read_u8()?,
]
};
print!(" {:02x}", device.address[0]);
for i in 0..7 {
print!(":{:02x}", device.address[1+i]);
}
let temp = NetworkEndian::read_f32(&[reader.read_u8()?, reader.read_u8()?, reader.read_u8()?, reader.read_u8()?]);
println!(" with {:.4}°C", temp);
if !silent {
print!(" {:02x}", device.address[0]);
for i in 0..7 {
print!(":{:02x}", device.address[1+i]);
}
println!(" with {:.4}°C", temp);
} else {
println!("{}", temp);
}
devices.push((device, temp));
}
Value::OneWireDeviceValuePairs(devices)
......@@ -176,11 +184,17 @@ fn handle_response(response: Response, reader: &mut Read) -> Result<Value, Error
reader.read_u8()?,
]
};
print!(" {:02x}", device.address[0]);
for i in 0..7 {
print!(":{:02x}", device.address[1+i]);
let temp = NetworkEndian::read_f32(&[reader.read_u8()?, reader.read_u8()?, reader.read_u8()?, reader.read_u8()?]);
if !silent {
print!(" {:02x}", device.address[0]);
for i in 0..7 {
print!(":{:02x}", device.address[1+i]);
}
println!(" with {:.4}°C", temp);
} else {
println!("{}", temp);
}
println!();
devices.push(device);
}
Value::OneWireDevices(devices)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment