From abe439c51c7abaaf5898861d96bde24fa1033ba0 Mon Sep 17 00:00:00 2001 From: imi415 Date: Mon, 3 Oct 2022 17:58:57 +0800 Subject: [PATCH] Updated server. Signed-off-by: imi415 --- .env.example | 1 + Gemfile.lock | 22 +++++++++++----------- src/client.rb | 13 ++++++++----- src/server.rb | 29 +++++++++++++++++++++-------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/.env.example b/.env.example index a7b78e5..444ac41 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ MOJI_APPCODE="YOUR_MOJI_APP_CODE" +MOJI_HAS_BASIC="false" CACHE_TTL="1800" diff --git a/Gemfile.lock b/Gemfile.lock index 88d0e1c..7fae885 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/imi415/moji_weather.git - revision: 5a08ea60b4f00e98496a38c9a2bcbec6aea6776e + revision: 4a56f6ccdad4b2cef66c0e6262aa9a9e36eabbad branch: master specs: moji_weather (0.0.1) @@ -15,9 +15,9 @@ GEM benchmark (0.2.0) cbor (0.5.9.6) diff-lcs (1.5.0) - dotenv (2.7.6) + dotenv (2.8.1) e2mmap (0.1.0) - faraday (1.10.0) + faraday (1.10.2) faraday-em_http (~> 1.0) faraday-em_synchrony (~> 1.0) faraday-excon (~> 1.1) @@ -54,29 +54,29 @@ GEM nokogiri (1.13.8-x86_64-linux) racc (~> 1.4) parallel (1.22.1) - parser (3.1.2.0) + parser (3.1.2.1) ast (~> 2.4.1) racc (1.6.0) rainbow (3.1.1) - regexp_parser (2.5.0) + regexp_parser (2.6.0) reverse_markdown (2.1.1) nokogiri rexml (3.2.5) - rubocop (1.32.0) + rubocop (1.36.0) json (~> 2.3) parallel (~> 1.10) - parser (>= 3.1.0.0) + parser (>= 3.1.2.1) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.19.1, < 2.0) + rubocop-ast (>= 1.20.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.19.1) + rubocop-ast (1.21.0) parser (>= 3.1.1.0) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) - solargraph (0.45.0) + solargraph (0.47.2) backport (~> 1.2) benchmark bundler (>= 1.17.2) @@ -93,7 +93,7 @@ GEM yard (~> 0.9, >= 0.9.24) thor (1.2.1) tilt (2.0.11) - unicode-display_width (2.2.0) + unicode-display_width (2.3.0) webrick (1.7.0) yard (0.9.28) webrick (~> 1.7.0) diff --git a/src/client.rb b/src/client.rb index 0531038..804f415 100644 --- a/src/client.rb +++ b/src/client.rb @@ -7,9 +7,10 @@ Bundler.require Dotenv.load Dotenv.require_keys('MOJI_APPCODE', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL') -begin - client = MQTT::Client.new +CITY_ID = 3 +client = MQTT::Client.new +begin client.host = ENV['IOT_MQTT_HOST'] client.port = ENV['IOT_MQTT_PORT'].to_i @@ -24,9 +25,11 @@ begin client.subscribe('iot/weather/testclient/response') - client.publish('iot/weather/testclient/request', { type: 'condition', city_id: 10 }.to_cbor) - client.publish('iot/weather/testclient/request', { type: 'aqi', city_id: 10 }.to_cbor) - client.publish('iot/weather/testclient/request', { type: 'forecast24', city_id: 10 }.to_cbor) + 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) client.get do |_, payload| p CBOR.decode(payload) diff --git a/src/server.rb b/src/server.rb index e827858..684e999 100644 --- a/src/server.rb +++ b/src/server.rb @@ -7,7 +7,13 @@ Bundler.require Dotenv.load Dotenv.require_keys('MOJI_APPCODE', 'CACHE_TTL', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL') -wxapi = MojiWeather::Api::RestClient.new(app_code: ENV['MOJI_APPCODE']) +@wxapi = MojiWeather::Api::RestClient.new(app_code: ENV['MOJI_APPCODE']) +@wxapi_simple = nil + +unless ENV['MOJI_HAS_BASIC'].nil? + @wxapi_simple = MojiWeather::Api::RestClient.new(app_code: ENV['MOJI_APPCODE'], cityid_base: 'http://aliv13.data.moji.com/whapi/json/alicityweather') +end + cache = LruRedux::TTL::Cache.new(100, ENV['CACHE_TTL'].to_i) def extract_device_id(topic) @@ -21,19 +27,26 @@ def extract_device_id(topic) end def extract_req_type(type) + wxapi = @wxapi_simple.nil? ? @wxapi : @wxapi_simple + case type when 'condition' - MojiWeather::Api::ApiType::CONDITION + [MojiWeather::Api::ApiType::CONDITION, wxapi] when 'aqi' - MojiWeather::Api::ApiType::AQI - when 'forecast24' - MojiWeather::Api::ApiType::FORECAST_24HRS + [MojiWeather::Api::ApiType::AQI, wxapi] + when 'forecast24h' + [MojiWeather::Api::ApiType::FORECAST_24HRS, @wxapi] + when 'forecast6d' + [MojiWeather::Api::ApiType::FORECAST_6DAYS, wxapi] + when 'forecast15d' + [MojiWeather::Api::ApiType::FORECAST_15DAYS, @wxapi] + else + [nil, nil] end end +client = MQTT::Client.new begin - client = MQTT::Client.new - client.host = ENV['IOT_MQTT_HOST'] client.port = ENV['IOT_MQTT_PORT'].to_i @@ -59,7 +72,7 @@ begin # decode CBOR object, retrieve request. dev_req = CBOR.decode(payload) - wx_cond = extract_req_type(dev_req['type']) + wx_cond, wxapi = extract_req_type(dev_req['type']) # Not a valid type next if wx_cond.nil?