Skip to content

Commit

Permalink
Merge pull request #69 from javierav/feature/inverter-model
Browse files Browse the repository at this point in the history
Change Inverter model attributes
  • Loading branch information
javierav authored Jan 7, 2024
2 parents 849b2e2 + 36331ad commit 658e320
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 26 deletions.
5 changes: 4 additions & 1 deletion app/models/inverter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class Inverter < ApplicationRecord
include Sqideable

enum :installation_type, %i[domestic other].to_enum_hash

belongs_to :protocol
belongs_to :zone

validates :name, presence: true
validates :timezone, presence: true
validates :installation_type, presence: true
end
12 changes: 9 additions & 3 deletions db/migrate/20231220151918_create_inverters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ def change
create_table :inverters do |t|
t.timestamps null: false
t.string :name, null: false
t.string :installation_type, null: false
t.integer :installation_power
t.integer :installation_panels
t.integer :installation_price
t.date :installation_date
t.references :zone, null: false, foreign_key: true
t.string :latitude
t.string :longitude
t.references :protocol, null: false, foreign_key: true
t.string :timezone, null: false
t.integer :loop_interval, null: false, default: 60
t.integer :archive_interval, null: false, default: 300
t.string :brand
t.string :model
t.string :serial_number
t.string :firmware_version
t.datetime :viewed_at
end
end
end
14 changes: 11 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions test/fixtures/inverters.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
house:
name: "Casa"
protocol: huawei_modbus
timezone: "Europe/Madrid"
DEFAULTS: &DEFAULTS
installation_type: "domestic"
protocol_id: 1
brand: "Huawei"
model: "SUN2000-3.68KTL-L1"
serial_number: "HV0000000000"
firmware_version: "V200R001C00SPC130"

house1:
<<: *DEFAULTS
name: "Casa 1"
zone_id: 1
serial_number: "HV0000000001"

house2:
<<: *DEFAULTS
name: "Casa 2"
zone_id: 1
serial_number: "HV0000000002"

house3:
<<: *DEFAULTS
name: "Casa 3"
zone_id: 1
serial_number: "HV0000000003"

house4:
<<: *DEFAULTS
name: "Casa 4"
zone_id: 2
serial_number: "HV0000000004"
19 changes: 10 additions & 9 deletions test/fixtures/protocols.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
huawei_modbus:
id: 1
name: "Huawei Modbus"
gateways:
- modbus_mqtt
- "modbus_mqtt"
solar_power:
address: 32_080
quantity: 2
type: :int32be
type: "int32be"
scale: 1
solar_energy:
address: 32_114
quantity: 2
type: :uint32be
type: "uint32be"
scale: 100
grid_power:
address: 37_113
quantity: 2
type: :int32be
type: "int32be"
scale: 1
grid_energy_export:
address: 37_119
quantity: 2
type: :int32be
type: "int32be"
scale: 100
grid_energy_import:
address: 37_121
quantity: 2
type: :int32be
type: "int32be"
scale: 100
model:
address: 30_000
quantity: 15
type: :string
type: "string"
serial_number:
address: 30_015
quantity: 10
type: :string
type: "string"
firmware_version:
address: 30_025
quantity: 10
type: :string
type: "string"
19 changes: 14 additions & 5 deletions test/models/inverter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@ class InverterTest < ActiveSupport::TestCase
assert inverter.errors.added?(:protocol, :blank)
end

test "save inverter without timezone" do
test "save inverter without zone" do
inverter = Inverter.new

assert_not inverter.save
assert inverter.errors.added?(:timezone, :blank)
assert inverter.errors.added?(:zone, :blank)
end

test "save inverter with name, protocol and timezone" do
inverter = Inverter.new(name: "Inverter 1", protocol: protocols(:huawei_modbus), timezone: "Europe/Madrid")
test "save inverter without installation_type" do
inverter = Inverter.new

assert_not inverter.save
assert inverter.errors.added?(:installation_type, :blank)
end

test "save inverter with name, installation_type, protocol and zone" do
inverter = Inverter.new(
name: "Inverter 1", installation_type: "domestic", protocol: protocols(:huawei_modbus), zone: zones(:peninsula)
)

assert inverter.save
end

test "sqid" do
inverter = inverters(:house)
inverter = inverters(:house1)

assert_equal inverter, Inverter.from_sqid(inverter.to_param)
assert_kind_of String, inverter.to_param
Expand Down

0 comments on commit 658e320

Please sign in to comment.