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,33 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class DiscoverAllServices
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 |service|
24
+ device.services << Service.new(service)
25
+ end
26
+ end
27
+
28
+ def handle_failure(response)
29
+ @access_controller.error = response.body
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class DiscoverAllServicesAndChars
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 |service|
24
+ new_service = Service.new(uuid: service["uuid"], primary: service["primary"], handle: service["handle"])
25
+ if service["characteristics"]
26
+ service["characteristics"].each do |char|
27
+ new_char = Characteristic.new(char)
28
+ new_service.characteristics << new_char
29
+ device.characteristics << new_char
30
+ end
31
+ end
32
+ device.services << new_service
33
+ end
34
+ end
35
+
36
+ def handle_failure(response)
37
+ @access_controller.error = response.body
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ module Cassia
2
+ module ResponseHandlers
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 handle(response)
12
+ if response.success?
13
+ handle_success(response)
14
+ else
15
+ handle_failure(response)
16
+ end
17
+ response.success?
18
+ end
19
+
20
+ private
21
+
22
+ def handle_success(response)
23
+ device = @router.connected_devices.detect {|device| device.mac == @device_mac}
24
+ service = device.services.detect {|service| service.uuid == @service_uuid}
25
+ response.body.each do |char|
26
+ new_char = Characteristic.new(char)
27
+ service.characteristics << new_char
28
+ device.characteristics << new_char
29
+ end
30
+ end
31
+
32
+ def handle_failure(response)
33
+ @access_controller.error = response.body
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module Cassia
2
+ module ResponseHandlers
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 handle(response)
12
+ device = @router.connected_devices.detect {|device| device.mac == @device_mac}
13
+ char = device.characteristics.detect {|char| char.uuid == @char_uuid}
14
+ if char
15
+ char.descriptors = response.body
16
+ else
17
+ @access_controller.error = "Desciptors Empty. Characteristic Not Found."
18
+ end
19
+ !(response.body.empty?)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class GetAllRoutersStatus
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
+ response.body.each do |router|
21
+ new_router = Router.new(router)
22
+ @access_controller.routers << new_router
23
+ end
24
+ end
25
+
26
+ def handle_failure(response)
27
+ @access_controller.error = JSON.parse(response.body)["error"]
28
+ @access_controller.error_description = JSON.parse(response.body)["error_description"]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class GetConnectedDevicesRouter
4
+ def initialize(access_controller, router: )
5
+ @access_controller = access_controller
6
+ @router = router
7
+ end
8
+
9
+ def handle(response)
10
+ if response.success?
11
+ handle_success(response)
12
+ else
13
+ handle_failure(response)
14
+ end
15
+ response.success?
16
+ end
17
+
18
+ private
19
+
20
+ def handle_success(response)
21
+ @router.connected_devices = response.body["nodes"].map do |device|
22
+ new_device = Device.new(device)
23
+ new_device.mac = device.delete("id")
24
+ new_device
25
+ end
26
+ end
27
+
28
+ def handle_failure(response)
29
+ @access_controller.error = response.body
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class GetToken
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.access_token = response.body["access_token"]
21
+ end
22
+
23
+ def handle_failure(response)
24
+ @access_controller.error = response.body["error"]
25
+ @access_controller.error_description = response.body["error_description"]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module Cassia
2
+ module ResponseHandlers
3
+ class OpenApState
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 = true
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 OpenConnectionState
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_open_connection = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_open_connection.each do |router|
23
+ router.connection_state_monitor_on = true
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 OpenNotify
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_open_notify = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_open_notify.each do |router|
23
+ router.notification_open = true
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 OpenScan
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_open_scan = @access_controller.routers.select {|router| @aps.include?(router.mac) }
22
+ routers_to_open_scan.each do |router|
23
+ router.scanning_on = true
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 SwitchAutoselect
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.autoselect_switch = response.body['flag']
21
+ end
22
+
23
+ def handle_failure(response)
24
+ @access_controller.error = JSON.parse(response.body)['error']
25
+ @access_controller.error_description = JSON.parse(response.body)['error_description']
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ module Cassia
2
+ module ResponseHandlers
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 handle(response)
13
+ device = @router.connected_devices.detect {|device| device.mac == @device_mac}
14
+ char = device.characteristics.detect {|char| char.handle == @handle}
15
+ if char
16
+ if @value == "0100"
17
+ char.notification_on = true
18
+ elsif @value == "0000"
19
+ char.notification_on = false
20
+ end
21
+ else
22
+ @access_controller.error = "Characteristic With Given Handle Not Found"
23
+ end
24
+ !(char.nil?)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,75 @@
1
+ module Cassia
2
+ class Router
3
+ include Virtus.model
4
+ attribute :_id, String
5
+ attribute :id, String
6
+ attribute :mac, String
7
+ attribute :name, String
8
+ attribute :group, String
9
+ attribute :status, String
10
+ attribute :model, String
11
+ attribute :version, String
12
+ attribute :position, String
13
+ attribute :time, Integer
14
+ attribute :ip, String
15
+ attribute :localip, String
16
+ attribute :uptime, Integer
17
+ attribute :offline_time, Integer
18
+ attribute :online_time, Integer
19
+ attribute :update_status, String
20
+ attribute :update_reason, String
21
+ attribute :update_version, String
22
+ attribute :update_progress, Integer
23
+ attribute :container, Hash
24
+ attribute :ap, Hash
25
+ attribute :connected_devices, Array[Cassia::Device], default: []
26
+ attribute :notification_open, Boolean
27
+ attribute :scanning_on, Boolean
28
+ attribute :connection_state_monitor_on, Boolean
29
+ attribute :ap_state_monitor_on, Boolean
30
+
31
+ def connect_local(access_controller, device_mac: , type: )
32
+ Cassia::Requests::ConnectLocal.new(access_controller, router: self, device_mac: device_mac, type: type).perform
33
+ end
34
+
35
+ def disconnect_local(access_controller, device_mac: )
36
+ Cassia::Requests::DisconnectLocal.new(access_controller, router: self, device_mac: device_mac).perform
37
+ end
38
+
39
+ def get_connected_devices(access_controller)
40
+ Cassia::Requests::GetConnectedDevicesRouter.new(access_controller, router: self).perform
41
+ end
42
+
43
+ def discover_all_services(access_controller, device_mac: )
44
+ Cassia::Requests::DiscoverAllServices.new(access_controller, router: self, device_mac: device_mac).perform
45
+ end
46
+
47
+ def discover_all_char(access_controller, device_mac: )
48
+ Cassia::Requests::DiscoverAllChar.new(access_controller, router: self, device_mac: device_mac).perform
49
+ end
50
+
51
+ def discover_char_of_service(access_controller, device_mac: , service_uuid: )
52
+ Cassia::Requests::DiscoverCharOfService.new(access_controller, router: self, device_mac: device_mac, service_uuid: service_uuid).perform
53
+ end
54
+
55
+ def discover_descriptor_of_char(access_controller, device_mac: , char_uuid: )
56
+ Cassia::Requests::DiscoverDescriptorOfChar.new(access_controller, router: self, device_mac: device_mac, char_uuid: char_uuid).perform
57
+ end
58
+
59
+ def discover_all_services_and_chars(access_controller, device_mac: )
60
+ Cassia::Requests::DiscoverAllServicesAndChars.new(access_controller, router: self, device_mac: device_mac).perform
61
+ end
62
+
63
+ def write_char_by_handle(access_controller, device_mac:, handle: , value: )
64
+ Cassia::Requests::WriteCharByHandle.new(access_controller, router: self, device_mac: device_mac, handle: handle, value: value).perform
65
+ end
66
+
67
+ def open_char_notification(access_controller, device_mac: , handle: )
68
+ Cassia::Requests::WriteCharByHandle.new(access_controller, router: self, device_mac: device_mac, handle: handle, value: "0100").perform
69
+ end
70
+
71
+ def close_char_notification(access_controller, device_mac: , handle: )
72
+ Cassia::Requests::WriteCharByHandle.new(access_controller, router: self, device_mac: device_mac, handle: handle, value: "0000").perform
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module Cassia
2
+ module Ruby
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Cassia
2
+ class Service
3
+ include Virtus.model
4
+ attribute :uuid, String
5
+ attribute :primary, Boolean
6
+ attribute :handle, Integer
7
+ attribute :characteristics, Array[Cassia::Characteristic]
8
+
9
+ def ==(other)
10
+ return self.uuid == other.uuid && self.primary == other.primary && self.handle == other.handle && self.characteristics == other.characteristics
11
+ end
12
+ end
13
+ end
data/lib/cassia.rb ADDED
@@ -0,0 +1,82 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "virtus"
4
+ require "base64"
5
+ require "json"
6
+ require "ld-eventsource"
7
+
8
+ require "cassia/api"
9
+ require "cassia/configuration"
10
+ require "cassia/default_logger"
11
+ require "cassia/characteristic"
12
+ require "cassia/service"
13
+ require "cassia/device"
14
+ require "cassia/router"
15
+ require "cassia/access_controller"
16
+ require "cassia/response_handlers/get_token"
17
+ require "cassia/response_handlers/get_all_routers_status"
18
+ require "cassia/response_handlers/switch_autoselect"
19
+ require "cassia/response_handlers/open_scan"
20
+ require "cassia/response_handlers/close_scan"
21
+ require "cassia/response_handlers/connect_device"
22
+ require "cassia/response_handlers/disconnect_device"
23
+ require "cassia/response_handlers/connect_local"
24
+ require "cassia/response_handlers/disconnect_local"
25
+ require "cassia/response_handlers/get_connected_devices_router"
26
+ require "cassia/response_handlers/open_notify"
27
+ require "cassia/response_handlers/close_notify"
28
+ require "cassia/response_handlers/open_connection_state"
29
+ require "cassia/response_handlers/close_connection_state"
30
+ require "cassia/response_handlers/open_ap_state"
31
+ require "cassia/response_handlers/close_ap_state"
32
+ require "cassia/response_handlers/discover_all_services"
33
+ require "cassia/response_handlers/discover_all_char"
34
+ require "cassia/response_handlers/discover_char_of_service"
35
+ require "cassia/response_handlers/discover_descriptor_of_char"
36
+ require "cassia/response_handlers/discover_all_services_and_chars"
37
+ require "cassia/response_handlers/write_char_by_handle"
38
+ require "cassia/requests/get_token"
39
+ require "cassia/requests/get_all_routers_status"
40
+ require "cassia/requests/switch_autoselect"
41
+ require "cassia/requests/open_scan"
42
+ require "cassia/requests/close_scan"
43
+ require "cassia/requests/connect_device"
44
+ require "cassia/requests/disconnect_device"
45
+ require "cassia/requests/connect_local"
46
+ require "cassia/requests/disconnect_local"
47
+ require "cassia/requests/get_connected_devices_router"
48
+ require "cassia/requests/open_notify"
49
+ require "cassia/requests/close_notify"
50
+ require "cassia/requests/open_connection_state"
51
+ require "cassia/requests/close_connection_state"
52
+ require "cassia/requests/open_ap_state"
53
+ require "cassia/requests/close_ap_state"
54
+ require "cassia/requests/combined_sse"
55
+ require "cassia/requests/discover_all_services"
56
+ require "cassia/requests/discover_all_char"
57
+ require "cassia/requests/discover_char_of_service"
58
+ require "cassia/requests/discover_descriptor_of_char"
59
+ require "cassia/requests/discover_all_services_and_chars"
60
+ require "cassia/requests/write_char_by_handle"
61
+
62
+ module Cassia
63
+ def self.configuration
64
+ @configuration ||= Configuration.new
65
+ end
66
+
67
+ def self.api
68
+ @api ||= Api.new
69
+ end
70
+
71
+ def self.logger
72
+ @logger ||= DefaultLogger.build
73
+ end
74
+
75
+ def self.logger=(logger)
76
+ @logger = logger
77
+ end
78
+
79
+ def self.configure
80
+ yield(configuration)
81
+ end
82
+ end