Class: Cocoro::Client
- Inherits:
-
Object
- Object
- Cocoro::Client
- Defined in:
- lib/cocoro/client.rb
Overview
A client for the Cocoro Air API that the official mobile apps use.
Constant Summary collapse
- BASE_URL =
"https://hms.cloudlabs.sharp.co.jp/hems/pfApi/ta"
- BASE_HEADERS =
{ "Content-Type": "application/json; charset=utf-8", Accept: "*/*", "Accept-Language" => "ja-jp", "User-Agent" => <<~UA.gsub(/[\r\n ]+/, " ") smartlink_v200i Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 UA }.freeze
- DEFAULT_RESULT_CHECK_INTERVAL =
seconds
0.7
- DEFAULT_RESULT_CHECK_MAX_ATTEMPTS =
20
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #control_device(device:, changes: [], result_check_interval: DEFAULT_RESULT_CHECK_INTERVAL, result_check_max_attempts: DEFAULT_RESULT_CHECK_MAX_ATTEMPTS) ⇒ Object
- #device_status(device:) ⇒ Object
- #devices ⇒ Object
-
#initialize(app_secret:, terminal_app_id_key:, logger: nil) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
Constructor Details
#initialize(app_secret:, terminal_app_id_key:, logger: nil) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cocoro/client.rb', line 32 def initialize(app_secret:, terminal_app_id_key:, logger: nil) @app_secret = app_secret @terminal_app_id_key = terminal_app_id_key @logger = logger || Logger.new($stdout).tap do |l| l.level = Logger::WARN end @client = Faraday.new( url: BASE_URL, headers: BASE_HEADERS ) do |f| f.use :cookie_jar f.request :json f.response :logger, @logger, { bodies: true, log_level: :debug } f.response :raise_error f.response :json f.adapter :net_http end end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
30 31 32 |
# File 'lib/cocoro/client.rb', line 30 def logger @logger end |
Instance Method Details
#control_device(device:, changes: [], result_check_interval: DEFAULT_RESULT_CHECK_INTERVAL, result_check_max_attempts: DEFAULT_RESULT_CHECK_MAX_ATTEMPTS) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cocoro/client.rb', line 90 def control_device( device:, changes: [], result_check_interval: DEFAULT_RESULT_CHECK_INTERVAL, result_check_max_attempts: DEFAULT_RESULT_CHECK_MAX_ATTEMPTS ) request_id = make_control_request!(device, changes) finished = false attempt = 0 until finished attempt += 1 result = fetch_control_result!(device, request_id) finished = result["status"] == "success" || result["status"] == "unmatch" next if finished raise Cocoro::Error, "Device not responding" if attempt >= result_check_max_attempts sleep result_check_interval end end |
#device_status(device:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cocoro/client.rb', line 74 def device_status(device:) query_params = { appSecret: @app_secret, boxId: device.box_id, echonetNode: device.echonet_node, echonetObject: device.echonet_object }.map { |k, v| [k, CGI.escape(v)].join("=") } response = handle_server_errors do @client.get( "control/deviceStatus?#{query_params.join("&")}" ) end json = response.body Cocoro::Status.new(json["deviceStatus"]["status"]) end |
#devices ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/cocoro/client.rb', line 65 def devices response = handle_server_errors do @client.get( "setting/boxInfo?appSecret=#{CGI.escape(@app_secret)}&mode=other" ) end Cocoro::Device.parse_box_info(response.body["box"], self) end |
#login ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cocoro/client.rb', line 51 def login body = { "terminalAppId" => "https://db.cloudlabs.sharp.co.jp/clpf/key/#{@terminal_app_id_key}" } @client.post( "setting/login?appSecret=#{CGI.escape(@app_secret)}", body, {} ) rescue Faraday::Error raise Cocoro::ServerError end |