cellsysBase/cellsysApp.js

583 lines
19 KiB
JavaScript
Raw Normal View History

2024-08-14 16:20:56 +08:00
import {
Query,
QueryTask,
QueryType,
EditTask,
EditType,
Operator,
CellsysType,
} from './cellsysUtil.js';
import * as CellsysPush from './cellsysPush.js';
import CellsysEvent from './cellsysEvent.js';
import CellsysFenceEvent from './cellsysFenceEvent.js';
import { PublishTarget } from './cellsysEnum';
class CellsysApp {
constructor(params) {
this.cellsysType = CellsysType.App;
if (!params) {
params = {};
}
this.id = params.id; //新接口如创建应用返回逻辑有变app_id为加密过后的appidid为未加密的id
this.appId = params.app_id || params.id; //新接口如创建应用返回逻辑有变app_id为加密过后的appidid为未加密的id
this.logo = params.logo;
this.name = params.name;
this.orgId = params.org_id;
this.orgName = params.org_name;
this.status = params.status ? params.status : 1;
this.options = params.options;
this.modelId = params.model_id;
this.description = params.description;
this.isShowApp = params.is_show_app;
this.isPublish = params.is_publish;
this.creatorId = params.create_by;
this.realName = params.realname; //创建人姓名
this.modelName = params.model_name;
this.abbreviation = params.abbreviation;
this.filePath = params.file_path;
this.createBy = params.create_by;
this.createTime = params.create_time;
this.updateTime = params.update_time;
this.buildPath = params.build_path;
this.viewPath = params.view_path;
this.modelOptions = params.model_options;
this.orgUnionStatus = params.orgUnionStatus;
this.isPublish = params.is_publish || 0;
this.publishRange = params.publish_range; //发布范围 0-群组 1-组织
this.publishGroup = params.publish_group ? params.publish_group : []; //发布的群组
this.isEditable = params.is_update === 1 ? true : false; //is_update表示是否允许其他人编辑 0-不允许 1-允许
this.isUnlock = params.is_unlock; //是否未锁定0-已锁定1-未锁定
this.tags = [];
if (params.tag_name) {
this.tags = params.tag_name.map((name) => {
return { name: name };
});
}
let creator = params.creator;
this.creator = {
memberId: null,
name: null,
};
if (creator) {
this.creator = {
memberId: creator.member_id,
name: creator.name,
};
}
let updater = params.updater;
this.updater = {
memberId: null,
name: null,
};
if (updater) {
this.updater = {
memberId: updater.member_id,
name: updater.name,
};
}
}
setId(value) {
this.id = value;
}
setIsPublish(value) {
this.isPublish = value;
}
setName(value) {
this.name = value;
}
setDescription(value) {
this.description = value;
}
setModelOptions(modelOption) {
this.modelOptions = modelOption;
}
setShowApp(value) {
if (value === 1) {
this.isShowApp = 1;
} else {
this.isShowApp = 0;
}
}
setOptions(value) {
this.options = value;
}
queryUserLocal(groupIds) {
let query = new Query();
if (groupIds instanceof Array === true) {
query.addFilter('group_id', Operator.In, groupIds);
} else {
query.addFilter('group_id', Operator.Equals, groupIds);
}
let queryTask = new QueryTask(QueryType.UserLocal);
return queryTask.execute(query);
}
queryMemberLocal() {
let query = new Query();
if (this.options && this.options['group']) {
query.addFilter('group_id', Operator.In, this.options['group']);
}
query.setOrder({ realname: 'asc' });
let queryTask = new QueryTask(QueryType.UserLocal);
return queryTask.execute(query);
}
queryLayerGroupsById(groupIds) {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
query.addFilter('id', Operator.In, groupIds);
query.setOrder({ id: 'asc' });
let queryTask = new QueryTask(QueryType.LayerGroup);
return queryTask.execute(query);
}
/**
* 查询app下的所有围栏事件信息
*/
queryFenceEvents(filters, pageInfo) {
let query = new Query();
// query.addFilter('org_id', Operator.Equals, this.orgId);
if (filters && filters.length > 0) {
filters.forEach((item) => {
query.addFilter(item['name'], item['operator'], item['value']);
});
}
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
query.setOrder({ datetime: 'desc' });
let queryType = QueryType.FenceEvent;
if (window.CELLSYSORG && window.CELLSYSORG.unionStatus === 1) {
queryType = QueryType.UnionFenceEvent;
}
let queryTask = new QueryTask(queryType, pageInfo ? true : false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
if (pageInfo) {
if (res.data) {
res.data = res.data.map((item) => {
return new CellsysFenceEvent(item);
});
resolve(res);
}
} else {
let resArr = res.map((item) => {
return new CellsysFenceEvent(item);
});
resolve(resArr);
}
})
.catch((err) => {
reject(err);
});
});
}
/**
* 查询app下的设备信息
*/
queryEquipments(pageInfo) {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
} else {
query.setCurrPage(1);
query.setPageSize(0);
}
query.setOrder({ id: 'asc' });
let queryTask = new QueryTask(QueryType.Equipment, pageInfo ? true : false);
return queryTask.execute(query);
}
queryFloatBalls() {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
let queryTask = new QueryTask(QueryType.FloatBall);
return queryTask.execute(query);
}
queryGroups(pageInfo, filters) {
let query = new Query();
// query.addFilter("app_id", Operator.Equals, this.id);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
if (filters && filters.length > 0) {
filters.forEach((item) => {
query.addFilter(item['name'], item['operator'], item['value']);
});
}
query.setOrder({ id: 'asc' });
let queryTask = new QueryTask(QueryType.Group, pageInfo ? true : false);
return queryTask.execute(query);
}
/**
* 查询事件
*/
queryEvents(eventTypeIds, pageInfo) {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
if (eventTypeIds && eventTypeIds.length > 0) {
query.addFilter('type', Operator.In, eventTypeIds);
}
query.setOrder({ datetime: 'desc' });
let queryTask = new QueryTask(QueryType.Event, pageInfo ? true : false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
if (pageInfo) {
if (res.data) {
res.data = res.data.map((item) => {
return new CellsysEvent(item);
});
resolve(res);
}
} else {
let resArr = res.map((item) => {
return new CellsysEvent(item);
});
resolve(resArr);
}
})
.catch((err) => {
reject(err);
});
});
}
/**
* 根据id集合查询事件
*/
queryEventByIds(eventIds, pageInfo, order) {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
if (eventIds && eventIds.length > 0) {
query.addFilter('id', Operator.In, eventIds);
}
if (order) {
query.setOrder(order);
} else {
query.setOrder({ datetime: 'desc' });
}
let queryTask = new QueryTask(QueryType.Event, pageInfo ? true : false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
if (pageInfo) {
if (res.data) {
res.data = res.data.map((item) => {
return new CellsysEvent(item);
});
resolve(res);
}
} else {
let resArr = res.map((item) => {
return new CellsysEvent(item);
});
resolve(resArr);
}
})
.catch((err) => {
reject(err);
});
});
}
/**
*查询事件类型
*/
loadEventType() {
let query = new Query();
query.addFilter('org_id', Operator.Equals, this.orgId);
query.setOrder({ id: 'asc' });
let queryTask = new QueryTask(QueryType.EventType);
return queryTask.execute(query);
}
/**
*
* 创建事件
*/
eventCreate(event) {
let editTask = new EditTask(EditType.Event_Create);
editTask.addParam('_name', event.name);
editTask.addParam('_description', event.description);
editTask.addParam('_type', event.type);
editTask.addParam('_file_path', event.filePath);
editTask.addParam('_geom', event.geometry);
return editTask.execute();
}
/**
*
* 更新事件
*/
eventUpdate(event) {
let editTask = new EditTask(EditType.Event_Update);
editTask.addParam('_id', event.id);
editTask.addParam('_name', event.name);
editTask.addParam('_description', event.description);
editTask.addParam('_type', event.type);
editTask.addParam('_file_path', event.filePath);
editTask.addParam('_geom', event.geometry);
return editTask.execute();
}
//订阅人员位置信息
subscribeMemberLoc(params) {
let param = Object.assign({}, params.object);
if (!param.orgId) param.orgId = this.orgId;
let MemberLocTopic = new CellsysPush.MemberLocTopic(param);
let pushTask = new CellsysPush.pushTask();
pushTask.subscribe({
cellsysTopic: MemberLocTopic,
onReceive: params.onReceive,
onSuccess: params.onSuccess,
onFail: params.onFail,
});
}
//取消订阅人员位置信息
unSubscribeMemberLoc(params) {
let param = Object.assign({}, params.object);
if (!param.orgId) param.orgId = this.orgId;
let MemberLocTopic = new CellsysPush.MemberLocTopic(param);
let pushTask = new CellsysPush.pushTask();
pushTask.unSubscribe({
cellsysTopic: MemberLocTopic,
onSuccess: params.onSuccess,
onFail: params.onFail,
});
}
//订阅设备信息
subscribeEquipment(params) {
let onSuccess = params.onSuccess,
onFail = params.onFail,
onReceive = params.onReceive;
let param = Object.assign({}, params.object);
if (!param.orgId) param.orgId = this.orgId;
let equipmentLocTopic = new CellsysPush.EquipmentTopic(param);
let pushTask = new CellsysPush.pushTask();
pushTask.subscribe({
cellsysTopic: equipmentLocTopic,
onReceive: onReceive,
onSuccess: onSuccess,
onFail: onFail,
});
}
//发布公告
appMessageSend(params) {
let onSuccess = params.onSuccess,
title = params.title,
message = params.message,
onFail = params.onFail;
let appNoticeTopic = new CellsysPush.appNoticeTopic(this),
pushTask = new CellsysPush.pushTask(),
time = new Date().getTime();
let payload = `${time};${title};${message};`;
pushTask.subscribe({
cellsysTopic: appNoticeTopic,
onSuccess: (res) => {
console.log('app公告订阅成功');
pushTask.send({
cellsysTopic: appNoticeTopic,
message: payload,
});
},
onFail: params.onFail,
onReceive: (res) => {
if (res && message === res.message) {
if (onSuccess) {
onSuccess(res);
}
}
},
});
}
//查询公告
queryMessage(pageInfo) {
let query = new Query();
query.addFilter('to_id', Operator.Equals, this.id);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
query.setOrder({ id: 'asc' });
let queryTask = new QueryTask(QueryType.Notice, pageInfo ? true : false);
return queryTask.execute(query);
}
//记录公告
sendAppNotice(title, message) {
let editTask = new EditTask(EditType.AppNotice_Create);
editTask.addParam('_app_id', this.id);
editTask.addParam('_title', title);
editTask.addParam('_message', message);
return editTask.execute();
}
/**
* 根据围栏id查询当前app下的围栏事件
*/
queryFenceEventsById(pageInfo, fenceIds) {
let query = new Query();
// query.addFilter("org_id", Operator.Equals, this.orgId);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
if (fenceIds && fenceIds.length > 0) {
query.addFilter('fence_id', Operator.In, fenceIds);
}
query.setOrder({ datetime: 'desc' });
let queryTask = new QueryTask(QueryType.FenceEvent, pageInfo ? true : false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
if (pageInfo) {
if (res.data) {
res.data = res.data.map((item) => {
return new CellsysFenceEvent(item);
});
resolve(res);
}
} else {
let resArr = res.map((item) => {
return new CellsysFenceEvent(item);
});
resolve(resArr);
}
})
.catch((err) => {
reject(err);
});
});
}
queryActionType() {
return {
1: { id: 1, name: '进入围栏' },
2: { id: 2, name: '离开围栏' },
};
}
/**
* 更新cellsysApp
*/
updateApp(app) {
let editTask = new EditTask(EditType.App_Update);
editTask.addParam('_id', app.id);
editTask.addParam('_name', app.name);
editTask.addParam('_description', app.description);
editTask.addParam('_uri', app.uri);
editTask.addParam('_is_show_app', app.isShowApp);
editTask.addParam('_options', app.options);
return editTask.execute();
}
/**
* 查询设备监控量字典列表
* @param filters
* @returns {Promise<unknown>}
*/
equipmentMonitorList(filters) {
let query = new Query();
if (filters) {
filters.forEach((item) => {
query.addFilter(item['name'], item['operator'], item['value']);
});
}
query.setOrder({ create_time: 'desc' });
let queryTask = new QueryTask(QueryType.EquipmentMonitors, false);
return queryTask.execute(query);
}
//发布功能
setPublish(publishStatus, range, ids, isEditable, remark) {
let editTask = new EditTask(EditType.AppPublish);
editTask.addParam('_id', this.id);
editTask.addParam('_app_name', this.name);
editTask.addParam('_is_publish', PublishTarget[publishStatus]);
editTask.addParam('_remark', remark);
if (publishStatus !== 'noPublish') {
if (range === 1) {
//发布到组织
editTask.addParam('_publish_org', ids);
} else {
editTask.addParam('_publish_group', ids);
}
editTask.addParam('_publish_range', range);
}
if (isEditable) {
editTask.addParam('_is_update', 1);
} else {
editTask.addParam('_is_update', 0);
}
return editTask.execute();
}
//查询协同数据发布记录列表
synergyDataPublishRecord(dataId, type) {
let query = new Query();
query.setOrder({ create_time: 'desc' });
query.addFilter('data_type', '=', 7);
query.setCurrPage(1);
query.setPageSize(1);
if (dataId) {
query.addFilter('data_id', '=', dataId);
}
if (type) {
query.addFilter('type', '=', type);
}
let queryTask = new QueryTask(QueryType.synergyDataPublishRecord, true);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
//锁定/解锁应用
setLock(params) {
let editTask = new EditTask(EditType.appLock);
editTask.addParam('_id', params.id);
editTask.addParam('_is_unlock', params.isUnlock);
return editTask.execute();
}
}
export default CellsysApp;