2024-05-02 21:37:11 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
2024-05-07 11:08:39 +03:00
|
|
|
import { BadgePosition, BadgeStyle, badgeStyleURLMap, MobileAppQRCodeSettings } from '@shared/models/mobile-app.models';
|
2024-05-02 21:37:11 +03:00
|
|
|
import { MobileAppService } from '@core/http/mobile-app.service';
|
|
|
|
|
import { WidgetContext } from '@home/models/widget-component.models';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
|
|
|
|
import { interval, mergeMap, Observable, Subject, takeUntil } from 'rxjs';
|
|
|
|
|
import { MINUTE } from '@shared/models/time/time.models';
|
|
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2024-05-07 17:14:31 +03:00
|
|
|
import { MobileAppQrCodeWidgetSettings } from '@home/components/widget/lib/cards/mobile-app-qr-code-widget.models';
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-mobile-app-qrcode-widget',
|
|
|
|
|
templateUrl: './mobile-app-qrcode-widget.component.html',
|
|
|
|
|
styleUrls: ['./mobile-app-qrcode-widget.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class MobileAppQrcodeWidgetComponent extends PageComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
ctx: WidgetContext;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
previewMode: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2024-05-07 11:08:39 +03:00
|
|
|
set mobileAppSettings(settings: MobileAppQRCodeSettings | MobileAppQrCodeWidgetSettings) {
|
2024-05-02 21:37:11 +03:00
|
|
|
if (settings) {
|
|
|
|
|
this.mobileAppSettingsValue = settings;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get mobileAppSettings() {
|
|
|
|
|
return this.mobileAppSettingsValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ViewChild('canvas', {static: false}) canvasRef: ElementRef<HTMLCanvasElement>;
|
|
|
|
|
|
|
|
|
|
private readonly destroy$ = new Subject<void>();
|
|
|
|
|
|
|
|
|
|
badgeStyle = BadgeStyle;
|
|
|
|
|
badgePosition = BadgePosition;
|
|
|
|
|
badgeStyleURLMap = badgeStyleURLMap;
|
|
|
|
|
|
2024-05-07 11:08:39 +03:00
|
|
|
private mobileAppSettingsValue: MobileAppQRCodeSettings | MobileAppQrCodeWidgetSettings;
|
2024-05-02 21:37:11 +03:00
|
|
|
private deepLinkTTL: number;
|
|
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected cd: ChangeDetectorRef,
|
|
|
|
|
private mobileAppService: MobileAppService,
|
|
|
|
|
private utilsService: UtilsService) {
|
|
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2024-05-10 11:08:57 +03:00
|
|
|
if (!this.previewMode) {
|
|
|
|
|
if (this.ctx) {
|
|
|
|
|
this.mobileAppSettings = this.ctx.settings;
|
|
|
|
|
} else {
|
|
|
|
|
this.mobileAppService.getMobileAppSettings().subscribe((settings => {
|
|
|
|
|
this.mobileAppSettings = settings;
|
|
|
|
|
this.cd.detectChanges();
|
|
|
|
|
}));
|
|
|
|
|
}
|
2024-05-02 21:37:11 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
this.getMobileAppDeepLink().subscribe(link => {
|
|
|
|
|
this.deepLinkTTL = Number(this.utilsService.getQueryParam('ttl', link)) * MINUTE;
|
|
|
|
|
this.updateQRCode(link);
|
|
|
|
|
interval(this.deepLinkTTL).pipe(
|
|
|
|
|
takeUntil(this.destroy$),
|
|
|
|
|
mergeMap(() => this.getMobileAppDeepLink())
|
|
|
|
|
).subscribe(link => this.updateQRCode(link));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMobileAppDeepLink(): Observable<string> {
|
|
|
|
|
return this.mobileAppService.getMobileAppDeepLink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateQRCode(link: string) {
|
|
|
|
|
import('qrcode').then((QRCode) => {
|
2024-05-07 17:14:31 +03:00
|
|
|
QRCode.toCanvas(this.canvasRef.nativeElement, link, { width: 100 });
|
2024-05-02 21:37:11 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|