Class: AwsIotDevice::MqttShadowClient::ShadowActionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shadow_name, mqtt_client, persistent_subscribe = false) ⇒ ShadowActionManager

Returns a new instance of ShadowActionManager.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 18

def initialize(shadow_name, mqtt_client, persistent_subscribe=false)
  @shadow_name = shadow_name
  @topic_manager = ShadowTopicManager.new(mqtt_client, shadow_name)
  @payload_parser = JSONPayloadParser.new
  @is_subscribed = {}
  @is_subscribed[:get] = false
  @is_subscribed[:update] = false
  @is_subscribed[:delete] = false
  @token_handler = TokenCreator.new(shadow_name, mqtt_client.client_id)
  @persistent_subscribe = persistent_subscribe
  @last_stable_version = -1 #Mean no currentely stable
  @topic_subscribed_callback = {}
  @topic_subscribed_callback[:get] = nil
  @topic_subscribed_callback[:update] = nil
  @topic_subscribed_callback[:delta] = nil
  @topic_subscribed_task_count = {}
  @topic_subscribed_task_count[:get] = 0
  @topic_subscribed_task_count[:update] = 0
  @topic_subscribed_task_count[:delete] = 0
  @token_pool = {}
  @token_callback = {}
  @task_count_mutex = Mutex.new
  @token_mutex = Mutex.new
  @parser_mutex = Mutex.new
  set_basic_callback
end

Instance Attribute Details

#loggerObject

This the main AWS action manager It enables the AWS IoT actions (get, update, delete) It enables the time control the time out after an action have been start Actions requests are send on the general actions topic and answer is retreived from accepted/refused/delta topics



16
17
18
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 16

def logger
  @logger
end

Instance Method Details

#logger?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 116

def logger?
  !@logger.nil? && @logger.is_a?(Logger)
end

#register_delete_callback(callback, &block) ⇒ Object



86
87
88
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 86

def register_delete_callback(callback, &block)
  register_action_callback(:delete, callback, &block)
end

#register_get_callback(callback, &block) ⇒ Object



78
79
80
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 78

def register_get_callback(callback, &block)
  register_action_callback(:get, callback, &block)
end

#register_shadow_delta_callback(callback, &block) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 90

def register_shadow_delta_callback(callback, &block)
  if callback.is_a?(Proc)
    @topic_subscribed_callback[:delta] = callback
  elsif block_given?
    @topic_subscribed_callback[:delta] = block
  end
  @topic_manager.shadow_topic_subscribe("delta", @default_callback)
end

#register_update_callback(callback, &block) ⇒ Object



82
83
84
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 82

def register_update_callback(callback, &block)
  register_action_callback(:update, callback, &block)
end

#remove_delete_callbackObject



107
108
109
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 107

def remove_delete_callback
  remove_action_callback(:delete)
end

#remove_get_callbackObject



99
100
101
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 99

def remove_get_callback
  remove_action_callback(:get)
end

#remove_shadow_delta_callbackObject



111
112
113
114
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 111

def remove_shadow_delta_callback
  @topic_subscribe_callback.delete[:delta]
  @topic_manager.shadow_topic_unsubscribe("delta")
end

#remove_update_callbackObject



103
104
105
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 103

def remove_update_callback
  remove_action_callback(:update)
end

#shadow_delete(timeout = 5, callback = nil, &block) ⇒ Object



74
75
76
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 74

def shadow_delete(timeout=5, callback=nil, &block)
  shadow_action(:delete, "", timeout, callback, &block)
end

#shadow_get(timeout = 5, callback = nil, &block) ⇒ Object

Send and publish packet with an empty payload contains in a valid JSON format. A unique token is generate and send in the packet in order to trace the action. Subscribe to the two get/accepted and get/rejected of the coresponding shadow. If the request is accpeted, the answer would be send on the get/accepted topic. It contains all the details of the shadow state in JSON document. A specific callback in Proc could be send parameter. Before exit, the function start a timer count down in the separate thread. If the time ran out, the timer_handler function is called and the get action is cancelled using the token.

Parameter:

> callback: the Proc to execute when the answer to th get request would be received.
             It should accept three different paramter:
               - payload : the answer content
               - response_status : among ['accepted', 'refused', 'delta']
               - token : the token assoicate to the get request

> timeout: the period after which the request should be canceled and timer_handler should be call

Returns :

> the token associate to the current action (which also store in @token_pool)


66
67
68
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 66

def shadow_get(timeout=5, callback=nil, &block)
  shadow_action(:get, "", timeout, callback, &block)
end

#shadow_update(payload, timeout = 5, callback = nil, &block) ⇒ Object



70
71
72
# File 'lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb', line 70

def shadow_update(payload, timeout=5, callback=nil, &block)
  shadow_action(:update, payload, timeout, callback, &block)
end