Updated server.

Signed-off-by: imi415 <imi415@imi.moe>
This commit is contained in:
imi415 2022-10-03 17:58:57 +08:00
parent 6b50f08c86
commit abe439c51c
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
4 changed files with 41 additions and 24 deletions

View File

@ -1,4 +1,5 @@
MOJI_APPCODE="YOUR_MOJI_APP_CODE" MOJI_APPCODE="YOUR_MOJI_APP_CODE"
MOJI_HAS_BASIC="false"
CACHE_TTL="1800" CACHE_TTL="1800"

View File

@ -1,6 +1,6 @@
GIT GIT
remote: https://github.com/imi415/moji_weather.git remote: https://github.com/imi415/moji_weather.git
revision: 5a08ea60b4f00e98496a38c9a2bcbec6aea6776e revision: 4a56f6ccdad4b2cef66c0e6262aa9a9e36eabbad
branch: master branch: master
specs: specs:
moji_weather (0.0.1) moji_weather (0.0.1)
@ -15,9 +15,9 @@ GEM
benchmark (0.2.0) benchmark (0.2.0)
cbor (0.5.9.6) cbor (0.5.9.6)
diff-lcs (1.5.0) diff-lcs (1.5.0)
dotenv (2.7.6) dotenv (2.8.1)
e2mmap (0.1.0) e2mmap (0.1.0)
faraday (1.10.0) faraday (1.10.2)
faraday-em_http (~> 1.0) faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0) faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1) faraday-excon (~> 1.1)
@ -54,29 +54,29 @@ GEM
nokogiri (1.13.8-x86_64-linux) nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4) racc (~> 1.4)
parallel (1.22.1) parallel (1.22.1)
parser (3.1.2.0) parser (3.1.2.1)
ast (~> 2.4.1) ast (~> 2.4.1)
racc (1.6.0) racc (1.6.0)
rainbow (3.1.1) rainbow (3.1.1)
regexp_parser (2.5.0) regexp_parser (2.6.0)
reverse_markdown (2.1.1) reverse_markdown (2.1.1)
nokogiri nokogiri
rexml (3.2.5) rexml (3.2.5)
rubocop (1.32.0) rubocop (1.36.0)
json (~> 2.3) json (~> 2.3)
parallel (~> 1.10) parallel (~> 1.10)
parser (>= 3.1.0.0) parser (>= 3.1.2.1)
rainbow (>= 2.2.2, < 4.0) rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0) regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.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) ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0) unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.19.1) rubocop-ast (1.21.0)
parser (>= 3.1.1.0) parser (>= 3.1.1.0)
ruby-progressbar (1.11.0) ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
solargraph (0.45.0) solargraph (0.47.2)
backport (~> 1.2) backport (~> 1.2)
benchmark benchmark
bundler (>= 1.17.2) bundler (>= 1.17.2)
@ -93,7 +93,7 @@ GEM
yard (~> 0.9, >= 0.9.24) yard (~> 0.9, >= 0.9.24)
thor (1.2.1) thor (1.2.1)
tilt (2.0.11) tilt (2.0.11)
unicode-display_width (2.2.0) unicode-display_width (2.3.0)
webrick (1.7.0) webrick (1.7.0)
yard (0.9.28) yard (0.9.28)
webrick (~> 1.7.0) webrick (~> 1.7.0)

View File

@ -7,9 +7,10 @@ Bundler.require
Dotenv.load Dotenv.load
Dotenv.require_keys('MOJI_APPCODE', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL') Dotenv.require_keys('MOJI_APPCODE', 'IOT_MQTT_HOST', 'IOT_MQTT_PORT', 'IOT_MQTT_SSL')
begin CITY_ID = 3
client = MQTT::Client.new
client = MQTT::Client.new
begin
client.host = ENV['IOT_MQTT_HOST'] client.host = ENV['IOT_MQTT_HOST']
client.port = ENV['IOT_MQTT_PORT'].to_i client.port = ENV['IOT_MQTT_PORT'].to_i
@ -24,9 +25,11 @@ begin
client.subscribe('iot/weather/testclient/response') 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: 'condition', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'aqi', city_id: 10 }.to_cbor) client.publish('iot/weather/testclient/request', { type: 'aqi', city_id: CITY_ID }.to_cbor)
client.publish('iot/weather/testclient/request', { type: 'forecast24', city_id: 10 }.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| client.get do |_, payload|
p CBOR.decode(payload) p CBOR.decode(payload)

View File

@ -7,7 +7,13 @@ 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')
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) cache = LruRedux::TTL::Cache.new(100, ENV['CACHE_TTL'].to_i)
def extract_device_id(topic) def extract_device_id(topic)
@ -21,19 +27,26 @@ def extract_device_id(topic)
end end
def extract_req_type(type) def extract_req_type(type)
wxapi = @wxapi_simple.nil? ? @wxapi : @wxapi_simple
case type case type
when 'condition' when 'condition'
MojiWeather::Api::ApiType::CONDITION [MojiWeather::Api::ApiType::CONDITION, wxapi]
when 'aqi' when 'aqi'
MojiWeather::Api::ApiType::AQI [MojiWeather::Api::ApiType::AQI, wxapi]
when 'forecast24' when 'forecast24h'
MojiWeather::Api::ApiType::FORECAST_24HRS [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
end end
client = MQTT::Client.new
begin begin
client = MQTT::Client.new
client.host = ENV['IOT_MQTT_HOST'] client.host = ENV['IOT_MQTT_HOST']
client.port = ENV['IOT_MQTT_PORT'].to_i client.port = ENV['IOT_MQTT_PORT'].to_i
@ -59,7 +72,7 @@ begin
# decode CBOR object, retrieve request. # decode CBOR object, retrieve request.
dev_req = CBOR.decode(payload) 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 # Not a valid type
next if wx_cond.nil? next if wx_cond.nil?