Java tutorial
/* * Copyright 2013 Netatmo * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.smartnsoft.hackathon.directenergie.ws; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; import com.netatmo.weatherstation.api.NetatmoHttpClient; import com.netatmo.weatherstation.api.NetatmoResponseHandler; import com.netatmo.weatherstation.api.NetatmoUtils; import com.netatmo.weatherstation.api.model.Measures; import com.netatmo.weatherstation.api.model.Params; import com.netatmo.weatherstation.api.model.Station; /** * This is just an example of how you can extend NetatmoHttpClient. Tokens are stored in the shared preferences of the app, but you can store them as * you wish as long as they are properly returned by the getters. If you want to add your own '/getmeasure' requests, this is also the place to do it. */ public final class NetatmoServices extends NetatmoHttpClient { final String CLIENT_ID = "528fce151c775957ae8b4614"; final String CLIENT_SECRET = "xFxVzeStC9iE6NVrMXTWgqZFz9AS"; SharedPreferences mSharedPrefs; public NetatmoServices(Context context) { mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); } private static volatile NetatmoServices instance; public static NetatmoServices getInstance(Context context) { if (instance == null) { synchronized (NetatmoServices.class) { if (instance == null) { instance = new NetatmoServices(context); } } } return instance; } @Override protected String getClientId() { return CLIENT_ID; } @Override protected String getClientSecret() { return CLIENT_SECRET; } @Override public void storeTokens(String refreshToken, String accessToken, long expiresAt) { SharedPreferences.Editor editor = mSharedPrefs.edit(); editor.putString(NetatmoUtils.KEY_REFRESH_TOKEN, refreshToken); editor.putString(NetatmoUtils.KEY_ACCESS_TOKEN, accessToken); editor.putLong(NetatmoUtils.KEY_EXPIRES_AT, expiresAt); editor.apply(); } @Override public void clearTokens() { SharedPreferences.Editor editor = mSharedPrefs.edit(); editor.clear(); editor.apply(); } @Override public String getAccessToken() { return mSharedPrefs.getString(NetatmoUtils.KEY_ACCESS_TOKEN, null); } @Override public String getRefreshToken() { return mSharedPrefs.getString(NetatmoUtils.KEY_REFRESH_TOKEN, null); } @Override public long getExpiresAt() { return mSharedPrefs.getLong(NetatmoUtils.KEY_EXPIRES_AT, 0); } public void loginAutomatic() { final String M = "netatmoLogin: "; Log.i(TAG, M); // NetatmoResponseHandler parses and handles the response. // You can also use JsonHttpResponseHandler and process the response as you wish. login("willy@smartnsoft.com", "aianeal", new NetatmoResponseHandler(this, NetatmoResponseHandler.REQUEST_LOGIN, null) { @Override public void onStart() { Log.i(TAG, M + " onStart:"); super.onStart(); } @Override public void onLoginResponse() { Log.i(TAG, M + " onLoginResponse:"); } @Override public void onFailure(Throwable e, JSONObject errorResponse) { Log.i(TAG, M + " onFailure:"); super.onFailure(e, errorResponse); Log.i(TAG, M + " onFailure:"); } @Override public void onSuccess(JSONObject response) { super.onSuccess(response); Log.i(TAG, M + " onSuccess:" + response.toString() + "'"); } @Override public void onFinish() { Log.i(TAG, M + " onFinish:"); super.onFinish(); } }); } /** * Initializing the action bar with the stations' names using the parsed response returned by * NetatmoHttpClient.getDevicesList(NetatmoResponseHandler). */ private List<Station> devicesList = new ArrayList<Station>(); public List<Station> getDevicesList() throws InterruptedException { final CountDownLatch deviceWait = new CountDownLatch(1); deviceWait.countDown(); getDevicesList(new NetatmoResponseHandler(NetatmoServices.this, NetatmoResponseHandler.REQUEST_GET_DEVICES_LIST, null) { @Override public void onGetDevicesListResponse(final List<Station> devices) { devicesList = devices; try { deviceWait.await(); } catch (InterruptedException e) { e.printStackTrace(); } } }); return devicesList; // synchronized (devicesList) // { // getDevicesList(new NetatmoResponseHandler(NetatmoServices.this, NetatmoResponseHandler.REQUEST_GET_DEVICES_LIST, null) // { // @Override // public void onGetDevicesListResponse(final List<Station> devices) // { // devicesList = devices; // } // }); // return devicesList; // } } private Measures measuresValue; public synchronized Measures getLastMeasures(String stationId, String moduleId) { final Object synchronizedObject = new Object(); final String[] types = new String[] { Params.TYPE_NOISE, Params.TYPE_CO2, Params.TYPE_PRESSURE, Params.TYPE_HUMIDITY, Params.TYPE_TEMPERATURE }; synchronized (synchronizedObject) { getLastMeasures(stationId, moduleId, Params.SCALE_MAX, types, new NetatmoResponseHandler(this, NetatmoResponseHandler.REQUEST_GET_LAST_MEASURES, types) { @Override public void onGetMeasuresResponse(Measures measures) { measuresValue = measures; synchronizedObject.notify(); } @Override public void onFinish() { super.onFinish(); } }); } return measuresValue; } }