james-fry / hassio-addons

Repository for hass.io add ons
99 stars 96 forks source link

rtl4332mqtt : Named pipe instead of reconnects? #22

Open rb2k opened 5 years ago

rb2k commented 5 years ago

The addon currently uses this combination to send each line to mqtt:

/usr/local/bin/rtl_433 -F json -R $PROTOCOL -f $FREQUENCY -g $GAIN -p $OFFSET | while read line
[...]
echo $line | /usr/bin/mosquitto_pub [...]

This works but causes a fork/exec on every single line that comes in.

My local script is heavily hacked modified, but basically I changed the script a bit to use this methodology:

  1. Create a named pipe:
    mkfifo /tmp/messages.pool
  2. Fork off a process that tails the pipe and a mosquitto one that reads from it For the 'cat 0<> ' see https://unix.stackexchange.com/questions/392697/reading-a-named-pipe-tail-or-cat
function start_tailing() {
  cat 0<> /tmp/messages | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_TOPIC                     
}
start_tailing &
  1. The blocking thing that iterates over each line and ultimately writes to the pipe:
echo $line > /tmp/messages.pool

That way we only fork/exec the mosquitto_pub process once rather than on every single line that comes in.

What do you think about that approach?

rb2k commented 5 years ago

I guess one problem I should mention: The custom channels disappear and the model name and id data ends up just in the payload.