123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- 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<String> _log = new List<String>();
- //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<ILoginScope> _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<string, object> _parameters = new Dictionary<string, object>()
- {
- { 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<ILogikalProject> GetProjects()
- {
- List<ILogikalProject> _result = null;
- if (_login != null)
- {
- IProjectCenterInfo _info = _login.CoreObject.ProjectCenterInfos.FirstOrDefault();
- if (_info != null)
- {
- _result = new List<ILogikalProject>();
- using (ICoreObjectResult<IProjectCenter> _center = _login.CoreObject.GetProjectCenter(_info))
- {
- IList<IBaseProjectInfo> _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<LogikalElevation> _elevations = new List<LogikalElevation>();
- ICoreObjectListResult<IPhase> phases = _project.CoreObject.GetChildren();
- foreach (ICoreObjectResult<IPhase> phase in phases.CoreObjectResults)
- {
- ICoreObjectListResult<IElevation> elevations = phase.CoreObject.GetChildren();
- foreach (ICoreObjectResult<IElevation> 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<string, object>() { });
- 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<IPhase> children = _project.CoreObject.GetChildren();
- foreach (ICoreObjectResult<IPhase> child in children.CoreObjectResults)
- {
- ICoreObjectResult<IElevation> 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<string, object>() { });
- 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();
- }
- }
- }
|