Files
activestep/Step.CmsConnectManager/MConnectSDKWrapper.cs
T
2020-09-12 16:11:43 +02:00

242 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MConnectSDK;
using Step.Database.Controllers;
using TeamDev.SDK.MVVM;
using static Step.CmsConnectManager.CMSConnectConstants;
using static Step.Model.Constants;
using static Step.Config.ServerConfig;
using Step.Model.DTOModels;
namespace Step.CmsConnectManager
{
public class MConnectSDKWrapper : IDisposable
{
private static MConnectClient _client { get; set; }
private static CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private static LicenseData License = new LicenseData();
private CancellationToken _cancellationToken = _cancellationTokenSource.Token;
public MConnectSDKWrapper()
{
if(_client == null)
_client = new MConnectClient();
}
public async Task<bool> InitSDK()
{
// Call SDK Function
var res = await _client.InitSDKAsync(YAML_CONFIG_FILE, _cancellationToken);
return ConnectionIsOk(res);
}
public async Task<bool> CheckActivationData()
{
// Call SDK Function
var res = await _client.GetClientStatusAsync();
if (!ConnectionIsOk(res))
return false;
if (!res.IsHmiEnrolled)
return false;
// Check if connection is ok and if token is needed
return await GetLicence();
}
public async Task<bool> TryActivation()
{
// Setup callback
Progress<Result> progress = new Progress<Result>();
progress.ProgressChanged += TryActivation_ProgressChanged;
// Start SDK progress
var res = await _client.TryActivationAsync(_cancellationToken, progress);
if (!ConnectionIsOk(res))
return false;
// If machine is already authenticated & enrolled return ok
if (res.IsAuth && res.IsHmiEnrolled)
{
UpdateClients(res);
return true;
}
// If not enrolled
if (res.IsAuth && !res.IsHmiEnrolled)
{
res = await Enroll();
UpdateClients(res);
return ConnectionIsOk(res) && res.IsHmiEnrolled;
}
if (res.IsHmiEnrolled && !res.IsAuth)
return await GetLicence();
return true;
}
public async Task<Result> Enroll()
{
// Useless progress parameter
Progress<Result> progress = new Progress<Result>();
var res = await _client.EnrollMachineAsync(_cancellationToken, progress);
return res;
}
public async Task<bool> GetLicence()
{
// Call SDK Function
var res = await _client.GetLicenseAsync();
// If the API is successful set license data
if(res.CallResultOk && res.Payload is LicensePayload)
{
var lic = await _client.GetLicenseAsync();
License = (lic.Payload as LicensePayload).Data;
}
return ConnectionIsOk(res);
}
public async Task<List<UserData>> GetUserOrganizzationList(bool getImage = false)
{
// TODO: REMOVE
var connectionStatus = await CheckActivationData();
if (!connectionStatus)
return null;
// Call SDK function
var res = await _client.getUserListAsync(getImage);
if (!ConnectionIsOk(res))
return null;
if(res.Payload is UserPayload payload)
return payload.UserList;
return null;
}
public async Task<List<UserData>> GetUserNotImported()
{
var res = await GetUserOrganizzationList(true);
if (res == null)
return null;
return res
.Where(x => x.LoginStatus != UserStatus.IMPORTED)
.ToList();
}
public async Task<UserData> TryUserLogin(string username, string password)
{
// Call SDK function
var res = await _client.TryUserLogin(username, password);
if (!ConnectionIsOk(res))
return null;
// Check if SDK return user data
if (res.Payload is UserPayload payload && payload.UserList.Count() > 0)
return payload.UserList.First();
return null;
}
// Callback
private void TryActivation_ProgressChanged(object sender, Result res)
{
// Check if data are ok
if (ConnectionIsOk(res) && res.Payload is ActivationPayload activation)
{
// Send data to connected client
MessageServices.Current.Publish(CONNECT_ACTIVATION_NOTIFICATIONS, null, activation);
}
}
private void UpdateClients(object res)
{
MessageServices.Current.Publish(CONNECT_GENERAL_DATA, null, res);
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
}
private bool ConnectionIsOk(Result res)
{
return res.CallResultOk && res.CloudStatusOk;
}
public async Task<bool> ImportConnectUsers(List<UserData> connectUserList)
{
UsersController userController = new UsersController();
//var userList = userController.GetCMSConnectUserList();
//var deletedUsers = userList.Where(x => !connectUserList.Any(y => x.CmsConnectUserId == y.UserId)).ToList();
//var newUser = connectUserList.Where(x => !userList.Any(y => x.UserId == y.CmsConnectUserId)).ToList();
foreach (var user in connectUserList)
{
userController.CreateCMSConnectUser(user.Username, "", user.Nome, user.Cognome, Config.ServerConfig.ServerStartupConfig.Language, user.Email, user.UserId, user.IsAdmin);
var res = await _client.setUserImported(SourceType.LOCAL, user.Username, true);
if (!ConnectionIsOk(res))
return false;
}
return true;
}
public async Task<bool> RemoveUserFromImported(string username)
{
var res = await _client.setUserImported(SourceType.LOCAL, username, false);
return ConnectionIsOk(res);
}
public bool SendMessage(DTOMessageModel message)
{
RedisController.SendMessage(message);
return true;
}
//private async void UpdateCmsConnectUsers()
//{
// MConnectSDKWrapper _sdk = new MConnectSDKWrapper();
// var connectUserList = await _sdk.GetUserList();
// UsersController userController = new UsersController();
// var userList = userController.GetCMSConnectUserList();
// var deletedUsers = userList.Where(x => !connectUserList.Any(y => x.CmsConnectUserId == y.UserId)).ToList();
// var newUser = connectUserList.Where(x => !userList.Any(y => x.UserId == y.CmsConnectUserId)).ToList();
// foreach (var user in newUser)
// {
// userController.CreateCMSConnectUser(user.Username, "", user.Nome, user.Cognome, Config.ServerConfig.ServerStartupConfig.Language, user.Email, user.UserId, user.IsAdmin);
// }
// foreach (var deletetUser in deletedUsers)
// {
// userController.DeleteUser(deletetUser);
// }
//}
}
}