cassia-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +50 -0
  3. data/.env.sample +3 -0
  4. data/.gitignore +12 -0
  5. data/.ruby-version +1 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +79 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +377 -0
  11. data/Rakefile +2 -0
  12. data/bin/console +17 -0
  13. data/bin/setup +8 -0
  14. data/cassia-ruby.gemspec +38 -0
  15. data/lib/cassia/access_controller.rb +111 -0
  16. data/lib/cassia/api.rb +39 -0
  17. data/lib/cassia/characteristic.rb +14 -0
  18. data/lib/cassia/configuration.rb +9 -0
  19. data/lib/cassia/default_logger.rb +11 -0
  20. data/lib/cassia/device.rb +14 -0
  21. data/lib/cassia/requests/close_ap_state.rb +31 -0
  22. data/lib/cassia/requests/close_connection_state.rb +38 -0
  23. data/lib/cassia/requests/close_notify.rb +38 -0
  24. data/lib/cassia/requests/close_scan.rb +38 -0
  25. data/lib/cassia/requests/combined_sse.rb +27 -0
  26. data/lib/cassia/requests/connect_device.rb +38 -0
  27. data/lib/cassia/requests/connect_local.rb +44 -0
  28. data/lib/cassia/requests/disconnect_device.rb +36 -0
  29. data/lib/cassia/requests/disconnect_local.rb +33 -0
  30. data/lib/cassia/requests/discover_all_char.rb +33 -0
  31. data/lib/cassia/requests/discover_all_services.rb +33 -0
  32. data/lib/cassia/requests/discover_all_services_and_chars.rb +33 -0
  33. data/lib/cassia/requests/discover_char_of_service.rb +34 -0
  34. data/lib/cassia/requests/discover_descriptor_of_char.rb +34 -0
  35. data/lib/cassia/requests/get_all_routers_status.rb +33 -0
  36. data/lib/cassia/requests/get_connected_devices_router.rb +34 -0
  37. data/lib/cassia/requests/get_token.rb +34 -0
  38. data/lib/cassia/requests/open_ap_state.rb +31 -0
  39. data/lib/cassia/requests/open_connection_state.rb +38 -0
  40. data/lib/cassia/requests/open_notify.rb +38 -0
  41. data/lib/cassia/requests/open_scan.rb +50 -0
  42. data/lib/cassia/requests/switch_autoselect.rb +37 -0
  43. data/lib/cassia/requests/write_char_by_handle.rb +35 -0
  44. data/lib/cassia/response_handlers/close_ap_state.rb +31 -0
  45. data/lib/cassia/response_handlers/close_connection_state.rb +32 -0
  46. data/lib/cassia/response_handlers/close_notify.rb +32 -0
  47. data/lib/cassia/response_handlers/close_scan.rb +32 -0
  48. data/lib/cassia/response_handlers/connect_device.rb +29 -0
  49. data/lib/cassia/response_handlers/connect_local.rb +32 -0
  50. data/lib/cassia/response_handlers/disconnect_device.rb +28 -0
  51. data/lib/cassia/response_handlers/disconnect_local.rb +31 -0
  52. data/lib/cassia/response_handlers/discover_all_char.rb +33 -0
  53. data/lib/cassia/response_handlers/discover_all_services.rb +33 -0
  54. data/lib/cassia/response_handlers/discover_all_services_and_chars.rb +41 -0
  55. data/lib/cassia/response_handlers/discover_char_of_service.rb +37 -0
  56. data/lib/cassia/response_handlers/discover_descriptor_of_char.rb +23 -0
  57. data/lib/cassia/response_handlers/get_all_routers_status.rb +32 -0
  58. data/lib/cassia/response_handlers/get_connected_devices_router.rb +33 -0
  59. data/lib/cassia/response_handlers/get_token.rb +29 -0
  60. data/lib/cassia/response_handlers/open_ap_state.rb +31 -0
  61. data/lib/cassia/response_handlers/open_connection_state.rb +32 -0
  62. data/lib/cassia/response_handlers/open_notify.rb +32 -0
  63. data/lib/cassia/response_handlers/open_scan.rb +32 -0
  64. data/lib/cassia/response_handlers/switch_autoselect.rb +29 -0
  65. data/lib/cassia/response_handlers/write_char_by_handle.rb +28 -0
  66. data/lib/cassia/router.rb +75 -0
  67. data/lib/cassia/ruby/version.rb +5 -0
  68. data/lib/cassia/service.rb +13 -0
  69. data/lib/cassia.rb +82 -0
  70. metadata +252 -0
