cocoro 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 45dac1b85f589ac09c01682a1edc38cb0176736c09c7925a840f9dc2f1ccdbc9
4
+ data.tar.gz: 9a184111ee00c3491226efbef049b925bbd7f2e2322cfa3bf3c70645fe157e65
5
+ SHA512:
6
+ metadata.gz: abc7cf5682668a46f109acab88449a7266c6c2cd08f9503c7234b2c71760f1778c8eff6cb81eb7ebe5b074d17f2da256ece113e2e4ee2e9f9864157f715e9152
7
+ data.tar.gz: 834021cdc1a98adeb9156961f2d36dc1098572dee7a468b62b4df0522897024a321802201758a7bd3fbfca6c7ccdb2e094987d2041ec0d406f6c4b560528e0c0
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 100
16
+
17
+ Metrics/MethodLength:
18
+ Max: 30
19
+
20
+ Metrics/ClassLength:
21
+ Max: 200
22
+
23
+ Metrics/ModuleLength:
24
+ Max: 200
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - spec/**/*_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cocoro.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cocoro (0.1.0)
5
+ faraday (~> 1.8)
6
+ faraday-cookie_jar (>= 0.0.7)
7
+ faraday_middleware (~> 1.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ diff-lcs (1.5.0)
14
+ domain_name (0.5.20190701)
15
+ unf (>= 0.0.5, < 1.0.0)
16
+ faraday (1.8.0)
17
+ faraday-em_http (~> 1.0)
18
+ faraday-em_synchrony (~> 1.0)
19
+ faraday-excon (~> 1.1)
20
+ faraday-httpclient (~> 1.0.1)
21
+ faraday-net_http (~> 1.0)
22
+ faraday-net_http_persistent (~> 1.1)
23
+ faraday-patron (~> 1.0)
24
+ faraday-rack (~> 1.0)
25
+ multipart-post (>= 1.2, < 3)
26
+ ruby2_keywords (>= 0.0.4)
27
+ faraday-cookie_jar (0.0.7)
28
+ faraday (>= 0.8.0)
29
+ http-cookie (~> 1.0.0)
30
+ faraday-em_http (1.0.0)
31
+ faraday-em_synchrony (1.0.0)
32
+ faraday-excon (1.1.0)
33
+ faraday-httpclient (1.0.1)
34
+ faraday-net_http (1.0.1)
35
+ faraday-net_http_persistent (1.2.0)
36
+ faraday-patron (1.0.0)
37
+ faraday-rack (1.0.0)
38
+ faraday_middleware (1.2.0)
39
+ faraday (~> 1.0)
40
+ http-cookie (1.0.4)
41
+ domain_name (~> 0.5)
42
+ multipart-post (2.1.1)
43
+ parallel (1.21.0)
44
+ parser (3.1.0.0)
45
+ ast (~> 2.4.1)
46
+ rainbow (3.0.0)
47
+ rake (13.0.6)
48
+ regexp_parser (2.2.0)
49
+ rexml (3.2.5)
50
+ rspec (3.10.0)
51
+ rspec-core (~> 3.10.0)
52
+ rspec-expectations (~> 3.10.0)
53
+ rspec-mocks (~> 3.10.0)
54
+ rspec-core (3.10.1)
55
+ rspec-support (~> 3.10.0)
56
+ rspec-expectations (3.10.1)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.10.0)
59
+ rspec-its (1.3.0)
60
+ rspec-core (>= 3.0.0)
61
+ rspec-expectations (>= 3.0.0)
62
+ rspec-mocks (3.10.2)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.10.0)
65
+ rspec-support (3.10.3)
66
+ rubocop (1.24.1)
67
+ parallel (~> 1.10)
68
+ parser (>= 3.0.0.0)
69
+ rainbow (>= 2.2.2, < 4.0)
70
+ regexp_parser (>= 1.8, < 3.0)
71
+ rexml
72
+ rubocop-ast (>= 1.15.1, < 2.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 1.4.0, < 3.0)
75
+ rubocop-ast (1.15.1)
76
+ parser (>= 3.0.1.1)
77
+ ruby-progressbar (1.11.0)
78
+ ruby2_keywords (0.0.5)
79
+ unf (0.1.4)
80
+ unf_ext
81
+ unf_ext (0.0.8)
82
+ unicode-display_width (2.1.0)
83
+
84
+ PLATFORMS
85
+ x86_64-darwin-20
86
+
87
+ DEPENDENCIES
88
+ cocoro!
89
+ rake (~> 13.0)
90
+ rspec (~> 3.0)
91
+ rspec-its (~> 1.3.0)
92
+ rubocop (~> 1.7)
93
+
94
+ BUNDLED WITH
95
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Tomasz Szczęśniak-Szlagowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Cocoro
2
+
3
+ Cocoro is a Ruby gem for controlling some of SHARP's air purifiers and humidifiers that you can normally control using Cocoro Air mobile apps.
4
+ The gem uses the same API that the mobile apps do, except it's totally unofficial, unsupported and not affiliated with SHARP in any way.
5
+ The moment they change the APIs the gem may break completely, so use it at your own risk.
6
+
7
+ I've only tested this on SHARP KI-LS70.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'cocoro'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install cocoro
24
+
25
+ ## Getting the API credentials
26
+
27
+ You do need to use the official Cocoro Air mobile app first, register your device etc.
28
+ Once you've done that, you need to find out what _app secret_ and _terminal app id key_ your Cocoro Air mobile app is using.
29
+ To do that, you'll have to look inside the HTTPS requests that it's making. You can use
30
+ [mitmproxy](https://mitmproxy.org/) (or any other dev proxy with SSL support, like [Charles Proxy](https://www.charlesproxy.com/)).
31
+ On Android there'll be another obstacle - you'll need to modify the apk to allow user added CA certificates.
32
+
33
+ My app secret is 43 alphanumeric characters followed by a `=` and my terminal app id key is 43 alphanumeric characters.
34
+
35
+ ## Usage
36
+
37
+ First use the credentials to log in, get a list of your devices and choose the one you want to control:
38
+
39
+ ```ruby
40
+ cocoro = Cocoro::Client.new(
41
+ app_secret: 'REPLACE_WITH_YOUR_CREDENTIALS',
42
+ terminal_app_id_key: 'REPLACE_WITH_YOUR_CREDENTIALS'
43
+ )
44
+ cocoro.login
45
+ device = cocoro.devices.first
46
+ ```
47
+
48
+ Now you can do various things like:
49
+ ```ruby
50
+ device.fetch_status!.air_volume # => 'medium'
51
+ device.set_air_volume!('quiet')
52
+ device.fetch_status!.air_volume # => 'quiet'
53
+ device.fetch_status!.to_h # => a hash of all the latest status information
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/spect88/cocoro.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Similar projects
71
+
72
+ The only similar project I'm aware of is the [python cocoro module](https://github.com/rcmdnk/cocoro).
73
+ At the time of writing this, the python module can't display any status information like current temperature/humidity.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "cocoro"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cocoro.gemspec ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cocoro/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cocoro"
7
+ spec.version = Cocoro::VERSION
8
+ spec.authors = ["Tomasz Szczęśniak-Szlagowski"]
9
+ spec.email = ["spect88@gmail.com"]
10
+
11
+ spec.summary = "An unofficial Cocoro Air client"
12
+ spec.description = "A client library talking to the same API that the Cocoro Air mobile apps " \
13
+ "do. Not affiliated with SHARP, this is a completely unofficial library " \
14
+ "which may break at any moment, so use it at your own risk."
15
+ spec.homepage = "https://github.com/spect88/cocoro"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 2.5.0"
18
+
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ spec.metadata["rubygems_mfa_required"] = "true"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/spect88/cocoro"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency "faraday", "~> 1.8"
36
+ spec.add_dependency "faraday-cookie_jar", ">= 0.0.7"
37
+ spec.add_dependency "faraday_middleware", "~> 1.2"
38
+
39
+ spec.add_development_dependency "rake", "~> 13.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "rspec-its", "~> 1.3.0"
42
+ spec.add_development_dependency "rubocop", "~> 1.7"
43
+ # For more information and examples about making a new gem, checkout our
44
+ # guide at: https://bundler.io/guides/creating_gem.html
45
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+ require "logger"
5
+ require "faraday"
6
+ require "faraday_middleware"
7
+ require "faraday-cookie_jar"
8
+
9
+ require_relative "device"
10
+ require_relative "error"
11
+
12
+ module Cocoro
13
+ # A client for the Cocoro Air API that the official mobile apps use.
14
+ class Client
15
+ BASE_URL = "https://hms.cloudlabs.sharp.co.jp/hems/pfApi/ta"
16
+ BASE_HEADERS = {
17
+ "Content-Type": "application/json; charset=utf-8",
18
+ Accept: "*/*",
19
+ "Accept-Language" => "ja-jp",
20
+ "User-Agent" => <<~UA.gsub(/[\r\n ]+/, " ")
21
+ smartlink_v200i Mozilla/5.0
22
+ (iPhone; CPU iPhone OS 14_4 like Mac OS X)
23
+ AppleWebKit/605.1.15 (KHTML, like Gecko)
24
+ Mobile/15E148
25
+ UA
26
+ }.freeze
27
+ DEFAULT_RESULT_CHECK_INTERVAL = 0.7 # seconds
28
+ DEFAULT_RESULT_CHECK_MAX_ATTEMPTS = 20
29
+
30
+ def initialize(app_secret:, terminal_app_id_key:, logger: nil)
31
+ @app_secret = app_secret
32
+ @terminal_app_id_key = terminal_app_id_key
33
+ @logger = logger || Logger.new($stdout).tap do |l|
34
+ l.level = Logger::WARN
35
+ end
36
+ @client = Faraday.new(
37
+ url: BASE_URL,
38
+ headers: BASE_HEADERS
39
+ ) do |f|
40
+ f.use :cookie_jar
41
+ f.request :json
42
+ f.response :logger, @logger, { bodies: true, log_level: :debug }
43
+ f.response :raise_error
44
+ f.response :json
45
+ f.adapter :net_http
46
+ end
47
+ end
48
+
49
+ def login
50
+ body = {
51
+ "terminalAppId" =>
52
+ "https://db.cloudlabs.sharp.co.jp/clpf/key/#{@terminal_app_id_key}"
53
+ }
54
+ @client.post(
55
+ "setting/login?appSecret=#{CGI.escape(@app_secret)}",
56
+ body,
57
+ {}
58
+ )
59
+ end
60
+
61
+ def devices
62
+ response = @client.get(
63
+ "setting/boxInfo?appSecret=#{CGI.escape(@app_secret)}&mode=other"
64
+ )
65
+ json = response.body
66
+ json["box"].flat_map do |box|
67
+ box["echonetData"].map do |data|
68
+ Cocoro::Device.new(
69
+ client: self,
70
+ box_id: box["boxId"],
71
+ device_id: data["deviceId"],
72
+ echonet_node: data["echonetNode"],
73
+ echonet_object: data["echonetObject"],
74
+ name: data["labelData"]["name"]
75
+ )
76
+ end
77
+ end
78
+ end
79
+
80
+ def device_status(device:)
81
+ query_params = {
82
+ appSecret: @app_secret,
83
+ boxId: device.box_id,
84
+ echonetNode: device.echonet_node,
85
+ echonetObject: device.echonet_object
86
+ }.map { |k, v| [k, CGI.escape(v)].join("=") }
87
+ response = @client.get(
88
+ "control/deviceStatus?#{query_params.join("&")}"
89
+ )
90
+ json = response.body
91
+ Cocoro::Status.new(json["deviceStatus"]["status"])
92
+ end
93
+
94
+ def control_device(
95
+ device:,
96
+ changes: [],
97
+ result_check_interval: DEFAULT_RESULT_CHECK_INTERVAL,
98
+ result_check_max_attempts: DEFAULT_RESULT_CHECK_MAX_ATTEMPTS
99
+ )
100
+ request_id = make_control_request!(device, changes)
101
+ finished = false
102
+ attempt = 0
103
+ until finished
104
+ attempt += 1
105
+ result = fetch_control_result!(device, request_id)
106
+ finished = result["status"] == "success" || result["status"] == "unmatch"
107
+ next if finished
108
+ raise Cocoro::Error, "Device not responding" if attempt >= result_check_max_attempts
109
+
110
+ sleep result_check_interval
111
+ end
112
+ end
113
+
114
+ protected
115
+
116
+ def make_control_request!(device, changes)
117
+ query_params = {
118
+ appSecret: @app_secret,
119
+ boxId: device.box_id
120
+ }.map { |k, v| [k, CGI.escape(v)].join("=") }
121
+ response = @client.post(
122
+ "control/deviceControl?#{query_params.join("&")}",
123
+ controlList: [
124
+ {
125
+ deviceId: device.device_id,
126
+ echonetNode: device.echonet_node,
127
+ echonetObject: device.echonet_object,
128
+ status: changes
129
+ }
130
+ ]
131
+ )
132
+ response.body["controlList"].first["id"]
133
+ end
134
+
135
+ def fetch_control_result!(device, request_id)
136
+ query_params = {
137
+ appSecret: @app_secret,
138
+ boxId: device.box_id
139
+ }.map { |k, v| [k, CGI.escape(v)].join("=") }
140
+ response = @client.post(
141
+ "control/controlResult?#{query_params.join("&")}",
142
+ resultList: [{ id: request_id }]
143
+ )
144
+ result = response.body["resultList"].first
145
+ @logger.info("Status: #{result["status"]}")
146
+ raise Cocoro::Error, "Error code: #{result["errorCode"]}" if result["status"] == "error"
147
+
148
+ result
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "status"
4
+ require_relative "payloads"
5
+
6
+ module Cocoro
7
+ Device = Struct.new(
8
+ :client,
9
+ :box_id,
10
+ :device_id,
11
+ :echonet_node,
12
+ :echonet_object,
13
+ :name,
14
+ keyword_init: true
15
+ ) do
16
+ def status
17
+ @status || fetch_status!
18
+ end
19
+
20
+ def fetch_status!
21
+ client.control_device(device: self)
22
+
23
+ @status = client.device_status(device: self)
24
+ end
25
+
26
+ # rubocop:disable Naming/AccessorMethodName
27
+ def set_power_on!(value)
28
+ send_payload!(:power_on, value)
29
+ end
30
+
31
+ def set_humidifier_on!(value)
32
+ send_payload!(:humidifier_on, value)
33
+ end
34
+
35
+ def set_air_volume!(value)
36
+ send_payload!(:air_volume, value)
37
+ end
38
+ # rubocop:enable Naming/AccessorMethodName
39
+
40
+ protected
41
+
42
+ def send_payload!(type, value)
43
+ valid_values = Cocoro::PAYLOADS[type].keys
44
+ unless valid_values.include?(value)
45
+ raise Cocoro::Error, "Invalid #{type} value: #{value}. Valid values: #{valid_values}"
46
+ end
47
+
48
+ payload = Cocoro::PAYLOADS[type][value]
49
+ client.control_device(device: self, changes: payload)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cocoro
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cocoro
4
+ PAYLOADS = {
5
+ power_on: {
6
+ false => [{
7
+ statusCode: "80",
8
+ valueType: "valueSingle",
9
+ valueSingle: {
10
+ code: "31"
11
+ }
12
+ }, {
13
+ statusCode: "f3",
14
+ valueType: "valueBinary",
15
+ valueBinary: {
16
+ code: "000300000000000000000000000000000000000000000000000000"
17
+ }
18
+ }],
19
+ true => [{
20
+ statusCode: "80",
21
+ valueType: "valueSingle",
22
+ valueSingle: {
23
+ code: "30"
24
+ }
25
+ }, {
26
+ statusCode: "f3",
27
+ valueType: "valueBinary",
28
+ valueBinary: {
29
+ code: "00030000000000000000000000FF00000000000000000000000000"
30
+ }
31
+ }]
32
+ },
33
+ humidifier_on: {
34
+ false => [{
35
+ statusCode: "f3",
36
+ valueType: "valueBinary",
37
+ valueBinary: {
38
+ code: "000900000000000000000000000000000000000000000000000000"
39
+ }
40
+ }],
41
+ true => [{
42
+ statusCode: "f3",
43
+ valueType: "valueBinary",
44
+ valueBinary: {
45
+ code: "000900000000000000000000000000FF0000000000000000000000"
46
+ }
47
+ }]
48
+ },
49
+ air_volume: {
50
+ "auto" => [{
51
+ statusCode: "f3",
52
+ valueType: "valueBinary",
53
+ valueBinary: {
54
+ code: "010100001000000000000000000000000000000000000000000000"
55
+ }
56
+ }],
57
+ "night" => [{
58
+ statusCode: "f3",
59
+ valueType: "valueBinary",
60
+ valueBinary: {
61
+ code: "010100001100000000000000000000000000000000000000000000"
62
+ }
63
+ }],
64
+ "pollen" => [{
65
+ statusCode: "f3",
66
+ valueType: "valueBinary",
67
+ valueBinary: {
68
+ code: "010100001300000000000000000000000000000000000000000000"
69
+ }
70
+ }],
71
+ "quiet" => [{
72
+ statusCode: "f3",
73
+ valueType: "valueBinary",
74
+ valueBinary: {
75
+ code: "010100001400000000000000000000000000000000000000000000"
76
+ }
77
+ }],
78
+ "medium" => [{
79
+ statusCode: "f3",
80
+ valueType: "valueBinary",
81
+ valueBinary: {
82
+ code: "010100001500000000000000000000000000000000000000000000"
83
+ }
84
+ }],
85
+ "strong" => [{
86
+ statusCode: "f3",
87
+ valueType: "valueBinary",
88
+ valueBinary: {
89
+ code: "010100001600000000000000000000000000000000000000000000"
90
+ }
91
+ }],
92
+ "omakase" => [{
93
+ statusCode: "f3",
94
+ valueType: "valueBinary",
95
+ valueBinary: {
96
+ code: "010000002000000000000000000000000000000000000000000000"
97
+ }
98
+ }],
99
+ "powerful" => [{
100
+ statusCode: "f3",
101
+ valueType: "valueBinary",
102
+ valueBinary: {
103
+ code: "010100004000000000000000000000000000000000000000000000"
104
+ }
105
+ }]
106
+ }
107
+ }.freeze
108
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cocoro
4
+ # Wrapper for device's status info format. Includes translation of the binary values.
5
+ class Status
6
+ LOCATIONS = {
7
+ # key: [property code, byte_offset, byte_length]
8
+ # Offset is 1-based because that's how logs API works too
9
+ temperature: ["F1", 4, 1], # °C
10
+ humidity: ["F1", 5, 1], # %
11
+ total_air_cleaned: ["F1", 22, 4], # m^3
12
+ pm25: ["F1", 28, 2], # ug/m^3 : sometimes weird values because of the first byte
13
+ f2unknown1: ["F2", 1, 1],
14
+ smell: ["F2", 15, 1], # 0/33/66/100 (100=dirty)
15
+ dust: ["F2", 16, 1], # 0/25/50/75/100 (100=dirty)
16
+ overall_cleanliness: ["F2", 18, 1], # 0/25/50/75/100 (100=dirty)
17
+ f2unknown19: ["F2", 19, 1],
18
+ enough_water: ["F2", 20, 1],
19
+ light_detected: ["F2", 21, 1],
20
+ air_volume: ["F3", 5, 1],
21
+ power_on: ["F3", 14, 1],
22
+ humidifier_on: ["F3", 16, 1],
23
+ f3unknown20: ["F3", 20, 1]
24
+ }.freeze
25
+ VALUES = {
26
+ enough_water: { 0 => false, 255 => true },
27
+ light_detected: { 0 => false, 255 => true },
28
+ air_volume: {
29
+ 16 => "auto",
30
+ 17 => "night",
31
+ 19 => "pollen",
32
+ 20 => "quiet",
33
+ 21 => "medium",
34
+ 22 => "strong",
35
+ 32 => "omakase",
36
+ 64 => "powerful"
37
+ },
38
+ power_on: { 0 => false, 255 => true },
39
+ humidifier_on: { 0 => false, 255 => true }
40
+ }.freeze
41
+
42
+ BOOLEAN_FIELDS = %i[enough_water light_detected power_on humidifier_on].freeze
43
+ NON_BOOLEAN_FIELDS = %i[
44
+ temperature humidity total_air_cleaned pm25 smell dust overall_cleanliness air_volume
45
+ ].freeze
46
+
47
+ def initialize(array_of_status_data)
48
+ @array = array_of_status_data
49
+ end
50
+
51
+ BOOLEAN_FIELDS.each do |field|
52
+ define_method(:"#{field}?") { read_value(field) }
53
+ end
54
+
55
+ NON_BOOLEAN_FIELDS.each do |field|
56
+ define_method(field) { read_value(field) }
57
+ end
58
+
59
+ # Get other data that we're not sure what it means, but could be useful for debugging
60
+ def unknowns
61
+ LOCATIONS
62
+ .keys
63
+ .select { |key| key.to_s =~ /unknown/ }
64
+ .map { |key| [key, read_value(key)] }
65
+ .to_h
66
+ .merge(
67
+ f1: read_binary_status("F1"),
68
+ f2: read_binary_status("F2"),
69
+ f3: read_binary_status("F3")
70
+ )
71
+ end
72
+
73
+ def to_h
74
+ (BOOLEAN_FIELDS + NON_BOOLEAN_FIELDS)
75
+ .map { |f| [f, read_value(f)] }
76
+ .to_h
77
+ .merge(unknowns)
78
+ end
79
+
80
+ protected
81
+
82
+ def read_value(name)
83
+ code, byte_offset, byte_length = LOCATIONS[name]
84
+ hex_code = read_binary_status(code)
85
+ return nil if hex_code.nil?
86
+
87
+ number = hex_code[(byte_offset - 1) * 2, byte_length * 2].to_i(16)
88
+ if VALUES[name]&.key?(number)
89
+ VALUES[name][number]
90
+ else
91
+ number
92
+ end
93
+ end
94
+
95
+ def read_binary_status(code)
96
+ @array
97
+ .find { |s| s["statusCode"] == code }
98
+ &.dig("valueBinary", "code")
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cocoro
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cocoro.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cocoro/version"
4
+ require_relative "cocoro/client"
5
+
6
+ # Wrapping module. Check out Cocoro::Client for the main part.
7
+ module Cocoro
8
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocoro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Szczęśniak-Szlagowski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-cookie_jar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.7'
111
+ description: A client library talking to the same API that the Cocoro Air mobile apps
112
+ do. Not affiliated with SHARP, this is a completely unofficial library which may
113
+ break at any moment, so use it at your own risk.
114
+ email:
115
+ - spect88@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".github/workflows/main.yml"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".rubocop.yml"
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE.txt
127
+ - README.md
128
+ - Rakefile
129
+ - bin/console
130
+ - bin/setup
131
+ - cocoro.gemspec
132
+ - lib/cocoro.rb
133
+ - lib/cocoro/client.rb
134
+ - lib/cocoro/device.rb
135
+ - lib/cocoro/error.rb
136
+ - lib/cocoro/payloads.rb
137
+ - lib/cocoro/status.rb
138
+ - lib/cocoro/version.rb
139
+ homepage: https://github.com/spect88/cocoro
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ allowed_push_host: https://rubygems.org
144
+ rubygems_mfa_required: 'true'
145
+ homepage_uri: https://github.com/spect88/cocoro
146
+ source_code_uri: https://github.com/spect88/cocoro
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: 2.5.0
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 3.2.15
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: An unofficial Cocoro Air client
166
+ test_files: []