UNPKG

@c8y/ngx-components

Version:

Angular modules for Cumulocity IoT applications

146 lines (141 loc) 5.67 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; import * as i1 from '@c8y/client'; import { gettext } from '@c8y/ngx-components/gettext'; import * as i2 from '@c8y/ngx-components'; import { map } from 'lodash-es'; const SERVICE_FRAGMENT = 'c8y_Service'; const SERVICE_COMMANDS_MAP = { START: { label: gettext('Start'), command: 'START', icon: 'play-circle', styleClass: 'text-success' }, STOP: { label: gettext('Stop'), command: 'STOP', icon: 'stop-circle', styleClass: 'text-danger' }, RESTART: { label: gettext('Restart'), command: 'RESTART', icon: 'refresh', styleClass: 'text-info' } }; class ServiceCommandService { constructor(operationService, alertService) { this.operationService = operationService; this.alertService = alertService; } /** * Check if the service supports service commands. * @param service The service object containing supported operations. * @returns boolean indicating if the service supports service commands. */ isServiceCommandSupported(service) { return service.c8y_SupportedOperations?.includes('c8y_ServiceCommand'); } /** * Get the list of supported service commands for the given service object. * @param service The service object containing the c8y_SupportedServiceCommands fragment. * @returns Array of supported commands. */ getSupportedCommands(service) { return service.c8y_SupportedServiceCommands || map(SERVICE_COMMANDS_MAP, 'command'); } /** * Check if a specific command is supported for the given service. * @param service The service object. * @param command The command to check (e.g., START, STOP). * @returns boolean indicating if the command is supported. */ isCommandSupported(service, command) { const supportedCommands = this.getSupportedCommands(service); return supportedCommands.includes(command); } /** * Get all supported commands for a list of services. * @param services The list of service objects to evaluate. * @returns Array of all unique supported commands. */ getAllSupportedCommands(services) { const allCommands = services .filter(service => this.isServiceCommandSupported(service)) .flatMap(service => this.getSupportedCommands(service)); return Array.from(new Set(allCommands)); } /** * Create an operation for the selected service command. * @param service The service object the operation should target. * @param command The command to execute (e.g., START, STOP). * @returns Promise of the created operation. */ createOperation({ id, name, serviceType }, command) { const operation = { deviceId: id, description: `${command} ${name}`, c8y_ServiceCommand: { command, serviceName: name, serviceType: serviceType } }; return this.operationService.create(operation); } /** * Generate a static list of action controls. * The specific logic for each service is handled dynamically in showIf and callback. * @returns Array of ActionControl objects. */ generateActionControls(commands) { return commands.map(command => ({ type: command.toLowerCase(), icon: this.getCommandIcon(command), iconClasses: this.getCommandIconClass(command), text: command, showIf: (service) => this.isServiceCommandSupported(service) && this.isCommandSupported(service, command), callback: async (service) => { try { await this.createOperation(service, command); this.alertService.success(gettext('Operation created.')); } catch (error) { const alertMessage = gettext('Could not create operation.'); this.alertService.danger(alertMessage, error); } } })); } /** * Get the icon for a specific command. * @param command The command name. * @returns Icon string for the command. */ getCommandIcon(command) { return SERVICE_COMMANDS_MAP[command]?.icon || 'console'; } /** * Get the icon class for a specific command. * @param command The command name. * @returns Icon class string for the command. */ getCommandIconClass(command) { return SERVICE_COMMANDS_MAP[command]?.styleClass; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: ServiceCommandService, deps: [{ token: i1.OperationService }, { token: i2.AlertService }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: ServiceCommandService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.15", ngImport: i0, type: ServiceCommandService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i1.OperationService }, { type: i2.AlertService }] }); /** * Generated bundle index. Do not edit. */ export { SERVICE_COMMANDS_MAP, SERVICE_FRAGMENT, ServiceCommandService }; //# sourceMappingURL=c8y-ngx-components-services-shared.mjs.map