00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package ikayaki.squid;
00024
00025 import java.io.File;
00026 import java.io.FileWriter;
00027 import java.io.IOException;
00028 import java.util.Stack;
00029
00043 public class SquidEmulator {
00044 public SquidEmulator() {
00045 try {
00046 jbInit();
00047 } catch (Exception ex) {
00048 ex.printStackTrace();
00049 }
00050 }
00051
00052
00053
00054
00055
00059 private static boolean online;
00060
00064 private static File logFile;
00065
00069 private static boolean usingOldLog;
00070
00074 private static int acceleration;
00075
00079 private static int deceleration;
00080
00088 private static int velocity;
00089
00093 private static String handlerStatus;
00094
00098 private static int commandedDistance;
00099
00103 private static int currentPosition;
00104
00108 private static int homePosition;
00109
00113 private static int commandedRotation;
00114
00118 private static int currentRotation;
00119
00123 private static int degausserCoil;
00124
00128 private static int degausserAmplitude;
00129
00133 private static int degausserDelay;
00134
00138 private static int degausserRamp;
00139
00143 private static char degausserStatus;
00144
00149 private static SerialIO handlerPort;
00150 private static SerialIO magnetometerPort;
00151 private static SerialIO degausserPort;
00152
00153 private static FileWriter logWriter = null;
00154
00155 private static HandlerEmu handler;
00156 private static MagnetometerEmu magnetometer;
00157 private static DegausserEmu degausser;
00158 private static boolean running;
00159
00166 public static void writeMessage(String message, SerialIO port) {
00167
00168 try {
00169 logWriter.write("SEND:" + message + "\n");
00170 logWriter.flush();
00171 } catch (IOException e) {
00172 System.err.println("Error on writing log file");
00173 }
00174 try {
00175 port.writeMessage(message);
00176 } catch (SerialIOException ex) {
00177 System.err.println(ex);
00178 }
00179 return;
00180 }
00181
00186 public static void main(String[] args) {
00187 System.out.println("Starting...");
00188 try {
00189 int samePort = Integer.parseInt(args[0]);
00190 handlerPort = SerialIO.openPort(new SerialParameters(args[1]));
00191 magnetometerPort = SerialIO.openPort(new SerialParameters(args[2]));
00192 if (samePort == 0) {
00193 degausserPort = SerialIO.openPort(new SerialParameters(args[3]));
00194 } else {
00195 degausserPort = magnetometerPort;
00196 }
00197 if (samePort == 0) {
00198 logFile = new File(args[4]);
00199 } else {
00200 logFile = new File(args[5]);
00201 }
00202
00203 logWriter = new FileWriter(logFile);
00204 } catch (SerialIOException e) {
00205 System.out.println(e);
00206 return;
00207 } catch (Exception e) {
00208 System.out.println(
00209 "Usage \"java SquidEmulator (0:Magnetometer and Degausser use different ports, 1: Magnetometer and Degausser use same port) HandlerPort MagnetometerPort (optional)DegausserPort Log_filename\"");
00210 return;
00211 }
00212
00213 running = true;
00214
00215 handler = new HandlerEmu();
00216 handler.start();
00217 magnetometer = new MagnetometerEmu();
00218 magnetometer.start();
00219 degausser = new DegausserEmu();
00220 degausser.start();
00221
00222 System.out.println("System running...");
00223 try {
00224 logWriter.write("SESSION STARTED:\n");
00225 logWriter.flush();
00226 } catch (IOException ex1) {
00227 System.out.println("Writing error");
00228 }
00229
00230 try {
00231
00232 System.in.read();
00233 } catch (IOException ex) {
00234 }
00235
00236 SerialIO.closeAllPorts();
00237 try {
00238 logWriter.close();
00239 } catch (IOException ex2) {
00240 }
00241
00242 running = false;
00243
00244 return;
00245 }
00246
00247 private void jbInit() throws Exception {
00248 }
00249
00254 private static class HandlerEmu extends Thread implements SerialIOListener {
00255
00256
00257
00258 private Stack<String> commandStack;
00259
00260
00261 private String lastMessagePart = "";
00262
00263 public HandlerEmu() {
00264
00265 handlerPort.addSerialIOListener(this);
00266 commandStack = new Stack<String>();
00267 }
00268
00269 public void run() {
00270 while (running) {
00271
00272 if (!commandStack.empty()) {
00273 String command = commandStack.pop();
00274 if (command.startsWith("V", 0)) {
00275 writeMessage("100" + command, handlerPort);
00276 } else if (command.startsWith("F", 0) || command.startsWith("%", 0)) {
00277 try {
00278 Thread.sleep(2000);
00279 } catch (InterruptedException ex1) {
00280 }
00281 writeMessage("perillä" + command, handlerPort);
00282 }
00283 }
00284 try {
00285 Thread.sleep(100);
00286 } catch (InterruptedException ex) {
00287 }
00288 }
00289 }
00290
00291 public void serialIOEvent(SerialIOEvent event) {
00292 int i;
00293 try {
00294 logWriter.write("HANDLER_RECIEVE:" + event.getCleanMessage() + "\n");
00295 logWriter.flush();
00296 } catch (IOException ex) {
00297 }
00298 commandStack.add(event.getCleanMessage());
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 }
00311 }
00312
00317 private static class MagnetometerEmu extends Thread implements SerialIOListener {
00318
00319 private Stack<String> commandStack;
00320
00321 public MagnetometerEmu() {
00322
00323 magnetometerPort.addSerialIOListener(this);
00324 commandStack = new Stack<String>();
00325 }
00326
00327
00328 public void run() {
00329 while (running) {
00330
00331 if (!commandStack.empty()) {
00332 String command = commandStack.pop();
00333 if (command.startsWith("SD", 1)) {
00334 writeMessage("+" + Math.random(), magnetometerPort);
00335 } else if (command.startsWith("SC", 1)) {
00336 writeMessage("+" + (int) (Math.random() * 100), magnetometerPort);
00337 } else {
00338 writeMessage("1", magnetometerPort);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348 }
00349 try {
00350 Thread.sleep(100);
00351 } catch (InterruptedException ex) {
00352 }
00353 }
00354
00355 }
00356
00357 public void serialIOEvent(SerialIOEvent event) {
00358 try {
00359 logWriter.write("MAGNETOMETER_RECIEVE:" + event.getCleanMessage() + "\n");
00360 logWriter.flush();
00361 } catch (IOException ex) {
00362 }
00363
00364 String message = event.getCleanMessage();
00365 String[] commands = message.split("/r");
00366
00367 if (commands[0].charAt(0) == 'A' || commands[0].charAt(0) == 'X' || commands[0].charAt(0) == 'Y' || commands[0].charAt(
00368 0) == 'Z') {
00369 commandStack.add(commands[0]);
00370 }
00371 }
00372 }
00373
00378 private static class DegausserEmu
00379 extends Thread implements SerialIOListener {
00380
00381
00382 private Stack<String> commandStack;
00383
00384 public DegausserEmu() {
00385
00386 degausserPort.addSerialIOListener(this);
00387 commandStack = new Stack<String>();
00388 }
00389
00390
00391 public void run() {
00392 while (running) {
00393
00394 if (!commandStack.empty()) {
00395 String command = commandStack.pop();
00396 if (command.startsWith("D", 0)) {
00397 writeMessage("DONE", degausserPort);
00398 }
00399 }
00400 try {
00401 Thread.sleep(100);
00402 } catch (InterruptedException ex) {
00403 }
00404 }
00405
00406 }
00407
00408 public void serialIOEvent(SerialIOEvent event) {
00409 try {
00410 logWriter.write("DEGAUSSER_RECIEVE:" + event.getCleanMessage() + "\n");
00411 logWriter.flush();
00412 } catch (IOException ex) {
00413 }
00414
00415 String message = event.getCleanMessage();
00416 String[] commands = message.split("/r");
00417
00418 if (commands[0].charAt(0) == 'D') {
00419 commandStack.add(commands[0]);
00420 }
00421 }
00422 }
00423 }