Skip to main content

Không gian

Mô hình dữ liệu

Mối quan hệ với các mô hình khác

/src/app/services/space/space.service.ts
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { catchError, firstValueFrom, Observable, of } from "rxjs";
import { ApiResponse } from "src/app/models/api/api-response.model";
import { PaginationResponseModel } from "src/app/models/pagination/pagination.response.model";
import { SpaceSpaceInModelAssignmentPostModel } from "src/app/models/project/space/editor/space-space-in-model-assignment.post.model";
import { SpaceSpaceInModelAutoAssignmentPostModel } from "src/app/models/project/space/editor/space-space-in-model-auto-assignment.post.model";
import { SpaceType } from "src/app/models/space/space-type.enum";
import { Space } from "src/app/models/space/space.model";
import { SpacePostModel } from "src/app/models/space/space.post.model";
import { SpacePutModel } from "src/app/models/space/space.put.model";
import { SpaceResponseModel } from "src/app/models/space/space.response.model";
import { BaseService } from "../base/base.service";
import { ConfigService } from "../config/config.service";
import { ImageService } from "../image/image.service";
import { LoadingService } from "../loading/loading-service";
import { MessageService } from "../message/message.service";
import { Image } from "../../models/image/image.model";
import { AssetResponseModel } from "src/app/models/asset/asset.response.model";
import { Asset } from "src/app/models/asset/asset.model";
import { ProjectSpaceIoTGroupAssignmentPutModel } from "src/app/models/project/space/project-space-iot-group-assignment.put.model";
import { CacheManagerService } from "../cache-manager/cache-manager.service";
import { PaginationData } from "src/app/models/pagination/pagination-data.model";
import { Project_v2_Service } from "../project-v2/project-v2.service";

@Injectable()
export class SpaceService extends BaseService {

constructor(
private http: HttpClient
, private messageService: MessageService
, private loadingService: LoadingService
, private configService: ConfigService
, private imageService: ImageService
, private cacheManagerService: CacheManagerService
, private project_v2_Service: Project_v2_Service
)
{
super();
this.baseAddress = `[fmApi]/v1/[clientId]/[projectId]/spaces`;
this.baseAssetAddress = `[fmApi]/v1/[clientId]/[projectId]/assets`;
}

public types: { type: SpaceType, name: string }[] = [
{ type: SpaceType.UNDEFINED, name: "-- UNDEFINED --" },
{ type: SpaceType.BALCONY, name: "BALCONY" },
{ type: SpaceType.CORRIDOR, name: "CORRIDOR" },
{ type: SpaceType.LEVEL, name: "LEVEL" },
{ type: SpaceType.ROAD, name: "ROAD" },
{ type: SpaceType.ROOM, name: "ROOM" },
{ type: SpaceType.SPACE, name: "SPACE" },
{ type: SpaceType.STAIR, name: "STAIR" },
{ type: SpaceType.TERRACE, name: "TERRACE" },
{ type: SpaceType.ZONE, name: "ZONE" },
];

private baseAddress: string;
private baseAssetAddress: string;
private httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};

//public pageCache: PaginationModel<Space> = new PaginationModel();
private _clearLocalCache()
{
this.cacheManagerService.clearLocalCacheWithNameIncluding(["/api/", "/spaces"]);
}


public async gets(
pageIndex: number = 0,
forceNew: boolean = false,
loadingDivId: string = "",
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.get<ApiResponse<PaginationResponseModel<SpaceResponseModel>>>(`${this.baseAddress}/page/${pageIndex}`
, this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 7 * 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
const data = response.result.items.map(x => new Space(x, this.project_v2_Service.currentProject, this.configService.appConfig.CM_URL.URL));
return new PaginationData(data, response.result.total);
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}

}

