-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
168 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Loops | ||
class Archive | ||
def self.run(store) | ||
new(store).run | ||
end | ||
|
||
def initialize(store) | ||
@store = store | ||
end | ||
|
||
def run | ||
::Archive.create!(@store.get.merge(datetime: Time.current)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Loops | ||
class Main | ||
def self.run(store) | ||
new(store).run | ||
end | ||
|
||
def initialize(store) | ||
@store = store | ||
end | ||
|
||
def run | ||
@store.set(Inverter.read) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class MetricStore | ||
def initialize | ||
@mutex = Mutex.new | ||
reset | ||
end | ||
|
||
def set(metrics) | ||
@mutex.synchronize do | ||
@metrics.each_key do |key| | ||
@metrics[key] << metrics[key] if metrics.key?(key) | ||
end | ||
end | ||
end | ||
|
||
def get | ||
@mutex.synchronize do | ||
@metrics.to_h { |key, values| [key, send("reduce_#{key}", values)] }.tap { reset } | ||
end | ||
end | ||
|
||
private | ||
|
||
def reset | ||
@metrics = { solar_power: [], solar_energy: [], grid_power: [], grid_energy_import: [], grid_energy_export: [] } | ||
end | ||
|
||
def reduce_solar_power(values) | ||
values.median || 0 | ||
end | ||
|
||
def reduce_solar_energy(values) | ||
values.max || 0.0 | ||
end | ||
|
||
def reduce_grid_power(values) | ||
values.median || 0 | ||
end | ||
|
||
def reduce_grid_energy_import(values) | ||
values.max || 0.0 | ||
end | ||
|
||
def reduce_grid_energy_export(values) | ||
values.max || 0.0 | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "test_helper" | ||
|
||
module Loops | ||
class ArchiveTest < ActiveSupport::TestCase | ||
test "create archive with stored data" do | ||
store = MetricStore.new | ||
|
||
store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13) | ||
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14) | ||
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15) | ||
|
||
assert_difference("::Archive.count", 1) do | ||
Loops::Archive.run(store) | ||
end | ||
|
||
archive = ::Archive.last | ||
|
||
assert_equal 2, archive.solar_power | ||
assert_in_delta(6.0, archive.solar_energy) | ||
assert_equal 8, archive.grid_power | ||
assert_in_delta(12.0, archive.grid_energy_import) | ||
assert_in_delta(15.0, archive.grid_energy_export) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "test_helper" | ||
|
||
module Loops | ||
class MainTest < ActiveSupport::TestCase | ||
test "store inverter data" do | ||
store = Minitest::Mock.new | ||
store.expect(:set, nil, [{ solar_power: 123 }]) | ||
|
||
Inverter.stub(:read, { solar_power: 123 }) do | ||
Loops::Main.run(store) | ||
store.verify | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "test_helper" | ||
|
||
class MetricStoreTest < ActiveSupport::TestCase | ||
test "get empty" do | ||
store = MetricStore.new | ||
|
||
assert_equal( | ||
{ solar_power: 0, solar_energy: 0.0, grid_power: 0, grid_energy_import: 0.0, grid_energy_export: 0.0 }, | ||
store.get | ||
) | ||
end | ||
|
||
test "get with values" do | ||
store = MetricStore.new | ||
|
||
store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13) | ||
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14) | ||
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15) | ||
|
||
assert_equal( | ||
{ solar_power: 2, solar_energy: 6.0, grid_power: 8, grid_energy_import: 12.0, grid_energy_export: 15.0 }, | ||
store.get | ||
) | ||
end | ||
|
||
test "reset after get" do | ||
store = MetricStore.new | ||
|
||
store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13) | ||
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14) | ||
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15) | ||
|
||
store.get | ||
|
||
assert_equal( | ||
{ solar_power: 0, solar_energy: 0.0, grid_power: 0, grid_energy_import: 0.0, grid_energy_export: 0.0 }, | ||
store.get | ||
) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.