Skip to content

Instantly share code, notes, and snippets.

@chendo
Last active January 11, 2022 11:49
Show Gist options
  • Save chendo/c4b8a28a244bd33d17778f61dfb189be to your computer and use it in GitHub Desktop.
Save chendo/c4b8a28a244bd33d17778f61dfb189be to your computer and use it in GitHub Desktop.

Revisions

  1. chendo revised this gist Jan 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion raven.rb
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@

    begin
    IO.popen('raven') do |io|
    puts "Process started, connecting to MQTT server"
    puts "Process started, connecting to MQTT server..."

    MQTT::Client.connect('mqtt://user:pass@host') do |c|
    puts "Connected to MQTT server."
  2. chendo revised this gist Jan 11, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions raven.rb
    Original file line number Diff line number Diff line change
    @@ -41,4 +41,5 @@
    $stderr.puts(ex.backtrace.join("\n"))
    $stderr.puts("Restarting...")
    sleep 2
    retry
    end
  3. chendo revised this gist Jan 11, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions raven.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # License: Public domain
    # Depends on https://github.com/nonspecialist/pyraven being installed and accessible at `raven`

    require 'bundler/inline'
  4. chendo revised this gist Jan 11, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions raven.rb
    Original file line number Diff line number Diff line change
    @@ -15,19 +15,24 @@
    begin
    IO.popen('raven') do |io|
    puts "Process started, connecting to MQTT server"

    MQTT::Client.connect('mqtt://user:pass@host') do |c|
    puts "Connected to MQTT server."

    while line = io.gets do
    begin
    data = JSON.parse(line)

    if demand = data['raw_demand']
    puts "#{Time.now.iso8601}\t Demand: #{demand}W"
    c.publish('energy/meter', demand)
    end

    rescue StandardError => ex
    $stderr.puts("Error publishing: #{ex}")
    end
    end

    end
    end
    rescue StandardError => ex
  5. chendo created this gist Jan 11, 2022.
    38 changes: 38 additions & 0 deletions raven.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    # Depends on https://github.com/nonspecialist/pyraven being installed and accessible at `raven`

    require 'bundler/inline'

    gemfile do
    source 'https://rubygems.org'
    gem 'mqtt'
    end

    require 'json'
    require 'time'

    ENV['PYTHONUNBUFFERED'] = 'true'

    begin
    IO.popen('raven') do |io|
    puts "Process started, connecting to MQTT server"
    MQTT::Client.connect('mqtt://user:pass@host') do |c|
    puts "Connected to MQTT server."
    while line = io.gets do
    begin
    data = JSON.parse(line)
    if demand = data['raw_demand']
    puts "#{Time.now.iso8601}\t Demand: #{demand}W"
    c.publish('energy/meter', demand)
    end
    rescue StandardError => ex
    $stderr.puts("Error publishing: #{ex}")
    end
    end
    end
    end
    rescue StandardError => ex
    $stderr.puts("Error: #{ex}")
    $stderr.puts(ex.backtrace.join("\n"))
    $stderr.puts("Restarting...")
    sleep 2
    end