/// <inheritdoc /> public SimpleHttpResponseMessage SendTestEmail(Stream postData) { NameValueCollection postKeyValuePairs; string recipientsCsv, username, password; SimpleHttpResponseMessage result; result = new SimpleHttpResponseMessage(); result.Message = "success"; // convert the postData into a collection postKeyValuePairs = ConvertPostStreamIntoNameValueCollection(postData); username = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "username"); password = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "password"); if (!UsernameAndPasswordAreCorrect(username, password)) { result.Message = "unauthorized"; return result; } // get recipient emails recipientsCsv = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "warning-email-recipients-csv"); Emailer emailer = new Emailer(); emailer.SendTestEmail(recipientsCsv); return result; }
/// <inheritdoc /> public SimpleHttpResponseMessage ReceiveSystemSettingsFromUserInterface(Stream postData) { string romId, friendlyName, username, password; int temperatureThreshold, counter; NameValueCollection postKeyValuePairs; SimpleHttpResponseMessage result; // assume that the save will be succesful, for now result = new SimpleHttpResponseMessage(); result.Message = "success"; // parse the application/x-www-form-urlencoded POST data into a collection postKeyValuePairs = ConvertPostStreamIntoNameValueCollection(postData); username = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "username"); password = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "password"); if (!UsernameAndPasswordAreCorrect(username, password)) { result.Message = "unauthorized"; return result; } // save the system wide settings, of which there are two SystemSettings systemSettings = new SystemSettings(); systemSettings.DataStoreDurationInDays = Convert.ToInt32(GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "data-store-duration-in-days")); systemSettings.HoursThatMustPassBetweenSendingDeviceSpecificWarningEmails = Convert.ToInt32(GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "max-email-freq-in-hours")); systemSettings.WarningEmailRecipientsInCsv = GetValueFromNameValueCollectionAndThenRemoveItToo(postKeyValuePairs, "warning-email-recipients-csv"); // instantiate empty variables in preparation for saving device specific settings romId = String.Empty; friendlyName = String.Empty; temperatureThreshold = Int32.MaxValue; // save the device specific settings, of which there are two for each device // a romId is the key for each device counter = 0; foreach (string key in postKeyValuePairs.AllKeys) { if (key.ToLowerInvariant().Contains("rom-id")) { counter = 0; romId = postKeyValuePairs[key]; } else if (key.ToLowerInvariant().Contains("friendly-name")) { ++counter; friendlyName = postKeyValuePairs[key]; } else if (key.ToLowerInvariant().Contains("temp-threshold")) { ++counter; temperatureThreshold = Convert.ToInt32(postKeyValuePairs[key]); } if (counter > 0 && counter % DeviceSettings.SYSTEM_SETTINGS_DEVICE_DATA_COUNT == 0) { DeviceSettings deviceSettings = new DeviceSettings() { FriendlyName = friendlyName, TemperatureThreshold = temperatureThreshold }; if (systemSettings.DeviceSettingsDictionary.ContainsKey(romId)) { systemSettings.DeviceSettingsDictionary[romId] = deviceSettings; } else { systemSettings.DeviceSettingsDictionary.Add(romId, new DeviceSettings() { FriendlyName = friendlyName, TemperatureThreshold = temperatureThreshold }); } } } systemSettings.Save(); return result; }