Skip to content

Commit

Permalink
Refactor Modbus connection
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 14, 2024
1 parent 58944c0 commit 06e1aec
Show file tree
Hide file tree
Showing 50 changed files with 525 additions and 276 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Layout/LineLength:
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Lint/MissingSuper:
Enabled: false

Metrics/AbcSize:
Exclude:
- "**/db/migrate/**/*.rb"
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ gem "tailwindcss-rails"
gem "turbo-rails"

## LIBRARIES
gem "bindata"
gem "bootsnap", require: false
gem "concurrent-ruby"
gem "foreman"
gem "http"
gem "mqtt"
gem "rails-settings-cached"
gem "rmodbus"
gem "rufus-scheduler"
gem "solid_queue"
gem "sqids"
Expand Down
7 changes: 5 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ GEM
rack (>= 0.9.0)
rouge (>= 1.0.0)
bigdecimal (3.1.5)
bindata (2.4.15)
bindex (0.8.1)
bootsnap (1.17.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -160,6 +161,7 @@ GEM
matrix (0.4.2)
mini_mime (1.1.5)
minitest (5.20.0)
mqtt (0.6.0)
msgpack (1.7.2)
mutex_m (0.2.0)
net-imap (0.4.1)
Expand Down Expand Up @@ -242,7 +244,6 @@ GEM
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rmodbus (1.3.3)
rouge (4.1.3)
rubocop (1.59.0)
json (~> 2.3)
Expand Down Expand Up @@ -328,18 +329,20 @@ PLATFORMS
DEPENDENCIES
amazing_print
better_errors
bindata
bootsnap
capybara
concurrent-ruby
debug
dotenv-rails
foreman
http
importmap-rails
mqtt
propshaft
puma
rails
rails-settings-cached
rmodbus
rubocop
rubocop-capybara
rubocop-minitest
Expand Down
9 changes: 9 additions & 0 deletions app/gateways/application_gateway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ApplicationGateway
def close
raise NotImplementedError
end

def read(*)
raise NotImplementedError
end
end
92 changes: 92 additions & 0 deletions app/gateways/modbus_mqtt_gateway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class ModbusMQTTGateway < ApplicationGateway
def initialize
@client = initialize_client
@transaction = initialize_transaction
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def read(register)
transaction = next_transaction

promise = Concurrent::Promise.execute do
@client.subscribe(mqtt_subscribe_topic)

request = Modbus::ADU.read_register(
transaction, modbus_slave_id, register.address, register.quantity
).to_binary_s

@client.publish(mqtt_publish_topic, request)

loop do
_topic, message = @client.get
adu = Modbus::ADU.parse(message)

break adu if adu.transaction == transaction
end
end

promise.value(30) ? register.from_binary(promise.value.get.data) : nil
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

def close
@client&.disconnect
end

private

def mqtt_host
Rails.configuration.x.gateways.modbus_mqtt.host
end

def mqtt_port
Rails.configuration.x.gateways.modbus_mqtt.port.try(:to_i)
end

def mqtt_ssl
Rails.configuration.x.gateways.modbus_mqtt.ssl.try(:to_boolean)
end

def mqtt_account
Rails.configuration.x.gateways.modbus_mqtt.account
end

def mqtt_password
Rails.configuration.x.gateways.modbus_mqtt.password
end

def mqtt_subscribe_topic
Rails.configuration.x.gateways.modbus_mqtt.subscribe_topic
end

def mqtt_publish_topic
Rails.configuration.x.gateways.modbus_mqtt.publish_topic
end

def modbus_slave_id
Rails.configuration.x.gateways.modbus_mqtt.slave_id.try(:to_i)
end

def initialize_client
MQTT::Client.connect(
host: mqtt_host,
port: mqtt_port,
ssl: mqtt_ssl,
username: mqtt_account,
password: mqtt_password
)
end

def initialize_transaction
Random.new.rand(65_535)
end

def next_transaction
id = @transaction
@transaction += 1
@transaction = 0 if @transaction > 65_535 # 2 bytes 0xFFFF
id
end
end
21 changes: 0 additions & 21 deletions app/lib/modbus_types.rb

This file was deleted.

21 changes: 0 additions & 21 deletions app/lib/solaris/inverter.rb

This file was deleted.

17 changes: 0 additions & 17 deletions app/lib/solaris/inverters/base.rb

This file was deleted.

57 changes: 0 additions & 57 deletions app/lib/solaris/inverters/huawei.rb

This file was deleted.

52 changes: 0 additions & 52 deletions app/lib/solaris/inverters/modbus_inverter.rb

This file was deleted.

36 changes: 0 additions & 36 deletions app/lib/solaris/loop.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ class Setting < RailsSettings::Base
field :inverter_brand, type: :string
field :inverter_model, type: :string
field :inverter_serial_number, type: :string
field :inverter_firmware_version, type: :string
field :viewed_at, type: :datetime
end
Loading

0 comments on commit 06e1aec

Please sign in to comment.