Die Funktion zum Ausführen eines Befehls in einen AsyncTask ausgelagert.
This commit is contained in:
parent
0f69c12531
commit
c66fa6cd32
2 changed files with 100 additions and 24 deletions
|
@ -0,0 +1,63 @@
|
|||
/*******************************************************************************
|
||||
* "THE VODKA-WARE LICENSE" (Revision 42):
|
||||
* Tim <tim@datenknoten.me> wrote this file. As long as you retain this notice you
|
||||
* can do whatever you want with this stuff. If we meet some day, and you think
|
||||
* this stuff is worth it, you can buy me a vodka in return — Tim Schumacher
|
||||
******************************************************************************/
|
||||
|
||||
package me.datenknoten.tueroeffner;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
/**
|
||||
* Created by tim on 27.01.2015.
|
||||
*/
|
||||
public class CommandExecuter extends AsyncTask<String, Integer, Boolean> {
|
||||
private String key = "";
|
||||
|
||||
public CommandExecuter(String Key) {
|
||||
this.key = Key;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... params) {
|
||||
int count = params.length;
|
||||
|
||||
try {
|
||||
for (String param : params) {
|
||||
executeCommand(param);
|
||||
// Escape early if cancel() is called
|
||||
if (isCancelled()) break;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a command to the door server.
|
||||
*
|
||||
* @param cmd
|
||||
*/
|
||||
private Boolean executeCommand(String cmd) throws IOException, URISyntaxException {
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet request = new HttpGet();
|
||||
URI uri = new URI("https://tuer.hackspace-jena.de/cgi-bin/kraut.space?secret="+key+"&cmd="+cmd);
|
||||
request.setURI(uri);
|
||||
HttpResponse response = client.execute(request);
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -45,6 +45,7 @@ import java.net.URISyntaxException;
|
|||
import java.util.List;
|
||||
|
||||
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
|
||||
final static String networkSSID = "\"KrautSpace\"";
|
||||
|
@ -169,38 +170,50 @@ public class MainActivity extends ActionBarActivity {
|
|||
}
|
||||
}
|
||||
|
||||
public void buttonOpenOuterDoor(View v) {
|
||||
executeCommand("outdoor_buzz");
|
||||
Toast.makeText(v.getContext(), getString(R.string.buzzer_success), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void executeCommand(String cmd) {
|
||||
private String getDoorKey() {
|
||||
EditText key_editor = (EditText) findViewById(R.id.txtPass);
|
||||
return key_editor.getText().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes outdoor buzz
|
||||
*
|
||||
* @param v
|
||||
*/
|
||||
public void buttonOpenOuterDoor(View v) {
|
||||
try {
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet request = new HttpGet();
|
||||
URI uri = new URI("https://tuer.hackspace-jena.de/cgi-bin/kraut.space?secret="+key_editor.getText()+"&cmd="+cmd);
|
||||
request.setURI(uri);
|
||||
HttpResponse response = client.execute(request);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
new CommandExecuter(getDoorKey()).doInBackground("outdoor_buzz");
|
||||
Toast.makeText(v.getContext(), getString(R.string.buzzer_success), Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(v.getContext(), "Konnte Befehl „Buzzer“ nicht ausführen.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes unlock indoor
|
||||
*
|
||||
* @param v
|
||||
*/
|
||||
public void buttonOpenInnerDoor(View v) {
|
||||
executeCommand("indoor_unlock");
|
||||
try {
|
||||
new CommandExecuter(getDoorKey()).doInBackground("indoor_unlock");
|
||||
Toast.makeText(v.getContext(), getString(R.string.door_unlock), Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(v.getContext(), "Konnte Befehl „Tür aufschließen“ nicht ausführen.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
public void buttonUnluckInnerDoor(View v) {
|
||||
executeCommand("indoor_open");
|
||||
/**
|
||||
* Executes open indoor
|
||||
*
|
||||
* @param v
|
||||
*/
|
||||
public void buttonUnlockInnerDoor(View v) {
|
||||
try {
|
||||
new CommandExecuter(getDoorKey()).doInBackground("indoor_open");
|
||||
Toast.makeText(v.getContext(), getString(R.string.door_open), Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(v.getContext(), "Konnte Befehl „Tür öffnen“ nicht ausführen.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue