-
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.
Also add checking for the correct discovery message to be received
- Loading branch information
1 parent
14a0f26
commit 4b15b2f
Showing
2 changed files
with
14 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'socket' | ||
require 'logger' | ||
|
||
module AlpacaDevice | ||
class DiscoveryService | ||
def initialize(configuration:) | ||
@discovery_host = configuration.discovery_host | ||
@discovery_port = configuration.discovery_port | ||
@alpaca_port = configuration.alpaca_port | ||
|
||
@logger = Logger.new(STDOUT) | ||
@logger.level = :info | ||
end | ||
|
||
def run | ||
server = UDPSocket.new | ||
server.bind(discovery_host, discovery_port) | ||
|
||
logger.info('DiscoveryService') { "Discovery service started at #{discovery_host}:#{discovery_port}..." } | ||
|
||
loop do | ||
text, sender = server.recvfrom(24) | ||
remote_port = sender[1] | ||
remote_host = sender[3] | ||
server.send "{\"alpacaport\": #{alpaca_port}}", 0, remote_host, remote_port | ||
|
||
logger.info('DiscoveryService') { "Received message '#{text}' from #{remote_host}:#{remote_port}" } | ||
|
||
if text == 'alpacadiscovery1' | ||
server.send "{\"alpacaport\": #{alpaca_port}}", 0, remote_host, remote_port | ||
end | ||
end | ||
|
||
server.close | ||
end | ||
|
||
private | ||
|
||
attr_reader :discovery_host, :discovery_port, :alpaca_port | ||
attr_reader :discovery_host, :discovery_port, :alpaca_port, :logger | ||
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module AlpacaDevice | ||
VERSION = "0.1.1" | ||
VERSION = '0.1.2' | ||
end |