public void Load() { if (!File.Exists(ConfigFile)) { return; } XmlDocument document = new XmlDocument(); document.Load(ConfigFile); XmlElement rootElem = document["Configuration"]; if (rootElem == null) { return; } TimerSetting = Convert.ToInt32(LoadAttributeValue(rootElem, "TimerSetting", "Value")); PartSize = Convert.ToInt32(LoadAttributeValue(rootElem, "PartSize", "Value")); MeteoInfoFile = LoadElementValue(rootElem, "MeteoInfoFile"); string fontName = LoadAttributeValue(rootElem, "MeteoInfoFont", "Name"); float size = Convert.ToSingle(LoadAttributeValue(rootElem, "MeteoInfoFont", "Size")); MeteoInfoFont = new Font(fontName, size); MeteoInfoFontColor = Color.FromName(LoadAttributeValue(rootElem, "MeteoInfoFontColor", "Color")); MeteoInfoOutlineColor = Color.FromName(LoadAttributeValue(rootElem, "MeteoInfoOutlineColor", "Color")); MeteoInfoOutlineWidth = Convert.ToInt32(LoadAttributeValue(rootElem, "MeteoInfoOutlineWidth", "Value")); Hostname = LoadAttributeValue(rootElem, "Hostname", "Value"); Username = LoadAttributeValue(rootElem, "Username", "Value"); Password = LoadAttributeValue(rootElem, "Password", "Value"); XmlElement cameraListNode = rootElem["CameraList"]; if (cameraListNode == null) { return; } XmlNodeList list = cameraListNode.GetElementsByTagName("Camera"); for (int i = 0; i < list.Count; ++i) { XmlElement elem = list.Item(i) as XmlElement; if (elem == null) { continue; } CameraSettings settings = new CameraSettings() { Name = elem.GetAttribute("Name"), SourceFolder = LoadElementValue(elem, "SourceFolder"), Identifier = LoadElementValue(elem, "Identifier"), TargetFolder = LoadElementValue(elem, "TargetFolder"), TargetFile = LoadElementValue(elem, "TargetFile") }; CameraSettingsList.Add(settings); } }
public void ProcessRequestAsync(CameraSettings cameraSettings, CancellationToken token) { Task.Factory.StartNew(() => { try { token.ThrowIfCancellationRequested(); if (IsSending(cameraSettings.Name)) { LogCameraMessage(cameraSettings.Name, "stále probíhá odesílání"); return; } string baseFolder = GetBaseFolder(cameraSettings.SourceFolder, cameraSettings.Identifier); LogCameraMessage(cameraSettings.Name, string.Format("prohledávám složku '{0}'.", baseFolder)); string filePath = FindRecentFile(baseFolder); if (!CheckRecentFile(cameraSettings.Name, filePath)) { LogCameraMessage(cameraSettings.Name, "žádná nová fotka"); return; } token.ThrowIfCancellationRequested(); string targetFile = !string.IsNullOrEmpty(cameraSettings.TargetFile) ? cameraSettings.TargetFile : Path.GetFileName(filePath); string tempFile = Path.GetTempFileName(); LogCameraMessage(cameraSettings.Name, "vytvářím fotku k odeslání"); CreateModifiedFile(filePath, tempFile); LogCameraMessage(cameraSettings.Name, "načítám data do bufferu"); byte[] buffer = ReadToBuffer(tempFile); token.ThrowIfCancellationRequested(); LogCameraMessage(cameraSettings.Name, "odesílám fotku na server"); #if DEBUG SaveToFile(cameraSettings.TargetFolder, targetFile, buffer); #else Send(cameraSettings.TargetFolder, targetFile, buffer); #endif LogCameraMessage(cameraSettings.Name, "fotka odeslána"); File.Delete(tempFile); } catch (WebException) { LogCameraMessage(cameraSettings.Name, "nepodařilo se odeslat data na FTP server"); LogCameraMessage(cameraSettings.Name, "chyba při odeslání dat na FTP server, zkontrolujte parametry připojení a existenci cílové složky na FTP serveru"); } catch (UriFormatException e) { LogCameraMessage(cameraSettings.Name, "nepodařilo se odeslat data na FTP server"); LogCameraMessage(cameraSettings.Name, String.Format("Chyba v URI: {0}", e.Message)); } catch (Exception e) { LogCameraMessage(cameraSettings.Name, "nepodařilo se odeslat data na FTP server"); LogCameraMessage(cameraSettings.Name, String.Format("neznámá chyba při odesílání dat: '{0}'", e.Message)); } finally { ResetCamera(cameraSettings.Name); } }, token); }