| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- using InABox.Logikal;
- using Ofcas.Lk.Api.Client.Core;
- using Ofcas.Lk.Api.Client.Ui;
- using Ofcas.Lk.Api.Shared;
- using System;
- using System.Collections.Generic;
- using System.Data.SQLite;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- 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
- {
- public event LogikalLogEvent Log;
- private void DoLog(String message) => Log?.Invoke(this, new LogikalLogArguments(message));
-
- private IServiceProxyUiResult _proxy;
- private ICoreObjectResult<ILoginScope> _login;
- public IntPtr WindowHandle { get; private set; }
- public LogikalServer(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- public LogikalResponse Connect(LogikalConnectRequest request)
- {
- if (_proxy != null)
- return new LogikalConnectResponse();
- // Check that LogiKal is actually running from the folder we have specified
- var _driveLetter = Path.GetPathRoot(request.Path)?.Split(':').FirstOrDefault()?.ToLower() ?? "c";
- var _processes = Process.GetProcessesByName("LogiKal");
- var _running = _processes.Any(x => x.MainModule?.FileName.ToLower().Contains($"{_driveLetter}\\common\\bin\\logikal.exe") == true);
- if (!_running)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.NotRunning,
- Message = $"LogiKal is not running at [{request.Path}]"
- };
- }
- var _p = ServiceProxyUiFactory.CreateServiceProxy(request.Path, "erp");
- var _status = _p.ServiceProxyUi.Start();
- if (_status.OperationCode != OperationCode.Accepted)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.CannotConnect,
- Message = $"Unable to connect to LogiKal at [{request.Path}]: {_status}"
- };
- }
- _proxy = _p;
- return new LogikalConnectResponse();
- }
- public LogikalResponse Disconnect()
- {
- if (_login != null)
- Logout();
- if (_proxy != null)
- {
- _proxy.ServiceProxyUi.Stop();
- _proxy.Dispose();
- }
- _proxy = null;
- return new LogikalDisconnectResponse();
- }
- private void DoOnDisconnecting()
- {
-
- }
- public LogikalResponse Login(LogikalLoginRequest request)
- {
- Dictionary<string, object> _parameters = new Dictionary<string, object>()
- {
- { WellKnownParameterKey.Login.ProgramMode, "erp" },
- { WellKnownParameterKey.Login.ApplicationHandle, WindowHandle },
- //{ WellKnownParameterKey.Login.UserName, username },
- //{ WellKnownParameterKey.Login.Password, password },
- };
- if (_proxy == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Disconnected,
- Message = $"LogiKal is not connected"
- };
- }
- if (_login != null)
- return new LogikalLoginResponse();
- var _check = _proxy.ServiceProxyUi.CanLogin(_parameters);
- if (!_check.CanExecute)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Restricted,
- Message = $"Login not allowed: {_check}!"
- };
- }
- try
- {
- var _l = _proxy.ServiceProxyUi.Login(_parameters);
- if (_l.OperationCode != OperationCode.Accepted)
- {
- _login = null;
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Failed,
- Message = $"Login failed: {_l}"
- };
- }
- else
- {
- _login = _l;
- return new LogikalLoginResponse();
- }
- }
- catch (Exception e)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Error,
- Message = $"{e.Message}\n{e.StackTrace}"
- };
- }
-
- }
- public LogikalResponse Logout()
- {
- if (_login != null)
- _login.Dispose();
- _login = null;
- return new LogikalLogoutResponse();
- }
- public bool IsLoggedIn() => _login != null;
- public LogikalResponse GetProjects(LogikalProjectsRequest request)
- {
- if (_proxy == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Disconnected,
- Message = $"LogiKal is not connected"
- };
- }
- if (_login == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.NotLoggedIn,
- Message = $"Not Logged In"
- };
- }
- List<LogikalProject> _result = null;
- IProjectCenterInfo _info = _login.CoreObject.ProjectCenterInfos.FirstOrDefault();
- if (_info != null)
- {
- _result = new List<LogikalProject>();
- 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,
- LastUpdated = _project.LastChangedDateTime,
- Created = _project.CreatedDateTime
- };
- _result.Add(_summary);
- }
- return new LogikalProjectsResponse<LogikalProject,LogikalElevation,LogikalPart>() { Projects = _result.ToArray() };
- }
- }
- else
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.NoProjectCenter,
- Message = $"Cannot Retrieve Project List: No ProjectCenterInfo available"
- };
- }
- }
-
- public LogikalResponse GetProject(LogikalProjectRequest request)
- {
- if (_proxy == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Disconnected,
- Message = $"LogiKal is not connected"
- };
- }
- if (_login == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.NotLoggedIn,
- Message = $"Not Logged In"
- };
- }
- LogikalProject _result = null;
- var _project = _login.CoreObject.GetProjectByGuid(request.ID);
- 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);
- }
- }
- return new LogikalProjectResponse<LogikalProject,LogikalElevation,LogikalPart>() { Project = _result };
- }
- else
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.InvalidProjectID,
- Message = $"Cannot Load Project {request.ID}"
- };
- }
- }
- public LogikalResponse GetElevation(LogikalElevationRequest request)
- {
- if (_proxy == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.Disconnected,
- Message = $"LogiKal is not connected"
- };
- }
- if (_login == null)
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.NotLoggedIn,
- Message = $"Not Logged In"
- };
- }
- LogikalElevation _result = null;
- var _project = _login.CoreObject.GetProjectFromElevation(request.ID);
- 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 == request.ID);
- 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);
- return new LogikalElevationResponse<LogikalElevation,LogikalPart>() { Elevation = _result };
- }
- }
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.ElevationNotFound,
- Message = $"Elevation Not Found {request.ID}"
- };
- }
- else
- {
- return new LogikalErrorResponse()
- {
- Status = LogikalStatus.InvalidElevationID,
- Message = $"Cannot Load Project from Elevation {request.ID}"
- };
- }
- }
-
-
- public void Dispose()
- {
- Disconnect();
- }
- }
- }
|