lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
442 lines • 22.5 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 { DataFrame, Method, Status } from "../../DataFrame";
import { DependencyContainer } from "../../DependencyContainer";
import { Helpers } from "../../Helpers";
import { ControllerConfigurationService } from "../Controller/ControllerConfigurationService";
import { ExalusConnectionService } from "../ExalusConnectionService";
import { ResponseResult } from "../FieldChangeResult";
import { LoggerService } from "../Logging/LoggerService";
import { ImageType } from "./IPicture";
import { Picture } from "./Picture";
export class PicturesService {
constructor() {
this._imageExistsCaseNumber = 0;
this._connection = Api.Get(ExalusConnectionService.ServiceName);
this._logger = Api.Get(LoggerService.ServiceName);
}
GetServiceName() {
return PicturesService.ServiceName;
}
AddPictureAsync(picture) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
//Check picture size - max 512kB
if ((picture.Base64Image.length * (3 / 4)) >= 512000) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - Base64Imgae size exceeded.`);
return Status.WrongData;
}
//Check if it is picture
let image = new Image();
const checkImage = new Promise((resolve, reject) => {
image.src = picture.Base64Image;
image.onload = function () {
resolve(true);
};
setTimeout(() => resolve(false), 5000);
});
if (!(yield checkImage)) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - cannot load image - ensure that given base64 string is an image!`);
return Status.WrongData;
}
if (image.height === 0 || image.width === 0) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - cannot read width/height of image - ensure that given base64 string is an image!`);
return Status.WrongData;
}
if (picture.Guid != null && picture.Guid != "") {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - Guid must be empty.`);
return Status.WrongData;
}
const pictureToAdd = new PictureFrame();
pictureToAdd.Guid = Helpers.GenerateUUID();
pictureToAdd.DateTime = new Date().toISOString();
pictureToAdd.ImageType = picture.ImageType;
pictureToAdd.OwnerGuid = `${picture.OwnerGuid}_${picture.OwnerIdentity}`;
pictureToAdd.Base64Image = picture.Base64Image;
const currentResult = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPictureRequest(pictureToAdd.Guid), 8000, false));
if (currentResult == null || currentResult.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - unknown error!`);
return Status.UnknownError;
}
//New picture
if (currentResult.Status == Status.ResourceDoesNotExists) {
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(new SetPictureRequest(pictureToAdd), 8000, false));
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - unknown error!`);
return Status.UnknownError;
}
return result.Status;
}
//Guid already exists - trying to add with different guid
else if (currentResult.Status == Status.OK) {
this._logger.Warning(PicturesService.ServiceName, `Cannot add picture - picture already exists, trying to add with regenerated GUID.`);
if (this._imageExistsCaseNumber < 2) {
this._imageExistsCaseNumber++;
return this.AddPictureAsync(picture);
}
else {
return Status.UnknownError;
}
}
//Error
else {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture - cannot get current picture status.`);
return Status.Error;
}
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot add picture! ${error}`);
return Status.FatalError;
}
});
}
EditPictureAsync(picture) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
try {
if (picture.Guid == "" || picture.Guid == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - guid is empty.`);
return Status.WrongData;
}
//Check picture size - max 512kB
if ((picture.Base64Image.length * (3 / 4)) >= 512000) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - Base64Imgae size exceeded.`);
return Status.WrongData;
}
//Check if it is picture
let image = new Image();
const checkImage = new Promise((resolve, reject) => {
image.src = picture.Base64Image;
image.onload = function () {
resolve(true);
};
setTimeout(() => resolve(false), 5000);
});
if (!(yield checkImage)) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - cannot load image - ensure that given base64 string is an image!`);
return Status.WrongData;
}
if (image.height === 0 || image.width === 0) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - cannot read width/height of image - ensure that given base64 string is an image!`);
return Status.WrongData;
}
const pictureToAdd = new PictureFrame();
pictureToAdd.DateTime = new Date().toISOString();
pictureToAdd.ImageType = picture.ImageType;
pictureToAdd.Base64Image = picture.Base64Image;
pictureToAdd.Guid = picture.Guid;
//for backward compatibility(profile pictures)
if (picture.Guid.includes("profilePicture") || picture.Guid.includes("devices_group")) {
pictureToAdd.OwnerGuid = picture.OwnerGuid;
}
else {
pictureToAdd.OwnerGuid = `${picture.OwnerGuid}_${picture.OwnerIdentity}`;
}
const currentResult = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPictureRequest(pictureToAdd.Guid), 8000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(PicturesService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! EditPictureAsync()`);
Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync();
if (currentResult == null || currentResult.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - unknown error!`);
return Status.UnknownError;
}
//Picture not exists
if (currentResult.Status == Status.ResourceDoesNotExists) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - picture not exists.`);
return Status.OperationNotPermitted;
}
//Picture already exists - editing picture
else if (currentResult.Status == Status.OK) {
const result = yield ((_c = this._connection) === null || _c === void 0 ? void 0 : _c.SendAndWaitForResponseAsync(new SetPictureRequest(pictureToAdd), 8000, false));
(_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Error(PicturesService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! EditPictureAsync()`);
Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync();
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - unknown error!`);
return Status.UnknownError;
}
return result.Status;
}
//Error
else {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture - cannot get current picture status.`);
return Status.Error;
}
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot edit picture! ${error}`);
return Status.FatalError;
}
});
}
DeletePictureAsync(picture) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
if (picture.Guid == "" || picture.Guid == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot delete picture - guid is empty.`);
return Status.WrongData;
}
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new DeletePictureRequest(picture.Guid), 8000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(PicturesService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! DeletePictureAsync()`);
Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync();
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot delete picture - unknown error!`);
return Status.UnknownError;
}
return result.Status;
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot delete picture! ${error}`);
return Status.FatalError;
}
});
}
GetPicturesInfoAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPictureInfoRequest(), 8000, false));
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures info - unknown error!`);
return new ResponseResult(Status.UnknownError, `NoDataInResult`);
}
if (result.Status != Status.OK) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures info - controler responded with status ${result.Status}.`);
return new ResponseResult(result.Status, `UnknownReason`);
}
if (result.Data == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures info - controler responded with OK, but no data in result.}.`);
return new ResponseResult(Status.UnknownError, `NoDataInResult`);
}
return result.Data.map(picInfo => {
const picInfoRes = new PictureInfo();
picInfoRes.DateTime = picInfo.DateTime;
picInfoRes.Guid = picInfo.Guid;
picInfoRes.OwnerGuid = picInfo.OwnerGuid;
if (picInfo.OwnerGuid.indexOf('_') != -1) {
picInfoRes.OwnerGuid = picInfo.OwnerGuid.substring(0, picInfo.OwnerGuid.indexOf('_'));
picInfoRes.OwnerIdentity = picInfo.OwnerGuid.substring(picInfo.OwnerGuid.indexOf('_') + 1);
}
//for backward compatybility (profile pictures, channel groups)
else if (picInfo.Guid.includes("profilePicture")) {
picInfoRes.OwnerIdentity = picInfo.Guid.substring(picInfo.Guid.indexOf('_') + 1);
picInfoRes.OwnerGuid = picInfo.OwnerGuid;
}
else if (picInfo.Guid.includes("devices_group")) {
picInfoRes.OwnerIdentity = picInfo.Guid.substring(0, picInfo.Guid.lastIndexOf('_'));
picInfoRes.OwnerGuid = picInfo.OwnerGuid;
}
else {
picInfoRes.OwnerGuid = picInfo.OwnerGuid;
picInfoRes.OwnerIdentity = "";
}
return picInfoRes;
});
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot get picture info! ${error}`);
return new ResponseResult(Status.FatalError, `ExceptionOccurred`);
}
});
}
GetPictureAsync(pictureGuid) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPictureRequest(pictureGuid), 8000, false));
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot get picture - unknown error!`);
return new ResponseResult(Status.UnknownError, `NoDataInResult`);
}
switch (result.Status) {
case Status.ResourceDoesNotExists:
return new ResponseResult(Status.ResourceDoesNotExists, `PictureNotFound`);
case Status.OK:
if (result.Data == null)
return new ResponseResult(Status.Error, `RespondedWithOKButNoData`);
const data = new PictureResult();
data.Base64Image = result.Data.Base64Image;
data.DateTime = result.Data.DateTime;
data.ImageType = result.Data.ImageType;
data.Guid = result.Data.Guid;
if (result.Data.OwnerGuid.indexOf('_') != -1) {
data.OwnerGuid = result.Data.OwnerGuid.substring(0, result.Data.OwnerGuid.indexOf('_'));
data.OwnerIdentity = result.Data.OwnerGuid.substring(result.Data.OwnerGuid.indexOf('_') + 1);
}
//for backward compatybility (profile pictures, channel groups)
else if (result.Data.Guid.includes("profilePicture")) {
data.OwnerIdentity = result.Data.Guid.substring(result.Data.Guid.indexOf('_') + 1);
data.OwnerGuid = result.Data.OwnerGuid;
}
else if (result.Data.Guid.includes("devices_group")) {
data.OwnerIdentity = result.Data.Guid.substring(0, result.Data.Guid.lastIndexOf('_'));
data.OwnerGuid = result.Data.OwnerGuid;
}
else {
data.OwnerGuid = result.Data.OwnerGuid;
data.OwnerIdentity = "";
}
return data;
default:
return new ResponseResult(result.Status, `UnknownReason`);
}
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot get picture! ${error}`);
return new ResponseResult(Status.FatalError, `ExceptionOccurred`);
}
});
}
GetPicturesListAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPictureListRequest(), 8000, false));
if (result == null || result.Status == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures - unknown error!`);
return new ResponseResult(Status.UnknownError, `NoDataInResult`);
}
if (result.Status != Status.OK) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures - controler responded with status ${result.Status}.`);
return new ResponseResult(result.Status, `UnknownReason`);
}
if (result.Data == null) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures - controler responded with OK, but no data in result.}.`);
return new ResponseResult(Status.UnknownError, `NoDataInResult`);
}
return result.Data.map(pic => {
const returnPic = new PictureResult();
returnPic.Base64Image = pic.Base64Image;
returnPic.DateTime = pic.DateTime;
returnPic.Guid = pic.Guid;
returnPic.ImageType = pic.ImageType;
if (pic.OwnerGuid.indexOf('_') != -1) {
returnPic.OwnerGuid = pic.OwnerGuid.substring(0, pic.OwnerGuid.indexOf('_'));
returnPic.OwnerIdentity = pic.OwnerGuid.substring(pic.OwnerGuid.indexOf('_') + 1);
}
//for backward compatybility (profile pictures)
else if (pic.Guid.includes("profilePicture")) {
returnPic.OwnerIdentity = pic.Guid.substring(pic.Guid.indexOf('_') + 1);
returnPic.OwnerGuid = pic.OwnerGuid;
}
else if (pic.Guid.includes("devices_group")) {
returnPic.OwnerIdentity = pic.Guid.substring(0, pic.Guid.lastIndexOf('_'));
returnPic.OwnerGuid = pic.OwnerGuid;
}
else {
returnPic.OwnerGuid = pic.OwnerGuid;
returnPic.OwnerIdentity = "";
}
return returnPic;
});
}
catch (error) {
this._logger.Error(PicturesService.ServiceName, `Cannot get pictures! ${error}`);
return new ResponseResult(Status.FatalError, `ExceptionOccurred`);
}
});
}
}
PicturesService.ServiceName = "PictureService";
class PictureResult extends Picture {
set DateTime(value) {
this._dateTime = value;
}
}
class PictureFrame {
constructor() {
this.Guid = "";
this.OwnerGuid = "";
this.Base64Image = "";
this.ImageType = ImageType.Device;
this.DateTime = "";
}
}
//PictureInfo
class PictureInfo {
constructor() {
this._guid = "";
this._dateTime = "";
this._ownerGuid = "";
this._ownerIdentity = "";
}
get Guid() {
return this._guid;
}
get DateTime() {
return this._dateTime;
}
get OwnerGuid() {
return this._ownerGuid;
}
get OwnerIdentity() {
return this._ownerIdentity;
}
set Guid(value) {
this._guid = value;
}
set DateTime(value) {
this._dateTime = value;
}
set OwnerGuid(value) {
this._ownerGuid = value;
}
set OwnerIdentity(value) {
this._ownerIdentity = value;
}
}
class PictureInfoFrame {
constructor() {
this.Guid = "";
this.DateTime = "";
this.OwnerGuid = "";
}
}
//Requests
class GetPictureRequest extends DataFrame {
constructor(pictureGuid) {
super();
this.Resource = "/pictures/picture";
this.Method = Method.Get;
this.Data = pictureGuid;
}
}
class GetPictureInfoRequest extends DataFrame {
constructor() {
super();
this.Resource = "/pictures/info";
this.Method = Method.Get;
}
}
class GetPictureListRequest extends DataFrame {
constructor() {
super();
this.Resource = "/pictures/list";
this.Method = Method.Get;
}
}
class SetPictureRequest extends DataFrame {
constructor(data) {
super();
this.Resource = "/pictures/picture";
this.Method = Method.Put;
this.Data = data;
}
}
class DeletePictureRequest extends DataFrame {
constructor(guid) {
super();
this.Resource = "/pictures/picture";
this.Method = Method.Delete;
this.Data = guid;
}
}
//# sourceMappingURL=PicturesService.js.map