crashtest/src/de/ctdo/crashtest/BuntiClient.java

105 lines
3.1 KiB
Java

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;
import java.io.*;
import java.net.Socket;
/**
* User: lpless
* Date: 10.05.12
* Time: 11:03
*/
public class BuntiClient {
private String baseAddress;
private HttpClient client = new DefaultHttpClient();
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) {
int value = 0;
if( green ) value |= 0x01;
if( yellow ) value |= 0x02;
if( red ) value |= 0x04;
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()));
outToServer.writeBytes("io set port 2 " + Integer.toHexString(value) + '\n');
String result = inFromServer.readLine();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
/* try {
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();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
}