//Changes the status of a door //Gets device by its id if exists, checks if device is a door, sets a state to true or false dependig on changeType //If device does not exist, throws DeviceDoesNotExistException //If device is not a door, throws InvalidCastException public void ChangeDoorStatus(int changeType, int deviceId) { AbsDevice device = GetDeviceById(deviceId); if (device == null) { throw new DeviceDoesNotExistException(); } if (device.Type == (AbsDevice.Types) 2) { Door door = (Door)device; switch (changeType) { case 0: door.Locked = false; break; case 1: door.Locked = true; break; case 2: door.Open = true; break; case 3: door.Open = false; break; case 4: door.OpenForTooLong = true; break; case 5: door.OpenForTooLong = false; break; case 6: door.OpenedForcibly = true; break; case 7: door.OpenedForcibly = false; break; } } else { throw new InvalidCastException(); } }
//Moves a device into a group //Finds a device by its id if exists, removes the device if exists, adds the device into ListOfGroups[newGroupIndex], calls OnPrintTree //If device does not exist, throws DeviceDoesNotExistException public void Move(int deviceId, int newGroupIndex) { AbsDevice device = GetDeviceById(deviceId); RemoveDevice(deviceId, true); ListOfGroups[newGroupIndex].ListOfDevices.Add(device); TreeToBePrinted = PrintTreeEvent.OnPrintTree; OnPrintTree(this, EventArgs.Empty); }
//Prints the status of a device //Finds a device by its id if exists, returns device.GetCurrentState() //If device does not exist, throws DeviceDoesNotExistException public string PrintStatus(int deviceId) { AbsDevice device = GetDeviceById(deviceId); if (device == null) { throw new DeviceDoesNotExistException(); } return(device.GetCurrentState()); }
//Whenever a device is updated, prints the status of the device into the console public void OnDeviceUpdated(object source, EventArgs e) { AbsDevice absDevice = (AbsDevice)source; Console.WriteLine(absDevice.GetCurrentState()); }
//Changes a property of a device //Finds a device by its id if exists, checks if the device has a property that is to be changed, changes property //If device does not exist, throws DeviceDoesNotExistException //If device is not of a correct type, throws InvalidCastException() public void Change(int changeType, int deviceId, dynamic property) { AbsDevice device = GetDeviceById(deviceId); if (device == null) { throw new DeviceDoesNotExistException(); } switch (changeType) { case 0: device.Id = int.Parse(property); break; case 1: device.Name = property; break; case 2: { if (device.Type == (AbsDevice.Types) 1) { CardReader cardReader = (CardReader)device; cardReader.AccessCardNumber = property; } else { throw new InvalidCastException(); } } break; case 3: { if (device.Type == (AbsDevice.Types) 3) { LedPanel ledPanel = (LedPanel)device; ledPanel.Message = property; } else { throw new InvalidCastException(); } } break; case 4: { if (device.Type == (AbsDevice.Types) 4) { Speaker speaker = (Speaker)device; if (property is string) { property = int.Parse(property); } speaker.Sound = (Speaker.Sounds)property; } else { throw new InvalidCastException(); } } break; case 5: { if (device.Type == (AbsDevice.Types) 4) { Speaker speaker = (Speaker)device; if (property is string) { property = float.Parse(property); } speaker.Volume = property; } else { throw new InvalidCastException(); } } break; } }