99 changed files with 1943 additions and 753 deletions
@ -0,0 +1,90 @@ |
|||
-- |
|||
-- Copyright © 2016-2021 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. |
|||
-- |
|||
|
|||
-- PROCEDURE: public.cleanup_events_by_ttl(bigint, bigint, bigint) |
|||
|
|||
DROP PROCEDURE IF EXISTS public.cleanup_events_by_ttl(bigint, bigint, bigint); |
|||
|
|||
CREATE OR REPLACE PROCEDURE public.cleanup_events_by_ttl( |
|||
ttl bigint, |
|||
debug_ttl bigint, |
|||
INOUT deleted bigint) |
|||
LANGUAGE 'plpgsql' |
|||
AS $BODY$ |
|||
DECLARE |
|||
ttl_ts bigint; |
|||
debug_ttl_ts bigint; |
|||
ttl_deleted_count bigint DEFAULT 0; |
|||
debug_ttl_deleted_count bigint DEFAULT 0; |
|||
BEGIN |
|||
IF ttl > 0 THEN |
|||
ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - ttl::bigint * 1000)::bigint; |
|||
|
|||
DELETE FROM event |
|||
WHERE ts < ttl_ts |
|||
AND NOT event_type IN ('DEBUG_RULE_NODE', 'DEBUG_RULE_CHAIN', 'DEBUG_CONVERTER', 'DEBUG_INTEGRATION'); |
|||
|
|||
GET DIAGNOSTICS ttl_deleted_count = ROW_COUNT; |
|||
END IF; |
|||
|
|||
IF debug_ttl > 0 THEN |
|||
debug_ttl_ts := (EXTRACT(EPOCH FROM current_timestamp) * 1000 - debug_ttl::bigint * 1000)::bigint; |
|||
|
|||
DELETE FROM event |
|||
WHERE ts < debug_ttl_ts |
|||
AND event_type IN ('DEBUG_RULE_NODE', 'DEBUG_RULE_CHAIN', 'DEBUG_CONVERTER', 'DEBUG_INTEGRATION'); |
|||
|
|||
GET DIAGNOSTICS debug_ttl_deleted_count = ROW_COUNT; |
|||
END IF; |
|||
|
|||
RAISE NOTICE 'Events removed by ttl: %', ttl_deleted_count; |
|||
RAISE NOTICE 'Debug Events removed by ttl: %', debug_ttl_deleted_count; |
|||
deleted := ttl_deleted_count + debug_ttl_deleted_count; |
|||
END |
|||
$BODY$; |
|||
|
|||
|
|||
-- Index: idx_event_ts |
|||
|
|||
DROP INDEX IF EXISTS public.idx_event_ts; |
|||
|
|||
-- Hint: add CONCURRENTLY to CREATE INDEX query in case of more then 1 million records or during live update |
|||
-- CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_event_ts |
|||
CREATE INDEX IF NOT EXISTS idx_event_ts |
|||
ON public.event |
|||
(ts DESC NULLS LAST) |
|||
WITH (FILLFACTOR=95); |
|||
|
|||
COMMENT ON INDEX public.idx_event_ts |
|||
IS 'This index helps to delete events by TTL using timestamp'; |
|||
|
|||
|
|||
-- Index: idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
|
|||
DROP INDEX IF EXISTS public.idx_event_tenant_entity_type_entity_event_type_created_time_des; |
|||
|
|||
-- CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
CREATE INDEX IF NOT EXISTS idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
ON public.event |
|||
(tenant_id ASC, entity_type ASC, entity_id ASC, event_type ASC, created_time DESC NULLS LAST) |
|||
WITH (FILLFACTOR=95); |
|||
|
|||
COMMENT ON INDEX public.idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
IS 'This index helps to open latest events on UI fast'; |
|||
|
|||
-- Index: idx_event_type_entity_id |
|||
-- Description: replaced with more suitable idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
DROP INDEX IF EXISTS public.idx_event_type_entity_id; |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.service.install; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import static org.mockito.ArgumentMatchers.anyString; |
|||
import static org.mockito.BDDMockito.willDoNothing; |
|||
import static org.mockito.Mockito.spy; |
|||
import static org.mockito.Mockito.times; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
public class PsqlEntityDatabaseSchemaServiceTest { |
|||
|
|||
@Test |
|||
public void givenPsqlDbSchemaService_whenCreateDatabaseSchema_thenVerifyPsqlIndexSpecificCall() throws Exception { |
|||
PsqlEntityDatabaseSchemaService service = spy(new PsqlEntityDatabaseSchemaService()); |
|||
willDoNothing().given(service).executeQueryFromFile(anyString()); |
|||
|
|||
service.createDatabaseSchema(); |
|||
|
|||
verify(service, times(1)).createDatabaseIndexes(); |
|||
verify(service, times(1)).executeQueryFromFile(PsqlEntityDatabaseSchemaService.SCHEMA_ENTITIES_SQL); |
|||
verify(service, times(1)).executeQueryFromFile(PsqlEntityDatabaseSchemaService.SCHEMA_ENTITIES_IDX_SQL); |
|||
verify(service, times(1)).executeQueryFromFile(PsqlEntityDatabaseSchemaService.SCHEMA_ENTITIES_IDX_PSQL_ADDON_SQL); |
|||
verify(service, times(3)).executeQueryFromFile(anyString()); |
|||
} |
|||
|
|||
@Test |
|||
public void givenPsqlDbSchemaService_whenCreateDatabaseIndexes_thenVerifyPsqlIndexSpecificCall() throws Exception { |
|||
PsqlEntityDatabaseSchemaService service = spy(new PsqlEntityDatabaseSchemaService()); |
|||
willDoNothing().given(service).executeQueryFromFile(anyString()); |
|||
|
|||
service.createDatabaseIndexes(); |
|||
|
|||
verify(service, times(1)).executeQueryFromFile(PsqlEntityDatabaseSchemaService.SCHEMA_ENTITIES_IDX_SQL); |
|||
verify(service, times(1)).executeQueryFromFile(PsqlEntityDatabaseSchemaService.SCHEMA_ENTITIES_IDX_PSQL_ADDON_SQL); |
|||
verify(service, times(2)).executeQueryFromFile(anyString()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.service.ttl; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.junit.Test; |
|||
import org.junit.runner.RunWith; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.test.context.SpringBootTest; |
|||
import org.springframework.test.context.junit4.SpringRunner; |
|||
|
|||
import static org.hamcrest.MatcherAssert.assertThat; |
|||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
|||
import static org.hamcrest.Matchers.is; |
|||
import static org.hamcrest.Matchers.lessThanOrEqualTo; |
|||
import static org.thingsboard.server.service.ttl.EventsCleanUpService.RANDOM_DELAY_INTERVAL_MS_EXPRESSION; |
|||
|
|||
@RunWith(SpringRunner.class) |
|||
@SpringBootTest(classes = EventsCleanUpServiceTest.class) |
|||
@Slf4j |
|||
public class EventsCleanUpServiceTest { |
|||
|
|||
@Value(RANDOM_DELAY_INTERVAL_MS_EXPRESSION) |
|||
long randomDelayMs; |
|||
@Value("${sql.ttl.events.execution_interval_ms}") |
|||
long executionIntervalMs; |
|||
|
|||
@Test |
|||
public void givenInterval_whenRandomDelay_ThenDelayInInterval() { |
|||
log.info("randomDelay {}", randomDelayMs); |
|||
log.info("executionIntervalMs {}", executionIntervalMs); |
|||
assertThat(executionIntervalMs, is(2220000L)); |
|||
assertThat(randomDelayMs, greaterThanOrEqualTo(0L)); |
|||
assertThat(randomDelayMs, lessThanOrEqualTo(executionIntervalMs)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.coap; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.eclipse.californium.core.coap.MessageObserver; |
|||
import org.eclipse.californium.core.coap.Response; |
|||
import org.eclipse.californium.elements.EndpointContext; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
@RequiredArgsConstructor |
|||
public class TbCoapMessageObserver implements MessageObserver { |
|||
|
|||
private final int msgId; |
|||
private final Consumer<Integer> onAcknowledge; |
|||
|
|||
@Override |
|||
public void onRetransmission() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onResponse(Response response) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onAcknowledgement() { |
|||
onAcknowledge.accept(msgId); |
|||
} |
|||
|
|||
@Override |
|||
public void onReject() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onTimeout() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onCancel() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onReadyToSend() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onConnecting() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onDtlsRetransmission(int flight) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onSent(boolean retransmission) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onSendError(Throwable error) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onContextEstablished(EndpointContext endpointContext) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onComplete() { |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.coap.callback; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.eclipse.californium.core.coap.CoAP; |
|||
import org.eclipse.californium.core.server.resources.CoapExchange; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.transport.TransportContext; |
|||
import org.thingsboard.server.common.transport.TransportServiceCallback; |
|||
import org.thingsboard.server.common.transport.auth.SessionInfoCreator; |
|||
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.transport.coap.AbstractCoapTransportResource; |
|||
|
|||
import java.util.UUID; |
|||
import java.util.function.BiConsumer; |
|||
|
|||
@Slf4j |
|||
public class CoapDeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponse> { |
|||
private final TransportContext transportContext; |
|||
private final CoapExchange exchange; |
|||
private final BiConsumer<TransportProtos.SessionInfoProto, DeviceProfile> onSuccess; |
|||
|
|||
public CoapDeviceAuthCallback(TransportContext transportContext, CoapExchange exchange, BiConsumer<TransportProtos.SessionInfoProto, DeviceProfile> onSuccess) { |
|||
this.transportContext = transportContext; |
|||
this.exchange = exchange; |
|||
this.onSuccess = onSuccess; |
|||
} |
|||
|
|||
@Override |
|||
public void onSuccess(ValidateDeviceCredentialsResponse msg) { |
|||
DeviceProfile deviceProfile = msg.getDeviceProfile(); |
|||
if (msg.hasDeviceInfo() && deviceProfile != null) { |
|||
TransportProtos.SessionInfoProto sessionInfoProto = SessionInfoCreator.create(msg, transportContext, UUID.randomUUID()); |
|||
onSuccess.accept(sessionInfoProto, deviceProfile); |
|||
} else { |
|||
exchange.respond(CoAP.ResponseCode.UNAUTHORIZED); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Throwable e) { |
|||
log.warn("Failed to process request", e); |
|||
exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.coap.callback; |
|||
|
|||
import org.eclipse.californium.core.coap.CoAP; |
|||
import org.eclipse.californium.core.server.resources.CoapExchange; |
|||
import org.thingsboard.server.common.transport.TransportServiceCallback; |
|||
|
|||
public class CoapNoOpCallback implements TransportServiceCallback<Void> { |
|||
private final CoapExchange exchange; |
|||
|
|||
public CoapNoOpCallback(CoapExchange exchange) { |
|||
this.exchange = exchange; |
|||
} |
|||
|
|||
@Override |
|||
public void onSuccess(Void msg) { |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Throwable e) { |
|||
exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* Copyright © 2016-2021 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.coap.callback; |
|||
|
|||
import org.eclipse.californium.core.coap.CoAP; |
|||
import org.eclipse.californium.core.coap.Response; |
|||
import org.eclipse.californium.core.server.resources.CoapExchange; |
|||
import org.thingsboard.server.common.transport.TransportServiceCallback; |
|||
|
|||
public class CoapOkCallback implements TransportServiceCallback<Void> { |
|||
|
|||
protected final CoapExchange exchange; |
|||
protected final CoAP.ResponseCode onSuccessResponse; |
|||
protected final CoAP.ResponseCode onFailureResponse; |
|||
|
|||
public CoapOkCallback(CoapExchange exchange, CoAP.ResponseCode onSuccessResponse, CoAP.ResponseCode onFailureResponse) { |
|||
this.exchange = exchange; |
|||
this.onSuccessResponse = onSuccessResponse; |
|||
this.onFailureResponse = onFailureResponse; |
|||
} |
|||
|
|||
@Override |
|||
public void onSuccess(Void msg) { |
|||
Response response = new Response(onSuccessResponse); |
|||
response.setConfirmable(isConRequest()); |
|||
exchange.respond(response); |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Throwable e) { |
|||
exchange.respond(onFailureResponse); |
|||
} |
|||
|
|||
protected boolean isConRequest() { |
|||
return exchange.advanced().getRequest().isConfirmable(); |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
-- |
|||
-- Copyright © 2016-2021 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. |
|||
-- |
|||
|
|||
-- This file describes PostgreSQL-specific indexes that not supported by hsql |
|||
-- It is not a stand-alone file! Run schema-entities-idx.sql before! |
|||
-- Note: Hibernate DESC order translates to native SQL "ORDER BY .. DESC NULLS LAST" |
|||
-- While creating index PostgreSQL transforms short notation (ts DESC) to the full (DESC NULLS FIRST) |
|||
-- That difference between NULLS LAST and NULLS FIRST prevents to hit index while querying latest by ts |
|||
-- That why we need to define DESC index explicitly as (ts DESC NULLS LAST) |
|||
|
|||
CREATE INDEX IF NOT EXISTS idx_event_ts |
|||
ON public.event |
|||
(ts DESC NULLS LAST) |
|||
WITH (FILLFACTOR=95); |
|||
|
|||
COMMENT ON INDEX public.idx_event_ts |
|||
IS 'This index helps to delete events by TTL using timestamp'; |
|||
|
|||
CREATE INDEX IF NOT EXISTS idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
ON public.event |
|||
(tenant_id ASC, entity_type ASC, entity_id ASC, event_type ASC, created_time DESC NULLS LAST) |
|||
WITH (FILLFACTOR=95); |
|||
|
|||
COMMENT ON INDEX public.idx_event_tenant_entity_type_entity_event_type_created_time_des |
|||
IS 'This index helps to open latest events on UI fast'; |
|||
@ -0,0 +1,48 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2021 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 [formGroup]="deviceCredentialsMqttFormGroup"> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.client-id</mat-label> |
|||
<input matInput formControlName="clientId"> |
|||
<mat-error *ngIf="deviceCredentialsMqttFormGroup.get('clientId').hasError('pattern')"> |
|||
{{ 'device.client-id-pattern' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.user-name</mat-label> |
|||
<input matInput formControlName="userName" [required]="!!deviceCredentialsMqttFormGroup.get('password').value"> |
|||
<mat-error *ngIf="deviceCredentialsMqttFormGroup.get('userName').hasError('required')"> |
|||
{{ 'device.user-name-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.password</mat-label> |
|||
<input matInput formControlName="password" |
|||
autocomplete="new-password" |
|||
(ngModelChange)="passwordChanged()" |
|||
[type]="hidePassword ? 'password' : 'text'"> |
|||
<button mat-icon-button matSuffix type="button" |
|||
(click)="hidePassword = !hidePassword" |
|||
[attr.aria-pressed]="hidePassword"> |
|||
<mat-icon>{{hidePassword ? 'visibility_off' : 'visibility'}}</mat-icon> |
|||
</button> |
|||
</mat-form-field> |
|||
<tb-error style="margin-top: -12px; display: block;" |
|||
[error]="deviceCredentialsMqttFormGroup.hasError('atLeastOne') ? |
|||
('device.client-id-or-user-name-necessary' | translate) : ''"></tb-error> |
|||
</section> |
|||
@ -0,0 +1,133 @@ |
|||
///
|
|||
/// Copyright © 2016-2021 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, forwardRef, Input, OnDestroy } from '@angular/core'; |
|||
import { |
|||
ControlValueAccessor, |
|||
FormBuilder, |
|||
FormGroup, |
|||
NG_VALIDATORS, |
|||
NG_VALUE_ACCESSOR, |
|||
ValidationErrors, |
|||
Validator, |
|||
ValidatorFn, |
|||
Validators |
|||
} from '@angular/forms'; |
|||
import { Subject } from 'rxjs'; |
|||
import { DeviceCredentialMQTTBasic } from '@shared/models/device.models'; |
|||
import { takeUntil } from 'rxjs/operators'; |
|||
import { isDefinedAndNotNull, isEmptyStr } from '@core/utils'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-device-credentials-mqtt-basic', |
|||
templateUrl: './device-credentials-mqtt-basic.component.html', |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => DeviceCredentialsMqttBasicComponent), |
|||
multi: true |
|||
}, |
|||
{ |
|||
provide: NG_VALIDATORS, |
|||
useExisting: forwardRef(() => DeviceCredentialsMqttBasicComponent), |
|||
multi: true, |
|||
}], |
|||
styleUrls: [] |
|||
}) |
|||
export class DeviceCredentialsMqttBasicComponent implements ControlValueAccessor, Validator, OnDestroy { |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
deviceCredentialsMqttFormGroup: FormGroup; |
|||
|
|||
hidePassword = true; |
|||
|
|||
private destroy$ = new Subject(); |
|||
private propagateChange = (v: any) => {}; |
|||
|
|||
constructor(public fb: FormBuilder) { |
|||
this.deviceCredentialsMqttFormGroup = this.fb.group({ |
|||
clientId: [null, [Validators.pattern(/^[A-Za-z0-9]+$/)]], |
|||
userName: [null], |
|||
password: [null] |
|||
}, {validators: this.atLeastOne(Validators.required, ['clientId', 'userName'])}); |
|||
this.deviceCredentialsMqttFormGroup.valueChanges.pipe( |
|||
takeUntil(this.destroy$) |
|||
).subscribe((value) => { |
|||
this.updateView(value); |
|||
}); |
|||
} |
|||
|
|||
ngOnDestroy(): void { |
|||
this.destroy$.next(); |
|||
this.destroy$.complete(); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void {} |
|||
|
|||
setDisabledState(isDisabled: boolean) { |
|||
this.disabled = isDisabled; |
|||
if (this.disabled) { |
|||
this.deviceCredentialsMqttFormGroup.disable({emitEvent: false}); |
|||
} else { |
|||
this.deviceCredentialsMqttFormGroup.enable({emitEvent: false}); |
|||
} |
|||
} |
|||
|
|||
validate(): ValidationErrors | null { |
|||
return this.deviceCredentialsMqttFormGroup.valid ? null : { |
|||
deviceCredentialsMqttBasic: false |
|||
}; |
|||
} |
|||
|
|||
writeValue(mqttBasic: string) { |
|||
if (isDefinedAndNotNull(mqttBasic) && !isEmptyStr(mqttBasic)) { |
|||
const value = JSON.parse(mqttBasic); |
|||
this.deviceCredentialsMqttFormGroup.patchValue(value, {emitEvent: false}); |
|||
} |
|||
} |
|||
|
|||
updateView(value: DeviceCredentialMQTTBasic) { |
|||
const formValue = JSON.stringify(value); |
|||
this.propagateChange(formValue); |
|||
} |
|||
|
|||
passwordChanged() { |
|||
const value = this.deviceCredentialsMqttFormGroup.get('password').value; |
|||
if (value !== '') { |
|||
this.deviceCredentialsMqttFormGroup.get('userName').setValidators([Validators.required]); |
|||
} else { |
|||
this.deviceCredentialsMqttFormGroup.get('userName').setValidators([]); |
|||
} |
|||
this.deviceCredentialsMqttFormGroup.get('userName').updateValueAndValidity({emitEvent: false}); |
|||
} |
|||
|
|||
private atLeastOne(validator: ValidatorFn, controls: string[] = null) { |
|||
return (group: FormGroup): ValidationErrors | null => { |
|||
if (!controls) { |
|||
controls = Object.keys(group.controls); |
|||
} |
|||
const hasAtLeastOne = group?.controls && controls.some(k => !validator(group.controls[k])); |
|||
|
|||
return hasAtLeastOne ? null : {atLeastOne: true}; |
|||
}; |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
///
|
|||
/// Copyright © 2016-2021 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 { NgModule } from '@angular/core'; |
|||
import { CommonModule } from '@angular/common'; |
|||
import { SharedModule } from '@shared/shared.module'; |
|||
import { CopyDeviceCredentialsComponent } from './copy-device-credentials.component'; |
|||
import { DeviceCredentialsComponent } from './device-credentials.component'; |
|||
import { DeviceCredentialsLwm2mComponent } from './device-credentials-lwm2m.component'; |
|||
import { DeviceCredentialsLwm2mServerComponent } from './device-credentials-lwm2m-server.component'; |
|||
import { DeviceCredentialsMqttBasicComponent } from './device-credentials-mqtt-basic.component'; |
|||
|
|||
@NgModule({ |
|||
declarations: [ |
|||
CopyDeviceCredentialsComponent, |
|||
DeviceCredentialsComponent, |
|||
DeviceCredentialsLwm2mComponent, |
|||
DeviceCredentialsLwm2mServerComponent, |
|||
DeviceCredentialsMqttBasicComponent |
|||
], |
|||
imports: [ |
|||
CommonModule, |
|||
SharedModule |
|||
], |
|||
exports: [ |
|||
CopyDeviceCredentialsComponent, |
|||
DeviceCredentialsComponent, |
|||
DeviceCredentialsLwm2mComponent, |
|||
DeviceCredentialsLwm2mServerComponent, |
|||
DeviceCredentialsMqttBasicComponent |
|||
] |
|||
}) |
|||
export class DeviceCredentialsModule { } |
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue