Class: LosantMqtt::Device
- Inherits:
-
Object
- Object
- LosantMqtt::Device
- Includes:
- Events::Emitter
- Defined in:
- lib/losant_mqtt/device.rb
Instance Attribute Summary collapse
-
#device_id ⇒ Object
readonly
Returns the value of attribute device_id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#secure ⇒ Object
readonly
Returns the value of attribute secure.
-
#should_retry ⇒ Object
readonly
Returns the value of attribute should_retry.
Instance Method Summary collapse
- #close ⇒ Object
- #command_topic ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(options = {}) ⇒ Device
constructor
A new instance of Device.
- #retry_lost_connection(wait = 5) ⇒ Object
- #send_state(state, time = nil) ⇒ Object
- #state_topic ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Device
Returns a new instance of Device.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/losant_mqtt/device.rb', line 29 def initialize( = {}) @was_connected = false @device_id = [:device_id].to_s @key = [:key].to_s @secret = [:secret].to_s @secure = .has_key?(:secure) ? !![:secure] : true @should_retry = .has_key?(:retry_lost_connection) ? !![:retry_lost_connection] : true raise ArgumentError.new("Invalid Device Id") if @device_id == "" raise ArgumentError.new("Invalid Key") if @key == "" raise ArgumentError.new("Invalid Secret") if @secret == "" end |
Instance Attribute Details
#device_id ⇒ Object (readonly)
Returns the value of attribute device_id.
27 28 29 |
# File 'lib/losant_mqtt/device.rb', line 27 def device_id @device_id end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
27 28 29 |
# File 'lib/losant_mqtt/device.rb', line 27 def key @key end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
27 28 29 |
# File 'lib/losant_mqtt/device.rb', line 27 def secret @secret end |
#secure ⇒ Object (readonly)
Returns the value of attribute secure.
27 28 29 |
# File 'lib/losant_mqtt/device.rb', line 27 def secure @secure end |
#should_retry ⇒ Object (readonly)
Returns the value of attribute should_retry.
27 28 29 |
# File 'lib/losant_mqtt/device.rb', line 27 def should_retry @should_retry end |
Instance Method Details
#close ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/losant_mqtt/device.rb', line 112 def close @connection.disconnect if @connection if @retry_timer @retry_timer.cancel @retry_timer = nil end true end |
#command_topic ⇒ Object
153 154 155 |
# File 'lib/losant_mqtt/device.rb', line 153 def command_topic @command_topic ||= LosantMqtt::COMMAND_TOPIC % { device_id: @device_id } end |
#connect ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/losant_mqtt/device.rb', line 48 def connect return self if @retry_timer || @connection begin @connection = DeviceConnection.connect( host: LosantMqtt.endpoint, port: @secure ? 8883 : 1883, secure: @secure, username: @key, password: @secret, client_id: @device_id) rescue Exception => ex if @was_connected && @should_retry @connection = nil emit(:close, self, ex) retry_lost_connection return self else raise ex end end @connection.on(:disconnected) do |reason| @connection = nil emit(:close, self, reason) if reason if @was_connected && @should_retry && !(reason. =~ /Authentication Error/) # if it was not an authentication error # and we ave successfully connected before # attempt to reconnect in a few seconds retry_lost_connection else raise reason end end end @connection.on(:connected) do if(@state_backlog) @connection.publish(state_topic, @state_backlog.to_json) @state_backlog = nil end if @was_connected emit(:reconnect, self) else @was_connected = true emit(:connect, self) end @connection.subscribe(command_topic) do |msg| begin msg = Utils.convert_ext_json(JSON.parse(msg)) rescue JSON::ParserError msg = nil end emit(:command, self, msg) if msg end end self end |
#connected? ⇒ Boolean
44 45 46 |
# File 'lib/losant_mqtt/device.rb', line 44 def connected? !!(@connection && @connection.connected?) end |
#retry_lost_connection(wait = 5) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/losant_mqtt/device.rb', line 141 def retry_lost_connection(wait=5) return false if @connection @retry_timer ||= EventMachine::Timer.new(wait) do @retry_timer = nil connect end end |
#send_state(state, time = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/losant_mqtt/device.rb', line 121 def send_state(state, time = nil) connect unless @connection time ||= Time.now time = time.to_time if time.respond_to?(:to_time) time = time.to_f time = time * 1000 if time < 1000000000000 # convert to ms since epoch time = time.round payload = { time: time, data: state } if connected? @connection.publish(state_topic, payload.to_json) else (@state_backlog ||= []).push(payload) end true end |
#state_topic ⇒ Object
149 150 151 |
# File 'lib/losant_mqtt/device.rb', line 149 def state_topic @state_topic ||= LosantMqtt::STATE_TOPIC % { device_id: @device_id } end |