91 changed files with 4066 additions and 637 deletions
|
After Width: | Height: | Size: 6.0 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 12 KiB |
@ -0,0 +1,148 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.mqtt.mqttv3.rpc; |
|||
|
|||
import io.netty.handler.codec.mqtt.MqttQoS; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.TenantProfile; |
|||
import org.thingsboard.server.common.data.TransportPayloadType; |
|||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.transport.mqtt.AbstractMqttIntegrationTest; |
|||
import org.thingsboard.server.transport.mqtt.MqttTestConfigProperties; |
|||
import org.thingsboard.server.transport.mqtt.limits.GatewaySessionLimits; |
|||
import org.thingsboard.server.transport.mqtt.limits.SessionLimits; |
|||
import org.thingsboard.server.transport.mqtt.mqttv3.MqttTestCallback; |
|||
import org.thingsboard.server.transport.mqtt.mqttv3.MqttTestClient; |
|||
import org.thingsboard.server.transport.mqtt.mqttv3.MqttTestSubscribeOnTopicCallback; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertTrue; |
|||
import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVICE_RPC_REQUESTS_TOPIC; |
|||
import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC; |
|||
import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVICE_RPC_RESPONSE_TOPIC; |
|||
|
|||
@DaoSqlTest |
|||
public class MqttClientSideRpcIntegrationTest extends AbstractMqttIntegrationTest { |
|||
|
|||
@Value("${transport.mqtt.netty.max_payload_size}") |
|||
private Integer maxPayloadSize; |
|||
|
|||
@Value("${transport.mqtt.msg_queue_size_per_device_limit}") |
|||
private int maxInflightMessages; |
|||
|
|||
@Test |
|||
public void getSessionLimitsRpcForDeviceTest() throws Exception { |
|||
loginSysAdmin(); |
|||
TenantProfile tenantProfile = doGet("/api/tenantProfile/" + tenantProfileId, TenantProfile.class); |
|||
DefaultTenantProfileConfiguration profileConfiguration = tenantProfile.getDefaultProfileConfiguration(); |
|||
|
|||
profileConfiguration.setTransportDeviceMsgRateLimit("20:600"); |
|||
profileConfiguration.setTransportDeviceTelemetryMsgRateLimit("10:600"); |
|||
profileConfiguration.setTransportDeviceTelemetryDataPointsRateLimit("40:600"); |
|||
|
|||
doPost("/api/tenantProfile", tenantProfile); |
|||
|
|||
var expectedLimits = new SessionLimits(); |
|||
var deviceLimits = new SessionLimits.SessionRateLimits(profileConfiguration.getTransportDeviceMsgRateLimit(), |
|||
profileConfiguration.getTransportDeviceTelemetryMsgRateLimit(), |
|||
profileConfiguration.getTransportDeviceTelemetryDataPointsRateLimit()); |
|||
expectedLimits.setRateLimits(deviceLimits); |
|||
expectedLimits.setMaxPayloadSize(maxPayloadSize); |
|||
expectedLimits.setMaxInflightMessages(maxInflightMessages); |
|||
|
|||
MqttTestConfigProperties configProperties = MqttTestConfigProperties.builder() |
|||
.deviceName("Test Get Service Configuration") |
|||
.transportPayloadType(TransportPayloadType.JSON) |
|||
.build(); |
|||
processBeforeTest(configProperties); |
|||
|
|||
MqttTestClient client = new MqttTestClient(); |
|||
client.connectAndWait(accessToken); |
|||
|
|||
MqttTestCallback callback = new MqttTestSubscribeOnTopicCallback(DEVICE_RPC_RESPONSE_TOPIC + "1"); |
|||
client.setCallback(callback); |
|||
client.subscribeAndWait(DEVICE_RPC_RESPONSE_SUB_TOPIC, MqttQoS.AT_MOST_ONCE); |
|||
|
|||
client.publishAndWait(DEVICE_RPC_REQUESTS_TOPIC + "1", "{\"method\":\"getSessionLimits\",\"params\":{}}".getBytes()); |
|||
|
|||
assertThat(callback.getSubscribeLatch().await(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)) |
|||
.as("await callback").isTrue(); |
|||
|
|||
var payload = callback.getPayloadBytes(); |
|||
SessionLimits actualLimits = JacksonUtil.fromBytes(payload, SessionLimits.class); |
|||
assertEquals(expectedLimits, actualLimits); |
|||
|
|||
client.disconnect(); |
|||
} |
|||
|
|||
@Test |
|||
public void getSessionLimitsRpcForGatewayTest() throws Exception { |
|||
loginSysAdmin(); |
|||
TenantProfile tenantProfile = doGet("/api/tenantProfile/" + tenantProfileId, TenantProfile.class); |
|||
DefaultTenantProfileConfiguration profileConfiguration = tenantProfile.getDefaultProfileConfiguration(); |
|||
|
|||
profileConfiguration.setTransportGatewayMsgRateLimit("100:600"); |
|||
profileConfiguration.setTransportGatewayTelemetryMsgRateLimit("50:600"); |
|||
profileConfiguration.setTransportGatewayTelemetryDataPointsRateLimit("200:600"); |
|||
|
|||
profileConfiguration.setTransportGatewayDeviceMsgRateLimit("20:600"); |
|||
profileConfiguration.setTransportGatewayDeviceTelemetryMsgRateLimit("10:600"); |
|||
profileConfiguration.setTransportGatewayDeviceTelemetryDataPointsRateLimit("40:600"); |
|||
|
|||
doPost("/api/tenantProfile", tenantProfile); |
|||
|
|||
var expectedLimits = new GatewaySessionLimits(); |
|||
var gatewayLimits = new SessionLimits.SessionRateLimits(profileConfiguration.getTransportGatewayMsgRateLimit(), |
|||
profileConfiguration.getTransportGatewayTelemetryMsgRateLimit(), |
|||
profileConfiguration.getTransportGatewayTelemetryDataPointsRateLimit()); |
|||
var gatewayDeviceLimits = new SessionLimits.SessionRateLimits(profileConfiguration.getTransportGatewayDeviceMsgRateLimit(), |
|||
profileConfiguration.getTransportGatewayDeviceTelemetryMsgRateLimit(), |
|||
profileConfiguration.getTransportGatewayDeviceTelemetryDataPointsRateLimit()); |
|||
expectedLimits.setGatewayRateLimits(gatewayLimits); |
|||
expectedLimits.setRateLimits(gatewayDeviceLimits); |
|||
expectedLimits.setMaxPayloadSize(maxPayloadSize); |
|||
expectedLimits.setMaxInflightMessages(maxInflightMessages); |
|||
|
|||
MqttTestConfigProperties configProperties = MqttTestConfigProperties.builder() |
|||
.gatewayName("Test Get Service Configuration Gateway") |
|||
.transportPayloadType(TransportPayloadType.JSON) |
|||
.build(); |
|||
processBeforeTest(configProperties); |
|||
|
|||
MqttTestClient client = new MqttTestClient(); |
|||
client.connectAndWait(gatewayAccessToken); |
|||
|
|||
MqttTestCallback callback = new MqttTestSubscribeOnTopicCallback(DEVICE_RPC_RESPONSE_TOPIC + "1"); |
|||
client.setCallback(callback); |
|||
client.subscribeAndWait(DEVICE_RPC_RESPONSE_SUB_TOPIC, MqttQoS.AT_MOST_ONCE); |
|||
|
|||
client.publishAndWait(DEVICE_RPC_REQUESTS_TOPIC + "1", "{\"method\":\"getSessionLimits\",\"params\":{}}".getBytes()); |
|||
|
|||
assertTrue(callback.getSubscribeLatch().await(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)); |
|||
|
|||
var payload = callback.getPayloadBytes(); |
|||
SessionLimits actualLimits = JacksonUtil.fromBytes(payload, GatewaySessionLimits.class); |
|||
assertEquals(expectedLimits, actualLimits); |
|||
|
|||
client.disconnect(); |
|||
} |
|||
} |
|||
@ -0,0 +1,128 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
package org.thingsboard.server.queue.rabbitmq; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.anyBoolean; |
|||
import static org.mockito.ArgumentMatchers.anyString; |
|||
import static org.mockito.Mockito.times; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
import com.rabbitmq.client.Channel; |
|||
import com.rabbitmq.client.Connection; |
|||
import com.rabbitmq.client.ConnectionFactory; |
|||
import com.rabbitmq.client.GetResponse; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.extension.ExtendWith; |
|||
import org.mockito.Mock; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
|||
import org.thingsboard.server.queue.TbQueueAdmin; |
|||
import org.thingsboard.server.queue.TbQueueMsgDecoder; |
|||
import org.thingsboard.server.queue.common.DefaultTbQueueMsg; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
class TbRabbitMqConsumerTemplateTest { |
|||
|
|||
private static final String TOPIC = "some-topic"; |
|||
|
|||
@Mock |
|||
private TbQueueAdmin admin; |
|||
|
|||
@Mock |
|||
private ConnectionFactory connectionFactory; |
|||
|
|||
@Mock |
|||
private TbQueueMsgDecoder<DefaultTbQueueMsg> decoder; |
|||
|
|||
@Mock |
|||
private Connection connection; |
|||
|
|||
@Mock |
|||
private Channel channel; |
|||
|
|||
@Mock |
|||
private TopicPartitionInfo partition; |
|||
|
|||
@Mock |
|||
private GetResponse getResponse; |
|||
|
|||
private TbRabbitMqConsumerTemplate<DefaultTbQueueMsg> consumer; |
|||
|
|||
private void setUpConsumerWithMaxPollMessages(int maxPollMessages) throws Exception { |
|||
when(connectionFactory.newConnection()).thenReturn(connection); |
|||
when(connection.createChannel()).thenReturn(channel); |
|||
TbRabbitMqSettings settings = new TbRabbitMqSettings(); |
|||
settings.setMaxPollMessages(maxPollMessages); |
|||
settings.setConnectionFactory(connectionFactory); |
|||
|
|||
consumer = new TbRabbitMqConsumerTemplate<>(admin, settings, TOPIC, decoder); |
|||
when(partition.getFullTopicName()).thenReturn(TOPIC); |
|||
consumer.subscribe(Set.of(partition)); |
|||
} |
|||
|
|||
@Test |
|||
void pollWithMax5PollMessagesReturnsEmptyListIfNoMessages() throws Exception { |
|||
setUpConsumerWithMaxPollMessages(5); |
|||
when(channel.basicGet(anyString(), anyBoolean())).thenReturn(null); |
|||
|
|||
assertThat(consumer.poll(0L)).isEmpty(); |
|||
|
|||
verify(channel).basicGet(anyString(), anyBoolean()); |
|||
} |
|||
|
|||
@Test |
|||
void pollWithMax5PollMessagesReturns5MessagesIfQueueContains5() throws Exception { |
|||
setUpConsumerWithMaxPollMessages(5); |
|||
when(getResponse.getBody()).thenReturn(newMessageBody()); |
|||
when(channel.basicGet(anyString(), anyBoolean())).thenReturn(getResponse); |
|||
|
|||
assertThat(consumer.poll(0L)).hasSize(5); |
|||
|
|||
verify(channel, times(5)).basicGet(anyString(), anyBoolean()); |
|||
} |
|||
|
|||
@Test |
|||
void pollWithMax1PollMessageReturns1MessageIfQueueContainsMore() throws Exception { |
|||
setUpConsumerWithMaxPollMessages(1); |
|||
when(getResponse.getBody()).thenReturn(newMessageBody()); |
|||
when(channel.basicGet(anyString(), anyBoolean())).thenReturn(getResponse); |
|||
|
|||
assertThat(consumer.poll(0L)).hasSize(1); |
|||
|
|||
verify(channel).basicGet(anyString(), anyBoolean()); |
|||
} |
|||
|
|||
@Test |
|||
void pollWithMax3PollMessagesReturns2MessagesIfQueueContains2() throws Exception { |
|||
setUpConsumerWithMaxPollMessages(3); |
|||
when(getResponse.getBody()).thenReturn(newMessageBody()); |
|||
when(channel.basicGet(anyString(), anyBoolean())).thenReturn(getResponse, getResponse, null); |
|||
|
|||
assertThat(consumer.poll(0L)).hasSize(2); |
|||
|
|||
verify(channel, times(3)).basicGet(anyString(), anyBoolean()); |
|||
} |
|||
|
|||
private byte[] newMessageBody() { |
|||
return ("{\"key\": \"" + UUID.randomUUID() + "\"}").getBytes(StandardCharsets.UTF_8); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.mqtt.limits; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class GatewaySessionLimits extends SessionLimits { |
|||
|
|||
private SessionRateLimits gatewayRateLimits; |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
package org.thingsboard.server.transport.mqtt.limits; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.TransportPayloadType; |
|||
|
|||
@Data |
|||
public class SessionLimits { |
|||
|
|||
private int maxPayloadSize; |
|||
private int maxInflightMessages; |
|||
private SessionRateLimits rateLimits; |
|||
|
|||
public record SessionRateLimits(String messages, String telemetryMessages, String telemetryDataPoints) {} |
|||
} |
|||
File diff suppressed because one or more lines are too long
@ -0,0 +1,38 @@ |
|||
<!-- |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<div class="tb-svg-xml" style="background: #fff;" [ngClass]="{'tb-disabled': disabled, 'fill-height': fillHeight, 'no-label': noLabel}" |
|||
tb-fullscreen |
|||
[fullscreen]="fullscreen" fxLayout="column"> |
|||
<div fxLayout="row" fxLayoutAlign="start center" style="min-height: 40px;" class="tb-svg-xml-toolbar"> |
|||
<label *ngIf="!noLabel" class="tb-title no-padding" [ngClass]="{'tb-error': !disabled && (hasErrors || required && !modelValue), 'tb-required': !disabled && required}">{{ label }}</label> |
|||
<span fxFlex></span> |
|||
<fieldset style="width: initial"> |
|||
<div matTooltip="{{(fullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}" |
|||
matTooltipPosition="above" |
|||
style="border-radius: 50%" |
|||
(click)="fullscreen = !fullscreen"> |
|||
<button type='button' mat-icon-button class="tb-mat-32"> |
|||
<mat-icon class="material-icons">{{ fullscreen ? 'fullscreen_exit' : 'fullscreen' }}</mat-icon> |
|||
</button> |
|||
</div> |
|||
</fieldset> |
|||
</div> |
|||
<div id="tb-svg-xml-panel" class="tb-svg-xml-content-panel" fxLayout="column"> |
|||
<div #svgXmlEditor id="tb-svg-xml-input" [ngStyle]="fillHeight ? {} : {minHeight: minHeight}" [ngClass]="{'fill-height': fillHeight}"></div> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,85 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
.tb-svg-xml { |
|||
position: relative; |
|||
|
|||
&.tb-disabled { |
|||
color: rgba(0, 0, 0, .38); |
|||
} |
|||
|
|||
&.fill-height { |
|||
height: 100%; |
|||
} |
|||
|
|||
&.no-label { |
|||
.tb-svg-xml-content-panel { |
|||
height: 100%; |
|||
} |
|||
.tb-svg-xml-toolbar { |
|||
position: absolute; |
|||
top: 0; |
|||
left: 0; |
|||
right: 0; |
|||
z-index: 10; |
|||
pointer-events: none; |
|||
button { |
|||
pointer-events: auto; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-svg-xml-content-panel { |
|||
height: calc(100% - 40px); |
|||
border: 1px solid #c0c0c0; |
|||
|
|||
#tb-svg-xml-input { |
|||
width: 100%; |
|||
min-width: 200px; |
|||
height: 100%; |
|||
} |
|||
} |
|||
|
|||
&:not(.tb-fullscreen) { |
|||
padding-bottom: 15px; |
|||
} |
|||
|
|||
.tb-svg-xml-toolbar { |
|||
& > * { |
|||
&:not(:last-child) { |
|||
margin-right: 4px; |
|||
} |
|||
} |
|||
button.mat-mdc-button-base, button.mat-mdc-button-base.tb-mat-32 { |
|||
background: rgba(220, 220, 220, .35); |
|||
align-items: center; |
|||
vertical-align: middle; |
|||
min-width: 32px; |
|||
min-height: 15px; |
|||
padding: 4px; |
|||
font-size: .8rem; |
|||
line-height: 15px; |
|||
&:not(.tb-help-popup-button) { |
|||
color: #7b7b7b; |
|||
} |
|||
} |
|||
button.mat-mdc-button-base:not(.mat-mdc-icon-button) { |
|||
height: 23px; |
|||
} |
|||
.tb-help-popup-button-loading { |
|||
background: #f3f3f3; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,211 @@ |
|||
///
|
|||
/// 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 { |
|||
ChangeDetectorRef, |
|||
Component, |
|||
ElementRef, |
|||
forwardRef, |
|||
Input, |
|||
OnDestroy, |
|||
OnInit, |
|||
ViewChild, |
|||
ViewEncapsulation |
|||
} from '@angular/core'; |
|||
import { ControlValueAccessor, UntypedFormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validator } from '@angular/forms'; |
|||
import { Ace } from 'ace-builds'; |
|||
import { getAce } from '@shared/models/ace/ace.models'; |
|||
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { UtilsService } from '@core/services/utils.service'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
import { CancelAnimationFrame, RafService } from '@core/services/raf.service'; |
|||
import { ResizeObserver } from '@juggle/resize-observer'; |
|||
import { coerceBoolean } from '@shared/decorators/coercion'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-svg-xml', |
|||
templateUrl: './svg-xml.component.html', |
|||
styleUrls: ['./svg-xml.component.scss'], |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => SvgXmlComponent), |
|||
multi: true |
|||
}, |
|||
{ |
|||
provide: NG_VALIDATORS, |
|||
useExisting: forwardRef(() => SvgXmlComponent), |
|||
multi: true, |
|||
} |
|||
], |
|||
encapsulation: ViewEncapsulation.None |
|||
}) |
|||
export class SvgXmlComponent implements OnInit, OnDestroy, ControlValueAccessor, Validator { |
|||
|
|||
@ViewChild('svgXmlEditor', {static: true}) |
|||
svgXmlEditorElmRef: ElementRef; |
|||
|
|||
private svgXmlEditor: Ace.Editor; |
|||
private editorsResizeCaf: CancelAnimationFrame; |
|||
private editorResize$: ResizeObserver; |
|||
private ignoreChange = false; |
|||
|
|||
@Input() label: string; |
|||
|
|||
@Input() disabled: boolean; |
|||
|
|||
@Input() |
|||
@coerceBoolean() |
|||
fillHeight = false; |
|||
|
|||
@Input() |
|||
@coerceBoolean() |
|||
noLabel = false; |
|||
|
|||
@Input() minHeight = '200px'; |
|||
|
|||
private requiredValue: boolean; |
|||
get required(): boolean { |
|||
return this.requiredValue; |
|||
} |
|||
@Input() |
|||
set required(value: boolean) { |
|||
this.requiredValue = coerceBooleanProperty(value); |
|||
} |
|||
|
|||
fullscreen = false; |
|||
|
|||
modelValue: string; |
|||
|
|||
hasErrors = false; |
|||
|
|||
private propagateChange = null; |
|||
|
|||
constructor(public elementRef: ElementRef, |
|||
private utils: UtilsService, |
|||
private translate: TranslateService, |
|||
protected store: Store<AppState>, |
|||
private raf: RafService, |
|||
private cd: ChangeDetectorRef) { |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
const editorElement = this.svgXmlEditorElmRef.nativeElement; |
|||
let editorOptions: Partial<Ace.EditorOptions> = { |
|||
mode: 'ace/mode/svg', |
|||
showGutter: true, |
|||
showPrintMargin: true, |
|||
readOnly: this.disabled |
|||
}; |
|||
|
|||
const advancedOptions = { |
|||
enableSnippets: true, |
|||
enableBasicAutocompletion: true, |
|||
enableLiveAutocompletion: true |
|||
}; |
|||
|
|||
editorOptions = {...editorOptions, ...advancedOptions}; |
|||
getAce().subscribe( |
|||
(ace) => { |
|||
this.svgXmlEditor = ace.edit(editorElement, editorOptions); |
|||
this.svgXmlEditor.session.setUseWrapMode(true); |
|||
this.svgXmlEditor.setValue(this.modelValue ? this.modelValue : '', -1); |
|||
this.svgXmlEditor.setReadOnly(this.disabled); |
|||
this.svgXmlEditor.on('change', () => { |
|||
if (!this.ignoreChange) { |
|||
this.updateView(); |
|||
} |
|||
}); |
|||
// @ts-ignore
|
|||
this.svgXmlEditor.session.on('changeAnnotation', () => { |
|||
const annotations = this.svgXmlEditor.session.getAnnotations(); |
|||
const hasErrors = annotations.filter(annotation => annotation.type === 'error').length > 0; |
|||
if (this.hasErrors !== hasErrors) { |
|||
this.hasErrors = hasErrors; |
|||
this.propagateChange(this.modelValue); |
|||
this.cd.markForCheck(); |
|||
} |
|||
}); |
|||
this.editorResize$ = new ResizeObserver(() => { |
|||
this.onAceEditorResize(); |
|||
}); |
|||
this.editorResize$.observe(editorElement); |
|||
} |
|||
); |
|||
} |
|||
|
|||
ngOnDestroy(): void { |
|||
if (this.editorResize$) { |
|||
this.editorResize$.disconnect(); |
|||
} |
|||
if (this.svgXmlEditor) { |
|||
this.svgXmlEditor.destroy(); |
|||
} |
|||
} |
|||
|
|||
private onAceEditorResize() { |
|||
if (this.editorsResizeCaf) { |
|||
this.editorsResizeCaf(); |
|||
this.editorsResizeCaf = null; |
|||
} |
|||
this.editorsResizeCaf = this.raf.raf(() => { |
|||
this.svgXmlEditor.resize(); |
|||
this.svgXmlEditor.renderer.updateFull(); |
|||
}); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void { |
|||
} |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
this.disabled = isDisabled; |
|||
if (this.svgXmlEditor) { |
|||
this.svgXmlEditor.setReadOnly(this.disabled); |
|||
} |
|||
} |
|||
|
|||
public validate(c: UntypedFormControl) { |
|||
return (!this.hasErrors) ? null : { |
|||
svgXml: { |
|||
valid: false, |
|||
}, |
|||
}; |
|||
} |
|||
|
|||
writeValue(value: string): void { |
|||
this.modelValue = value; |
|||
if (this.svgXmlEditor) { |
|||
this.ignoreChange = true; |
|||
this.svgXmlEditor.setValue(this.modelValue ? this.modelValue : '', -1); |
|||
this.ignoreChange = false; |
|||
} |
|||
} |
|||
|
|||
updateView() { |
|||
const editorValue = this.svgXmlEditor.getValue(); |
|||
if (this.modelValue !== editorValue) { |
|||
this.modelValue = editorValue; |
|||
this.propagateChange(this.modelValue); |
|||
this.cd.markForCheck(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
:host { |
|||
tb-toggle-header { |
|||
&.fill-height { |
|||
height: 100%; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue