// this only checks for an affordance, if the desired device category is fullfilled by the devices // this selects a device from the list of previously selected and created devices public RealDevice GetDeviceDtoForAffordance(IAssignableDevice dev, IEnumerable <CalcDeviceDto> calcDevices, int locationID, ObservableCollection <DeviceAction> allDeviceActions, List <DeviceCategoryDto> deviceCategories) { switch (dev.AssignableDeviceType) { case AssignableDeviceType.Device: return((RealDevice)dev); case AssignableDeviceType.DeviceCategory: var dc = (DeviceCategory)dev; var deviceCategoryDto = deviceCategories.Single(x => x.FullCategoryName == dc.FullPath); var ldt = new LocationDeviceTuple(dc, locationID); if (_pickedDevices.ContainsKey(ldt) && _pickedDevices[ldt] != null) { return(_pickedDevices[ldt]); } var existingDevicesFromCategory = new List <CalcDeviceDto>(); foreach (var calcDevice in calcDevices) { if (calcDevice.DeviceCategoryGuid == deviceCategoryDto.Guid) { existingDevicesFromCategory.Add(calcDevice); } } if (existingDevicesFromCategory.Count > 0) { dc.RefreshSubDevices(); foreach (var realDevice in dc.SubDevices) { if (realDevice.Name == existingDevicesFromCategory[0].Name) { return(realDevice); } } throw new LPGException("One device should have been picked!"); } return(null); case AssignableDeviceType.DeviceAction: { var da = (DeviceAction)dev; return(da.Device); } case AssignableDeviceType.DeviceActionGroup: { var da = GetDeviceActionFromGroup(dev, calcDevices, allDeviceActions); return(da?.Device); } default: throw new LPGException("Missed a AssignableDeviceType"); } }
public DeviceAction GetAutoDeviceActionFromGroup(IAssignableDevice dev, List <IAssignableDevice> otherDevicesAtLocation, EnergyIntensityType energyIntensity, ObservableCollection <DeviceAction> allDeviceActions, int locationID) { if (dev == null) { throw new LPGException("Device was null"); } switch (dev.AssignableDeviceType) { case AssignableDeviceType.DeviceAction: return((DeviceAction)dev); case AssignableDeviceType.DeviceActionGroup: // schauen, ob schon zugeordnet var deviceActionGroup = (DeviceActionGroup)dev; var ldt = new LocationDeviceTuple(deviceActionGroup, locationID); if (_pickedDeviceActions.ContainsKey(ldt)) { return(_pickedDeviceActions[ldt]); } // pruefen, ob eins der anderen devices fuer diese group zutrifft var myActions = deviceActionGroup.GetDeviceActions(allDeviceActions); foreach (var deviceAction in myActions) { if (otherDevicesAtLocation.Contains(deviceAction)) { var action = (DeviceAction)otherDevicesAtLocation.First(anondev => anondev == deviceAction); _pickedDevices.Add(ldt, action.Device); return(action); } } // see if another device from the same device category was already picked // einfach eins aussuchen var pickdevAction = PickDeviceFromGroup(deviceActionGroup, energyIntensity, allDeviceActions, locationID); _pickedDeviceActions.Add(ldt, pickdevAction); // ReSharper disable once ConditionIsAlwaysTrueOrFalse // ReSharper disable once HeuristicUnreachableCode if (pickdevAction == null) { // ReSharper disable once HeuristicUnreachableCode throw new LPGException("Picked device was null"); } return(pickdevAction); default: throw new LPGException("Forgotten AssignableDeviceType in GetAutoDeviceDevice. Please Report!"); } }
public RealDevice GetAutoDeviceDeviceFromDeviceCategoryOrDevice(IAssignableDevice dev, List <IAssignableDevice> alldevices, EnergyIntensityType energyIntensity, ObservableCollection <DeviceAction> deviceActions, int locationID) { if (dev == null) { throw new LPGException("Device was null"); } // bereits ein realDevice switch (dev.AssignableDeviceType) { case AssignableDeviceType.Device: return((RealDevice)dev); case AssignableDeviceType.DeviceCategory: // schauen, ob schon zugeordnet var dc = (DeviceCategory)dev; var ldt = new LocationDeviceTuple(dc, locationID); if (_pickedDevices.ContainsKey(ldt)) { return(_pickedDevices[ldt]); } // pruefen, ob eins der anderen devices fuer diese category zutrifft foreach (var assignableDevice in alldevices) { if (assignableDevice.AssignableDeviceType == AssignableDeviceType.Device) { var rd = (RealDevice)assignableDevice; if (rd.DeviceCategory == dc) { _pickedDevices.Add(ldt, rd); return(rd); } } } // einfach eins aussuchen var pickdev = PickDeviceFromCategory(dc, energyIntensity, deviceActions); _pickedDevices.Add(ldt, pickdev); return(pickdev); default: throw new LPGException("Forgotten Assignable Device Type! Please Report."); } }
private DeviceAction PickDeviceFromGroup([NotNull] DeviceActionGroup deviceGroup, EnergyIntensityType energyIntensity, [NotNull][ItemNotNull] ObservableCollection <DeviceAction> allDeviceActions, int locationID) { var deviceActions = deviceGroup.GetDeviceActions(allDeviceActions); if (deviceActions.Count == 0) { throw new DataIntegrityException("The device action group " + deviceGroup.Name + " has no devices but it is used. A device could not be picked from it."); } if (deviceActions[0].Device == null) { throw new LPGException("Device was null"); } var usedDeviceCategory = deviceActions[0].Device.DeviceCategory; if (usedDeviceCategory == null) { throw new LPGException("usedDeviceCategory was null"); } var ldt2 = new LocationDeviceTuple(usedDeviceCategory, locationID); if (_pickedDevices.ContainsKey(ldt2)) { // find the device picked earlier var rd = _pickedDevices[ldt2]; deviceActions = deviceActions.Where(x => x.Device == rd).ToList(); if (deviceActions.Count == 0) { throw new DataIntegrityException( "The device action group " + deviceGroup.Name + " has no entry for the device " + rd.PrettyName + ". Please fix.", rd); } } DeviceAction pickedDevice = null; // look for this device group in the device selection if (_deviceSelection != null && _deviceSelection.Actions.Count > 0) { foreach (var item in _deviceSelection.Actions) { if (item.DeviceActionGroup == deviceGroup) { pickedDevice = item.DeviceAction; } } } if (pickedDevice == null) { switch (energyIntensity) { case EnergyIntensityType.Random: var dstval = _random.Next(deviceActions.Count); pickedDevice = deviceActions[dstval]; break; case EnergyIntensityType.EnergySaving: { var pickeDeviceAction = deviceActions[0]; foreach (var deviceAction in deviceActions) { if (pickeDeviceAction.CalculateWeightedEnergyUse() > deviceAction.CalculateWeightedEnergyUse()) { pickeDeviceAction = deviceAction; } } pickedDevice = pickeDeviceAction; } break; case EnergyIntensityType.EnergySavingPreferMeasured: { var actions = TryToGetMeasured(deviceActions); var pickeDeviceAction = actions[0]; foreach (var deviceAction in actions) { if (pickeDeviceAction.CalculateWeightedEnergyUse() > deviceAction.CalculateWeightedEnergyUse()) { pickeDeviceAction = deviceAction; } } pickedDevice = pickeDeviceAction; } break; case EnergyIntensityType.EnergyIntensive: { var pickeDeviceAction = deviceActions[0]; foreach (var deviceAction in deviceActions) { if (pickeDeviceAction.CalculateWeightedEnergyUse() < deviceAction.CalculateWeightedEnergyUse()) { pickeDeviceAction = deviceAction; } } pickedDevice = pickeDeviceAction; } break; case EnergyIntensityType.EnergyIntensivePreferMeasured: { var actions = TryToGetMeasured(deviceActions); var pickeDeviceAction = actions[0]; foreach (var deviceAction in deviceActions) { if (pickeDeviceAction.CalculateWeightedEnergyUse() < deviceAction.CalculateWeightedEnergyUse()) { pickeDeviceAction = deviceAction; } } pickedDevice = pickeDeviceAction; } break; default: throw new LPGException("Unknown EnergyIntensityType"); } } Logger.Debug( "Picked " + pickedDevice.Name + " from " + deviceActions.Count + " device actions in the group."); if (pickedDevice == null) { throw new LPGException("Picked device was null"); } return(pickedDevice); }
// this tries to double check if it's a device selection that's already been done public RealDevice GetOrPickDevice(IAssignableDevice dev, Location devLocation, EnergyIntensityType energyIntensity, List <IAssignableDevice> allDevLocations, ObservableCollection <DeviceAction> allDeviceActions) { // if it's already a device, we are done switch (dev.AssignableDeviceType) { case AssignableDeviceType.Device: return((RealDevice)dev); case AssignableDeviceType.DeviceCategory: { // check if we already dealt with this category at this Location var dc = (DeviceCategory)dev; var ldt = new LocationDeviceTuple(dc, devLocation.IntID); if (_pickedDevices.ContainsKey(ldt)) // && _pickedDevices[ldt] != null) { return(_pickedDevices[ldt]); } // pick a new one var result = PickRealDeviceFromCategoryAtHHLocation(dc, allDevLocations, energyIntensity, devLocation, allDeviceActions); if (result != null) { _pickedDevices.Add(ldt, result); } return(result); } case AssignableDeviceType.DeviceAction: return(((DeviceAction)dev).Device); case AssignableDeviceType.DeviceActionGroup: { // check if we already dealt with this group at this Location var deviceActionGroup = (DeviceActionGroup)dev; var ldt = new LocationDeviceTuple(deviceActionGroup, devLocation.IntID); if (_pickedDeviceActions.ContainsKey(ldt)) // && _pickedDevices[ldt] != null) { return(_pickedDeviceActions[ldt].Device); } var firstDeviceAction = deviceActionGroup.GetDeviceActions(allDeviceActions).First(); if (firstDeviceAction.Device == null) { throw new LPGException("Device was null"); } var usedDeviceCategory = firstDeviceAction.Device.DeviceCategory; if (usedDeviceCategory == null) { throw new LPGException("used device category was null"); } var ldt2 = new LocationDeviceTuple(usedDeviceCategory, devLocation.IntID); if (_pickedDevices.ContainsKey(ldt2)) { // find the device picked earlier var rd = _pickedDevices[ldt2]; return(rd); } // pick a new one var result = PickDeviceActionFromGroupAtHHLocation(deviceActionGroup, allDevLocations, energyIntensity, devLocation, allDeviceActions); _pickedDeviceActions.Add(ldt, result); if (result == null) { throw new LPGException("Device was null"); } _pickedDevices.Add(ldt2, result.Device); return(result.Device); } default: throw new LPGException("Forgotten AssignableDeviceType. Please report!"); } }