-
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
50 changed files
with
525 additions
and
276 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
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,9 @@ | ||
class ApplicationGateway | ||
def close | ||
raise NotImplementedError | ||
end | ||
|
||
def read(*) | ||
raise NotImplementedError | ||
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,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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.