Use logger for logging.
This commit is contained in:
parent
abe439c51c
commit
61561bd1a4
|
@ -1,5 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'logger'
|
||||||
|
|
||||||
require 'bundler'
|
require 'bundler'
|
||||||
|
|
||||||
Bundler.require
|
Bundler.require
|
||||||
|
@ -7,6 +9,8 @@ Bundler.require
|
||||||
Dotenv.load
|
Dotenv.load
|
||||||
Dotenv.require_keys('MOJI_APPCODE', 'CACHE_TTL', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL')
|
Dotenv.require_keys('MOJI_APPCODE', 'CACHE_TTL', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL')
|
||||||
|
|
||||||
|
@logger = Logger.new($stdout)
|
||||||
|
|
||||||
@wxapi = MojiWeather::Api::RestClient.new(app_code: ENV['MOJI_APPCODE'])
|
@wxapi = MojiWeather::Api::RestClient.new(app_code: ENV['MOJI_APPCODE'])
|
||||||
@wxapi_simple = nil
|
@wxapi_simple = nil
|
||||||
|
|
||||||
|
@ -59,7 +63,7 @@ begin
|
||||||
|
|
||||||
client.connect
|
client.connect
|
||||||
|
|
||||||
puts "[#{Time.now}] client [#{client}] connected..."
|
@logger.info "client [#{client}] connected..."
|
||||||
|
|
||||||
client.subscribe('iot/weather/#')
|
client.subscribe('iot/weather/#')
|
||||||
|
|
||||||
|
@ -67,7 +71,7 @@ begin
|
||||||
# this method also checks if this is from a request topic.
|
# this method also checks if this is from a request topic.
|
||||||
dev_id = extract_device_id(topic)
|
dev_id = extract_device_id(topic)
|
||||||
unless dev_id.nil?
|
unless dev_id.nil?
|
||||||
puts "[#{Time.now}][#{dev_id}] <- #{payload.length}B"
|
@logger.info "[#{dev_id}] <- #{payload.length}B"
|
||||||
|
|
||||||
# decode CBOR object, retrieve request.
|
# decode CBOR object, retrieve request.
|
||||||
dev_req = CBOR.decode(payload)
|
dev_req = CBOR.decode(payload)
|
||||||
|
@ -82,7 +86,7 @@ begin
|
||||||
elsif !dev_req['location'].nil?
|
elsif !dev_req['location'].nil?
|
||||||
{ location: { lat: dev_req['location']['lat'], lon: dev_req['location']['lon'] } }
|
{ location: { lat: dev_req['location']['lat'], lon: dev_req['location']['lon'] } }
|
||||||
else
|
else
|
||||||
puts "[#{Time.now}][#{dev_id}] not a valid request"
|
@logger.warn "[#{dev_id}] not a valid request"
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -93,28 +97,28 @@ begin
|
||||||
|
|
||||||
# Cache missed..
|
# Cache missed..
|
||||||
if api_resp.nil?
|
if api_resp.nil?
|
||||||
puts "[#{Time.now}][#{dev_id}] cache missed"
|
@logger.info "[#{dev_id}] cache missed"
|
||||||
# Request external service for weather information
|
# Request external service for weather information
|
||||||
api_resp = wxapi.query(wx_cond, req_params)
|
api_resp = wxapi.query(wx_cond, req_params)
|
||||||
|
|
||||||
# Update cache entry
|
# Update cache entry
|
||||||
cache["cache_#{req_params}"] = api_resp
|
cache["cache_#{req_params}"] = api_resp
|
||||||
else
|
else
|
||||||
puts "[#{Time.now}][#{dev_id}] cache hit"
|
@logger.info "[#{dev_id}] cache hit"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Encode CBOR object
|
# Encode CBOR object
|
||||||
resp = api_resp.to_cbor
|
resp = api_resp.to_cbor
|
||||||
|
|
||||||
# Publish to response topic.
|
# Publish to response topic.
|
||||||
puts "[#{Time.now}][#{dev_id}] -> #{resp.length}B"
|
@logger.info "[#{dev_id}] -> #{resp.length}B"
|
||||||
|
|
||||||
# Send response
|
# Send response
|
||||||
client.publish("iot/weather/#{dev_id}/response", resp)
|
client.publish("iot/weather/#{dev_id}/response", resp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue SystemExit, Interrupt
|
rescue SystemExit, Interrupt
|
||||||
puts "[#{Time.now}] Interrupt caught, client [#{client}] disconnect."
|
@logger.warn "Interrupt caught, client [#{client}] disconnect."
|
||||||
client.disconnect
|
client.disconnect
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
p e
|
p e
|
||||||
|
|
Loading…
Reference in New Issue