Hey, all, I need your help, please. Recently, I am working on writing a TCP client program on sunrise. And I want to run it as a background task. I wrote the code by reading the manual. But when I just synchronised the code to the cabinet, the light was red and I couldn't see the error log. The code is shown below, can anyone of you who know how to write a background task help me work out of it. Thanks very much!!!
That is the code of background task:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.inject.Inject;
import com.kuka.roboticsAPI.applicationModel.tasks.RoboticsAPIBackgroundTask;
import com.kuka.roboticsAPI.controllerModel.Controller;
import com.kuka.roboticsAPI.uiModel.ApplicationDialogType;
import com.kuka.task.ITaskFunctionMonitor;
import com.kuka.task.TaskFunctionMonitor;
/**
* Implementation of a background task.
* <p>
* The background task provides a {@link RoboticsAPIBackgroundTask#initialize()}
* and a {@link RoboticsAPIBackgroundTask#run()} method, which will be called
* successively in the task lifecycle.<br>
* The task will terminate automatically after the <code>run</code> method has
* finished or after stopping the task.
* <p>
* <b>It is imperative to call <code>super.dispose()</code> when overriding the
* {@link RoboticsAPITask#dispose()} method.</b>
* @see UseRoboticsAPIContext
*
*/
public class BackgroundTask extends RoboticsAPIBackgroundTask {
@Inject
Controller kUKA_Sunrise_Cabinet_1;
private IApplicationInformationFunction appInfoFunction;
private ITaskFunctionMonitor appInfoMonitor;
@Override
public void initialize() {
// initialize your task here
appInfoFunction =getTaskFunction(IApplicationInformationFunction.class);
appInfoMonitor =TaskFunctionMonitor.create(appInfoFunction);
}
@Override
public void run() {
// your task execution starts here
try{
Socket server = new Socket("172.31.1.100", 30000);
final BufferedReader in = new BufferedReader(new InputStreamReader(
server.getInputStream()));
PrintWriter out = new PrintWriter(server.getOutputStream());
BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
new Thread() {
public void run() {
while (true) {
try {
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
break;
}
}
};
}.start();
while (true) {
String str = wt.readLine();
if(str=="finished")
{
appInfoFunction.SetCondition(true);
}
if(appInfoFunction.isSelectingFinished())
{
out.println("finished");
out.flush();
}
if (str.equals("1")) {
break;
}
}
server.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}