Class: LosantMqtt::Device

Inherits:
Object
  • Object
show all
Includes:
Events::Emitter
Defined in:
lib/losant_mqtt/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Device

Returns a new instance of Device.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/losant_mqtt/device.rb', line 29

def initialize(options = {})
  @was_connected = false

  @device_id     = options[:device_id].to_s
  @key           = options[:key].to_s
  @secret        = options[:secret].to_s
  @secure        = options.has_key?(:secure) ? !!options[:secure] : true
  @should_retry  = options.has_key?(:retry_lost_connection) ?
    !!options[: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_idObject (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

#keyObject (readonly)

Returns the value of attribute key.



27
28
29
# File 'lib/losant_mqtt/device.rb', line 27

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



27
28
29
# File 'lib/losant_mqtt/device.rb', line 27

def secret
  @secret
end

#secureObject (readonly)

Returns the value of attribute secure.



27
28
29
# File 'lib/losant_mqtt/device.rb', line 27

def secure
  @secure
end

#should_retryObject (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

#closeObject



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_topicObject



153
154
155
# File 'lib/losant_mqtt/device.rb', line 153

def command_topic
  @command_topic ||= LosantMqtt::COMMAND_TOPIC % { device_id: @device_id }
end

#connectObject



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.message =~ /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

Returns:

  • (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_topicObject



149
150
151
# File 'lib/losant_mqtt/device.rb', line 149

def state_topic
  @state_topic ||= LosantMqtt::STATE_TOPIC % { device_id: @device_id }
end