LogikalServer.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SQLite;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Threading;
  9. using InABox.Logikal;
  10. using Ofcas.Lk.Api.Client.Core;
  11. using Ofcas.Lk.Api.Shared;
  12. namespace PRSLogikal
  13. {
  14. public class LogikalLogArguments
  15. {
  16. public String Message { get; private set; }
  17. public LogikalLogArguments(string message)
  18. {
  19. Message = message;
  20. }
  21. }
  22. public delegate void LogikalLogEvent(object sender, LogikalLogArguments args);
  23. public class LogikalServer : IDisposable
  24. {
  25. //private readonly List<String> _log = new List<String>();
  26. //public String[] Log => _log.ToArray();
  27. public event LogikalLogEvent Log;
  28. private void DoLog(String message) => Log?.Invoke(this, new LogikalLogArguments(message));
  29. private IServiceProxyResult _proxy;
  30. private ICoreObjectResult<ILoginScope> _login;
  31. public LogikalStatus Connect(string path)
  32. {
  33. Disconnect();
  34. var _p = ServiceProxyFactory.CreateServiceProxy(path);
  35. var _status = _p.ServiceProxy.Start();
  36. if (_status.OperationCode != OperationCode.Accepted)
  37. {
  38. DoLog($"Unable to connect to Logikal at [{path}]: {_status}");
  39. return LogikalStatus.Error;
  40. }
  41. _proxy = _p;
  42. return LogikalStatus.Ok;
  43. }
  44. public LogikalStatus Disconnect()
  45. {
  46. Logout();
  47. if (_proxy != null)
  48. {
  49. _proxy.ServiceProxy.Stop();
  50. _proxy.Dispose();
  51. }
  52. _proxy = null;
  53. return LogikalStatus.Ok;
  54. }
  55. private void DoOnDisconnecting()
  56. {
  57. }
  58. public LogikalStatus Login(string username, string password)
  59. {
  60. Dictionary<string, object> _parameters = new Dictionary<string, object>()
  61. {
  62. { WellKnownParameterKey.Login.ProgramMode, "erp" },
  63. { WellKnownParameterKey.Login.UserName, username },
  64. { WellKnownParameterKey.Login.Password, password },
  65. };
  66. if (_proxy == null)
  67. {
  68. DoLog($"Logikal is not connected");
  69. return LogikalStatus.Error;
  70. }
  71. var _check = _proxy.ServiceProxy.CanLogin(_parameters);
  72. if (!_check.CanExecute)
  73. {
  74. DoLog($"Login not allowed: {_check}!");
  75. return LogikalStatus.Restricted;
  76. }
  77. try
  78. {
  79. var _l = _proxy.ServiceProxy.Login(_parameters);
  80. if (_l.OperationCode != OperationCode.Accepted)
  81. {
  82. DoLog($"Login failed: {_l}");
  83. _login = null;
  84. }
  85. else
  86. _login = _l;
  87. }
  88. catch (Exception e)
  89. {
  90. Log?.Invoke(this, new LogikalLogArguments($"{e.Message}\n{e.StackTrace}"));
  91. }
  92. return _login != null ? LogikalStatus.Ok : LogikalStatus.Failed;
  93. }
  94. public LogikalStatus Logout()
  95. {
  96. if (_login != null)
  97. _login.Dispose();
  98. _login = null;
  99. return LogikalStatus.Ok;
  100. }
  101. public bool IsLoggedIn() => _login != null;
  102. public IEnumerable<ILogikalProject> GetProjects()
  103. {
  104. List<ILogikalProject> _result = null;
  105. if (_login != null)
  106. {
  107. IProjectCenterInfo _info = _login.CoreObject.ProjectCenterInfos.FirstOrDefault();
  108. if (_info != null)
  109. {
  110. _result = new List<ILogikalProject>();
  111. using (ICoreObjectResult<IProjectCenter> _center = _login.CoreObject.GetProjectCenter(_info))
  112. {
  113. IList<IBaseProjectInfo> _projects = _center.CoreObject.ChildrenInfos;
  114. foreach (var _project in _projects)
  115. {
  116. var _summary = new LogikalProject()
  117. {
  118. ID = _project.Guid,
  119. Name = _project.Name,
  120. PersonInCharge = _project.PersonInCharge,
  121. Path = _project.Path
  122. };
  123. _result.Add(_summary);
  124. }
  125. }
  126. }
  127. else
  128. DoLog($"Cannot Retrieve Project List: No ProjectCenterInfo available");
  129. }
  130. else
  131. DoLog($"Cannot Retrieve Project List: Not Logged In");
  132. return _result;
  133. }
  134. public ILogikalProject GetProject(Guid projectid)
  135. {
  136. ILogikalProject _result = null;
  137. if (_login != null)
  138. {
  139. var _project = _login.CoreObject.GetProjectByGuid(projectid);
  140. if (_project != null)
  141. {
  142. _result = new LogikalProject()
  143. {
  144. ID = _project.CoreObject.Id,
  145. Name = _project.CoreObject.Info.Name
  146. };
  147. List<LogikalElevation> _elevations = new List<LogikalElevation>();
  148. ICoreObjectListResult<IPhase> phases = _project.CoreObject.GetChildren();
  149. foreach (ICoreObjectResult<IPhase> phase in phases.CoreObjectResults)
  150. {
  151. ICoreObjectListResult<IElevation> elevations = phase.CoreObject.GetChildren();
  152. foreach (ICoreObjectResult<IElevation> elevation in elevations.CoreObjectResults)
  153. {
  154. var _summary = new LogikalElevation()
  155. {
  156. ID = elevation.CoreObject.Id,
  157. Name = elevation.CoreObject.Info.Name,
  158. Phase = phase.CoreObject.Info.Name
  159. };
  160. using (var ms = new MemoryStream())
  161. {
  162. IStreamResult thumbnail =
  163. elevation.CoreObject.GetThumbnail(new Dictionary<string, object>() { });
  164. thumbnail.Stream.CopyTo(ms);
  165. _summary.Thumbnail = ms.GetBuffer();
  166. }
  167. _elevations.Add(_summary);
  168. }
  169. }
  170. }
  171. else
  172. DoLog($"Cannot Load Project {projectid}");
  173. }
  174. else
  175. {
  176. DoLog($"Cannot Load Project: Not Logged In");
  177. }
  178. return _result;
  179. }
  180. public ILogikalElevation GetElevation(Guid elevationid)
  181. {
  182. LogikalElevation _result = null;
  183. if (_login != null)
  184. {
  185. var _project = _login.CoreObject.GetProjectFromElevation(elevationid);
  186. if (_project != null)
  187. {
  188. ICoreObjectListResult<IPhase> children = _project.CoreObject.GetChildren();
  189. foreach (ICoreObjectResult<IPhase> child in children.CoreObjectResults)
  190. {
  191. ICoreObjectResult<IElevation> elevation = child.CoreObject.GetChildren().CoreObjectResults.FirstOrDefault(x=>x.CoreObject.Id == elevationid);
  192. if (elevation != null)
  193. {
  194. _result = new LogikalElevation()
  195. {
  196. ID = elevation.CoreObject.Id,
  197. Name = elevation.CoreObject.Info.Name
  198. };
  199. using (var ms = new MemoryStream())
  200. {
  201. IDrawingResult thumbnail =
  202. elevation.CoreObject.GetDrawing(new Dictionary<string, object>() { });
  203. thumbnail.Stream.CopyTo(ms);
  204. _result.Drawing = ms.GetBuffer();
  205. }
  206. IStreamResult parts = elevation.CoreObject.GetPartsList();
  207. var file = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
  208. using (var fs = new FileStream(file, FileMode.OpenOrCreate))
  209. parts.Stream.CopyTo(fs);
  210. var sb = new SQLiteConnectionStringBuilder();
  211. sb.DataSource = file;
  212. var _connection = new SQLiteConnection(sb.ToString());
  213. _connection.Open();
  214. // Parse the file here
  215. _connection.Close();
  216. File.Delete(file);
  217. }
  218. }
  219. }
  220. else
  221. DoLog($"Cannot Load Project from Elevation {elevationid}");
  222. }
  223. else
  224. DoLog($"Cannot Retrieve Project List: Not Logged In");
  225. return _result;
  226. }
  227. public void Dispose()
  228. {
  229. Disconnect();
  230. }
  231. }
  232. }