PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/IpCamEmu.Core/ConfigurationStaff/ConfigurationHelper.cs

#
C# | 218 lines | 182 code | 23 blank | 13 comment | 13 complexity | eed68854010398d0fba0dc2ecba7b966 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Xml;
  7. using HDE.IpCamEmu.Core.MJpeg;
  8. using HDE.IpCamEmu.Core.Source;
  9. namespace HDE.IpCamEmu.Core.ConfigurationStaff
  10. {
  11. public static class ConfigurationHelper
  12. {
  13. /// <summary>
  14. /// Configuration approach specification:
  15. ///
  16. /// Configuration simplification.
  17. /// Configuration.xml should be searched in the following order.
  18. /// 1) command line argument -Configuration=path to configuration file (if any)
  19. /// Relative paths are resolved relative to current folder.
  20. /// 2) folder with application\Configuration.xml
  21. ///
  22. /// Relative paths in sources are resolved relative to source location.
  23. /// </summary>
  24. /// <param name="customConfiguration">Command line argument (or null)</param>
  25. /// <returns></returns>
  26. public static ServerSettingsBase[] Load(string customConfiguration)
  27. {
  28. string configurationToLoad;
  29. if (customConfiguration != null)
  30. {
  31. if (Path.IsPathRooted(customConfiguration))
  32. {
  33. configurationToLoad = customConfiguration;
  34. }
  35. else
  36. {
  37. configurationToLoad = new FileInfo(
  38. Path.Combine(
  39. Directory.GetCurrentDirectory(),
  40. customConfiguration))
  41. .FullName;
  42. }
  43. }
  44. else
  45. {
  46. configurationToLoad = new FileInfo(
  47. Path.Combine(
  48. Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  49. "Configuration.xml"))
  50. .FullName;
  51. }
  52. var result = new List<ServerSettingsBase>();
  53. var document = new XmlDocument();
  54. document.Load(configurationToLoad);
  55. var servers = document.SelectNodes("//Configuration/Servers/Server");
  56. var rootPath = Path.GetDirectoryName(configurationToLoad);
  57. foreach (XmlNode serverConfig in servers)
  58. {
  59. var configType = serverConfig.Attributes["Type"].Value.ToLowerInvariant();
  60. switch (configType)
  61. {
  62. case "mjpeg":
  63. result.Add(CreateMjpegConfig(serverConfig, rootPath));
  64. break;
  65. default:
  66. throw new ArgumentOutOfRangeException(configType);
  67. }
  68. }
  69. return result.ToArray();
  70. }
  71. private static ServerSettingsBase CreateMjpegConfig(XmlNode document, string rootPath)
  72. {
  73. return new MJpegServerSettings(
  74. document.SelectSingleNode("Uri").InnerText,
  75. uint.Parse(document.SelectSingleNode("FrameDelay").InnerText),
  76. CreateSource(document.SelectSingleNode("Source"), rootPath)
  77. );
  78. }
  79. private static SourceSettings CreateSource(XmlNode document, string rootPath)
  80. {
  81. var sourceType = document.SelectSingleNode("@Type").InnerText;
  82. SourceSettings result;
  83. switch (sourceType.ToLowerInvariant())
  84. {
  85. case "videofile":
  86. result = CreateVideoFileSettings(document, rootPath);
  87. break;
  88. case "folder":
  89. result = CreateFolderSettings(document, rootPath);
  90. break;
  91. case "webcam":
  92. result = CreateWebCamSettings(document);
  93. break;
  94. default:
  95. throw new ArgumentOutOfRangeException(sourceType);
  96. }
  97. result.ImageFormat = ParseImageFormat(document.SelectSingleNode("Format").InnerText);
  98. result.Name = document.SelectSingleNode("@Name").InnerText;
  99. return result;
  100. }
  101. private static SourceSettings CreateWebCamSettings(XmlNode document)
  102. {
  103. return new WebCamSettings
  104. {
  105. InstanciateMode = InstanciateMode.InstancePerServer, //!
  106. RotateY = bool.Parse(document.SelectSingleNode("RotateY").InnerText),
  107. InputVideoDeviceId = int.Parse(document.SelectSingleNode("InputVideoDeviceId").InnerText),
  108. Height = uint.Parse(document.SelectSingleNode("CameraRealHeight").InnerText),
  109. Width = uint.Parse(document.SelectSingleNode("CameraRealWidth").InnerText),
  110. ReadSpeed = uint.Parse(document.SelectSingleNode("ReadSpeed").InnerText),
  111. RegionOfInterest = ParseRegionOfInterest(document),
  112. };
  113. }
  114. private static SourceSettings CreateFolderSettings(XmlNode document, string rootPath)
  115. {
  116. return new FolderSettings()
  117. {
  118. InstanciateMode = ParseInstanciateMode(document.SelectSingleNode("InstanciateMode").InnerText),
  119. BufferFrames = uint.Parse(document.SelectSingleNode("BufferFrames").InnerText),
  120. Folder = ResolvePath(document.SelectSingleNode("Folder").InnerText, rootPath, true),
  121. RegionOfInterest = ParseRegionOfInterest(document),
  122. };
  123. }
  124. private static TimeSpan ParseTimeSpan(string text)
  125. {
  126. return TimeSpan.ParseExact(text, "G", CultureInfo.InvariantCulture);
  127. }
  128. private static SourceSettings CreateVideoFileSettings(XmlNode document, string rootPath)
  129. {
  130. return new VideoFileSettings
  131. {
  132. InstanciateMode = ParseInstanciateMode(document.SelectSingleNode("InstanciateMode").InnerText),
  133. RotateY = bool.Parse(document.SelectSingleNode("RotateY").InnerText),
  134. File = ResolvePath(document.SelectSingleNode("File").InnerText, rootPath, false),
  135. BufferFrames = uint.Parse(document.SelectSingleNode("BufferFrames").InnerText),
  136. TimeStart = ParseTimeSpan(document.SelectSingleNode("TimeStart").InnerText),
  137. TimeEnd = ParseTimeSpan(document.SelectSingleNode("TimeEnd").InnerText),
  138. TimeStep = ParseTimeSpan(document.SelectSingleNode("TimeStep").InnerText),
  139. RegionOfInterest = ParseRegionOfInterest(document)
  140. };
  141. }
  142. private static string ResolvePath(string path, string root, bool isPathToFolder)
  143. {
  144. string realPath = path;
  145. if (!Path.IsPathRooted(realPath))
  146. {
  147. realPath = Path.Combine(root, realPath);
  148. }
  149. return isPathToFolder ?
  150. new DirectoryInfo(realPath).FullName :
  151. new FileInfo(realPath).FullName;
  152. }
  153. private static InstanciateMode ParseInstanciateMode(string value)
  154. {
  155. if (string.Compare(value, InstanciateMode.InstancePerClient.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
  156. {
  157. return InstanciateMode.InstancePerClient;
  158. }
  159. if (string.Compare(value, InstanciateMode.InstancePerServer.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
  160. {
  161. return InstanciateMode.InstancePerServer;
  162. }
  163. throw new ArgumentOutOfRangeException(value);
  164. }
  165. private static Region ParseRegionOfInterest(XmlNode document)
  166. {
  167. var regionOfInterestNode = document.SelectSingleNode("RegionOfInterest");
  168. if (regionOfInterestNode == null)
  169. {
  170. return new Region();
  171. }
  172. return new Region(
  173. uint.Parse(regionOfInterestNode.Attributes["FromLine"].Value),
  174. uint.Parse(regionOfInterestNode.Attributes["FromColumn"].Value),
  175. uint.Parse(regionOfInterestNode.Attributes["Width"].Value),
  176. uint.Parse(regionOfInterestNode.Attributes["Height"].Value)
  177. );
  178. }
  179. private static IpCamEmuImageFormat ParseImageFormat(string format)
  180. {
  181. IpCamEmuImageFormat result;
  182. if (IpCamEmuImageFormat.TryParse(
  183. format,
  184. true,
  185. out result))
  186. {
  187. return result;
  188. }
  189. else
  190. {
  191. throw new ArgumentOutOfRangeException(format);
  192. }
  193. }
  194. }
  195. }