In case if there are some nuts like me around here, here is what I did, so you can peek and steal good ideas
I suppose one who can understand this code, will figure out layout itself, the magic is code itself.
I use Volley as REST lib, but you can use any or write your own. Android devs know what is Volley.
Have fun!
Code: Select all
package com.example.z_wave;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends Activity implements ErrorListener, OnCheckedChangeListener {
final static String OFF_SWITCH = "http://192.168.1.101:8083/ZWaveAPI/Run/devices[2].instances[0].commandClasses[37].Set(0)";
final static String ON_SWITCH = "http://192.168.1.101:8083/ZWaveAPI/Run/devices[2].instances[0].commandClasses[37].Set(255)";
final static String LEVEL = "http://192.168.1.101:8083/ZWaveAPI/Run/devices[2].instances[0].commandClasses[37].data.level";
private RequestQueue mQueue;
private Response.Listener<JSONObject> mResponse = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
};
private Response.Listener<JSONObject> mLevelResponse = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
boolean level = response.getBoolean("value");
mSwitch.setChecked(level);
} catch (JSONException e) {
e.printStackTrace();
}
}
};
private Switch mSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQueue = Volley.newRequestQueue(getApplicationContext());
mSwitch = (Switch) findViewById(R.id.switch1);
mSwitch.setOnCheckedChangeListener(this);
requestLevel();
}
private void requestLevel() {
JsonObjectRequest jsonRequest1 = new JsonObjectRequest(Method.POST, LEVEL, null, mLevelResponse, this);
mQueue.add(jsonRequest1);
}
@Override
protected void onDestroy() {
mQueue.cancelAll(this);
super.onDestroy();
}
@Override
public void onErrorResponse(VolleyError error) {
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
JsonObjectRequest jsonRequest1 ;
if (isChecked) {
jsonRequest1 = new JsonObjectRequest(Method.POST, ON_SWITCH, null, mResponse, this);
} else {
jsonRequest1 = new JsonObjectRequest(Method.POST, OFF_SWITCH, null, mResponse, this);
}
mQueue.add(jsonRequest1);
}
}