23 changed files with 820 additions and 56 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,96 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 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. |
|||
|
|||
--> |
|||
<section class="tb-widget-settings" [formGroup]="gpioControlWidgetSettingsForm" fxLayout="column"> |
|||
<fieldset class="fields-group"> |
|||
<legend class="group-title" translate>widgets.gpio.panel-settings</legend> |
|||
<tb-color-input |
|||
required |
|||
formControlName="switchPanelBackgroundColor" |
|||
icon="format_color_fill" |
|||
label="{{ 'widgets.gpio.background-color' | translate }}" openOnInput colorClearButton> |
|||
</tb-color-input> |
|||
<fieldset class="fields-group"> |
|||
<legend class="group-title" translate>widgets.gpio.gpio-switches</legend> |
|||
<div fxLayout="column"> |
|||
<div class="tb-control-list"> |
|||
<div *ngFor="let gpioItemControl of gpioListFormArray().controls; trackBy: trackByGpioItem; |
|||
let $index = index; last as isLast;" |
|||
fxLayout="column" [ngStyle]="!isLast ? {paddingBottom: '8px'} : {}"> |
|||
<tb-gpio-item [formControl]="gpioItemControl" |
|||
[expanded]="gpioItemControl.new" |
|||
(removeGpioItem)="removeGpioItem($index)"> |
|||
</tb-gpio-item> |
|||
</div> |
|||
</div> |
|||
<div *ngIf="!gpioListFormArray().controls.length"> |
|||
<span translate fxLayoutAlign="center center" |
|||
class="tb-prompt">widgets.gpio.no-gpio-switches</span> |
|||
</div> |
|||
<div style="padding-top: 16px;"> |
|||
<button mat-raised-button color="primary" |
|||
type="button" |
|||
(click)="addGpioItem()"> |
|||
<span translate>widgets.gpio.add-gpio-switch</span> |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</fieldset> |
|||
</fieldset> |
|||
<fieldset class="fields-group"> |
|||
<legend class="group-title" translate>widgets.rpc.rpc-settings</legend> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.rpc.request-timeout</mat-label> |
|||
<input required matInput type="number" min="0" formControlName="requestTimeout"> |
|||
</mat-form-field> |
|||
<fieldset class="fields-group" formGroupName="gpioStatusRequest"> |
|||
<legend class="group-title" translate>widgets.gpio.gpio-status-request</legend> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.method-name</mat-label> |
|||
<input required matInput formControlName="method"> |
|||
</mat-form-field> |
|||
<tb-json-content |
|||
required |
|||
label="{{ 'widgets.gpio.method-body' | translate }}" |
|||
[contentType]="contentTypes.JSON" |
|||
validateOnChange |
|||
formControlName="paramsBody"> |
|||
</tb-json-content> |
|||
</fieldset> |
|||
<fieldset class="fields-group" formGroupName="gpioStatusChangeRequest"> |
|||
<legend class="group-title" translate>widgets.gpio.gpio-status-change-request</legend> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.method-name</mat-label> |
|||
<input required matInput formControlName="method"> |
|||
</mat-form-field> |
|||
<tb-json-content |
|||
required |
|||
label="{{ 'widgets.gpio.method-body' | translate }}" |
|||
[contentType]="contentTypes.JSON" |
|||
validateOnChange |
|||
formControlName="paramsBody"> |
|||
</tb-json-content> |
|||
</fieldset> |
|||
<tb-js-func formControlName="parseGpioStatusFunction" |
|||
required |
|||
[globalVariables]="functionScopeVariables" |
|||
[functionArgs]="['body', 'pin']" |
|||
functionTitle="{{ 'widgets.gpio.parse-gpio-status-function' | translate }}" |
|||
helpId="widget/lib/rpc/parse_gpio_status_fn"> |
|||
</tb-js-func> |
|||
</fieldset> |
|||
</section> |
|||
@ -0,0 +1,145 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 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 { Component } from '@angular/core'; |
|||
import { WidgetSettings, WidgetSettingsComponent } from '@shared/models/widget.models'; |
|||
import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { GpioItem, gpioItemValidator } from '@home/components/widget/lib/settings/gpio/gpio-item.component'; |
|||
import { ContentType } from '@shared/models/constants'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-gpio-control-widget-settings', |
|||
templateUrl: './gpio-control-widget-settings.component.html', |
|||
styleUrls: ['./../widget-settings.scss'] |
|||
}) |
|||
export class GpioControlWidgetSettingsComponent extends WidgetSettingsComponent { |
|||
|
|||
gpioControlWidgetSettingsForm: FormGroup; |
|||
|
|||
contentTypes = ContentType; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
private fb: FormBuilder) { |
|||
super(store); |
|||
} |
|||
|
|||
protected settingsForm(): FormGroup { |
|||
return this.gpioControlWidgetSettingsForm; |
|||
} |
|||
|
|||
protected defaultSettings(): WidgetSettings { |
|||
return { |
|||
switchPanelBackgroundColor: '#008a00', |
|||
gpioList: [], |
|||
requestTimeout: 500, |
|||
gpioStatusRequest: { |
|||
method: 'getGpioStatus', |
|||
paramsBody: '{}' |
|||
}, |
|||
gpioStatusChangeRequest: { |
|||
method: 'setGpioStatus', |
|||
paramsBody: '{\n "pin": "{$pin}",\n "enabled": "{$enabled}"\n}' |
|||
}, |
|||
parseGpioStatusFunction: 'return body[pin] === true;' |
|||
}; |
|||
} |
|||
|
|||
protected onSettingsSet(settings: WidgetSettings) { |
|||
this.gpioControlWidgetSettingsForm = this.fb.group({ |
|||
|
|||
// Panel settings
|
|||
|
|||
switchPanelBackgroundColor: [settings.switchPanelBackgroundColor, [Validators.required]], |
|||
|
|||
// --> GPIO switches
|
|||
|
|||
gpioList: this.prepareGpioListFormArray(settings.gpioList), |
|||
|
|||
// RPC settings
|
|||
|
|||
requestTimeout: [settings.requestTimeout, [Validators.min(0), Validators.required]], |
|||
|
|||
// --> GPIO status request
|
|||
|
|||
gpioStatusRequest: this.fb.group({ |
|||
method: [settings.gpioStatusRequest?.method, [Validators.required]], |
|||
paramsBody: [settings.gpioStatusRequest?.paramsBody, [Validators.required]] |
|||
}, {validators: [Validators.required]}), |
|||
|
|||
// --> GPIO status change request
|
|||
|
|||
gpioStatusChangeRequest: this.fb.group({ |
|||
method: [settings.gpioStatusChangeRequest?.method, [Validators.required]], |
|||
paramsBody: [settings.gpioStatusChangeRequest?.paramsBody, [Validators.required]] |
|||
}, {validators: [Validators.required]}), |
|||
|
|||
parseGpioStatusFunction: [settings.parseGpioStatusFunction, [Validators.required]] |
|||
}); |
|||
} |
|||
|
|||
protected doUpdateSettings(settingsForm: FormGroup, settings: WidgetSettings) { |
|||
settingsForm.setControl('gpioList', this.prepareGpioListFormArray(settings.gpioList), {emitEvent: false}); |
|||
} |
|||
|
|||
private prepareGpioListFormArray(gpioList: GpioItem[] | undefined): FormArray { |
|||
const gpioListControls: Array<AbstractControl> = []; |
|||
if (gpioList) { |
|||
gpioList.forEach((gpioItem) => { |
|||
gpioListControls.push(this.fb.control(gpioItem, [gpioItemValidator(false)])); |
|||
}); |
|||
} |
|||
return this.fb.array(gpioListControls, [(control: AbstractControl) => { |
|||
const gpioItems = control.value; |
|||
if (!gpioItems || !gpioItems.length) { |
|||
return { |
|||
gpioItems: true |
|||
}; |
|||
} |
|||
return null; |
|||
}]); |
|||
} |
|||
|
|||
gpioListFormArray(): FormArray { |
|||
return this.gpioControlWidgetSettingsForm.get('gpioList') as FormArray; |
|||
} |
|||
|
|||
public trackByGpioItem(index: number, gpioItemControl: AbstractControl): any { |
|||
return gpioItemControl; |
|||
} |
|||
|
|||
public removeGpioItem(index: number) { |
|||
(this.gpioControlWidgetSettingsForm.get('gpioList') as FormArray).removeAt(index); |
|||
} |
|||
|
|||
public addGpioItem() { |
|||
const gpioItem: GpioItem = { |
|||
pin: null, |
|||
label: null, |
|||
row: null, |
|||
col: null |
|||
}; |
|||
const gpioListArray = this.gpioControlWidgetSettingsForm.get('gpioList') as FormArray; |
|||
const gpioItemControl = this.fb.control(gpioItem, [gpioItemValidator(false)]); |
|||
(gpioItemControl as any).new = true; |
|||
gpioListArray.push(gpioItemControl); |
|||
this.gpioControlWidgetSettingsForm.updateValueAndValidity(); |
|||
if (!this.gpioControlWidgetSettingsForm.valid) { |
|||
this.onSettingsChanged(this.gpioControlWidgetSettingsForm.value); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 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. |
|||
|
|||
--> |
|||
<mat-expansion-panel class="gpio-item" fxFlex [formGroup]="gpioItemFormGroup" [(expanded)]="expanded"> |
|||
<mat-expansion-panel-header> |
|||
<div fxFlex fxLayout="row" fxLayoutAlign="start center"> |
|||
<mat-panel-title> |
|||
<div fxLayout="row" fxFlex fxLayoutAlign="start center"> |
|||
<div [innerHTML]="gpioItemHtml()"></div> |
|||
<div *ngIf="hasColor" class="tb-color-preview" style="margin-left: 6px;"> |
|||
<div class="tb-color-result" [ngStyle]="{background: gpioItemFormGroup.get('color').value}"></div> |
|||
</div> |
|||
</div> |
|||
</mat-panel-title> |
|||
<span fxFlex></span> |
|||
<button *ngIf="!disabled" mat-icon-button style="min-width: 40px;" |
|||
type="button" |
|||
(click)="removeGpioItem.emit()" |
|||
matTooltip="{{ 'action.remove' | translate }}" |
|||
matTooltipPosition="above"> |
|||
<mat-icon>delete</mat-icon> |
|||
</button> |
|||
</div> |
|||
</mat-expansion-panel-header> |
|||
<ng-template matExpansionPanelContent> |
|||
<div fxLayout="column" fxLayoutGap="0.5em"> |
|||
<mat-divider></mat-divider> |
|||
<section class="tb-widget-settings" fxLayout="column"> |
|||
<section fxLayout="row" fxLayoutGap="8px" fxLayoutAlign="start center"> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.pin</mat-label> |
|||
<input required matInput type="number" min="1" step="1" formControlName="pin"> |
|||
</mat-form-field> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.label</mat-label> |
|||
<input required matInput formControlName="label"> |
|||
</mat-form-field> |
|||
</section> |
|||
<section fxLayout="row" fxLayoutGap="8px" fxLayoutAlign="start center"> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.row</mat-label> |
|||
<input required matInput type="number" min="0" step="1" formControlName="row"> |
|||
</mat-form-field> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>widgets.gpio.column</mat-label> |
|||
<input required matInput type="number" min="0" max="1" step="1" formControlName="col"> |
|||
</mat-form-field> |
|||
</section> |
|||
<tb-color-input |
|||
*ngIf="hasColor" |
|||
required |
|||
formControlName="color" |
|||
icon="format_color_fill" |
|||
label="{{ 'widgets.gpio.color' | translate }}" openOnInput colorClearButton> |
|||
</tb-color-input> |
|||
</section> |
|||
</div> |
|||
</ng-template> |
|||
</mat-expansion-panel> |
|||
@ -0,0 +1,40 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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. |
|||
*/ |
|||
:host { |
|||
display: block; |
|||
.mat-expansion-panel { |
|||
box-shadow: none; |
|||
&.gpio-item { |
|||
border: 1px groove rgba(0, 0, 0, .25); |
|||
.mat-expansion-panel-header { |
|||
padding: 0 24px 0 8px; |
|||
&.mat-expanded { |
|||
height: 48px; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
:host ::ng-deep { |
|||
.mat-expansion-panel { |
|||
&.gpio-item { |
|||
.mat-expansion-panel-body { |
|||
padding: 0 8px 8px; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,150 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 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 { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'; |
|||
import { |
|||
AbstractControl, |
|||
ControlValueAccessor, |
|||
FormBuilder, |
|||
FormGroup, |
|||
NG_VALUE_ACCESSOR, ValidationErrors, ValidatorFn, |
|||
Validators |
|||
} from '@angular/forms'; |
|||
import { PageComponent } from '@shared/components/page.component'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
import { isNumber } from '@core/utils'; |
|||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; |
|||
|
|||
export interface GpioItem { |
|||
pin: number; |
|||
label: string; |
|||
row: number; |
|||
col: number; |
|||
color?: string; |
|||
} |
|||
|
|||
export function gpioItemValidator(hasColor: boolean): ValidatorFn { |
|||
return (control: AbstractControl) => { |
|||
const gpioItem: GpioItem = control.value; |
|||
if (!gpioItem |
|||
|| !isNumber(gpioItem.pin) || gpioItem.pin < 1 |
|||
|| !isNumber(gpioItem.row) || gpioItem.row < 0 |
|||
|| !isNumber(gpioItem.col) || gpioItem.col < 0 || gpioItem.col > 1 |
|||
|| !gpioItem.label |
|||
|| (hasColor && !gpioItem.color) |
|||
) { |
|||
return { |
|||
gpioItem: true |
|||
}; |
|||
} |
|||
return null; |
|||
}; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-gpio-item', |
|||
templateUrl: './gpio-item.component.html', |
|||
styleUrls: ['./gpio-item.component.scss', './../widget-settings.scss'], |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => GpioItemComponent), |
|||
multi: true |
|||
} |
|||
] |
|||
}) |
|||
export class GpioItemComponent extends PageComponent implements OnInit, ControlValueAccessor { |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
@Input() |
|||
expanded = false; |
|||
|
|||
@Input() |
|||
hasColor = false; |
|||
|
|||
@Output() |
|||
removeGpioItem = new EventEmitter(); |
|||
|
|||
private modelValue: GpioItem; |
|||
|
|||
private propagateChange = null; |
|||
|
|||
public gpioItemFormGroup: FormGroup; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
private translate: TranslateService, |
|||
private domSanitizer: DomSanitizer, |
|||
private fb: FormBuilder) { |
|||
super(store); |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
this.gpioItemFormGroup = this.fb.group({ |
|||
pin: [null, [Validators.required, Validators.min(1)]], |
|||
label: [null, [Validators.required]], |
|||
row: [null, [Validators.required, Validators.min(0)]], |
|||
col: [null, [Validators.required, Validators.min(0), Validators.max(1)]] |
|||
}); |
|||
if (this.hasColor) { |
|||
this.gpioItemFormGroup.addControl('color', this.fb.control(null, [Validators.required])); |
|||
} |
|||
this.gpioItemFormGroup.valueChanges.subscribe(() => { |
|||
this.updateModel(); |
|||
}); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void { |
|||
} |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
this.disabled = isDisabled; |
|||
if (isDisabled) { |
|||
this.gpioItemFormGroup.disable({emitEvent: false}); |
|||
} else { |
|||
this.gpioItemFormGroup.enable({emitEvent: false}); |
|||
} |
|||
} |
|||
|
|||
writeValue(value: GpioItem): void { |
|||
this.modelValue = value; |
|||
this.gpioItemFormGroup.patchValue( |
|||
value, {emitEvent: false} |
|||
); |
|||
} |
|||
|
|||
gpioItemHtml(): SafeHtml { |
|||
const value: GpioItem = this.gpioItemFormGroup.value; |
|||
const pin = isNumber(value.pin) && value.pin > 0 ? value.pin : 'Undefined'; |
|||
const row = isNumber(value.row) && value.row > -1 ? value.row : 'Undefined'; |
|||
const col = isNumber(value.col) && value.col > -1 ? value.col : 'Undefined'; |
|||
const label = value.label || 'Undefined'; |
|||
return this.domSanitizer.bypassSecurityTrustHtml(`${label} (<small>pin:</small>${pin}) - [<small>row:</small>${row}:<small>col:</small>${col}]`); |
|||
} |
|||
|
|||
private updateModel() { |
|||
const value: GpioItem = this.gpioItemFormGroup.value; |
|||
this.modelValue = value; |
|||
this.propagateChange(this.modelValue); |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 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. |
|||
|
|||
--> |
|||
<section class="tb-widget-settings" [formGroup]="gpioPanelWidgetSettingsForm" fxLayout="column"> |
|||
<fieldset class="fields-group"> |
|||
<legend class="group-title" translate>widgets.gpio.panel-settings</legend> |
|||
<tb-color-input |
|||
required |
|||
formControlName="ledPanelBackgroundColor" |
|||
icon="format_color_fill" |
|||
label="{{ 'widgets.gpio.background-color' | translate }}" openOnInput colorClearButton> |
|||
</tb-color-input> |
|||
<fieldset class="fields-group"> |
|||
<legend class="group-title" translate>widgets.gpio.gpio-leds</legend> |
|||
<div fxLayout="column"> |
|||
<div class="tb-control-list"> |
|||
<div *ngFor="let gpioItemControl of gpioListFormArray().controls; trackBy: trackByGpioItem; |
|||
let $index = index; last as isLast;" |
|||
fxLayout="column" [ngStyle]="!isLast ? {paddingBottom: '8px'} : {}"> |
|||
<tb-gpio-item [formControl]="gpioItemControl" |
|||
[hasColor]="true" |
|||
[expanded]="gpioItemControl.new" |
|||
(removeGpioItem)="removeGpioItem($index)"> |
|||
</tb-gpio-item> |
|||
</div> |
|||
</div> |
|||
<div *ngIf="!gpioListFormArray().controls.length"> |
|||
<span translate fxLayoutAlign="center center" |
|||
class="tb-prompt">widgets.gpio.no-gpio-leds</span> |
|||
</div> |
|||
<div style="padding-top: 16px;"> |
|||
<button mat-raised-button color="primary" |
|||
type="button" |
|||
(click)="addGpioItem()"> |
|||
<span translate>widgets.gpio.add-gpio-led</span> |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</fieldset> |
|||
</fieldset> |
|||
</section> |
|||
@ -0,0 +1,114 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 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 { Component } from '@angular/core'; |
|||
import { WidgetSettings, WidgetSettingsComponent } from '@shared/models/widget.models'; |
|||
import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { GpioItem, gpioItemValidator } from '@home/components/widget/lib/settings/gpio/gpio-item.component'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-gpio-panel-widget-settings', |
|||
templateUrl: './gpio-panel-widget-settings.component.html', |
|||
styleUrls: ['./../widget-settings.scss'] |
|||
}) |
|||
export class GpioPanelWidgetSettingsComponent extends WidgetSettingsComponent { |
|||
|
|||
gpioPanelWidgetSettingsForm: FormGroup; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
private fb: FormBuilder) { |
|||
super(store); |
|||
} |
|||
|
|||
protected settingsForm(): FormGroup { |
|||
return this.gpioPanelWidgetSettingsForm; |
|||
} |
|||
|
|||
protected defaultSettings(): WidgetSettings { |
|||
return { |
|||
ledPanelBackgroundColor: '#008a00', |
|||
gpioList: [] |
|||
}; |
|||
} |
|||
|
|||
protected onSettingsSet(settings: WidgetSettings) { |
|||
this.gpioPanelWidgetSettingsForm = this.fb.group({ |
|||
|
|||
// Panel settings
|
|||
|
|||
ledPanelBackgroundColor: [settings.ledPanelBackgroundColor, [Validators.required]], |
|||
|
|||
// --> GPIO leds
|
|||
|
|||
gpioList: this.prepareGpioListFormArray(settings.gpioList), |
|||
|
|||
}); |
|||
} |
|||
|
|||
protected doUpdateSettings(settingsForm: FormGroup, settings: WidgetSettings) { |
|||
settingsForm.setControl('gpioList', this.prepareGpioListFormArray(settings.gpioList), {emitEvent: false}); |
|||
} |
|||
|
|||
private prepareGpioListFormArray(gpioList: GpioItem[] | undefined): FormArray { |
|||
const gpioListControls: Array<AbstractControl> = []; |
|||
if (gpioList) { |
|||
gpioList.forEach((gpioItem) => { |
|||
gpioListControls.push(this.fb.control(gpioItem, [gpioItemValidator(true)])); |
|||
}); |
|||
} |
|||
return this.fb.array(gpioListControls, [(control: AbstractControl) => { |
|||
const gpioItems = control.value; |
|||
if (!gpioItems || !gpioItems.length) { |
|||
return { |
|||
gpioItems: true |
|||
}; |
|||
} |
|||
return null; |
|||
}]); |
|||
} |
|||
|
|||
gpioListFormArray(): FormArray { |
|||
return this.gpioPanelWidgetSettingsForm.get('gpioList') as FormArray; |
|||
} |
|||
|
|||
public trackByGpioItem(index: number, gpioItemControl: AbstractControl): any { |
|||
return gpioItemControl; |
|||
} |
|||
|
|||
public removeGpioItem(index: number) { |
|||
(this.gpioPanelWidgetSettingsForm.get('gpioList') as FormArray).removeAt(index); |
|||
} |
|||
|
|||
public addGpioItem() { |
|||
const gpioItem: GpioItem = { |
|||
pin: null, |
|||
label: null, |
|||
row: null, |
|||
col: null, |
|||
color: null |
|||
}; |
|||
const gpioListArray = this.gpioPanelWidgetSettingsForm.get('gpioList') as FormArray; |
|||
const gpioItemControl = this.fb.control(gpioItem, [gpioItemValidator(true)]); |
|||
(gpioItemControl as any).new = true; |
|||
gpioListArray.push(gpioItemControl); |
|||
this.gpioPanelWidgetSettingsForm.updateValueAndValidity(); |
|||
if (!this.gpioPanelWidgetSettingsForm.valid) { |
|||
this.onSettingsChanged(this.gpioPanelWidgetSettingsForm.value); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue