Class: Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco-deviot/gateway.rb

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, deviot_server, mqtt_server, account = '', kind = 'device') ⇒ Gateway

Returns a new instance of Gateway.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cisco-deviot/gateway.rb', line 33

def initialize(name, deviot_server, mqtt_server,  = '', kind = 'device')
  @name = name
  @owner = 
  @kind = kind
  @mode = MODE_MQTT
  @deviot_server = deviot_server
  @things = Hash.new
  @connector = MqttConnector.new(self, mqtt_server)
  @host = @connector.host
  @port = @connector.port
  @data = @connector.data
  @action = @connector.action
  @registration_started = FALSE
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/cisco-deviot/gateway.rb', line 30

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



31
32
33
# File 'lib/cisco-deviot/gateway.rb', line 31

def owner
  @owner
end

Instance Method Details

#call_action(data) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cisco-deviot/gateway.rb', line 128

def call_action(data)
  name = data.delete('name')
  action = data.delete('action')
  if name && action && @things.has_key?(name)
    thing = @things[name]
    action_model = thing.actions.find{|a| a.name == action}
    if thing.respond_to?(action) && action_model
      args = action_model.parameters.collect{|x| data[x.name]}
      if action_model.need_payload
        args.push(delete('payload'))
      end
      thing.send(action, *args)
      @@logger.info("#{thing} called with #{action}(#{data})")
    else
      raise ArgumentError.new("no action #{action} defined in thing #{thing}")
    end
  else
    raise ArgumentError.new("thing #{name} is not registered")
  end
end

#register(thing) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cisco-deviot/gateway.rb', line 103

def register(thing)
  unless thing.is_a?(Thing)
    raise ArgumentError.new("#{thing} is not a thing")
  end
  if @things.has_key?(thing.id)
    @@logger.warn("thing #{thing} is already registered")
  else
    @things.store(thing.id, thing)
    @@logger.info("thing #{thing} registered")
  end
end

#send_data(data) ⇒ Object



124
125
126
# File 'lib/cisco-deviot/gateway.rb', line 124

def send_data(data)
  @connector.publish(data)
end

#startObject



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
# File 'lib/cisco-deviot/gateway.rb', line 48

def start
  if @registration_started
    @@logger.warn("gateway service #{@name} already started")
  else
    @registration_started = TRUE

    Thread.new {
      registered_ok = 0
      while @registration_started
        begin
          uri = URI.parse(@deviot_server)
          http = Net::HTTP.new(uri.host, uri.port)
          http.read_timeout = 10
          http.open_timeout = 10
          request = Net::HTTP::Post.new('/api/v1/gateways', {'Content-Type': 'application/json'})
          request.body = {name: @name, kind: @kind, owner: @owner,
                          host: @host, port: @port, mode: @mode,
                          data: @data, action: @action,
                          sensors: @things.values.collect{|x| x.get_model}}.to_json
          response = http.request(request)
          if response.code.to_i < 300
            if registered_ok != 2
              @@logger.info("gateway service #{@name} registered to #{@deviot_server}")
            end
            registered_ok = 2
          else
            if registered_ok != 1
              @@logger.error("failed to register gateway #{@name} to #{@deviot_server}: #{response.code}")
            end
            registered_ok = 1
          end
        rescue StandardError => e
          if registered_ok != 1
            @@logger.error("failed to register gateway #{@name} to #{@deviot_server}: #{e}")
          end
          registered_ok = 1
        end
        sleep(100)
      end
    }
    @connector.start
    @@logger.info("gateway service #{@name} started")
  end
end

#stopObject



93
94
95
96
97
98
99
100
101
# File 'lib/cisco-deviot/gateway.rb', line 93

def stop
  if @registration_started
    @connector.stop
    @registration_started = FALSE
    @@logger.error("gateway service #{@name} stopped")
  else
    @@logger.warn("gateway service #{@name} already stopped")
  end
end

#unregister(thing) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/cisco-deviot/gateway.rb', line 115

def unregister(thing)
  if @things.has_key?(thing.id)
    @things.delete(thing.id)
    @@logger.info("thing #{thing} is unregistered")
  else
    @@logger.warn("thing #{thing} is not registered")
  end
end