lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
689 lines • 38.9 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 { ExalusConnectionService } from "../ExalusConnectionService";
import { ResponseResult } from "../FieldChangeResult";
import { SessionService } from "../Session/SessionService";
import { AddNewGroupErrorCode, ChannelsGroup, SetObjectsInGroupAndOrderResponseCode } from "./IChannelsGroupsService";
import { LoggerService } from "../Logging/LoggerService";
import { DevicesService } from './DevicesService';
import { ControllerConfigurationService } from "../Controller/ControllerConfigurationService";
import { DependencyContainer } from "../../DependencyContainer";
export class ChannelsGroupsService {
constructor() {
this._connection = null;
this._synchronized = false;
this._synchronizationTaskCompletionSource = null;
this._groups = [];
this._objectsGroupingSupported = false;
this._configurationService = Api.Get(ControllerConfigurationService.ServiceName);
this._connection = Api.Get(ExalusConnectionService.ServiceName);
this._synchronizationTaskCompletionSource = new Promise((resolve) => {
Api.Get(SessionService.ServiceName).OnUserLoggedInEvent().Subscribe((user) => __awaiter(this, void 0, void 0, function* () {
yield this.GetGroupsAsync(true);
resolve();
this._synchronized = true;
}));
});
}
SetObjectsInGroupAndOrderAsync(group, objects) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let channelsIds = [];
let objectsIds = [];
for (let obj of objects) {
if (this.IsDevice(obj)) {
for (let channel of obj.Channels) {
channelsIds.push(`${channel.ChannelId}`);
}
objectsIds.push(`${obj.Guid}`);
}
else if (this.IsDeviceChannel(obj)) {
channelsIds.push(`${obj.ChannelId}`);
objectsIds.push(`${obj.ChannelId}`);
}
else
throw new Error("Passed object is not a device or device channel!");
}
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetObjectsInGroupAndOrderRequest(group, objectsIds), 15000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetObjectsInGroupAndOrderAsync()`);
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
return new ResponseResult(SetObjectsInGroupAndOrderResponseCode.Success, "");
case Status.NotSupportedMethod:
case Status.ResourceDoesNotExists:
case Status.ResourceIsNotAvailable:
return new ResponseResult(SetObjectsInGroupAndOrderResponseCode.FunctionNotSupported, "");
case Status.WrongData:
switch (result.Data) {
case "GroupDoesNotExists":
return new ResponseResult(SetObjectsInGroupAndOrderResponseCode.GroupDoesNotExists, result.Data);
}
return new ResponseResult(SetObjectsInGroupAndOrderResponseCode.FatalError, result.Data);
case Status.Error:
case Status.FatalError:
default:
return new ResponseResult(SetObjectsInGroupAndOrderResponseCode.FatalError, "");
}
});
}
IsDevice(obj) {
return obj.Channels !== undefined;
}
IsDeviceChannel(obj) {
return obj.GetDevice !== undefined;
}
LogGroupsOrder(orderedGroups) {
const log = Api.Get(LoggerService.ServiceName);
orderedGroups.forEach((group, index) => {
log.Debug(ChannelsGroupsService.ServiceName, `Group ${group.Name} order position [on array]: ${index} set position: ${group.OrderPosition}`);
});
}
LogObjectsInGroupOrder(group, channels) {
const log = Api.Get(LoggerService.ServiceName);
log.Debug(ChannelsGroupsService.ServiceName, `Group ${group.Name} objects order:`);
channels.forEach((channel, index) => {
log.Debug(ChannelsGroupsService.ServiceName, `Channel ${channel.Name} order position [on array]: ${index} channelId: ${channel.ChannelId}`);
});
for (const [key, value] of Object.entries(group.ObjectsOrder)) {
log.Debug(`new order [GROUP ${group.Name}] Order: [${key}] = ${value}`);
}
}
ChangeGroupsOrderAsync(orderedGroups) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const orderedObjectsGuid = orderedGroups.select(a => a.Guid).toArray();
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new ChangeGroupsOrderRequest(orderedObjectsGuid), 15000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! ChangeGroupsOrderAsync()`);
this._configurationService.ExitConfigurationModeAsync();
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
yield this.GetGroupsAsync(false);
return Status.OK;
default:
Api.Get(LoggerService.ServiceName).Error(ChannelsGroupsService.ServiceName, `Failed to change groups order, controller responded with error ${result === null || result === void 0 ? void 0 : result.Status} data: ${result === null || result === void 0 ? void 0 : result.Data}`);
this.LogGroupsOrder(orderedGroups);
return result === null || result === void 0 ? void 0 : result.Status;
}
});
}
ChangeObjectsPositionsInGroupAsync(group, channels) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const objs = channels.select(a => a.ChannelId).toArray();
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new MoveObjectsInGroupRequest(group.Guid, objs), 15000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! ChangeObjectsPositionsInGroupAsync()`);
this._configurationService.ExitConfigurationModeAsync();
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
return Status.OK;
default:
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to change objects positions in group, controller responded with error ${result === null || result === void 0 ? void 0 : result.Status} data: ${result === null || result === void 0 ? void 0 : result.Data}`);
this.LogObjectsInGroupOrder(group, channels);
return result === null || result === void 0 ? void 0 : result.Status;
}
});
}
IsGroupsAndObjectsOrderingAvailable() {
return this._objectsGroupingSupported;
//return !this._groups.any(a => a.ObjectsOrder === undefined || a.ObjectsOrder === null);
}
MoveGroupPositionAsync(group, newPosition) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new MoveGroupRequest(group.Guid, newPosition), 15000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! MoveGroupPositionAsync()`);
this._configurationService.ExitConfigurationModeAsync();
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
yield this.GetGroupsAsync(false);
return Status.OK;
default:
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to move group position, controller responded with error ${result === null || result === void 0 ? void 0 : result.Status} data: ${result === null || result === void 0 ? void 0 : result.Data}`);
return result === null || result === void 0 ? void 0 : result.Status;
}
});
}
MoveObjectInGroupPositionAsync(group, channel, newPosition) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const objectId = `${channel.GetDevice().Guid}_${channel.Number}`;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new MoveObjectInGroupRequest(group.Guid, objectId, newPosition), 15000, false));
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! MoveObjectInGroupPositionAsync()`);
this._configurationService.ExitConfigurationModeAsync();
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
return Status.OK;
default:
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to move object in group position, controller responded with error ${result === null || result === void 0 ? void 0 : result.Status} data: ${result === null || result === void 0 ? void 0 : result.Data}`);
return result === null || result === void 0 ? void 0 : result.Status;
}
});
}
AddDeviceChannelToGroupByGuidAsync(channel_1, groupGuid_1) {
return __awaiter(this, arguments, void 0, function* (channel, groupGuid, skipDataUpdate = false) {
var _a, _b, _c, _d;
const req = new AddDeviceChannelToGroupRequest();
req.Data.Channel = channel.Number;
let device = channel.GetDevice();
if (device == null)
throw new Error("Device is not set on this channel!");
req.Data.DeviceGuid = (_a = channel.GetDevice()) === null || _a === void 0 ? void 0 : _a.Guid;
req.Data.GroupGuid = groupGuid;
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! AddDeviceChannelToGroupByGuidAsync()`);
this._configurationService.ExitConfigurationModeAsync();
if (result != null && result.Status != null) {
if (result.Status == Status.OK)
channel.ChannelGroups.push(groupGuid);
else
(_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Error(ChannelsGroupsService.ServiceName, `Failed to add device channel ${channel.Name} to group ${groupGuid}, controller responded with error ${result.Status} data: ${result.Data}`);
if (!skipDataUpdate) {
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
}
return result.Status;
}
if (!skipDataUpdate) {
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
}
return Status.Error;
});
}
RemoveDeviceChannelFromGroupByGuidAsync(channel_1, groupGuid_1) {
return __awaiter(this, arguments, void 0, function* (channel, groupGuid, skipDataUpdate = false) {
var _a, _b, _c;
const req = new RemoveDeviceChannelToGroupRequest();
let device = channel.GetDevice();
if (device == null)
throw new Error("Device is not set on this channel!");
req.Data.DeviceGuid = (_a = channel.GetDevice()) === null || _a === void 0 ? void 0 : _a.Guid;
req.Data.Channel = channel.Number;
req.Data.GroupGuid = groupGuid;
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
if (!skipDataUpdate)
this._configurationService.ExitConfigurationModeAsync();
if (result != null && result.Status != null) {
if (result.Status == Status.OK)
channel.ChannelGroups = channel.ChannelGroups.where(a => a != groupGuid).toArray();
else
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to remove device channel ${channel.Name} from group ${groupGuid}, controller responded with error ${result.Status} data: ${result.Data}`);
if (!skipDataUpdate)
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
return result.Status;
}
if (!skipDataUpdate)
yield this.GetGroupsAsync(false);
yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
return Status.Error;
});
}
UpsertGroupWithDevicesChannelsAsync(group, deviceChannels) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
if (this._groups.all(g => g.Guid != group.Guid)) {
let result = yield this.AddNewGroupWithIconAsync(group.Name, group.IconName);
if (result instanceof ChannelsGroup)
group = result;
else if (result instanceof ResponseResult)
return result;
else
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
}
/// add devices channels to groups that are not already in groups
for (let channel of deviceChannels.where(a => a.ChannelGroups.all(b => b !== group.Guid))) {
let status = yield this.AddDeviceChannelToGroupAsync(channel, group, true);
if (status !== Status.OK) {
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(ChannelsGroupsService.ServiceName, `Failed to add device channel ${channel.Name} to group ${group.Name}, controller responded with error ${status}`);
return new ResponseResult(AddNewGroupErrorCode.UnknownError, `Failed to add device channel ${channel.Name} to group ${group.Name}, controller responded with error ${status}`);
}
}
let devices = yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
for (let device of devices) {
for (let channel of device.Channels.except(deviceChannels, (a, b) => a.GetDevice().Guid == b.GetDevice().Guid && a.Number == b.Number)) {
for (var gr of channel.ChannelGroups.where(gr => group.Guid == gr)) {
let status = yield this.RemoveDeviceChannelFromGroupAsync(channel, group, true);
if (status !== Status.OK) {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `Failed to remove device channel ${channel.Name} from group ${group.Name}, controller responded with error ${status}`);
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! UpsertGroupWithDevicesChannelsAsync()`);
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
return new ResponseResult(AddNewGroupErrorCode.UnknownError, `Failed to remove device channel ${channel.Name} from group ${group.Name}, controller responded with error ${status}`);
}
}
}
}
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
return group;
});
}
UpdateGroupWithDevicesChannelsAsync(groupGuid, deviceChannels) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
/// add devices channels to groups that are not already in groups
for (let channel of deviceChannels.where(a => a.ChannelGroups.all(b => b !== groupGuid))) {
let status = yield this.AddDeviceChannelToGroupByGuidAsync(channel, groupGuid, true);
if (status !== Status.OK) {
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(ChannelsGroupsService.ServiceName, `Failed to add device channel ${channel.Name} to group ${groupGuid}, controller responded with error ${status}`);
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! UpdateGroupWithDevicesChannelsAsync()`);
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
return Status.FatalError;
}
}
let devices = yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
for (let device of devices) {
for (let channel of device.Channels.except(deviceChannels, (a, b) => a.GetDevice().Guid == b.GetDevice().Guid && a.Number == b.Number)) {
for (var gr of channel.ChannelGroups.where(gr => groupGuid == gr)) {
let status = yield this.RemoveDeviceChannelFromGroupByGuidAsync(channel, groupGuid, true);
if (status !== Status.OK) {
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to remove device channel ${channel.Name} from group ${groupGuid}, controller responded with error ${status}`);
(_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Error(ChannelsGroupsService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! UpdateGroupWithDevicesChannelsAsync()`);
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
return Status.FatalError;
}
}
}
}
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
return Status.OK;
});
}
ChangeGroupNameByGuidAsync(groupGuid, name) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let req = new AddUpdateChannelsGroupsRequest();
let gr = new ApiGroupObject();
let tmp = this._groups.first(a => a.Guid === groupGuid);
gr.Guid = groupGuid;
gr.Name = name;
gr.IconName = tmp.IconName;
(_a = req.Data) === null || _a === void 0 ? void 0 : _a.push(gr);
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
this._groups = yield this.GetGroupsAsync(false);
if (result != null && result.Status != null) {
switch (result.Status) {
case Status.OK:
return new ResponseResult(AddNewGroupErrorCode.Success, "");
case Status.WrongData:
switch (result.Data) {
case "NameToShort":
return new ResponseResult(AddNewGroupErrorCode.NameToShort, "At least 3 characters required");
case "NameAlreadyExists":
return new ResponseResult(AddNewGroupErrorCode.NameAlreadyExists, "Group with this name already exists");
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
}
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
});
}
ChangeGroupNameAndIconAsync(group, name, iconName) {
return this.ChangeGroupNameAndIconByGuidAsync(group.Guid, name, iconName);
}
ChangeGroupNameAndIconByGuidAsync(groupGuid, name, iconName) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let req = new AddUpdateChannelsGroupsRequest();
let gr = new ApiGroupObject();
let tmp = this._groups.first(a => a.Guid === groupGuid);
gr.Guid = groupGuid;
gr.Name = name;
gr.IconName = iconName;
gr.ObjectsOrder = tmp.ObjectsOrder;
(_a = req.Data) === null || _a === void 0 ? void 0 : _a.push(gr);
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
this._groups = yield this.GetGroupsAsync(false);
if (result != null && result.Status != null) {
switch (result.Status) {
case Status.OK:
return new ResponseResult(AddNewGroupErrorCode.Success, "");
case Status.WrongData:
switch (result.Data) {
case "NameToShort":
return new ResponseResult(AddNewGroupErrorCode.NameToShort, "At least 3 characters required");
case "NameAlreadyExists":
return new ResponseResult(AddNewGroupErrorCode.NameAlreadyExists, "Group with this name already exists");
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
}
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
});
}
ChangeGroupIconNameByGuidAsync(groupGuid, iconName) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let req = new AddUpdateChannelsGroupsRequest();
let gr = new ApiGroupObject();
let tmp = this._groups.first(a => a.Guid === groupGuid);
gr.Guid = groupGuid;
gr.Name = tmp.Name;
gr.IconName = iconName;
(_a = req.Data) === null || _a === void 0 ? void 0 : _a.push(gr);
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
this._groups = yield this.GetGroupsAsync(false);
if (result != null && result.Status != null) {
switch (result.Status) {
case Status.OK:
return new ResponseResult(AddNewGroupErrorCode.Success, "");
case Status.WrongData:
switch (result.Data) {
case "NameToShort":
return new ResponseResult(AddNewGroupErrorCode.NameToShort, "At least 3 characters required");
case "NameAlreadyExists":
return new ResponseResult(AddNewGroupErrorCode.NameAlreadyExists, "Group with this name already exists");
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
}
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
});
}
GetServiceName() {
return ChannelsGroupsService.ServiceName;
}
WaitForSynchronizationAsync() {
return this._synchronizationTaskCompletionSource;
}
GetGroupByGuid(guid) {
return this._groups.first(a => a.Guid === guid);
}
ChangeGroupNameAsync(group, name) {
return this.ChangeGroupNameByGuidAsync(group.Guid, name);
}
ChangeGroupIconNameAsync(group, iconName) {
return this.ChangeGroupNameByGuidAsync(group.Guid, iconName);
}
RemoveGroupAsync(group) {
return this.RemoveGroupByGuidAsync(group.Guid);
}
RemoveGroupByGuidAsync(groupGuid) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let req = new RemoveGroupRequest();
req.Data = groupGuid;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
if (result != null && result.Status != null)
return result === null || result === void 0 ? void 0 : result.Status;
return Status.Error;
});
}
GetGroupsAsync() {
return __awaiter(this, arguments, void 0, function* (useCache = true) {
var _a, _b;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetGroupsRequest(), 15000, useCache));
let groups = [];
switch (result === null || result === void 0 ? void 0 : result.Status) {
case Status.OK:
let i = 0;
let log = Api.Get(LoggerService.ServiceName);
let devs = yield Api.Get(DevicesService.ServiceName).GetDevicesAsync();
(_b = result.Data) === null || _b === void 0 ? void 0 : _b.forEach(group => {
i++;
let gr = new ChannelsGroup();
gr.Guid = group.Guid;
gr.Name = group.Name;
gr.IconName = group.IconName;
gr.ObjectsOrder = group.ObjectsOrder;
gr.OrderPosition = group.OrderPosition;
if (gr.OrderPosition == undefined)
gr.OrderPosition = i;
if (gr.ObjectsOrder == undefined) {
this._objectsGroupingSupported = false;
gr.ObjectsOrder = {};
let tmp = devs.selectMany(a => a.Channels.where(b => b.ChannelGroups.any(c => c == gr.Guid)));
let position = 0;
for (let channel of tmp) {
position++;
gr.ObjectsOrder[position] = channel.ChannelId;
}
}
else
this._objectsGroupingSupported = true;
if (!useCache)
log.Info(ChannelsGroupsService.ServiceName, `Got group: ${gr.Name} ${gr.Guid} order: ${gr.OrderPosition} objects order: ${JSON.stringify(gr.ObjectsOrder)}`);
groups.push(gr);
});
this._groups = groups;
return groups;
default:
throw new Error(`Failed to get groups, API responded with status ${result === null || result === void 0 ? void 0 : result.Status} data: ${result === null || result === void 0 ? void 0 : result.Data}`);
}
});
}
AddDeviceChannelToGroupAsync(channel, group, skipDataUpdate = false) {
return this.AddDeviceChannelToGroupByGuidAsync(channel, group.Guid, skipDataUpdate);
}
RemoveDeviceChannelFromGroupAsync(channel, group, skipDataUpdate = false) {
return this.RemoveDeviceChannelFromGroupByGuidAsync(channel, group.Guid, skipDataUpdate);
}
AddNewGroupAsync(name) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
let req = new AddUpdateChannelsGroupsRequest();
let gr = new ApiGroupObject();
gr.Name = name;
(_a = req.Data) === null || _a === void 0 ? void 0 : _a.push(gr);
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
if (result != null && result.Status != null) {
switch (result.Status) {
case Status.OK:
return this._groups.first(a => a.Name === name);
case Status.WrongData:
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to add new group, controller responded with error ${result.Status} data: ${result.Data}`);
switch (result.Data) {
case "NameToShort":
return new ResponseResult(AddNewGroupErrorCode.NameToShort, "At least 3 characters required");
case "NameAlreadyExists":
return new ResponseResult(AddNewGroupErrorCode.NameAlreadyExists, "Group with this name already exists");
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
}
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
});
}
AddNewGroupWithIconAsync(name, iconName) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
let req = new AddUpdateChannelsGroupsRequest();
let gr = new ApiGroupObject();
gr.Name = name;
gr.IconName = iconName;
(_a = req.Data) === null || _a === void 0 ? void 0 : _a.push(gr);
const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(req, 15000, false));
this._configurationService.ExitConfigurationModeAsync();
yield this.GetGroupsAsync(false);
if (result != null && result.Status != null) {
switch (result.Status) {
case Status.OK:
return this._groups.first(a => a.Name === name);
case Status.WrongData:
(_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(ChannelsGroupsService.ServiceName, `Failed to add new group, controller responded with error ${result.Status} data: ${result.Data}`);
switch (result.Data) {
case "NameToShort":
return new ResponseResult(AddNewGroupErrorCode.NameToShort, "At least 3 characters required");
case "NameAlreadyExists":
return new ResponseResult(AddNewGroupErrorCode.NameAlreadyExists, "Group with this name already exists");
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
default:
return new ResponseResult(AddNewGroupErrorCode.OtherError, `Response error: ${result.Status} data: ${result.Data}`);
}
}
return new ResponseResult(AddNewGroupErrorCode.UnknownError, "");
});
}
}
ChannelsGroupsService.ServiceName = "ChannelsGroupsService";
class MoveGroupRequest extends DataFrame {
constructor(groupGuid, newPosition) {
super();
this.Resource = "/devices/groups/group/move";
this.Method = Method.Post;
this.Data = new MoveGroup();
this.Data.GroupGuid = groupGuid;
this.Data.NewPosition = newPosition;
}
}
class ChangeGroupsOrderRequest extends DataFrame {
constructor(orderedObjectsGuid) {
super();
this.Resource = "/devices/groups/move";
this.Method = Method.Post;
this.Data = orderedObjectsGuid;
}
}
class SetObjectsInGroupAndOrderRequest extends DataFrame {
constructor(group, objectsIds) {
super();
this.Resource = "/devices/groups/group/objects/set";
this.Method = Method.Post;
this.Data = new ObjectsAndOrderInGroup();
this.Data.GroupGuid = group.Guid;
this.Data.ObjectsIds = objectsIds;
}
}
class ObjectsAndOrderInGroup {
constructor() {
this.GroupGuid = "";
this.ObjectsIds = [];
}
}
class MoveGroup {
constructor() {
this.GroupGuid = "";
this.NewPosition = 0;
}
}
class MoveObjectInGroupRequest extends DataFrame {
constructor(groupGuid, objectId, newPosition) {
super();
this.Resource = "/devices/groups/group/object/move";
this.Method = Method.Post;
this.Data = new MoveObjectInGroup();
this.Data.GroupGuid = groupGuid;
this.Data.NewPosition = newPosition;
this.Data.ObjectId = objectId;
}
}
class MoveObjectsInGroupRequest extends DataFrame {
constructor(groupGuid, orderedObjectsIds) {
super();
this.Resource = "/devices/groups/group/objects/move";
this.Method = Method.Post;
this.Data = new MoveObjectsInGroup();
this.Data.GroupGuid = groupGuid;
this.Data.OrderedObjectsIds = orderedObjectsIds;
}
}
class MoveObjectInGroup {
constructor() {
this.GroupGuid = "";
this.ObjectId = "";
this.NewPosition = 0;
}
}
class MoveObjectsInGroup {
constructor() {
this.GroupGuid = "";
this.OrderedObjectsIds = [];
}
}
class RemoveGroupRequest extends DataFrame {
constructor() {
super();
this.Resource = "/devices/groups/group";
this.Method = Method.Delete;
}
}
class GetGroupsRequest extends DataFrame {
constructor() {
super();
this.Resource = "/devices/groups/";
this.Method = Method.Get;
}
}
class AddUpdateChannelsGroupsRequest extends DataFrame {
constructor() {
super();
this.Resource = "/devices/groups/group";
this.Method = Method.Put;
this.Data = [];
}
}
class AddDeviceChannelToGroupRequest extends DataFrame {
constructor() {
super();
this.Resource = "/devices/device/channel/groups/group/";
this.Method = Method.Put;
this.Data = new AddRemoveDeviceChannelToGroup();
}
}
class RemoveDeviceChannelToGroupRequest extends DataFrame {
constructor() {
super();
this.Resource = "/devices/device/channel/groups/group/";
this.Method = Method.Delete;
this.Data = new AddRemoveDeviceChannelToGroup();
}
}
class ApiGroupObject {
constructor() {
this.Id = 0;
this.Guid = "";
this.Name = "";
this.IconName = "";
this.OrderPosition = 0;
this.ObjectsOrder = {};
}
}
class AddRemoveDeviceChannelToGroup {
constructor() {
this.GroupGuid = "";
this.DeviceGuid = "";
this.Channel = 0;
}
}
//# sourceMappingURL=ChannelsGroupsService.js.map