2012-05-10 13:53:18 +00:00
|
|
|
package de.ctdo.crashtest;
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
2012-05-30 12:37:55 +00:00
|
|
|
import java.io.*;
|
|
|
|
import java.net.Socket;
|
2012-05-10 13:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User: lpless
|
|
|
|
* Date: 10.05.12
|
|
|
|
* Time: 11:03
|
|
|
|
*/
|
|
|
|
public class BuntiClient {
|
2012-05-22 21:11:08 +00:00
|
|
|
private String baseAddress;
|
|
|
|
private HttpClient client = new DefaultHttpClient();
|
2012-05-30 12:37:55 +00:00
|
|
|
|
2012-05-10 13:53:18 +00:00
|
|
|
public BuntiClient(String server, int port) {
|
|
|
|
baseAddress = "http://" + server + ":" + port + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPar56(int id, int red, int green, int blue) {
|
|
|
|
try {
|
|
|
|
HttpPost post = new HttpPost(baseAddress + "/control/devices");
|
|
|
|
post.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
StringEntity entity = new StringEntity(
|
|
|
|
"{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": "+id+", \"options\": { \"red\": "+
|
|
|
|
red+", \"green\": "+green+", \"blue\": "+blue+" } } ] }" ,
|
|
|
|
"UTF-8");
|
|
|
|
|
|
|
|
post.setEntity(entity);
|
|
|
|
|
|
|
|
HttpResponse response = client.execute(post);
|
|
|
|
System.out.println(response);
|
|
|
|
|
|
|
|
post.abort();
|
|
|
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLampel(boolean red, boolean yellow, boolean green) {
|
2012-05-22 21:11:08 +00:00
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
if( green ) value |= 0x01;
|
|
|
|
if( yellow ) value |= 0x02;
|
|
|
|
if( red ) value |= 0x04;
|
|
|
|
|
2012-05-30 12:37:55 +00:00
|
|
|
try {
|
|
|
|
Socket client = new Socket("lampel.ctdo.de", 2701);
|
|
|
|
client.setSoTimeout(2000);
|
|
|
|
|
|
|
|
|
|
|
|
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
|
|
|
|
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
|
|
|
2012-05-22 21:11:08 +00:00
|
|
|
|
2012-05-30 12:37:55 +00:00
|
|
|
outToServer.writeBytes("io set port 2 " + Integer.toHexString(value) + '\n');
|
|
|
|
|
|
|
|
String result = inFromServer.readLine();
|
|
|
|
|
|
|
|
client.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2012-05-22 21:11:08 +00:00
|
|
|
|
|
|
|
/* try {
|
2012-05-10 13:53:18 +00:00
|
|
|
HttpPost post = new HttpPost(baseAddress + "/control/devices");
|
|
|
|
post.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
StringEntity entity = new StringEntity(
|
|
|
|
"{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": 4, \"options\": { \"red\": "+
|
|
|
|
red+", \"green\": "+green+", \"yellow\": "+yellow+" } } ] }" ,
|
|
|
|
"UTF-8");
|
|
|
|
|
|
|
|
post.setEntity(entity);
|
|
|
|
|
|
|
|
HttpResponse response = client.execute(post);
|
|
|
|
System.out.println(response);
|
|
|
|
|
|
|
|
post.abort();
|
|
|
|
|
2012-05-30 12:37:55 +00:00
|
|
|
|
2012-05-10 13:53:18 +00:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2012-05-22 21:11:08 +00:00
|
|
|
}*/
|
2012-05-10 13:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|