/// <summary> /// Returns the list of <see cref="Leds"/> which are member of the <see cref="Group"/> having the specified <see cref="Group.Id"/>. /// The methods throws a <see cref="ResourceNotFoundException"/> if the underlying <see cref="Group"/> does not exist. /// </summary> /// <param name="id"><see cref="Group.Id"/></param> /// <returns><see cref="GroupLeds"/> containing the <see cref="Leds"/> being member of the <see cref="Group"/></returns> public GroupLeds GetGroupLeds(int id) { IGroupDataSet gds = _groupStorage.GetGroup(id); if (gds != null) { GroupLeds gLeds = new GroupLeds(); gLeds.GroupId = gds.Id; foreach (string ledId in gds.Leds) { ILedDataSet lds = _ledStorage.GetLed(ledId); if (lds != null) { Led led = new Led(); led.ControllerId = lds.ControllerId; led.LedNumber = lds.LedNumber; led.RgbValue = lds.RgbValue; gLeds.Leds.Add(led); } } return(gLeds); } else { throw new ResourceNotFoundException(ResourceNotFoundException.MSG_GROUP_NOT_FOUND.Replace("{VALUE}", id + "")); } }
/// <summary> /// Initializes this <see cref="FileBasedGroupStorage"/> and reads all <see cref="GroupDataSetJson"/> from the home directory as well as the current <see cref="IdCounter"/>. /// </summary> /// <param name="homeDirectory"></param> public void Initialize(string homeDirectory) { _homeDirectory = homeDirectory; if (!Directory.Exists(homeDirectory)) { Directory.CreateDirectory(homeDirectory); File.WriteAllText(_homeDirectory + "ID.config", 0 + ""); } _groups = new List <IGroupDataSet>(); //load all groups foreach (string file in Directory.EnumerateFiles(_homeDirectory, "*.json")) { string json = File.ReadAllText(file); IGroupDataSet gds = JsonSerializer.DeserializeJson <GroupDataSetJson>(json); if (gds != null) { _groups.Add(gds); } } //load ID counter string id = File.ReadAllText(_homeDirectory + "ID.config"); _idCounter = Int32.Parse(id); }
public void ClearLeds(int id) { IGroupDataSet gds = GetGroup(id); if (gds == null) { throw new Exception("Group not found"); } else { gds.Leds.Clear(); SaveToDisk(gds); } }
public void SetName(int id, string name) { IGroupDataSet gds = GetGroup(id); if (gds == null) { throw new Exception("Group not found"); } else { gds.Name = name; SaveToDisk(gds); } }
/// <summary> /// Returns the <see cref="Group"/> having the passed <see cref="Group.Id"/>. /// The methods throws a <see cref="ResourceNotFoundException"/> if the requested <see cref="Group"/> does not exist. /// </summary> /// <param name="id"><see cref="Group.Id"/></param> /// <returns>the <see cref="Group"/> having the passed ID</returns> public Group GetGroup(int id) { IGroupDataSet gds = _groupStorage.GetGroup(id); if (gds != null) { Group group = new Group(); group.Id = gds.Id; group.Name = gds.Name; return(group); } else { throw new ResourceNotFoundException(ResourceNotFoundException.MSG_GROUP_NOT_FOUND.Replace("{VALUE}", id + "")); } }
public void SetLeds(int id, IList <string> ledIds) { IGroupDataSet gds = GetGroup(id); if (gds == null) { throw new Exception("Group not found"); } else { gds.Leds.Clear(); foreach (string ledId in ledIds) { gds.Leds.Add(ledId); } SaveToDisk(gds); } }
/// Sets the color of all <see cref="Leds"/> being member of the <see cref="Group"/> having the specified <see cref="Group.Id"/>. /// The methods throws a <see cref="ResourceNotFoundException"/> if the underlying <see cref="Group"/> does not exist. /// </summary> /// <param name="id"></param> /// <param name="rgbValue"></param> public void SetColorOfGroup(int id, string rgbValue) { IGroupDataSet gds = _groupStorage.GetGroup(id); if (gds != null) { foreach (string ledId in gds.Leds) { if (_ledStorage.HasLed(ledId)) { _ledStorage.SetColor(ledId, rgbValue); } } } else { throw new ResourceNotFoundException(ResourceNotFoundException.MSG_GROUP_NOT_FOUND.Replace("{VALUE}", id + "")); } }
public void RemoveLed(int id, string ledId) { IGroupDataSet gds = GetGroup(id); if (gds == null) { throw new Exception("Group not found"); } else { for (int i = 0; i < gds.Leds.Count; i++) { if (gds.Leds[i].CompareTo(ledId) == 0) { gds.Leds.RemoveAt(i); SaveToDisk(gds); return; } } } }
public void AddLed(int id, string ledId) { IGroupDataSet gds = GetGroup(id); if (gds == null) { throw new Exception("Group not found"); } else { foreach (string lId in gds.Leds) { if (ledId.CompareTo(lId) == 0) { throw new Exception("LED with ID: " + ledId + " is already member of the group with ID: " + id); } } gds.Leds.Add(ledId); SaveToDisk(gds); } }
/// <summary> /// Deletes the <see cref="IGroupDataSet"/> from disk. /// </summary> /// <param name="data"></param> private void RemoveFromDisk(IGroupDataSet data) { File.Delete(_homeDirectory + data.Id + ".json"); }
/// <summary> /// Saves the passed <see cref="IGroupDataSet"/> to disk. /// </summary> /// <param name="data"></param> private void SaveToDisk(IGroupDataSet data) { File.WriteAllText(_homeDirectory + data.Id + ".json", JsonSerializer.SerializeJson((GroupDataSetJson)data)); }