5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/json_rpc_over_mqtt_example/mqtt/connect.rb', line 5
def self.start
::MQTT::Client.connect(BROKER_ADDRESS) do |c|
c.get(SERVER_TOPIC_NAME) do |topic, message|
logger.info("TOPIC: #{topic} MESSAGE:#{message}")
begin
request = JSONRPC::Request.new(message)
unless request.params.nil?
result = method(request.method).call(**request.params)
else
result = method(request.method).call
end
c.publish(
CLIENT_TOPIC_NAME,
JSONRPC::Response.generate(request.id, result)
)
rescue ::JSON::ParserError => e
c.publish(
CLIENT_TOPIC_NAME,
JSONRPC::ErrorResponse.parse_error
)
rescue NameError => e
c.publish(
CLIENT_TOPIC_NAME,
JSONRPC::ErrorResponse.method_not_found(request.id)
)
end
end
end
end
|