lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
247 lines • 12.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Api } from "../../Api";
import { ExalusConnectionService } from "../ExalusConnectionService";
import { ConnectionState } from "../IExalusConnectionService";
import { TypedEvent } from "../../TypedEvent";
import { AccessLevel, Gender } from '../Users/IUser';
import { LoginError } from "./ISessionService";
import { DataFrame, Method, Status } from '../../DataFrame';
import { DependencyContainer } from "../../DependencyContainer";
import { User } from "../Users/User";
import { ControllerConfigurationService } from "../Controller/ControllerConfigurationService";
export class SessionService {
constructor() {
this._onUserLoggedInEvent = new TypedEvent();
this._onUserLoggedOutEvent = new TypedEvent();
this._email = "";
this._password = "";
this._authToken = "";
this._loginTaskCompletionSource = this.CreateSessionCompletionSourceAsync();
this._alreadySubsribedToNetworkEvents = false;
this._user = null;
}
CreateSessionCompletionSourceAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Creating _loginTaskCompletionSource`);
this._loginTaskCompletionSource = new Promise((resolve) => {
let onUserLoggedIn = (user) => {
var _a;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Logged in as ${user.Name}, completing CreateSessionCompletionSourceAsync()`);
resolve();
};
this.OnUserLoggedInEvent().Subscribe(onUserLoggedIn);
});
});
}
RestoreSessionAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Creating RestoreSessionAsync ${this._email} ${this._password}`);
if (this._user === undefined || this._user === null)
return;
const connection = Api.Get(ExalusConnectionService.ServiceName);
const auth = connection.GetAuthorizationInfo();
if (auth === null) {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(SessionService.ServiceName, `RestoreSessionAsync failed, no authorization info`);
return;
}
yield connection.AuthorizeAsync(auth);
yield this.UserLogInAsync(this._email, this._password);
});
}
WaitForSessionCreationAsync() {
return __awaiter(this, void 0, void 0, function* () {
return this._loginTaskCompletionSource;
});
}
OnUserLoggedInEvent() {
return this._onUserLoggedInEvent;
}
OnUserLoggedOutEvent() {
return this._onUserLoggedOutEvent;
}
get User() {
return this._user;
}
UserLogOutAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
var logoutRequest = new DataFrame();
logoutRequest.Resource = "/users/user/logout";
logoutRequest.Method = Method.Put;
this._email = "";
this._password = "";
//var connection = Api.Get<IExalusConnectionService>(ExalusConnectionService);
//connection.OnConnectionStateChangedEvent().Unsubscribe(this.OnConnectionStateChanged);
const result = yield Api.Get(ExalusConnectionService.ServiceName)
.SendAndWaitForResponseAsync(logoutRequest, 20000, false);
if (result.Status == Status.OK) {
this._onUserLoggedOutEvent.Invoke(this._user);
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, "User has been logged out.");
}
else
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, "Failed to logout user!");
});
}
OnConnectionStateChanged(connectionState) {
return __awaiter(this, void 0, void 0, function* () {
switch (connectionState) {
case ConnectionState.Disconnected:
case ConnectionState.Failed:
break;
case ConnectionState.Connected:
Api.Get(SessionService.ServiceName).RestoreSessionAsync();
break;
}
});
}
AuthorizeAppAsync(authToken) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
var connection = Api.Get(ExalusConnectionService.ServiceName);
const result = yield connection.SendAndWaitForResponseAsync(new AuthorizeAppRequest(authToken), 20000, false);
if (result.Status == Status.OK && result.Data != null) {
this._authToken = authToken;
var user = new User();
this._user = user;
user.AccessLevel = result.Data.AccessLevel;
user.Email = result.Data.Email;
this._email = result.Data.Email;
user.Gender = result.Data.Gender;
user.Guid = result.Data.Guid;
user.IsAccountOnline = result.Data.IsAccountOnline;
user.Name = result.Data.Name;
user.IsActive = result.Data.IsActive;
user.IsBanned = result.Data.IsBanned;
user.Phone = result.Data.Phone;
user.Surname = result.Data.Surname;
user.SoftwareVersion = result.Data.SoftwareVersion;
if (result.Data.ConfigurationTime !== undefined)
ControllerConfigurationService.ConfigurationTimeRetrieved = new Date(result.Data.ConfigurationTime);
if (!this._alreadySubsribedToNetworkEvents) {
this._alreadySubsribedToNetworkEvents = true;
connection.OnConnectionStateChangedEvent().Subscribe(this.OnConnectionStateChanged);
Api.Get(ExalusConnectionService.ServiceName).SubscribeTo("/info/users/user/loggedOut", (data) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `User logged out: ${JSON.stringify(data.Data)}`);
(_b = this._onUserLoggedOutEvent) === null || _b === void 0 ? void 0 : _b.Invoke(this._user);
}));
}
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Did logging in succeded?: ${result.Status == Status.OK}`);
this._onUserLoggedInEvent.Invoke(this._user);
return user;
}
else if (result.Status === Status.OperationNotPermitted) {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, `App login response: ${result.Data}`);
return LoginError.AuthDisabled;
}
else if (result.Status === Status.ResourceDoesNotExists) {
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Debug(SessionService.ServiceName, `App login response: ${result.Data}`);
return LoginError.MethodNotSupported;
}
else {
(_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Debug(SessionService.ServiceName, `App login response: ${result.Data}`);
return LoginError.WrongAuthData;
}
});
}
UserLogInAsync(email, password) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
var connection = Api.Get(ExalusConnectionService.ServiceName);
const result = yield connection.SendAndWaitForResponseAsync(new LoginUserRequest(email, password), 20000, false);
if (result.Status == Status.OK && result.Data != null) {
this._email = email;
this._password = password;
var user = new User();
this._user = user;
user.AccessLevel = result.Data.AccessLevel;
user.Email = result.Data.Email;
user.Gender = result.Data.Gender;
user.Guid = result.Data.Guid;
user.IsAccountOnline = result.Data.IsAccountOnline;
user.Name = result.Data.Name;
user.IsActive = result.Data.IsActive;
user.IsBanned = result.Data.IsBanned;
user.Phone = result.Data.Phone;
user.Surname = result.Data.Surname;
user.SoftwareVersion = result.Data.SoftwareVersion;
if (result.Data.ConfigurationTime !== undefined)
ControllerConfigurationService.ConfigurationTimeRetrieved = new Date(result.Data.ConfigurationTime);
if (!this._alreadySubsribedToNetworkEvents) {
this._alreadySubsribedToNetworkEvents = true;
connection.OnConnectionStateChangedEvent().Subscribe(this.OnConnectionStateChanged);
Api.Get(ExalusConnectionService.ServiceName).SubscribeTo("/info/users/user/loggedOut", (data) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `User logged out: ${JSON.stringify(data.Data)}`);
(_b = this._onUserLoggedOutEvent) === null || _b === void 0 ? void 0 : _b.Invoke(this._user);
}));
}
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Did logging in succeded?: ${result.Status == Status.OK}`);
this._onUserLoggedInEvent.Invoke(this._user);
return user;
}
else {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, `User login response: ${result.Data}`);
return LoginError.WrongAuthData;
}
});
}
GetServiceName() {
return SessionService.ServiceName;
}
}
SessionService.ServiceName = "SessionService";
export class UserData {
constructor() {
this.Guid = "";
this.Name = "";
this.Surname = "";
this.Password = "";
this.Email = "";
this.Phone = "";
this.IsActive = true;
this.IsBanned = false;
this.IsAccountOnline = false;
this.AccessLevel = AccessLevel.Any;
this.Gender = Gender.Unknown;
this.ConfigurationTime = "";
this.IsGeolocationSet = false;
this.SoftwareVersion = "";
this.UpdatesChannel = "";
}
}
class UserLoginData {
constructor(email, password) {
this.Email = "";
this.Password = "";
this.Email = email;
this.Password = password;
}
}
class AuthorizeAppRequest extends DataFrame {
constructor(authToken) {
super();
this.Data = authToken;
this.Resource = "/applications/app/auth";
this.Method = Method.Put;
}
}
class LoginUserRequest extends DataFrame {
constructor(email, password) {
super();
this.Data = new UserLoginData(email, password);
this.Resource = "/users/user/login";
this.Method = Method.Put;
}
}
//# sourceMappingURL=SessionService.js.map