123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- 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, ILogikalApp
- {
-
- //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 void Dispose()
- {
- Disconnect();
- }
- }
- }
|