using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; using System.Reflection; using Event_Generator; using Lift_Controller; namespace LiftSimulatorForm { delegate void DisplayPictureBoxImageDelegate(PictureBox pb, Image im); delegate void DisplayButtonImageDelegate(Button b, Image im); delegate void SetButtonColorDelegate(Button b, Color c); public partial class LiftSimulatorForm : Form { public LiftSimulatorForm() { InitializeComponent(); // Initialize floor label postions floorLabelPositions = new int[numberOfFloors]; floorLabelPositions[0] = floor0Lab.Location.Y; floorLabelPositions[1] = floor1Lab.Location.Y; floorLabelPositions[2] = floor2Lab.Location.Y; floorLabelPositions[3] = floor3Lab.Location.Y; // Set up array of lift and floor buttons liftButtons = new Button[numberOfFloors]; liftButtons[0] = floor0Button; liftButtons[1] = floor1Button; liftButtons[2] = floor2Button; liftButtons[3] = floor3Button; floorUpButtons = new Button[numberOfFloors]; floorDownButtons=new Button[numberOfFloors]; floorUpButtons[0] = floor0UpButton; floorUpButtons[1] = floor1UpButton; floorUpButtons[2] = floor2UpButton; floorDownButtons[1] = floor1DownButton; floorDownButtons[2] = floor2DownButton; floorDownButtons[3] = floor3DownButton; // Set initial lift position and speed currentFloor = 0; currentPosition = floorLabelPositions[currentFloor]; liftSize = liftPanel.Size.Height; setLiftPosition(currentPosition); // Initialize labels floorLabel.Text = "Floor " + currentFloor; eventRateLabel.Text = "Mean Event Time " + meanLiftEventTime; eventCountLabel.Text = "Event Count " + eventCount; // Initialize the lift controller liftController = new LiftController(numberOfFloors,currentPosition,floorLabelPositions); liftController.LiftDelay = 1; // Set up and down lift and floor button request states floorUpButtonRequest = new bool[numberOfFloors]; floorDownButtonRequest = new bool[numberOfFloors]; liftButtonRequest = new bool[numberOfFloors]; for (int f = 0; f < numberOfFloors; f++) { floorUpButtonRequest[f] = false; floorDownButtonRequest[f] = false; liftButtonRequest[f] = false; } // Initialize image icons as embedded objects Assembly assembly; Stream imageStream; assembly = Assembly.GetExecutingAssembly(); string[] resNames = assembly.GetManifestResourceNames(); foreach (string s in resNames) { if (s.EndsWith(".GIF")) { // attach to stream to the resource in the manifest imageStream = assembly.GetManifestResourceStream(s); string[] split = s.Split(new Char[] {'.'}); if (split[1].Equals("blackDownArrow")) blackDownArrow = new Bitmap(imageStream); else if (split[1].Equals("blackUpArrow")) blackUpArrow = new Bitmap(imageStream); else if (split[1].Equals("redDownArrow")) redDownArrow = new Bitmap(imageStream); else if (split[1].Equals("redUpArrow")) redUpArrow = new Bitmap(imageStream); else if (split[1].Equals("goIcon")) goIcon = new Bitmap(imageStream); else if (split[1].Equals("pauseIcon")) pauseIcon = new Bitmap(imageStream); } } // Initialize lift threads liftControlThread = new Thread(new ParameterizedThreadStart(startControllerThread)); // Initialize lift event state change event handler liftEvState = new LiftEventState(); liftEvState.NewLiftEvent += new liftEventHandler(newLiftEventHandler); // Initialize the sensor state change event handler liftController.SensorStateChangeEvent += new sensorStateChangeDelegate(liftSensorStateChangeHandler); // Initialize the lift position change event handler liftController.PositionChangeEvent += new positionChangeDelegate(positionChangeHandler); // Initialize lift event generator liftEventGenerator = new LiftEventGenerator(numberOfFloors, meanLiftEventTime, liftEvState); } public void positionChangeHandler() { int position = liftController.CurrentPosition; if (position != currentPosition) { currentPosition = position; setLiftPosition(currentPosition); int floor = liftController.CurrentFloor; if (floor>=0) floorLabel.Text = "Floor " + floor; } } public void newLiftEventHandler(LiftEventState newLiftEventState) { // Determine the lift button which would have been pressed to interactively generate // the event Button button = null; Event_Generator.Direction dir= newLiftEventState.Dir; LiftEventType liftEventType = newLiftEventState.LiftEvType; int floor = newLiftEventState.Floor; if (liftEventType == LiftEventType.FloorButtonEvent) { if (dir == Event_Generator.Direction.Up) button = floorUpButtons[floor]; else if (dir == Event_Generator.Direction.Down) button = floorDownButtons[floor]; } else if (liftEventType == LiftEventType.LiftButtonEvent) button = liftButtons[floor]; handleEventRequest(button); } public void liftSensorStateChangeHandler() { // Direction sensors if (liftController.UpSensor.On) upPictureBox.Invoke(new DisplayPictureBoxImageDelegate(DisplayPictureBoxImage), new object[] { upPictureBox, redUpArrow }); else if (!liftController.UpSensor.On) upPictureBox.Invoke(new DisplayPictureBoxImageDelegate(DisplayPictureBoxImage), new object[] { upPictureBox, blackUpArrow }); if (liftController.DownSensor.On) downPictureBox.Invoke(new DisplayPictureBoxImageDelegate(DisplayPictureBoxImage), new object[] { downPictureBox, redDownArrow }); else if (!liftController.DownSensor.On) downPictureBox.Invoke(new DisplayPictureBoxImageDelegate(DisplayPictureBoxImage), new object[] { downPictureBox, blackDownArrow }); for (int f = 0; f < numberOfFloors; f++) { // Lift button sensors if (liftController.LiftButtonSensors[f].On) { liftButtons[f].Invoke(new SetButtonColorDelegate(SetButtonColor), new object[] { liftButtons[f], Color.Red }); } else liftButtons[f].Invoke(new SetButtonColorDelegate(SetButtonColor), new object[] { liftButtons[f], Color.Black }); // Floor button sensors if (liftController.FloorUpButtonSensors[f].On) { if (f < (numberOfFloors - 1)) floorUpButtons[f].Invoke(new DisplayButtonImageDelegate(DisplayButtonImage), new object[] { floorUpButtons[f], redUpArrow }); } else { if (f<(numberOfFloors-1)) floorUpButtons[f].Invoke(new DisplayButtonImageDelegate(DisplayButtonImage), new object[] { floorUpButtons[f], blackUpArrow }); } if (liftController.FloorDownButtonSensors[f].On) { if (f > 0) floorDownButtons[f].Invoke(new DisplayButtonImageDelegate(DisplayButtonImage), new object[] { floorDownButtons[f], redDownArrow }); } else { if (f > 0) floorDownButtons[f].Invoke(new DisplayButtonImageDelegate(DisplayButtonImage), new object[] { floorDownButtons[f], blackDownArrow }); } } } private void setLiftPosition(int position) { liftPanel.Top = position - liftSize; } private void DisplayPictureBoxImage(PictureBox pb, Image im) { pb.Image = im; } private void DisplayButtonImage(Button b, Image im) { b.Image = im; } private void SetButtonColor(Button b, Color c) { b.ForeColor = c; } private Thread liftControlThread; private int numberOfFloors = 4; private int eventCount = 0; private double meanLiftEventTime = 1.5; // Initial mean rate of 1.5 seconds between lift/floor button presses private int[] floorLabelPositions; private bool[] floorUpButtonRequest, floorDownButtonRequest, liftButtonRequest; private bool simulationRunning = false; private Button[] liftButtons, floorUpButtons, floorDownButtons; private int currentFloor, currentPosition, liftSize; private Image blackUpArrow, blackDownArrow, redUpArrow, redDownArrow; private Image pauseIcon, goIcon; private LiftController liftController; private LiftEventGenerator liftEventGenerator; private LiftEventState liftEvState; private void decreaseLiftSpeedButton_Click(object sender, EventArgs e) { liftController.LiftDelay *= 2; } private void increaseLiftSpeedButton_Click(object sender, EventArgs e) { if (liftController.LiftDelay > 1) liftController.LiftDelay /= 2; } private void startControllerThread(object obj) { int toFloor = (int)obj; liftController.startLift(toFloor); } private void handleEventRequest(object sender) { if (sender is Button) // Request generated interactively { Button floorButton = (Button)sender; // Find which button Button b = (Button)sender; int floor = -1; for (int f = 0; f < numberOfFloors; f++) { if (b == floorUpButtons[f]) { floor = f; if ((!liftController.FloorUpButtonSensors[floor].On) && (liftController.CurrentFloor != floor)) { // Set lift controller sensor states liftController.FloorUpButtonSensors[floor].On = true; eventCountLabel.Text = "Event Count " + eventCount++; if (!liftControlThread.IsAlive) { liftControlThread = new Thread(new ParameterizedThreadStart(startControllerThread)); liftControlThread.Start(floor); } } } else if (b == floorDownButtons[f]) { floor = f; if ((!liftController.FloorDownButtonSensors[floor].On) && (liftController.CurrentFloor != floor)) { floorDownButtonRequest[floor] = true; eventCountLabel.Text = "Event Count " + eventCount++; // Set lift controller sensor states liftController.FloorDownButtonSensors[floor].On = true; if (!liftControlThread.IsAlive) { liftControlThread = new Thread(new ParameterizedThreadStart(startControllerThread)); liftControlThread.Start(floor); } } } else if (b == liftButtons[f]) { floor = f; if ((!liftController.LiftButtonSensors[floor].On) && (liftController.CurrentFloor != floor)) { liftButtonRequest[floor] = true; eventCountLabel.Text = "Event Count " + eventCount++; // Set lift controller sensor states liftController.LiftButtonSensors[floor].On = true; if (!liftControlThread.IsAlive) { liftControlThread = new Thread(new ParameterizedThreadStart(startControllerThread)); liftControlThread.Start(floor); } } } } } } private void floor0Button_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor1Button_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor2Button_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor3Button_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor0UpButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor1UpButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor1DownButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor2UpButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor2DownButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void floor3DownButton_Click(object sender, EventArgs e) { handleEventRequest(sender); } private void decreaseEventRateButton_Click(object sender, EventArgs e) { if (meanLiftEventTime > 1) { meanLiftEventTime -= 0.5; eventRateLabel.Text = "Mean Event Time " + meanLiftEventTime; liftEventGenerator.MeanEventTime = meanLiftEventTime; } } private void increaseEventRateButton_Click(object sender, EventArgs e) { meanLiftEventTime += 0.5; eventRateLabel.Text = "Mean Event Time " + meanLiftEventTime; liftEventGenerator.MeanEventTime = meanLiftEventTime; } private void simulatorButton_Click(object sender, EventArgs e) { Button b = (Button)sender; if (!simulationRunning) { b.Image = pauseIcon; simulationRunning = true; liftEventGenerator.start(); } else { b.Image = goIcon; simulationRunning = false; liftEventGenerator.pause(); } } private void LiftSimulatorForm_FormClosing(object sender, FormClosingEventArgs e) { liftEventGenerator.closeEventThread(); liftControlThread.Abort(); } } }