private void emptyTruck() { partsLoaded = 0; myState = MyState.Unloading; cfLead.msgConveyorReady(this); partGroupCount = partGroupEndOfConveyor = 0; transducer.fireEvent(TChannel.TRUCK, TEvent.TRUCK_DO_EMPTY, null); }
/** * Contstructor for SimpleConveyorAgent * * @param cfLead conveyor family that comes before this one * @param transducer transducer that allows communication with the GUI * @param index the index of the conveyor within the factory * @param name name of the conveyor */ public ConveyorAgentWithTruck( ConveyorFamily cfLead, Transducer transducer, Integer index, String name) { super(name); this.cfLead = cfLead; parts = new LinkedList<Part>(); this.transducer = transducer; myIndex = new Integer(index); myState = MyState.Normal; readyMessageTimer = new Timer(200, this); startConveyorTimer = new Timer(800, this); readyMessageTimer.setRepeats(false); startConveyorTimer.setRepeats(false); this.transducer.register(this, TChannel.SENSOR); this.transducer.register(this, TChannel.CONVEYOR); this.transducer.register(this, TChannel.TRUCK); Object[] args = new Object[1]; args[0] = myIndex; this.transducer.fireEvent(TChannel.CONVEYOR, TEvent.CONVEYOR_DO_START, args); cfLead.msgConveyorReady(this); }
/** * This method is called when the startConveyorTimer fires. The purpose of the timer is to spread * out the parts to prevent a sensor from thinking that two parts are one. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == startConveyorTimer) conveyorRestart = false; cfLead.msgConveyorReady(this); }
/** * This method is called by the transducer in order for the front end to communicate with the * agent */ public void eventFired(TChannel channel, TEvent event, Object[] args) { if (channel == TChannel.CONVEYOR && myIndex.equals((Integer) args[0])) { if (event == TEvent.CONVEYOR_BROKEN) { conveyorBroken = true; System.err.println(getName() + " is broken"); stateChanged(); } else if (event == TEvent.CONVEYOR_FIXED) { conveyorBroken = false; System.out.println(getName() + " is fixed"); stateChanged(); } } else if (channel == TChannel.SENSOR && (myIndex.equals((Integer) args[0] / 2))) { if ((Integer) args[0] % 2 == 0) { if (event == TEvent.SENSOR_FIXED) { sensorBroken = false; cfLead.msgConveyorPartReceived(this); cfLead.msgConveyorReady(this); System.out.println( "Sensor " + (myIndex * 2) + " is reported as fixed\n" + getName() + " is resuming normal operation"); partGroupCount++; stateChanged(); } else if (event == TEvent.SENSOR_GUI_PRESSED) { cfLead.msgConveyorPartReceived(this); stateChanged(); } else if (event == TEvent.SENSOR_GUI_RELEASED) { if (++partGroupCount < groupSize) { if (!conveyorRestart) readyMessageTimer.start(); } } } else if ((Integer) args[0] % 2 == 1) { if (event == TEvent.SENSOR_FIXED) { sensorBroken = false; System.out.println( "Sensor " + (myIndex * 2 + 1) + " is reported as fixed\n" + getName() + " is resuming normal operation"); partGroupEndOfConveyor++; stateChanged(); } else if (event == TEvent.SENSOR_GUI_PRESSED) { partGroupEndOfConveyor++; followSensorDepressed = true; stateChanged(); } else if (event == TEvent.SENSOR_GUI_RELEASED) { followSensorDepressed = false; stateChanged(); } } } else if (channel == TChannel.TRUCK) { if (event == TEvent.TRUCK_BROKEN) { truckBroken = true; System.err.println("Truck broken"); stateChanged(); } else if (event == TEvent.TRUCK_FIXED) { truckBroken = false; System.out.println("Truck fixed"); stateChanged(); } else if (event == TEvent.TRUCK_GUI_EMPTY_FINISHED) { myState = MyState.Normal; stateChanged(); } else if (event == TEvent.TRUCK_GUI_LOAD_FINISHED) { parts.remove(); if (++partsLoaded == groupSize) { myState = MyState.DoneLoading; } stateChanged(); } } }