Class: Microgear

Inherits:
Object
  • Object
show all
Defined in:
lib/microgear.rb

Constant Summary collapse

MINTOKDELAYTIME =
100
MAXTOKDELAYTIME =
15000
MGREV =
'NJ1'
GEARAUTHSITE =
"http://61.91.14.29:8080/"
GEARAUTHREQUESTTOKENENDPOINT =
"/oauth/request_token"
GEARAUTHACCESSTOKENENDPOINT =
"/oauth/access_token"

Instance Method Summary collapse

Constructor Details

#initialize(gearkey, gearsecret, appid, args = {}) ⇒ Microgear

Returns a new instance of Microgear.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/microgear.rb', line 22

def initialize(gearkey,gearsecret, appid, args = {})
  @logger = Logger.new(STDERR)
  @logger = Logger.new(STDOUT)
  if args[:debugmode]
    @logger.level = Logger::DEBUG
  else
    @logger.level = Logger::INFO
  end

  @gearkey = gearkey
  @gearsecret = gearsecret

  @appid = appid
  @gearname = nil

  @accesstoken = nil
  @requesttoken = nil
  @client = nil
  @scope = args[:scope] || ""

  @gearexaddress = nil
  @gearexport = nil

  @subscriptions = []
  @callbacks = {}
end

Instance Method Details

#chat(who, message) ⇒ Object



169
170
171
# File 'lib/microgear.rb', line 169

def chat(who, message)
  publish('/gearname/'+who, message)
end

#connectObject



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/microgear.rb', line 49

def connect
  get_token
  @logger.info  @accesstoken
  @logger.debug "gearID       : "+@appid
  nonce = SecureRandom.hex(8)
  hkey = @accesstoken["secret"]+"&"+@gearsecret
  conn_opts = {
    host: @gearexaddress,
    username: @gearkey+'%'+Time.now.to_i.to_s,
    client_id: @accesstoken["token"],
    keep_alive: 10
  }
  digest = OpenSSL::Digest.new('sha1')
  conn_opts[:password] = Base64.encode64(OpenSSL::HMAC.digest(digest, hkey, @accesstoken["token"]+"%"+conn_opts[:username])).strip
  @logger.debug "mqttuser     : "+conn_opts[:username]
  @logger.debug "mqttpassword : "+conn_opts[:password]
  @subscriptions.push('/piegear/'+conn_opts[:client_id]+'/#')
  @mqtt_loop = Fiber.new do
    MQTT::Client.connect(conn_opts) do |client|
      @logger.info "Connected"
      gear_state = Fiber.yield [:started,nil]
      @logger.debug "auto subscribe "+@subscriptions.join(",")
      client.subscribe(*@subscriptions)
      loop do
        case gear_state[0]
        when :idle
          unless client.queue_empty?
            topic,message = client.get
            if topic
              Fiber.yield [:message, [topic,message]]
            end
          end
        when :subscribe
          client.subscribe(*gear_state[1])
        when :unsubscribe
          client.unsubscribe(*gear_state[1])
        when :publish
          client.publish(*gear_state[1])
          if @subscriptions.include? gear_state[1][0]
            topic,message = client.get
            if topic and @callbacks[topic]
              @callbacks[topic].call(topic,message)
            end
          end
        when :close
          client.close
          Fiber.yield [:exit, nil]
        end
        gear_state = Fiber.yield [:ready, nil]
      end
    end
  end
  loop do
    if @mqtt_loop.alive?
      mqtt_state = @mqtt_loop.resume [:idle, nil]
      case mqtt_state[0]
      when :started
        gear_state = [:started, nil]
      when :message
        if mqtt_state[1][1].index("/piegear/") == 0
          @logger.debug '* control message > '+mqtt_state[1][1]+' : '+mqtt_state[1][2]
        else
          if @callbacks[mqtt_state[1][0]]
            @callbacks[mqtt_state[1][0]].call(*mqtt_state[1])
          end
        end
      when :ready
        if block_given?
          yield
        end
      when :exit
        break
      end
    else
      break
    end
  end
end

#publish(topic, message) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/microgear.rb', line 160

def publish(topic,message)
  if @mqtt_loop and @mqtt_loop.alive?
    @mqtt_loop.resume [:publish, ['/'+@appid+topic, message]]
  else
    raise IOError, "You must connect before publish!"
  end

end

#setname(name, &callback) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/microgear.rb', line 152

def setname(name, &callback)
  if @gearname
    unsubscribe('/gearname/'+@gearname)
  end
  @gearname = name
  subscribe('/gearname/'+name, &callback)
end

#subscribe(topic, &callback) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/microgear.rb', line 128

def subscribe(topic, &callback)
  topic = "/"+@appid+topic
  unless @subscriptions.include? topic
    @subscriptions.push(topic)
  end
  if callback.class == Proc
    @callbacks[topic] = callback
  end
  if @mqtt_loop
    @mqtt_loop.resume [:subscribe, [topic]]
  end
end

#unsubscribe(topic) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/microgear.rb', line 141

def unsubscribe(topic)
  @logger.debug @subscriptions.index('/'+@appid+topic)
  @logger.debug @subscriptions.join ","
  if @subscriptions.index('/'+@appid+topic)
    @subscriptions.delete('/'+@appid+topic)
    if @mqtt_loop
      @mqtt_loop.resume [:unsubscribe, ["/"+@appid+topic]]
    end
  end
end