1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import java.io.IOException;
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import com.siemens.icm.io.ATCommand;
import com.siemens.icm.io.ATCommandFailedException;
import com.siemens.icm.io.ATCommandResponseListener;
/**
* The first Task for the Worker.
*
* This class does the real work.
*
*/
public class Task1 extends Thread implements ATCommandResponseListener {
/**
* pointer to the MIDlet.
*
* we nee the pointer to call methods of the MIDlet.
*/
Worker worker;
ATCommand atcommand;
URCBuffer urcbuffer;
/**
* constructor with the calling MIDlet as argument,
*
* @param worker
*/
public Task1(Worker worker) {
super();
this.worker = worker;
}
/**
* enters the PIN for SIM access if required
*
* @param pin
* PIN for SIM access
* @return success
*/
private boolean enterpin(String pin) {
String response;
try {
// checks, if PIN is required
response = atcommand.send("AT+CPIN?\r");
if (response.indexOf("READY") > -1) {
System.out.println("no PIN required");
return true;
}
System.out.println("enter PIN " + pin);
atcommand.send("AT+CPIN=" + pin + "\r");
response = atcommand.send("AT+CPIN?\r");
if (response.indexOf("READY") > -1) {
System.out.println("PIN accepted");
return true;
}
System.out.println(response);
System.out.println("PIN rejected");
return false;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ATCommandFailedException e) {
e.printStackTrace();
}
return false;
}
/**
* do all the settings in the module.
*
*/
private void configure() {
// configuration for incoming short messages
try {
// use text mode for reading and sending short messages
atcommand.send("AT+CMGF=1\r");
// Set the preferred store to "mobile terminal"
atcommand.send("AT+CPMS=\"MT\"\r");
// Require URCs for incoming messages
atcommand.send("AT+CNMI=1,1\r");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ATCommandFailedException e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("starting with the first task ...");
urcbuffer = new URCBuffer();
try {
atcommand = new ATCommand(false);
atcommand.addListener(urcbuffer);
enterpin("1750");
configure();
CommConnection connection = (CommConnection) Connector
.open("comm:com0;baudrate=115200");
int i = 0;
while (Worker.isWorking()) { // call of a static method
// here the work should be done ...
System.out.println(i);
i++;
// Is a URC in the buffer?
String newurc = urcbuffer.getnext();
if (newurc != null) {
// if yes, it is checked if a short message was received
if (newurc.indexOf("+CMTI") >= 0) {
// if yes, read the short message
String slot = newurc.substring(newurc.indexOf(",") + 1);
System.out.println("read message in slot #" + slot);
String response = atcommand.send("AT+CMGR=" + slot
+ "\r");
System.out.println(response);
}
}
// switch to normal mode
if (i == 1) {
atcommand.send("AT+CFUN=1\r");
System.out.println("normal mode");
}
// switch power down mode
if (i == 2) {
String response = atcommand.send("AT+CCLK?\r");
int p = response.indexOf(34);
String date = response.substring(p+1, p + 18);
System.out.println("current time: " + date);
int m1 = Integer.parseInt(date.substring(13, 14));
int m2 = Integer.parseInt(date.substring(12, 13));
int h1 = Integer.parseInt(date.substring(10, 11));
int h2 = Integer.parseInt(date.substring(9, 10));
m1 += 2;
if (m1 > 9) {
m1 = m1 - 10;
m2++;
}
if (m2 > 5) {
m2 = 0;
h1++;
}
if (h1 > 9) {
h1 = 0;
h2++;
}
// date change not supported here!
String wakeup = date.substring(0, 9) + Integer.toString(h2)
+ Integer.toString(h1) + ":" + Integer.toString(m2)
+ Integer.toString(m1) + date.substring(14);
System.out.println("wake up time: " + wakeup);
String command = "AT+CALA=\"" + wakeup + "\",0,0,\"wakeup\"\r";
response = atcommand.send(command);
System.out.println(response);
System.out.println("power down mode");
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// for our edit-compile-run cycle we need to get back control
// over
// the module!
if (i > 4) {
connection.close();
break;
}
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ATCommandFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// done
worker.taskcounter(-1);
}
public void ATResponse(String response) {
System.out.println(response);
}
} |
Partager