/
MachineController.java
847 lines (730 loc) · 24 KB
/
MachineController.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
Part of the ReplicatorG project - http://www.replicat.org
Copyright (c) 2008 Zach Smith
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package replicatorg.app;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import replicatorg.app.exceptions.BuildFailureException;
import replicatorg.app.exceptions.GCodeException;
import replicatorg.app.exceptions.JobCancelledException;
import replicatorg.app.exceptions.JobEndException;
import replicatorg.app.exceptions.JobException;
import replicatorg.app.exceptions.JobRewindException;
import replicatorg.app.tools.XML;
import replicatorg.app.ui.MainWindow;
import replicatorg.drivers.Driver;
import replicatorg.drivers.DriverFactory;
import replicatorg.drivers.EstimationDriver;
import replicatorg.drivers.OnboardParameters;
import replicatorg.drivers.SDCardCapture;
import replicatorg.drivers.SimulationDriver;
import replicatorg.machine.MachineListener;
import replicatorg.machine.MachineProgressEvent;
import replicatorg.machine.MachineState;
import replicatorg.machine.MachineStateChangeEvent;
import replicatorg.machine.MachineToolStatusEvent;
import replicatorg.machine.model.MachineModel;
import replicatorg.machine.model.ToolModel;
import replicatorg.model.GCodeSource;
import replicatorg.model.StringListSource;
/**
* The MachineController object controls a single machine. It contains a single
* machine driver object. All machine operations (building, stopping, pausing)
* are performed asynchronously by a thread maintained by the MachineController;
* calls to MachineController ordinarily trigger an operation and return
* immediately.
*
* @author phooky
*
*/
public class MachineController {
private MachineState state = new MachineState();
public MachineState getMachineState() { return state.clone(); }
private void setState(MachineState state) {
MachineState oldState = this.state;
this.state = state;
if (!oldState.equals(state)) {
emitStateChange(oldState,state);
// wake up machine thread
synchronized(machineThread) {
machineThread.notify();
}
}
}
private void setState(MachineState.State state) {
MachineState newState = getMachineState();
newState.setState(state);
setState(newState);
}
// Build statistics
private int linesProcessed = -1;
private int linesTotal = -1;
private double startTimeMillis = -1;
/**
* The MachineThread is responsible for communicating with the machine.
*
* @author phooky
*
*/
class MachineThread extends Thread {
private long lastPolled = 0;
private boolean pollingEnabled = false;
private long pollIntervalMs = 1000;
synchronized void startStatusPolling(long interval) {
pollingEnabled = true;
pollIntervalMs = interval;
}
synchronized void stopStatusPolling() {
pollingEnabled = false;
}
/**
* Run the warmup commands.
*
* @throws BuildFailureException
* @throws InterruptedException
*/
private void runWarmupCommands() throws BuildFailureException, InterruptedException {
Base.logger.info("Running warmup commands.");
buildCodesInternal(new StringListSource(warmupCommands));
}
private void runCooldownCommands() throws BuildFailureException, InterruptedException {
Base.logger.info("Running cooldown commands.");
buildCodesInternal(new StringListSource(cooldownCommands));
}
// Indicates that the driver has control of the thread. On a stop or abort, we will want to
// interrupt the driver explicitly by interrupting the thread, so we need to understand when
// we're in this code.
private boolean inDriver = false;
// Interrupt any current driver call.
// The driver implementation should be smart enough to check for interrupted status when not blocking
// on IO, and handle interruptedexceptions by dumping out.
public void interruptDriver() {
synchronized(driver) {
if (inDriver) {
this.interrupt();
}
}
}
private boolean buildCodesInternal(GCodeSource source) throws BuildFailureException, InterruptedException {
if (!state.isBuilding()) {
// Do not continue build if the machine is not building or paused
return false;
}
// Flush any parser cached data
driver.getParser().init(driver);
Iterator<String> i = source.iterator();
while (i.hasNext()) {
String line = i.next();
linesProcessed++;
if (Thread.interrupted()) {
Base.logger.info("build thread interrupted");
return false;
}
// use our parser to handle the stuff.
if (simulator.isSimulating())
simulator.parse(line);
if (!state.isSimulating()) { driver.parse(line); }
try {
GCodeParser.StopInfo info = driver.getParser().getStops();
if (info != null &&
Base.preferences.getBoolean("machine.optionalstops",true) &&
state.isBuilding() &&
state.isInteractiveTarget()) {
JobException e = info.getException();
if (info.isOptional()) {
int result = JOptionPane.showConfirmDialog(null, info.getMessage(),
"Continue Build?", JOptionPane.YES_NO_OPTION);
if (result != JOptionPane.YES_OPTION) {
e = info.getCancelException();
}
} else {
JOptionPane.showMessageDialog(null, info.getMessage(),
"Build stop", JOptionPane.INFORMATION_MESSAGE);
}
if (e != null) {
throw e;
}
}
} catch (JobEndException e) {
return false;
} catch (JobCancelledException e) {
return false;
} catch (JobRewindException e) {
i = source.iterator();
continue;
} catch (JobException e) {
Base.logger.severe("Unknown job exception emitted");
}
// simulate the command.
if (simulator.isSimulating())
simulator.execute();
try {
if (!state.isSimulating()) {
// The inDriver bracketing is to avoid
// interrupting this thread outside of the
// call to the driver code.
synchronized(driver) {
inDriver = true;
}
driver.execute();
synchronized(driver) {
inDriver = false;
}
}
} catch (GCodeException e) {
// TODO: prompt the user to continue.
Base.logger.severe("Error: " + e.getMessage());
} catch (InterruptedException ie) {
// We're in the middle of a stop or shutdown
inDriver = false;
}
// did we get any errors?
if (!state.isSimulating()) {
driver.checkErrors();
}
// are we paused?
if (state.isPaused()) {
if (!state.isSimulating()) driver.pause();
while (state.isPaused()) {
synchronized(this) { wait(); }
}
if (!state.isSimulating()) driver.unpause();
}
// bail if we got interrupted.
if (state.getState() == MachineState.State.STOPPING) {
driver.stop();
return false;
}
// send out updates
if (pollingEnabled) {
long curMillis = System.currentTimeMillis();
if (lastPolled + pollIntervalMs <= curMillis) {
lastPolled = curMillis;
pollStatus();
}
}
MachineProgressEvent progress =
new MachineProgressEvent((double)System.currentTimeMillis()-startTimeMillis,
estimatedBuildTime,
linesProcessed,
linesTotal);
emitProgress(progress);
}
// wait for driver to finish up.
if (!state.isSimulating()) while (!driver.isFinished()) {
Thread.sleep(100);
}
return true;
}
/**
* Reset machine to its basic state.
*
*/
private synchronized void resetInternal() {
driver.getMachine().currentTool().setTargetTemperature(0);
driver.getMachine().currentTool().setPlatformTargetTemperature(0);
driver.reset();
}
public boolean isReady() { return state.isReady(); }
public void pollStatus() {
if (state.isBuilding()) {
if (Base.preferences.getBoolean("build.monitor_temp",false)) {
driver.readTemperature();
emitToolStatus(driver.getMachine().currentTool());
}
}
}
public void forceReset() {
setState(new MachineState(MachineState.State.NOT_ATTACHED));
interruptDriver();
resetInternal();
}
public void reset() {
forceReset();
connect();
}
public void connect() {
setState(new MachineState(MachineState.State.CONNECTING));
}
GCodeSource currentSource;
private void buildInternal(GCodeSource source) {
startTimeMillis = System.currentTimeMillis();
linesProcessed = 0;
linesTotal = warmupCommands.size() +
cooldownCommands.size() +
source.getLineCount();
startStatusPolling(1000);
try {
if (!state.isSimulating()) {
driver.getCurrentPosition(); // reconcile position
}
runWarmupCommands();
Base.logger.info("Running build.");
buildCodesInternal(source);
runCooldownCommands();
if (!state.isSimulating()) {
driver.invalidatePosition();
}
setState(new MachineState(MachineState.State.READY));
} catch (BuildFailureException e) {
JOptionPane.showMessageDialog(null, e.getMessage(),
"Build Failure", JOptionPane.ERROR_MESSAGE);
} catch (InterruptedException e) {
Base.logger.warning("MachineController interrupted");
} finally {
stopStatusPolling();
}
}
String remoteName = null;
private void buildRemoteInternal(String remoteName) {
if (remoteName == null || !(driver instanceof SDCardCapture)) return;
driver.getCurrentPosition(); // reconcille position
SDCardCapture sdcc = (SDCardCapture)driver;
if (!processSDResponse(sdcc.playback(remoteName))) {
setState(MachineState.State.STOPPING);
return;
}
while (!driver.isFinished()) {
try {
// are we paused?
if (state.isPaused()) {
driver.pause();
while (state.isPaused()) {
synchronized(this) { wait(); }
}
driver.unpause();
}
// bail if we got interrupted.
if (state.getState() == MachineState.State.STOPPING) {
driver.stop();
return;
}
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
driver.invalidatePosition();
setState(new MachineState(MachineState.State.READY));
}
public void build(GCodeSource source) {
currentSource = source;
setState(new MachineState(MachineState.State.BUILDING,MachineState.Target.MACHINE));
}
public void simulate(GCodeSource source) {
currentSource = source;
setState(new MachineState(MachineState.State.BUILDING,MachineState.Target.SIMULATOR));
}
public void upload(GCodeSource source, String remoteName) {
currentSource = source;
this.remoteName = remoteName;
setState(new MachineState(MachineState.State.BUILDING,MachineState.Target.SD_UPLOAD));
}
public void buildToFile(GCodeSource source, String path) {
currentSource = source;
this.remoteName = path;
setState(new MachineState(MachineState.State.BUILDING,MachineState.Target.FILE));
}
public void buildRemote(String remoteName) {
this.remoteName = remoteName;
setState(MachineState.State.PLAYBACK);
}
public void pauseBuild() {
if (state.isBuilding() && !state.isPaused()) {
MachineState newState = getMachineState();
newState.setPaused(true);
setState(newState);
}
}
public void resumeBuild() {
if (state.isBuilding() && state.isPaused()) {
MachineState newState = getMachineState();
newState.setPaused(false);
setState(newState);
}
}
public void stopBuild() {
driver.getMachine().currentTool().setTargetTemperature(0);
driver.getMachine().currentTool().setPlatformTargetTemperature(0);
if (state.isBuilding()) {
setState(MachineState.State.STOPPING);
interruptDriver();
}
}
public void shutdown() {
running = false;
stopBuild();
synchronized(this) { notify(); }
}
private boolean running = true;
public void run() {
while (running) {
try {
if (state.getState() == MachineState.State.BUILDING) {
if (state.getTarget() == MachineState.Target.SD_UPLOAD) {
if (driver instanceof SDCardCapture) {
SDCardCapture sdcc = (SDCardCapture)driver;
if (processSDResponse(sdcc.beginCapture(remoteName))) {
buildInternal(currentSource);
Base.logger.info("Captured bytes: " +Integer.toString(sdcc.endCapture()));
} else { setState(MachineState.State.STOPPING); }
} else {
setState(MachineState.State.STOPPING);
}
} else if (state.getTarget() == MachineState.Target.FILE) {
if (driver instanceof SDCardCapture) {
SDCardCapture sdcc = (SDCardCapture)driver;
try {
sdcc.beginFileCapture(remoteName);
buildInternal(currentSource);
sdcc.endFileCapture();
} catch (Exception e) {
e.printStackTrace();
}
} else {
setState(MachineState.State.STOPPING);
}
} else {
// Ordinary build
buildInternal(currentSource);
}
} else if (state.getState() == MachineState.State.PLAYBACK) {
buildRemoteInternal(remoteName);
} else if (state.getState() == MachineState.State.CONNECTING) {
driver.initialize();
if (driver.isInitialized()) {
readName();
setState(MachineState.State.READY);
} else {
setState(MachineState.State.NOT_ATTACHED);
}
} else if (state.getState() == MachineState.State.STOPPING) {
setState(MachineState.State.READY);
} else {
synchronized(this) {
if (state.getState() == MachineState.State.READY ||
state.getState() == MachineState.State.NOT_ATTACHED ||
state.getState() == MachineState.State.PLAYBACK ||
state.isPaused()) {
wait();
} else {
}
}
}
} catch (InterruptedException ie) {
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void readName() {
if (driver instanceof OnboardParameters) {
String n = ((OnboardParameters)driver).getMachineName();
if (n != null && n.length() > 0) {
name = n;
}
}
}
MachineThread machineThread = new MachineThread();
// The GCode source of the current build source.
protected GCodeSource source;
// this is the xml config for this machine.
protected Node machineNode;
// The name of our machine.
protected String name;
public String getName() { return name; }
// Our driver object. Null when no driver is selected.
public Driver driver = null;
// the simulator driver
protected SimulationDriver simulator;
// our current thread.
protected Thread thread;
// estimated build time in millis
protected double estimatedBuildTime = 0;
// our warmup/cooldown commands
protected Vector<String> warmupCommands;
protected Vector<String> cooldownCommands;
/**
* Creates the machine object.
*/
public MachineController(Node mNode) {
// save our XML
machineNode = mNode;
parseName();
Base.logger.info("Loading machine: " + name);
// load our various objects
loadDriver();
loadExtraPrefs();
machineThread = new MachineThread();
machineThread.start();
}
public void setCodeSource(GCodeSource source) {
this.source = source;
}
// TODO: hide this behind an API
private MainWindow window; // for responses to errors, etc.
public void setMainWindow(MainWindow window) { this.window = window; }
static Map<SDCardCapture.ResponseCode,String> sdErrorMap =
new EnumMap<SDCardCapture.ResponseCode,String>(SDCardCapture.ResponseCode.class);
{
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_NO_CARD,
"No SD card was detected. Please make sure you have a working, formatted\n" +
"SD card in the motherboard's SD slot and try again.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_INIT,
"ReplicatorG was unable to initialize the SD card. Please make sure that\n" +
"the SD card works properly.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_PARTITION,
"ReplicatorG was unable to read the SD card's partition table. Please check\n" +
"that the card is partitioned properly.\n" +
"If you believe your SD card is OK, try resetting your device and restarting\n" +
"ReplicatorG."
);
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_FS,
"ReplicatorG was unable to open the filesystem on the SD card. Please make sure\n" +
"that the SD card has a single partition formatted with a FAT16 filesystem.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_ROOT_DIR,
"ReplicatorG was unable to read the root directory on the SD card. Please\n"+
"check to see if the SD card was formatted properly.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_LOCKED,
"The SD card cannot be written to because it is locked. Remove the card,\n" +
"switch the lock off, and try again.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_NO_FILE,
"ReplicatorG could not find the build file on the SD card.");
sdErrorMap.put(SDCardCapture.ResponseCode.FAIL_GENERIC,"Unknown SD card error.");
}
/**
* Process an SD response code and throw up an appropriate dialog for the user.
* @param code the response from the SD request
* @return true if the code indicates success; false if the operation should be aborted
*/
public boolean processSDResponse(SDCardCapture.ResponseCode code) {
if (code == SDCardCapture.ResponseCode.SUCCESS) return true;
String message = sdErrorMap.get(code);
JOptionPane.showMessageDialog(
window,
message,
"SD card error",
JOptionPane.ERROR_MESSAGE);
return false;
}
private void parseName() {
NodeList kids = machineNode.getChildNodes();
for (int j = 0; j < kids.getLength(); j++) {
Node kid = kids.item(j);
if (kid.getNodeName().equals("name")) {
name = kid.getFirstChild().getNodeValue().trim();
return;
}
}
name = "Unknown";
}
public boolean buildRemote(String remoteName) {
machineThread.buildRemote(remoteName);
return true;
}
/**
* Begin running a job.
*/
public boolean execute() {
// start simulator
if (simulator != null && Base.preferences.getBoolean("build.showSimulator",false))
simulator.createWindow();
// estimate build time.
Base.logger.info("Estimating build time...");
estimate();
// do that build!
Base.logger.info("Beginning build.");
machineThread.build(source);
return true;
}
public boolean simulate() {
// start simulator
if (simulator != null)
simulator.createWindow();
// estimate build time.
Base.logger.info("Estimating build time...");
estimate();
// do that build!
Base.logger.info("Beginning simulation.");
machineThread.simulate(source);
return true;
}
public void estimate() {
if (source == null) { return; }
try {
EstimationDriver estimator = new EstimationDriver();
estimator.setMachine(loadModel());
// run each line through the estimator
for (String line : source) {
// parse only if line is NOT a Twitterbot M code
if ((line.indexOf(GCodeParser.TB_CODE + Integer.toString(GCodeParser.TB_INIT)) == -1) &&
(line.indexOf(GCodeParser.TB_CODE + Integer.toString(GCodeParser.TB_MESSAGE)) == -1) &&
(line.indexOf(GCodeParser.TB_CODE + Integer.toString(GCodeParser.TB_CLEANUP)) == -1)) {
// use our parser to handle the stuff.
estimator.parse(line);
estimator.execute();
}
}
if (simulator != null) {
simulator.setSimulationBounds(estimator.getBounds());
}
// oh, how this needs to be cleaned up...
if (driver instanceof SimulationDriver) {
((SimulationDriver)driver).setSimulationBounds(estimator.getBounds());
}
estimatedBuildTime = estimator.getBuildTime();
Base.logger.info("Estimated build time is: "
+ EstimationDriver.getBuildTimeString(estimatedBuildTime));
} catch (InterruptedException e) {
assert (false);
// Should never happen
}
}
private MachineModel loadModel() {
MachineModel model = new MachineModel();
model.loadXML(machineNode);
return model;
}
private void loadDriver() {
// load our utility drivers
if (Base.preferences.getBoolean("machinecontroller.simulator",true)) {
Base.logger.info("Loading simulator.");
simulator = new SimulationDriver();
simulator.setMachine(loadModel());
}
Node driverXml = null;
// load our actual driver
NodeList kids = machineNode.getChildNodes();
for (int j = 0; j < kids.getLength(); j++) {
Node kid = kids.item(j);
if (kid.getNodeName().equals("driver")) {
driverXml = kid;
}
}
driver = DriverFactory.factory(driverXml);
driver.setMachine(loadModel());
// Initialization is now handled by the machine thread when it
// is placed in a connecting state.
}
private void loadExtraPrefs() {
String[] commands = null;
String command = null;
warmupCommands = new Vector<String>();
if (XML.hasChildNode(machineNode, "warmup")) {
String warmup = XML.getChildNodeValue(machineNode, "warmup");
commands = warmup.split("\n");
for (int i = 0; i < commands.length; i++) {
command = commands[i].trim();
warmupCommands.add(new String(command));
// System.out.println("Added warmup: " + command);
}
}
cooldownCommands = new Vector<String>();
if (XML.hasChildNode(machineNode, "cooldown")) {
String cooldown = XML.getChildNodeValue(machineNode, "cooldown");
commands = cooldown.split("\n");
for (int i = 0; i < commands.length; i++) {
command = commands[i].trim();
cooldownCommands.add(new String(command));
// System.out.println("Added cooldown: " + command);
}
}
}
public Driver getDriver() {
return driver;
}
public SimulationDriver getSimulatorDriver() {
return simulator;
}
MachineModel cachedModel = null;
public MachineModel getModel() {
if (cachedModel == null) { cachedModel = loadModel(); }
return cachedModel;
}
synchronized public void stop() {
machineThread.stopBuild();
}
synchronized public boolean isInitialized() {
return (driver != null && driver.isInitialized());
}
synchronized public void pause() {
machineThread.pauseBuild();
}
synchronized public void upload(String remoteName) {
machineThread.upload(source, remoteName);
}
synchronized public void buildToFile(String path) {
machineThread.buildToFile(source, path);
}
synchronized public void unpause() {
machineThread.resumeBuild();
}
synchronized public void reset() {
machineThread.reset();
}
synchronized public void connect() {
machineThread.connect();
}
synchronized public boolean isPaused() {
return getMachineState().isPaused();
}
public void dispose() {
if (machineThread != null) {
machineThread.shutdown();
try {
machineThread.join(5000);
} catch (Exception e) { e.printStackTrace(); }
}
if (driver != null) {
driver.dispose();
}
if (getSimulatorDriver() != null) {
getSimulatorDriver().dispose();
}
driver.dispose();
setState(new MachineState(MachineState.State.NOT_ATTACHED));
}
private Vector<MachineListener> listeners = new Vector<MachineListener>();
public void addMachineStateListener(MachineListener listener) {
listeners.add(listener);
listener.machineStateChanged(new MachineStateChangeEvent(this,getMachineState()));
}
public void removeMachineStateListener(MachineListener listener) {
listeners.remove(listener);
}
protected void emitStateChange(MachineState prev, MachineState current) {
MachineStateChangeEvent e = new MachineStateChangeEvent(this, current, prev);
for (MachineListener l : listeners) {
l.machineStateChanged(e);
}
}
protected void emitProgress(MachineProgressEvent progress) {
for (MachineListener l : listeners) {
l.machineProgress(progress);
}
}
protected void emitToolStatus(ToolModel tool) {
MachineToolStatusEvent e = new MachineToolStatusEvent(this, tool);
for (MachineListener l : listeners) {
l.toolStatusChanged(e);
}
}
}