public byte[] ProcessMessage(string msg, string authorization) { if (!_controllerStorage.HasControllerByUuid(authorization)) { throw new UnauthorizedAccessException(); } else { string[] split = msg.Split('#'); if (split.Length != 6) { throw new BadRequestException("Invalid message format: Unexpected count of parameter"); } else { if (split[0].CompareTo("HELLO") != 0) { throw new BadRequestException("Invalid message format: Invalid command"); } if (split[1].CompareTo(authorization) != 0) { throw new BadRequestException("Invalid message format: Authorization mismatch"); } string uuId = authorization; string deviceName = split[2]; string firmwareId = split[3]; int minorVersion; int majorVersion; if (!Int32.TryParse(split[4], out minorVersion) || !Int32.TryParse(split[5], out majorVersion)) { throw new BadRequestException("Invalid message format: Minor/Major version not convertible"); } _controllerStorage.SetFirmware(uuId, minorVersion, majorVersion, firmwareId, deviceName); //return preferred color //return Encoding.ASCII.GetBytes("_NOPE"); int id = _controllerStorage.GetControllerByUuId(uuId).Id; IList <ILedDataSet> leds = _ledStorage.GetAllLedsOfController(id); //prepare the response message byte[] responseMsg = new byte[(leds.Count * 4)]; for (int i = 0; i < leds.Count; i++) { byte[] val = ConvertRgbValue(leds[i].RgbValue); responseMsg[(i * 4)] = (byte)i; responseMsg[(i * 4) + 1] = val[0]; responseMsg[(i * 4) + 2] = val[1]; responseMsg[(i * 4) + 3] = val[2]; } return(responseMsg); } } }
/// <summary> /// Returns an instance of <see cref="ControllerLeds"/> encompassing all <see cref="Led"/>s of the controller having the passed <see cref="Controller.Id"/>. /// The method throws an <see cref="ResourceNotFoundException"/> if the specified <see cref="Controller"/> does not exists. /// </summary> /// <param name="id"><see cref="Controller.Id"/> of the controller</param> /// <returns>instance of <see cref="ControllerLeds"/> encompassing the Leds</returns> public ControllerLeds GetLedsOfController(int id) { if (!_controllerStorage.HasControllerById(id)) { throw new ResourceNotFoundException(ResourceNotFoundException.MSG_CONTROLLER_NOT_FOUND.Replace("{VALUE}", id + "")); } ControllerLeds controllerLeds = new ControllerLeds(); controllerLeds.ControllerId = id; foreach (ILedDataSet lds in _ledStorage.GetAllLedsOfController(id)) { Led led = new Led(); led.ControllerId = lds.ControllerId; led.LedNumber = lds.LedNumber; led.RgbValue = lds.RgbValue; controllerLeds.Leds.Add(led); } return(controllerLeds); }