public async getAllNoParents(
forceNew: boolean = false,
loadingDivId: string = "",
)
{
try
{
if(!forceNew && this.project_v2_Service.currentProject && this.project_v2_Service.currentProject.space.orderedItems?.length > 0)
return this.project_v2_Service.currentProject.space.orderedItems;

this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.get<ApiResponse<SpaceResponseModel[]>>(`${this.baseAddress}/all-no-parents`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 7 * 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response.result?.length > 0)
{
const items = response.result.map(x => new Space(x, this.project_v2_Service.currentProject, this.configService.appConfig.CM_URL.URL));
if(this.project_v2_Service.currentProject)
{
this.project_v2_Service.currentProject.space.set(items);
return this.project_v2_Service.currentProject.space.orderedItems;
}

return items;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getAllChildren(
item: Space,
forceNew: boolean = false,
loadingDivId: string = ""
)
{
try
{
if(item.children.isRetrieved) return item.children.items;

this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.get<ApiResponse<SpaceResponseModel[]>>(`${this.baseAddress}/children/${item.id}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 7 * 24 * 60, forceNew)
)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
if(response.result.length > 0)
{
const items = response.result.map(x => new Space(x, item.project, this.configService.appConfig.CM_URL.URL));
items.forEach(x => {
x.parent = item;
x.level = item.level + 1
});
item.children.set(items);

return item.children.items;
}

item.children.set([]);
return [];
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async get(
spaceId: string,
loadingDivId: string = ""
)
{
try
{
if(this.project_v2_Service.currentProject)
{
const cache = this.project_v2_Service.currentProject.space.getByProp(spaceId, "id");
if(cache)
{
return cache;
}
}

this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.get<ApiResponse<SpaceResponseModel>>(`${this.baseAddress}/single-${spaceId}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 7 * 24 * 60)
)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if(response.result)
{
const item = new Space(response.result, this.project_v2_Service.currentProject, this.configService.appConfig.CM_URL.URL);
if(this.project_v2_Service.currentProject) this.project_v2_Service.currentProject.space.add(item);
return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async post(
model: SpacePostModel,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.post<ApiResponse<SpaceResponseModel>>(`${this.baseAddress}`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response?.result)
{
const item = new Space(response.result, this.project_v2_Service.currentProject, this.configService.appConfig.CM_URL.URL);
this.project_v2_Service.currentProject?.space.add(item);
this.messageService.setMessage("Successfully");
this._clearLocalCache();
return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}


public search(keyword: string): Observable<ApiResponse<SpaceResponseModel[]>>
{
this.messageService.clearMessage();
if (!keyword?.trim()) {
// if not search term, return empty hero array.
return of(new ApiResponse<SpaceResponseModel[]>());
}
return this.http.get<ApiResponse<SpaceResponseModel[]>>(`${this.baseAddress}/search/${keyword.trim()}`,
this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 60)
)
.pipe(
catchError(this.handleError)
);
}

public async put(
model: SpacePutModel
)
{
try
{
this.loadingService.show("");
const response = await firstValueFrom(this.http.put<ApiResponse<SpaceResponseModel>>(`${this.baseAddress}`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide();
if(response?.result)
{
const item = new Space(response.result, this.project_v2_Service.currentProject, this.configService.appConfig.CM_URL.URL);
//project?.space.addItem(item);
this.project_v2_Service.currentProject?.space.add(item);
this.project_v2_Service.currentProject?.space.processingParentAndChildren();
this.messageService.setMessage("Successfully");

this._clearLocalCache();

return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide();
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async delete(
id: string)
{
try
{
this.loadingService.show("");
const response = await firstValueFrom(this.http.delete<ApiResponse<boolean>>(`${this.baseAddress}/${id}`, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide();
if(response?.result)
{
this.project_v2_Service.currentProject?.space.removeById(id);
this.messageService.setMessage("successfully");

this._clearLocalCache();

return true;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide();
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async modelAssignments(models: SpaceSpaceInModelAssignmentPostModel[], loadingDivId: string = "")
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.put<ApiResponse<SpaceResponseModel[]>>(`${this.baseAddress}/model-assignments`, models, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response?.result)
{
this.messageService.setMessage("Successfully: " + response?.result.length);
this._clearLocalCache();
return response?.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async modelAutoAssignments(models: SpaceSpaceInModelAutoAssignmentPostModel[], loadingDivId: string = "")
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.post<ApiResponse<boolean>>(`${this.baseAddress}/model-assignments`, models, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response?.result)
{
this.messageService.setMessage("Successfully");
this._clearLocalCache();
return true;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async modelClearAllAssignments(loadingDivId: string = "")
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.delete<ApiResponse<SpaceResponseModel[]>>(`${this.baseAddress}/model-assignments`, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response?.result)
{
this.messageService.setMessage("Successfully: " + response?.result.length);
this._clearLocalCache();
return response?.result;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getImages(item: Space, loadingDivId: string = "")
{
if(item.image.isRetrieved) return true;

this.messageService.clearMessage();
try
{
const response = await this.imageService.getsByHostId(item.id, false, loadingDivId);
if(response && response.length > 0)
{
const items = response.map(x => new Image(x, this.configService.appConfig.CM_URL.URL));
item.image.set(items);
item.processImageRootPaths(this.configService.appConfig.CM_URL.URL);
return true;
}
else
{
item.image.set([]);
return false;
}
}
catch(err)
{
console.error(err);
this.loadingService.hide(loadingDivId);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async getsAsset(
item: Space,
status: string = "all",
system: string = "ALL",
from: number = -1,
to: number = -1,
pageIndex: number = 0,
forceNew: boolean = false,
loadingDivId: string = ""
)
{
try
{
if(status.includes("all")) status = "all";
if(system.includes("ALL")) system = "ALL";

this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.get<ApiResponse<PaginationResponseModel<AssetResponseModel>>>(`${this.baseAssetAddress}/space-${item.id}/page-${pageIndex}/filter?from=${from}&status=${status}&system=${system}&to=${to}`
, this.cacheManagerService.getLocalCacheHttpOptions(this.httpOptions.headers, 60, forceNew)
)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if (response.result)
{
const data = response.result.items.map(x => new Asset(x, this.configService.appConfig.CM_URL.URL));
return new PaginationData(data, response.result.total);
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch (err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async deleteImage(
item: Space,
image: Image,
loadingDivId: string = ""
)
{
this.messageService.clearMessage();
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.delete<ApiResponse<boolean>>(`${this.baseAddress}/images/${item.id}/${image.id}`, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if (response.result)
{
item.image.removeById(image.id);
this.messageService.setMessage("Successfully!", 2222);
return true;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch (err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

public async setDefaultImage(
item: Space,
image: Image,
loadingDivId: string = ""
)
{
this.messageService.clearMessage();
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.put<ApiResponse<boolean>>(`${this.baseAddress}/images/default/${item.id}/${image.id}`, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);

if (response.result)
{
item.imageRootPath = image.serverFileRootPath;
item.imageRootPathUrl = image.originalImageSrc;
this.messageService.setMessage("Successfully", 2222);
this._clearLocalCache();
return true;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch (err)
{
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}

//#region IoT

//#region Group Assignment
public async putIoTGroup(
item: Space,
model: ProjectSpaceIoTGroupAssignmentPutModel,
loadingDivId: string = ""
)
{
try
{
this.loadingService.show("", -1, loadingDivId);
const response = await firstValueFrom(this.http.put<ApiResponse<SpaceResponseModel>>(`${this.baseAddress}/iot-groups`, model, this.httpOptions)
.pipe(
catchError(this.handleError)
));
this.loadingService.hide(loadingDivId);
if(response?.result)
{
item.energyDeviceGroup = response.result.energyDeviceGroup;
item.presenceDeviceGroup = response.result.presenceDeviceGroup;
item.airQualityGroup = response.result.airQualityGroup;

this.messageService.setMessage("Successfully");
this._clearLocalCache();
return item;
}
else
{
this.messageService.setErrorMessage(response.errorMessage);
return false;
}
}
catch(err) {
this.loadingService.hide(loadingDivId);
console.error(err);
this.messageService.setErrorMessage("Error: Can not connect to the server!");
return false;
}
}
//#endregion

//#endregion
}