Jawbone Up24を買った。Apple Storeで売ってた。
前のUpと比べてbluetoothで同期できるので楽というのもあるが、webhookによるサーバープッシュAPIの方が気になってて買った。

UP for Developers: Pub Sub


通知

oauthで認証してもらったユーザーが寝たり・起きたり・活動したりすると、約10秒後に自作のwebアプリにjsonでwebhookが通知される。
jawbone up—(bluetooth)—>スマホ—(HTTP)—>jawboneのサーバー—(HTTP webhook)—>自作webアプリ
と通知がリレーされていくわけです。

最近色々とまともなAPIのあるガジェットは増えてるけど、出力側ばっかりで、入力というかトリガー側(センサー)になるやつはあまり無い。


ソースコード

前に作ったJawbone UpのAPIで睡眠時間などを取得するを少し改造した。

https://github.com/shokai/jawbone-up-api-study


設定

アプリケーションの設定にpubsubのURLを書くだけで通知が来るようになる。


プログラム

post ‘/pubsub’を追加した。あとAPIのscopeを色々追加した。move_readが無いと運動した時に空の通知が来てしまっていたので。

https://github.com/shokai/jawbone-up-api-study/tree/master/auth
def app_root
"#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}"
end

def oauth_client
@client ||= OAuth2::Client.new(CLIENT_ID, APP_SECRET,
:site => 'https://jawbone.com',
:authorize_url => '/auth/oauth2/auth',
:token_url => '/auth/oauth2/token')
end

get '/' do
unless session[:oauth_token]
@mes = %Q{<p><a href="/login">login</a></p>}
else
@mes = %Q{<p><a href="/logout">logout</a></p>
<p>your token : #{session[:oauth_token]}</p>}
end
end

get '/login' do
scope = "basic_read extended_read mood_read move_read sleep_read generic_event_read"
redirect oauth_client.auth_code.authorize_url(:scope => scope,
:redirect_uri => "#{app_root}/auth")
end

get '/auth' do
code = params["code"]
halt 400, 'code missing' unless code
begin
session[:oauth_token] = oauth_client.auth_code.get_token(code).token
puts "TOKEN : "+session[:oauth_token]
rescue => e
STDERR.puts e.message
end
redirect "/"
end

get '/logout' do
session.delete :oauth_token
redirect "/"
end

post '/pubsub' do
request.body.rewind
puts request.body.read
"ok"
end


こういうのがpushされてくる
{"events": [{"action": "enter_sleep_mode", "timestamp": 1396797071, "user_xid": "gvT5W2FhlrHsB3r9Wq_unA"}], "notification_timestamp": 1396797074}
{"events": [{"action": "exit_sleep_mode", "timestamp": 1396797115, "user_xid": "gvT5W2FhlrHsB3r9Wq_unA"}], "notification_timestamp": 1396797117}
{"events": [{"action": "creation", "timestamp": 1396797240, "user_xid": "gvT5W2FhlrHsB3r9Wq_unA", "type": "move", "event_xid": "mD99tHyRBx4xNT7HoSCUmA"}], "notification_timestamp": 1396798341}