@@ -0,0 +1,34 @@
1
+ module Cassia
2
+ module Requests
3
+ class DiscoverCharOfService
4
+ def initialize(access_controller, router: , device_mac: , service_uuid: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ @service_uuid = service_uuid
9
+ end
10
+
11
+ def path
12
+ "/api/gatt/nodes/#{@device_mac}/services/#{@service_uuid}/characteristics?mac=#{@router.mac}"
13
+ end
14
+
15
+ def headers
16
+ {
17
+ 'Authorization' => "Bearer #{access_token}",
18
+ 'Content-Type' => "application/json"
19
+ }
20
+ end
21
+
22
+ def perform
23
+ Cassia::ResponseHandlers::DiscoverCharOfService.new(@access_controller, router: @router, device_mac: @device_mac, service_uuid: @service_uuid).handle(Cassia.api.get(self))
24
+ end
25
+
26
+ private
27
+
28
+ def access_token
29
+ @access_controller.get_token if @access_controller.access_token.nil?
30
+ @access_controller.access_token
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module Cassia
2
+ module Requests
3
+ class DiscoverDescriptorOfChar
4
+ def initialize(access_controller, router: , device_mac: , char_uuid: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ @char_uuid = char_uuid
9
+ end
10
+
11
+ def path
12
+ "/api/gatt/nodes/#{@device_mac}/characteristics/#{@char_uuid}/descriptors?mac=#{@router.mac}"
13
+ end
14
+
15
+ def headers
16
+ {
17
+ 'Authorization' => "Bearer #{access_token}",
18
+ 'Content-Type' => "application/json"
19
+ }
20
+ end
21
+
22
+ def perform
23
+ Cassia::ResponseHandlers::DiscoverDescriptorOfChar.new(@access_controller, router: @router, device_mac: @device_mac, char_uuid: @char_uuid).handle(Cassia.api.get(self))
24
+ end
25
+
26
+ private
27
+
28
+ def access_token
29
+ @access_controller.get_token if @access_controller.access_token.nil?
30
+ @access_controller.access_token
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ module Cassia
2
+ module Requests
3
+ class GetAllRoutersStatus
4
+ attr_reader :access_token
5
+
6
+ def initialize(access_controller)
7
+ @access_controller = access_controller
8
+ end
9
+
10
+ def path
11
+ '/api/cassia/hubs'
12
+ end
13
+
14
+ def headers
15
+ {
16
+ 'Authorization' => "Bearer #{access_token}",
17
+ 'Content-Type' => "application/json"
18
+ }
19
+ end
20
+
21
+ def perform
22
+ Cassia::ResponseHandlers::GetAllRoutersStatus.new(@access_controller).handle(Cassia.api.get(self))
23
+ end
24
+
25
+ private
26
+
27
+ def access_token
28
+ @access_controller.get_token if @access_controller.access_token.nil?
29
+ @access_controller.access_token
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ module Cassia
2
+ module Requests
3
+ class GetConnectedDevicesRouter
4
+ attr_reader :access_token
5
+
6
+ def initialize(access_controller, router: )
7
+ @access_controller = access_controller
8
+ @router = router
9
+ end
10
+
11
+ def path
12
+ "/api/gap/nodes?connection_state=connected&mac=#{@router.mac}"
13
+ end
14
+
15
+ def headers
16
+ {
17
+ 'Authorization' => "Bearer #{access_token}",
18
+ 'Content-Type' => "application/json"
19
+ }
20
+ end
21
+
22
+ def perform
23
+ Cassia::ResponseHandlers::GetConnectedDevicesRouter.new(@access_controller, router: @router).handle(Cassia.api.get(self))
24
+ end
25
+
26
+ private
27
+
28
+ def access_token
29
+ @access_controller.get_token if @access_controller.access_token.nil?
30
+ @access_controller.access_token
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module Cassia
2
+ module Requests
3
+ class GetToken
4
+ def initialize(access_controller)
5
+ @access_controller = access_controller
6
+ end
7
+
8
+ def path
9
+ '/api/oauth2/token'
10
+ end
11
+
12
+ def body
13
+ { 'grant_type' => "client_credentials" }.to_json
14
+ end
15
+
16
+ def headers
17
+ {
18
+ 'Authorization' => "Basic #{get_encode}",
19
+ 'Content-Type' => "application/json"
20
+ }
21
+ end
22
+
23
+ def perform
24
+ Cassia::ResponseHandlers::GetToken.new(@access_controller).handle(Cassia.api.post(self))
25
+ end
26
+
27
+ private
28
+
29
+ def get_encode
30
+ Base64.encode64("#{Cassia.configuration.client_id}:#{Cassia.configuration.secret}").strip
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ module Cassia
2
+ module Requests
3
+ class OpenApState
4
+ def initialize(access_controller)
5
+ @access_controller = access_controller
6
+ end
7
+
8
+ def path
9
+ '/api/aps/ap-state/open'
10
+ end
11
+
12
+ def headers
13
+ {
14
+ 'Authorization' => "Bearer #{access_token}",
15
+ 'Content-Type' => "application/json"
16
+ }
17
+ end
18
+
19
+ def perform
20
+ Cassia::ResponseHandlers::OpenApState.new(@access_controller).handle(Cassia.api.get(self))
21
+ end
22
+
23
+ private
24
+
25
+ def access_token
26
+ @access_controller.get_token if @access_controller.access_token.nil?
27
+ @access_controller.access_token
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ module Cassia
2
+ module Requests
3
+ class OpenConnectionState
4
+ def initialize(access_controller, aps: )
5
+ @access_controller = access_controller
6
+ @aps = aps
7
+ end
8
+
9
+ def path
10
+ '/api/aps/connection-state/open'
11
+ end
12
+
13
+ def body
14
+ {
15
+ 'aps' => @aps
16
+ }.to_json
17
+ end
18
+
19
+ def headers
20
+ {
21
+ 'Authorization' => "Bearer #{access_token}",
22
+ 'Content-Type' => "application/json"
23
+ }
24
+ end
25
+
26
+ def perform
27
+ Cassia::ResponseHandlers::OpenConnectionState.new(@access_controller, aps: @aps).handle(Cassia.api.post(self))
28
+ end
29
+
30
+ private
31
+
32
+ def access_token
33
+ @access_controller.get_token if @access_controller.access_token.nil?
34
+ @access_controller.access_token
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ module Cassia
2
+ module Requests
3
+ class OpenNotify
4
+ def initialize(access_controller, aps: )
5
+ @access_controller = access_controller
6
+ @aps = aps
7
+ end
8
+
9
+ def path
10
+ '/api/aps/notify/open'
11
+ end
12
+
13
+ def body
14
+ {
15
+ 'aps' => @aps
16
+ }.to_json
17
+ end
18
+
19
+ def headers
20
+ {
21
+ 'Authorization' => "Bearer #{access_token}",
22
+ 'Content-Type' => "application/json"
23
+ }
24
+ end
25
+
26
+ def perform
27
+ Cassia::ResponseHandlers::OpenNotify.new(@access_controller, aps: @aps).handle(Cassia.api.post(self))
28
+ end
29
+
30
+ private
31
+
32
+ def access_token
33
+ @access_controller.get_token if @access_controller.access_token.nil?
34
+ @access_controller.access_token
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ module Cassia
2
+ module Requests
3
+ class OpenScan
4
+ def initialize(access_controller, aps: , chip: nil, active: nil, filter_name: nil, filter_mac: nil,
5
+ filter_uuid: nil)
6
+ @access_controller = access_controller
7
+ @aps = aps
8
+ @chip = chip
9
+ @active = active
10
+ @filter_name = filter_name
11
+ @filter_mac = filter_mac
12
+ @filter_uuid = filter_uuid
13
+ end
14
+
15
+ def path
16
+ '/api/aps/scan/open'
17
+ end
18
+
19
+ def body
20
+ body = {
21
+ 'aps' => @aps
22
+ }
23
+ body['chip'] = @chip unless @chip.nil?
24
+ body['active'] = @active unless @active.nil?
25
+ body['filter_name'] = @filter_name unless @filter_name.nil?
26
+ body['filter_mac'] = @filter_mac unless @filter_mac.nil?
27
+ body['filter_uuid'] = @filter_uuid unless @filter_uuid.nil?
28
+ body.to_json
29
+ end
30
+
31
+ def headers
32
+ {
33
+ 'Authorization' => "Bearer #{access_token}",
34
+ 'Content-Type' => "application/json"
35
+ }
36
+ end
37
+
38
+ def perform
39
+ Cassia::ResponseHandlers::OpenScan.new(@access_controller, aps: @aps).handle(Cassia.api.post(self))
40
+ end
41
+
42
+ private
43
+
44
+ def access_token
45
+ @access_controller.get_token if @access_controller.access_token.nil?
46
+ @access_controller.access_token
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ module Cassia
2
+ module Requests
3
+ class SwitchAutoselect
4
+
5
+ def initialize(access_controller, flag: 1)
6
+ @access_controller = access_controller
7
+ @flag = flag
8
+ end
9
+
10
+ def path
11
+ '/api/aps/ap-select-switch'
12
+ end
13
+
14
+ def body
15
+ { 'flag' => @flag }.to_json
16
+ end
17
+
18
+ def headers
19
+ {
20
+ 'Authorization' => "Bearer #{access_token}",
21
+ 'Content-Type' => "application/json"
22
+ }
23
+ end
24
+
25
+ def perform
26
+ Cassia::ResponseHandlers::SwitchAutoselect.new(@access_controller).handle(Cassia.api.post(self))
27
+ end
28
+
29
+ private
30
+
31
+ def access_token
32
+ @access_controller.get_token if @access_controller.access_token.nil?
33
+ @access_controller.access_token
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ module Cassia
2
+ module Requests
3
+ class WriteCharByHandle
4
+ def initialize(access_controller, router: , device_mac: , handle: , value: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ @handle = handle
9
+ @value = value
10
+ end
11
+
12
+ def path
13
+ "/api/gatt/nodes/#{@device_mac}/handle/#{@handle}/value/#{@value}?mac=#{@router.mac}"
14
+ end
15
+
16
+ def headers
17
+ {
18
+ 'Authorization' => "Bearer #{access_token}",
19
+ 'Content-Type' => "application/json"
20
+ }
21
+ end
22
+
23
+ def perform
24
+ Cassia::ResponseHandlers::WriteCharByHandle.new(@access_controller, router: @router, device_mac: @device_mac, handle: @handle, value: @value).handle(Cassia.api.get(self))
25
+ end
26
+
27
+ private
28
+
29
+ def access_token
30
+ @access_controller.get_token if @access_controller.access_token.nil?
31
+ @access_controller.access_token
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class CloseApState
4
+ def initialize(access_controller)
5
+ @access_controller = access_controller
6
+ end
7
+
8
+ def handle(response)
9
+ if response.success?
10
+ handle_success
11
+ else
12
+ handle_failure(response)
13
+ end
14
+ response.success?
15
+ end
16
+
17
+ private
18
+
19
+ def handle_success
20
+ @access_controller.routers.each do |router|
21
+ router.ap_state_monitor_on = false
22
+ end
23
+ end
24
+
25
+ def handle_failure(response)
26
+ @access_controller.error = JSON.parse(response.body)['error']
27
+ @access_controller.error_description = JSON.parse(response.body)['error_description']
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class CloseConnectionState
4
+ def initialize(access_controller, aps: )
5
+ @access_controller = access_controller
6
+ @aps = aps
7
+ end
8
+
9
+ def handle(response)
10
+ if response.success?
11
+ handle_success
12
+ else
13
+ handle_failure(response)
14
+ end
15
+ response.success?
16
+ end
17
+
18
+ private
19
+
20
+ def handle_success
21
+ routers_to_close_connection = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_close_connection.each do |router|
23
+ router.connection_state_monitor_on = false
24
+ end
25
+ end
26
+
27
+ def handle_failure(response)
28
+ @access_controller.error = JSON.parse(response.body)['error']
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class CloseNotify
4
+ def initialize(access_controller, aps: )
5
+ @access_controller = access_controller
6
+ @aps = aps
7
+ end
8
+
9
+ def handle(response)
10
+ if response.success?
11
+ handle_success
12
+ else
13
+ handle_failure(response)
14
+ end
15
+ response.success?
16
+ end
17
+
18
+ private
19
+
20
+ def handle_success
21
+ routers_to_close_notify = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_close_notify.each do |router|
23
+ router.notification_open = false
24
+ end
25
+ end
26
+
27
+ def handle_failure(response)
28
+ @access_controller.error = JSON.parse(response.body)['error']
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class CloseScan
4
+ def initialize(access_controller, aps: )
5
+ @access_controller = access_controller
6
+ @aps = aps
7
+ end
8
+
9
+ def handle(response)
10
+ if response.success?
11
+ handle_success
12
+ else
13
+ handle_failure(response)
14
+ end
15
+ response.success?
16
+ end
17
+
18
+ private
19
+
20
+ def handle_success
21
+ routers_to_close_scan = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_close_scan.each do |router|
23
+ router.scanning_on = false
24
+ end
25
+ end
26
+
27
+ def handle_failure(response)
28
+ @access_controller.error = JSON.parse(response.body)['error']
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class ConnectDevice
4
+ def initialize(access_controller)
5
+ @access_controller = access_controller
6
+ end
7
+
8
+ def handle(response)
9
+ if response.success?
10
+ handle_success(response)
11
+ else
12
+ handle_failure(response)
13
+ end
14
+ response.success?
15
+ end
16
+
17
+ private
18
+
19
+ def handle_success(response)
20
+ new_device = Cassia::Device.new(mac: response.body['device'])
21
+ @access_controller.connected_devices << new_device
22
+ end
23
+
24
+ def handle_failure(response)
25
+ @access_controller.error = response.body['error']
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class ConnectLocal
4
+ def initialize(access_controller, router: , device_mac: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ end
9
+
10
+ def handle(response)
11
+ if response.success?
12
+ handle_success(response)
13
+ else
14
+ handle_failure(response)
15
+ end
16
+ response.success?
17
+ end
18
+
19
+ private
20
+
21
+ def handle_success(response)
22
+ new_device = Cassia::Device.new(mac: @device_mac)
23
+ @router.connected_devices << new_device
24
+ @access_controller.connected_devices << new_device
25
+ end
26
+
27
+ def handle_failure(response)
28
+ @access_controller.error = response.body
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class DisconnectDevice
4
+ def initialize(access_controller)
5
+ @access_controller = access_controller
6
+ end
7
+
8
+ def handle(response)
9
+ if response.success?
10
+ handle_success(response)
11
+ else
12
+ handle_failure(response)
13
+ end
14
+ response.success?
15
+ end
16
+
17
+ private
18
+
19
+ def handle_success(response)
20
+ @access_controller.connected_devices.select! {|device| device.mac != response.body['device']}
21
+ end
22
+
23
+ def handle_failure(response)
24
+ @access_controller.error = response.body['error']
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class DisconnectLocal
4
+ def initialize(access_controller, router: , device_mac: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ end
9
+
10
+ def handle(response)
11
+ if response.success?
12
+ handle_success
13
+ else
14
+ handle_failure(response)
15
+ end
16
+ response.success?
17
+ end
18
+
19
+ private
20
+
21
+ def handle_success
22
+ @access_controller.connected_devices.select! {|device| device.mac != @device_mac}
23
+ @router.connected_devices.select! {|device| device.mac != @device_mac}
24
+ end
25
+
26
+ def handle_failure(response)
27
+ @access_controller.error = response.body
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class DiscoverAllChar
4
+ def initialize(access_controller, router: , device_mac: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ @device_mac = device_mac
8
+ end
9
+
10
+ def handle(response)
11
+ if response.success?
12
+ handle_success(response)
13
+ else
14
+ handle_failure(response)
15
+ end
16
+ response.success?
17
+ end
18
+
19
+ private
20
+
21
+ def handle_success(response)
22
+ device = @router.connected_devices.detect {|device| device.mac == @device_mac}
23
+ response.body.each do |char|
24
+ device.characteristics << Characteristic.new(char)
25
+ end
26
+ end
27
+
28
+ def handle_failure(response)
29
+ @access_controller.error = response.body
30
+ end
31
+ end
32
+ end
33
+ end