using System; using System.Collections.Generic; using System.Data.SQLite; using System.Dynamic; using System.IO; using System.Linq; using System.Windows; using System.Windows.Threading; using InABox.Logikal; using Ofcas.Lk.Api.Client.Core; using Ofcas.Lk.Api.Shared; namespace PRSLogikal { public class LogikalLogArguments { public String Message { get; private set; } public LogikalLogArguments(string message) { Message = message; } } public delegate void LogikalLogEvent(object sender, LogikalLogArguments args); public class LogikalServer : IDisposable { //private readonly List _log = new List(); //public String[] Log => _log.ToArray(); public event LogikalLogEvent Log; private void DoLog(String message) => Log?.Invoke(this, new LogikalLogArguments(message)); private IServiceProxyResult _proxy; private ICoreObjectResult _login; public LogikalStatus Connect(string path) { Disconnect(); var _p = ServiceProxyFactory.CreateServiceProxy(path); var _status = _p.ServiceProxy.Start(); if (_status.OperationCode != OperationCode.Accepted) { DoLog($"Unable to connect to Logikal at [{path}]: {_status}"); return LogikalStatus.Error; } _proxy = _p; return LogikalStatus.Ok; } public LogikalStatus Disconnect() { Logout(); if (_proxy != null) { _proxy.ServiceProxy.Stop(); _proxy.Dispose(); } _proxy = null; return LogikalStatus.Ok; } private void DoOnDisconnecting() { } public LogikalStatus Login(string username, string password) { Dictionary _parameters = new Dictionary() { { WellKnownParameterKey.Login.ProgramMode, "erp" }, { WellKnownParameterKey.Login.UserName, username }, { WellKnownParameterKey.Login.Password, password }, }; if (_proxy == null) { DoLog($"Logikal is not connected"); return LogikalStatus.Error; } var _check = _proxy.ServiceProxy.CanLogin(_parameters); if (!_check.CanExecute) { DoLog($"Login not allowed: {_check}!"); return LogikalStatus.Restricted; } try { var _l = _proxy.ServiceProxy.Login(_parameters); if (_l.OperationCode != OperationCode.Accepted) { DoLog($"Login failed: {_l}"); _login = null; } else _login = _l; } catch (Exception e) { Log?.Invoke(this, new LogikalLogArguments($"{e.Message}\n{e.StackTrace}")); } return _login != null ? LogikalStatus.Ok : LogikalStatus.Failed; } public LogikalStatus Logout() { if (_login != null) _login.Dispose(); _login = null; return LogikalStatus.Ok; } public bool IsLoggedIn() => _login != null; public IEnumerable GetProjects() { List _result = null; if (_login != null) { IProjectCenterInfo _info = _login.CoreObject.ProjectCenterInfos.FirstOrDefault(); if (_info != null) { _result = new List(); using (ICoreObjectResult _center = _login.CoreObject.GetProjectCenter(_info)) { IList _projects = _center.CoreObject.ChildrenInfos; foreach (var _project in _projects) { var _summary = new LogikalProject() { ID = _project.Guid, Name = _project.Name, PersonInCharge = _project.PersonInCharge, Path = _project.Path }; _result.Add(_summary); } } } else DoLog($"Cannot Retrieve Project List: No ProjectCenterInfo available"); } else DoLog($"Cannot Retrieve Project List: Not Logged In"); return _result; } public ILogikalProject GetProject(Guid projectid) { ILogikalProject _result = null; if (_login != null) { var _project = _login.CoreObject.GetProjectByGuid(projectid); if (_project != null) { _result = new LogikalProject() { ID = _project.CoreObject.Id, Name = _project.CoreObject.Info.Name }; List _elevations = new List(); ICoreObjectListResult phases = _project.CoreObject.GetChildren(); foreach (ICoreObjectResult phase in phases.CoreObjectResults) { ICoreObjectListResult elevations = phase.CoreObject.GetChildren(); foreach (ICoreObjectResult elevation in elevations.CoreObjectResults) { var _summary = new LogikalElevation() { ID = elevation.CoreObject.Id, Name = elevation.CoreObject.Info.Name, Phase = phase.CoreObject.Info.Name }; using (var ms = new MemoryStream()) { IStreamResult thumbnail = elevation.CoreObject.GetThumbnail(new Dictionary() { }); thumbnail.Stream.CopyTo(ms); _summary.Thumbnail = ms.GetBuffer(); } _elevations.Add(_summary); } } } else DoLog($"Cannot Load Project {projectid}"); } else { DoLog($"Cannot Load Project: Not Logged In"); } return _result; } public ILogikalElevation GetElevation(Guid elevationid) { LogikalElevation _result = null; if (_login != null) { var _project = _login.CoreObject.GetProjectFromElevation(elevationid); if (_project != null) { ICoreObjectListResult children = _project.CoreObject.GetChildren(); foreach (ICoreObjectResult child in children.CoreObjectResults) { ICoreObjectResult elevation = child.CoreObject.GetChildren().CoreObjectResults.FirstOrDefault(x=>x.CoreObject.Id == elevationid); if (elevation != null) { _result = new LogikalElevation() { ID = elevation.CoreObject.Id, Name = elevation.CoreObject.Info.Name }; using (var ms = new MemoryStream()) { IDrawingResult thumbnail = elevation.CoreObject.GetDrawing(new Dictionary() { }); thumbnail.Stream.CopyTo(ms); _result.Drawing = ms.GetBuffer(); } IStreamResult parts = elevation.CoreObject.GetPartsList(); var file = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); using (var fs = new FileStream(file, FileMode.OpenOrCreate)) parts.Stream.CopyTo(fs); var sb = new SQLiteConnectionStringBuilder(); sb.DataSource = file; var _connection = new SQLiteConnection(sb.ToString()); _connection.Open(); // Parse the file here _connection.Close(); File.Delete(file); } } } else DoLog($"Cannot Load Project from Elevation {elevationid}"); } else DoLog($"Cannot Retrieve Project List: Not Logged In"); return _result; } public void Dispose() { Disconnect(); } } }