MQTT_WeatherGateway/src/client.rb

43 lines
1.3 KiB
Ruby
Raw Normal View History

2022-07-24 16:50:42 +00:00
# frozen_string_literal: true
require 'bundler'
Bundler.require
Dotenv.load
Dotenv.require_keys('MOJI_APPCODE', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL')
CITY_ID = 3
2022-07-24 16:50:42 +00:00
client = MQTT::Client.new
begin
2022-07-24 16:50:42 +00:00
client.host = ENV['IOT_MQTT_HOST']
client.port = ENV['IOT_MQTT_PORT'].to_i
unless ENV['IOT_MQTT_SSL'].nil? || ENV['IOT_MQTT_SSL'] == 'false' || ENV['IOT_MQTT_SSL'] == 'no'
client.ssl = true
client.cert_file = ENV['IOT_MQTT_SSL_CERT_FILE']
client.key_file = ENV['IOT_MQTT_SSL_KEY_FILE']
client.ca_file = ENV['IOT_MQTT_SSL_CA_FILE']
end
client.connect
client.subscribe('iot/weather/testclient/response')
client.publish('iot/weather/testclient/request', { type: 'condition', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'aqi', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'forecast24h', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'forecast6d', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'forecast15d', city_id: CITY_ID }.to_cbor)
2022-07-24 16:50:42 +00:00
2022-07-31 08:35:02 +00:00
client.get do |_, payload|
p CBOR.decode(payload)
end
2022-07-24 16:50:42 +00:00
rescue SystemExit, Interrupt
puts "Interrupt caught, client [#{client}] disconnect."
client.disconnect
rescue StandardError => e
p e
end