Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-131557 / 25.04 / Use the new services for websocket connection #11030

Merged
merged 13 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions src/app/core/guards/translations-loaded.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
catchError, map, timeout,
} from 'rxjs/operators';
import { LanguageService } from 'app/services/language.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

/**
* Ensures that translations have been loaded.
Expand All @@ -18,9 +18,9 @@ export class TranslationsLoadedGuard {
isConnected = false;
constructor(
private languageService: LanguageService,
private wsManager: WebSocketConnectionService,
private wsHandler: WebSocketHandlerService,
) {
this.wsManager.isConnected$.pipe(untilDestroyed(this)).subscribe((isConnected) => {
this.wsHandler.isConnected$.pipe(untilDestroyed(this)).subscribe((isConnected) => {
this.isConnected = isConnected;
});
}
Expand Down
16 changes: 10 additions & 6 deletions src/app/core/guards/websocket-connection.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Router } from '@angular/router';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService } from '@ngx-translate/core';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

@UntilDestroy()
@Injectable({ providedIn: 'root' })
export class WebSocketConnectionGuard {
isConnected = false;
constructor(
private wsManager: WebSocketConnectionService,
private wsManager: WebSocketHandlerService,
protected router: Router,
private matDialog: MatDialog,
private dialogService: DialogService,
Expand All @@ -20,7 +20,9 @@ export class WebSocketConnectionGuard {
this.wsManager.isClosed$.pipe(untilDestroyed(this)).subscribe((isClosed) => {
if (isClosed) {
this.resetUi();
this.wsManager.isClosed$ = false;
// TODO: Test why manually changing close status is needed
// Test a shutdown function to see how UI acts when this isn't done
// this.wsManager.isClosed$ = false;
}
});

Expand All @@ -34,7 +36,7 @@ export class WebSocketConnectionGuard {

private resetUi(): void {
this.closeAllDialogs();
if (!this.wsManager.shutDownInProgress) {
if (!this.wsManager.isSystemShuttingDown) {
this.router.navigate(['/signin']);
}
}
Expand All @@ -49,8 +51,10 @@ export class WebSocketConnectionGuard {
this.dialogService.fullScreenDialog({
title: this.translate.instant('Access restricted'),
message: this.translate.instant('Access from your IP is restricted'),
}).pipe(untilDestroyed(this)).subscribe(() => {
this.wsManager.reconnect();
}).pipe(untilDestroyed(this)).subscribe({
next: () => {
this.wsManager.reconnect();
},
});
}

Expand Down
10 changes: 4 additions & 6 deletions src/app/core/testing/classes/mock-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { when } from 'jest-when';
import { Observable, Subject, of } from 'rxjs';
Expand All @@ -20,8 +19,8 @@ import {
} from 'app/interfaces/api/api-job-directory.interface';
import { ApiEvent } from 'app/interfaces/api-message.interface';
import { Job } from 'app/interfaces/job.interface';
import { ApiService } from 'app/services/api.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { ApiService } from 'app/services/websocket/api.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

/**
* Better than just expect.anything() because it allows null and undefined.
Expand All @@ -46,11 +45,10 @@ export class MockApiService extends ApiService {
private jobIdCounter = 1;

constructor(
protected override router: Router,
protected override wsManager: WebSocketConnectionService,
protected override wsHandler: WebSocketHandlerService,
protected override translate: TranslateService,
) {
super(router, wsManager, translate);
super(wsHandler, translate);

this.call = jest.fn();
this.job = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { environment } from 'environments/environment';
import { Observable, of } from 'rxjs';
Expand All @@ -8,8 +7,8 @@ import { MockEnclosureConfig } from 'app/core/testing/mock-enclosure/interfaces/
import { MockEnclosureGenerator } from 'app/core/testing/mock-enclosure/mock-enclosure-generator.utils';
import { ApiCallMethod, ApiCallParams, ApiCallResponse } from 'app/interfaces/api/api-call-directory.interface';
import { SystemInfo } from 'app/interfaces/system-info.interface';
import { ApiService } from 'app/services/api.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { ApiService } from 'app/services/websocket/api.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

@Injectable({
providedIn: 'root',
Expand All @@ -19,11 +18,10 @@ export class MockEnclosureApiService extends ApiService {
private mockStorage = new MockEnclosureGenerator(this.mockConfig);

constructor(
router: Router,
wsManager: WebSocketConnectionService,
wsManager: WebSocketHandlerService,
translate: TranslateService,
) {
super(router, wsManager, translate);
super(wsManager, translate);

console.warn('MockEnclosureApiService is in effect. Some calls will be mocked');
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/testing/utils/empty-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMissingInjectionErrorFactory, getMissingInjectionErrorObservable } from 'app/core/testing/utils/missing-injection-factories';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';

export class EmptyApiService {
readonly clearSubscriptions$ = getMissingInjectionErrorObservable(ApiService.name);
Expand Down
15 changes: 7 additions & 8 deletions src/app/core/testing/utils/mock-api.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
ExistingProvider, FactoryProvider, forwardRef, ValueProvider,
} from '@angular/core';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { MockApiService } from 'app/core/testing/classes/mock-api.service';
import {
Expand All @@ -12,8 +11,8 @@ import {
import { ApiCallMethod } from 'app/interfaces/api/api-call-directory.interface';
import { ApiJobDirectory, ApiJobMethod } from 'app/interfaces/api/api-job-directory.interface';
import { Job } from 'app/interfaces/job.interface';
import { ApiService } from 'app/services/api.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { ApiService } from 'app/services/websocket/api.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

/**
* This is a sugar syntax for creating simple api mocks.
Expand Down Expand Up @@ -48,8 +47,8 @@ export function mockApi(
return [
{
provide: ApiService,
useFactory: (router: Router, wsManager: WebSocketConnectionService, translate: TranslateService) => {
const mockApiService = new MockApiService(router, wsManager, translate);
useFactory: (wsHandler: WebSocketHandlerService, translate: TranslateService) => {
const mockApiService = new MockApiService(wsHandler, translate);
(mockResponses || []).forEach((mockResponse) => {
if (mockResponse.type === MockApiResponseType.Call) {
mockApiService.mockCall(mockResponse.method, mockResponse.response);
Expand All @@ -62,15 +61,15 @@ export function mockApi(
});
return mockApiService;
},
deps: [Router, WebSocketConnectionService, TranslateService],
deps: [WebSocketHandlerService, TranslateService],
},
{
provide: MockApiService,
useExisting: forwardRef(() => ApiService),
},
{
provide: WebSocketConnectionService,
useValue: ({ send: jest.fn() } as unknown as WebSocketConnectionService),
provide: WebSocketHandlerService,
useValue: ({ send: jest.fn() } as unknown as WebSocketHandlerService),
},
];
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/core/testing/utils/mock-auth.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { MockAuthService } from 'app/core/testing/classes/mock-auth.service';
import { AccountAttribute } from 'app/enums/account-attribute.enum';
import { Role } from 'app/enums/role.enum';
import { LoggedInUser } from 'app/interfaces/ds-cache.interface';
import { ApiService } from 'app/services/api.service';
import { AuthService } from 'app/services/auth/auth.service';
import { TokenLastUsedService } from 'app/services/token-last-used.service';
import { WebSocketConnectionService } from 'app/services/websocket-connection.service';
import { ApiService } from 'app/services/websocket/api.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

export const dummyUser = {
privilege: {
Expand Down Expand Up @@ -40,7 +40,7 @@ export function mockAuth(
provide: AuthService,
useFactory: () => {
const mockService = new MockAuthService(
createSpyObject(WebSocketConnectionService, {
createSpyObject(WebSocketHandlerService, {
isConnected$: of(true),
}),
createSpyObject(Store),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AlertEffects } from 'app/modules/alerts/store/alert.effects';
import { adapter, alertReducer, alertsInitialState } from 'app/modules/alerts/store/alert.reducer';
import { alertStateKey, selectAlerts } from 'app/modules/alerts/store/alert.selectors';
import { FormatDateTimePipe } from 'app/modules/pipes/format-date-time/format-datetime.pipe';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';
import { systemConfigReducer, SystemConfigState } from 'app/store/system-config/system-config.reducer';
import { systemConfigStateKey } from 'app/store/system-config/system-config.selectors';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { AlertsPanelPageObject } from 'app/modules/alerts/components/alerts-pane
import { AlertEffects } from 'app/modules/alerts/store/alert.effects';
import { adapter, alertReducer, alertsInitialState } from 'app/modules/alerts/store/alert.reducer';
import { alertStateKey } from 'app/modules/alerts/store/alert.selectors';
import { ApiService } from 'app/services/api.service';
import { SystemGeneralService } from 'app/services/system-general.service';
import { ApiService } from 'app/services/websocket/api.service';
import { adminUiInitialized } from 'app/store/admin-panel/admin.actions';
import { haInfoReducer } from 'app/store/ha-info/ha-info.reducer';
import { haInfoStateKey } from 'app/store/ha-info/ha-info.selectors';
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/alerts/store/alert.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
import {
AlertSlice, selectDismissedAlerts, selectIsAlertPanelOpen, selectUnreadAlerts,
} from 'app/modules/alerts/store/alert.selectors';
import { ApiService } from 'app/services/api.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';
import { adminUiInitialized } from 'app/store/admin-panel/admin.actions';
import { alertIndicatorPressed } from 'app/store/topbar/topbar.actions';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { AuditEntry } from 'app/interfaces/audit/audit.interface';
import { Job } from 'app/interfaces/job.interface';
import { ExportButtonComponent } from 'app/modules/buttons/export-button/export-button.component';
import { SortDirection } from 'app/modules/ix-table/enums/sort-direction.enum';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ApiService } from 'app/services/websocket/api.service';
import { selectIsHaLicensed } from 'app/store/ha-info/ha-info.selectors';

describe('ExportButtonComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { AdvancedSearchQuery, SearchQuery } from 'app/modules/forms/search-input
import { SortDirection } from 'app/modules/ix-table/enums/sort-direction.enum';
import { TableSort } from 'app/modules/ix-table/interfaces/table-sort.interface';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';
import { AppState } from 'app/store';
import { selectIsHaLicensed } from 'app/store/ha-info/ha-info.selectors';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { CopyButtonComponent } from 'app/modules/buttons/copy-button/copy-button
import { DialogService } from 'app/modules/dialog/dialog.service';
import { IxIconComponent } from 'app/modules/ix-icon/ix-icon.component';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';

@UntilDestroy()
@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
JobProgressDialogConfig,
} from 'app/modules/dialog/components/job-progress/job-progress-dialog.component';
import { IxIconHarness } from 'app/modules/ix-icon/ix-icon.harness';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';

describe('JobProgressDialogComponent', () => {
let spectator: Spectator<JobProgressDialogComponent<unknown>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { JobState } from 'app/enums/job-state.enum';
import { Job, JobProgress } from 'app/interfaces/job.interface';
import { IxIconComponent } from 'app/modules/ix-icon/ix-icon.component';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';

export interface JobProgressDialogConfig<Result> {
job$: Observable<Job<Result>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { Job } from 'app/interfaces/job.interface';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { IxIconComponent } from 'app/modules/ix-icon/ix-icon.component';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';

@UntilDestroy()
@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { mockCall, mockApi } from 'app/core/testing/utils/mock-api.utils';
import { Job } from 'app/interfaces/job.interface';
import { CopyButtonComponent } from 'app/modules/buttons/copy-button/copy-button.component';
import { ShowLogsDialogComponent } from 'app/modules/dialog/components/show-logs-dialog/show-logs-dialog.component';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ApiService } from 'app/services/websocket/api.service';

describe('ShowLogsDialogComponent', () => {
let spectator: Spectator<ShowLogsDialogComponent>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { Job } from 'app/interfaces/job.interface';
import { CopyButtonComponent } from 'app/modules/buttons/copy-button/copy-button.component';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { DownloadService } from 'app/services/download.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';

@UntilDestroy()
@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ServiceStatus } from 'app/enums/service-status.enum';
import { Service } from 'app/interfaces/service.interface';
import { StartServiceDialogComponent, StartServiceDialogResult } from 'app/modules/dialog/components/start-service-dialog/start-service-dialog.component';
import { IxSlideToggleHarness } from 'app/modules/forms/ix-forms/components/ix-slide-toggle/ix-slide-toggle.harness';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';
import { ServicesState } from 'app/store/services/services.reducer';
import { selectServices } from 'app/store/services/services.selectors';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { IxSlideToggleComponent } from 'app/modules/forms/ix-forms/components/ix
import { FakeProgressBarComponent } from 'app/modules/loader/components/fake-progress-bar/fake-progress-bar.component';
import { SnackbarService } from 'app/modules/snackbar/services/snackbar.service';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { ApiService } from 'app/services/websocket/api.service';
import { ServicesState } from 'app/store/services/services.reducer';
import { selectService } from 'app/store/services/services.selectors';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { IxValidatorsService } from 'app/modules/forms/ix-forms/services/ix-vali
import { emailValidator } from 'app/modules/forms/ix-forms/validators/email-validation/email-validation';
import { ImageValidatorService } from 'app/modules/forms/ix-forms/validators/image-validator/image-validator.service';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';

@UntilDestroy()
@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IxInputComponent } from 'app/modules/forms/ix-forms/components/ix-input
import { IxTextareaComponent } from 'app/modules/forms/ix-forms/components/ix-textarea/ix-textarea.component';
import { FormErrorHandlerService } from 'app/modules/forms/ix-forms/services/form-error-handler.service';
import { ImageValidatorService } from 'app/modules/forms/ix-forms/validators/image-validator/image-validator.service';
import { ApiService } from 'app/services/api.service';
import { ApiService } from 'app/services/websocket/api.service';

@UntilDestroy()
@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
import { ProductType } from 'app/enums/product-type.enum';
import { FeedbackService } from 'app/modules/feedback/services/feedback.service';
import { SnackbarComponent } from 'app/modules/snackbar/components/snackbar/snackbar.component';
import { ApiService } from 'app/services/api.service';
import { SentryService } from 'app/services/sentry.service';
import { SystemGeneralService } from 'app/services/system-general.service';
import { UploadService } from 'app/services/upload.service';
import { ApiService } from 'app/services/websocket/api.service';
import { SystemInfoState } from 'app/store/system-info/system-info.reducer';
import { selectProductType, selectSystemInfoState } from 'app/store/system-info/system-info.selectors';

Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/feedback/services/feedback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import {
SimilarIssue,
} from 'app/modules/feedback/interfaces/file-ticket.interface';
import { SnackbarComponent } from 'app/modules/snackbar/components/snackbar/snackbar.component';
import { ApiService } from 'app/services/api.service';
import { SentryService } from 'app/services/sentry.service';
import { SystemGeneralService } from 'app/services/system-general.service';
import { UploadService } from 'app/services/upload.service';
import { ApiService } from 'app/services/websocket/api.service';
import { AppState } from 'app/store';
import { SystemInfoState } from 'app/store/system-info/system-info.reducer';
import { selectProductType, selectSystemInfoState, waitForSystemInfo } from 'app/store/system-info/system-info.selectors';
Expand Down
Loading
Loading