knob.component.ts
14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
///
/// Copyright © 2016-2020 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, ElementRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { PageComponent } from '@shared/components/page.component';
import { WidgetContext } from '@home/models/widget-component.models';
import { UtilsService } from '@core/services/utils.service';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { isDefined, isNumber } from '@core/utils';
import { CanvasDigitalGauge, CanvasDigitalGaugeOptions } from '@home/components/widget/lib/canvas-digital-gauge';
import * as tinycolor_ from 'tinycolor2';
import { ResizeObserver } from '@juggle/resize-observer';
import GenericOptions = CanvasGauges.GenericOptions;
const tinycolor = tinycolor_;
interface KnobSettings {
minValue: number;
maxValue: number;
initialValue: number;
title: string;
getValueMethod: string;
setValueMethod: string;
requestTimeout: number;
}
@Component({
selector: 'tb-knob',
templateUrl: './knob.component.html',
styleUrls: ['./knob.component.scss']
})
export class KnobComponent extends PageComponent implements OnInit, OnDestroy {
@ViewChild('knob', {static: true}) knobRef: ElementRef<HTMLElement>;
@ViewChild('knobContainer', {static: true}) knobContainerRef: ElementRef<HTMLElement>;
@ViewChild('knobTopPointerContainer', {static: true}) knobTopPointerContainerRef: ElementRef<HTMLElement>;
@ViewChild('knobTopPointer', {static: true}) knobTopPointerRef: ElementRef<HTMLElement>;
@ViewChild('knobValueContainer', {static: true}) knobValueContainerRef: ElementRef<HTMLElement>;
@ViewChild('knobValue', {static: true}) knobValueRef: ElementRef<HTMLElement>;
@ViewChild('knobErrorContainer', {static: true}) knobErrorContainerRef: ElementRef<HTMLElement>;
@ViewChild('knobError', {static: true}) knobErrorRef: ElementRef<HTMLElement>;
@ViewChild('knobTitleContainer', {static: true}) knobTitleContainerRef: ElementRef<HTMLElement>;
@ViewChild('knobTitle', {static: true}) knobTitleRef: ElementRef<HTMLElement>;
@ViewChild('knobMinmaxContainer', {static: true}) knobMinmaxContainerRef: ElementRef<HTMLElement>;
@ViewChild('textMeasure', {static: true}) textMeasureRef: ElementRef<HTMLElement>;
@ViewChild('canvasBar', {static: true}) canvasBarElementRef: ElementRef<HTMLElement>;
@Input()
ctx: WidgetContext;
value = '0';
error = '';
title = '';
minValue: number;
maxValue: number;
private startDeg = -1;
private currentDeg = 0;
private rotation = 0;
private lastDeg = 0;
private moving = false;
private minDeg = -45;
private maxDeg = 225;
private isSimulated: boolean;
private requestTimeout: number;
private getValueMethod: string;
private setValueMethod: string;
private executingUpdateValue: boolean;
private scheduledValue: number;
private rpcValue: number;
private knob: JQuery<HTMLElement>;
private knobContainer: JQuery<HTMLElement>;
private knobTopPointerContainer: JQuery<HTMLElement>;
private knobTopPointer: JQuery<HTMLElement>;
private knobValueContainer: JQuery<HTMLElement>;
private knobValue: JQuery<HTMLElement>;
private knobTitleContainer: JQuery<HTMLElement>;
private knobTitle: JQuery<HTMLElement>;
private knobErrorContainer: JQuery<HTMLElement>;
private knobError: JQuery<HTMLElement>;
private knobMinmaxContainer: JQuery<HTMLElement>;
private minmaxLabel: JQuery<HTMLElement>;
private textMeasure: JQuery<HTMLElement>;
private canvasBarElement: HTMLElement;
private canvasBar: CanvasDigitalGauge;
private knobResize$: ResizeObserver;
constructor(private utils: UtilsService,
protected store: Store<AppState>) {
super(store);
}
ngOnInit(): void {
this.knob = $(this.knobRef.nativeElement);
this.knobContainer = $(this.knobContainerRef.nativeElement);
this.knobTopPointerContainer = $(this.knobTopPointerContainerRef.nativeElement);
this.knobTopPointer = $(this.knobTopPointerRef.nativeElement);
this.knobValueContainer = $(this.knobValueContainerRef.nativeElement);
this.knobValue = $(this.knobValueRef.nativeElement);
this.knobTitleContainer = $(this.knobTitleContainerRef.nativeElement);
this.knobTitle = $(this.knobTitleRef.nativeElement);
this.knobErrorContainer = $(this.knobErrorContainerRef.nativeElement);
this.knobError = $(this.knobErrorRef.nativeElement);
this.knobMinmaxContainer = $(this.knobMinmaxContainerRef.nativeElement);
this.minmaxLabel = this.knobMinmaxContainer.find<HTMLElement>('.minmax-label');
this.textMeasure = $(this.textMeasureRef.nativeElement);
this.canvasBarElement = this.canvasBarElementRef.nativeElement;
this.knobResize$ = new ResizeObserver(() => {
this.resize();
});
this.knobResize$.observe(this.knobContainerRef.nativeElement);
this.init();
}
ngOnDestroy(): void {
if (this.knobResize$) {
this.knobResize$.disconnect();
}
}
private init() {
const settings: KnobSettings = this.ctx.settings;
this.minValue = isDefined(settings.minValue) ? settings.minValue : 0;
this.maxValue = isDefined(settings.maxValue) ? settings.maxValue : 100;
this.title = isDefined(settings.title) ? settings.title : '';
const levelColors = ['#19ff4b', '#ffff19', '#ff3232'];
const canvasBarData: CanvasDigitalGaugeOptions = {
renderTo: this.canvasBarElement,
hideValue: true,
neonGlowBrightness: 0,
gaugeWidthScale: 0.4,
gaugeColor: 'rgba(0, 0, 0, 0)',
levelColors,
minValue: this.minValue,
maxValue: this.maxValue,
gaugeType: 'donut',
dashThickness: 2,
donutStartAngle: 3/4*Math.PI,
donutEndAngle: 9/4*Math.PI,
animation: false
};
this.canvasBar = new CanvasDigitalGauge(canvasBarData).draw();
this.knob.on('click', (e) => {
if (this.moving) {
this.moving = false;
return false;
}
e.preventDefault();
const offset = this.knob.offset();
const center = {
y : offset.top + this.knob.height()/2,
x: offset.left + this.knob.width()/2
};
const rad2deg = 180/Math.PI;
const t: Touch = ((e.originalEvent as any).touches) ? (e.originalEvent as any).touches[0] : e;
const a = center.y - t.pageY;
const b = center.x - t.pageX;
let deg = Math.atan2(a,b)*rad2deg;
if(deg < 0){
deg = 360 + deg;
}
if (deg > this.maxDeg) {
if (deg - 360 > this.minDeg) {
deg = deg - 360;
} else {
return false;
}
}
this.currentDeg = deg;
this.lastDeg = deg;
this.knobTopPointerContainer.css('transform','rotate('+(this.currentDeg)+'deg)');
this.turn(this.degreeToRatio(this.currentDeg));
this.rotation = this.currentDeg;
this.startDeg = -1;
});
this.knob.on('mousedown touchstart', (e) => {
e.preventDefault();
const offset = this.knob.offset();
const center = {
y : offset.top + this.knob.height()/2,
x: offset.left + this.knob.width()/2
};
const rad2deg = 180/Math.PI;
this.knob.on('mousemove.rem touchmove.rem', (ev) => {
this.moving = true;
const t: Touch = ((ev.originalEvent as any).touches) ? (ev.originalEvent as any).touches[0] : ev;
const a = center.y - t.pageY;
const b = center.x - t.pageX;
let deg = Math.atan2(a,b)*rad2deg;
if(deg < 0){
deg = 360 + deg;
}
if(this.startDeg === -1){
this.startDeg = deg;
}
let tmp = Math.floor((deg-this.startDeg) + this.rotation);
if(tmp < 0){
tmp = 360 + tmp;
}
else if(tmp > 359){
tmp = tmp % 360;
}
if (tmp > this.maxDeg) {
if (tmp - 360 > this.minDeg) {
tmp = tmp - 360;
} else {
const deltaMax = Math.abs(this.maxDeg - this.lastDeg);
const deltaMin = Math.abs(this.minDeg - this.lastDeg);
if (deltaMax < deltaMin) {
tmp = this.maxDeg;
} else {
tmp = this.minDeg;
}
}
}
if(Math.abs(tmp - this.lastDeg) > 180){
this.startDeg = deg;
this.rotation = this.currentDeg;
return false;
}
this.currentDeg = tmp;
this.lastDeg = tmp;
this.knobTopPointerContainer.css('transform','rotate('+(this.currentDeg)+'deg)');
this.turn(this.degreeToRatio(this.currentDeg));
});
$(document).on('mouseup.rem touchend.rem',() => {
this.knob.off('.rem');
$(document).off('.rem');
this.rotation = this.currentDeg;
this.startDeg = -1;
});
});
const initialValue = isDefined(settings.initialValue) ? settings.initialValue : this.minValue;
this.setValue(initialValue);
const subscription = this.ctx.defaultSubscription;
const rpcEnabled = subscription.rpcEnabled;
this.isSimulated = this.utils.widgetEditMode;
this.requestTimeout = 500;
if (settings.requestTimeout) {
this.requestTimeout = settings.requestTimeout;
}
this.getValueMethod = 'getValue';
if (settings.getValueMethod && settings.getValueMethod.length) {
this.getValueMethod = settings.getValueMethod;
}
this.setValueMethod = 'setValue';
if (settings.setValueMethod && settings.setValueMethod.length) {
this.setValueMethod = settings.setValueMethod;
}
if (!rpcEnabled) {
this.onError('Target device is not set!');
} else {
if (!this.isSimulated) {
this.rpcRequestValue();
}
}
}
private degreeToRatio(degree: number): number {
return (degree-this.minDeg)/(this.maxDeg-this.minDeg);
}
private ratioToDegree(ratio: number): number {
return this.minDeg + ratio*(this.maxDeg-this.minDeg);
}
private turn(ratio: number) {
const value = Number((this.minValue + (this.maxValue - this.minValue)*ratio).toFixed(this.ctx.decimals));
if (this.canvasBar.value !== value) {
this.canvasBar.value = value;
}
this.updateColor(this.canvasBar.getValueColor());
this.onValue(value);
}
private resize() {
const width = this.knobContainer.width();
const height = this.knobContainer.height();
const size = Math.min(width, height);
this.knob.css({width: size, height: size});
this.canvasBar.update({width: size, height: size} as GenericOptions);
this.setFontSize(this.knobTitle, this.title, this.knobTitleContainer.height(), this.knobTitleContainer.width());
this.setFontSize(this.knobError, this.error, this.knobErrorContainer.height(), this.knobErrorContainer.width());
const minmaxHeight = this.knobMinmaxContainer.height();
this.minmaxLabel.css({fontSize: minmaxHeight+'px', lineHeight: minmaxHeight+'px'});
this.checkValueSize();
}
private checkValueSize() {
const fontSize = this.knobValueContainer.height()/3.3;
const containerWidth = this.knobValueContainer.width();
this.setFontSize(this.knobValue, this.value+'', fontSize, containerWidth);
}
private setFontSize(element: JQuery<HTMLElement>, text: string, fontSize: number, maxWidth: number) {
let textWidth = this.measureTextWidth(text, fontSize);
while (textWidth > maxWidth) {
fontSize--;
if (fontSize < 0) {
break;
}
textWidth = this.measureTextWidth(text, fontSize);
}
element.css({fontSize: fontSize+'px', lineHeight: fontSize+'px'});
}
private measureTextWidth(text: string, fontSize: number): number {
this.textMeasure.css({fontSize: fontSize+'px', lineHeight: fontSize+'px'});
this.textMeasure.html(text);
return this.textMeasure.width();
}
private setValue(value: number) {
const ratio = (value-this.minValue) / (this.maxValue - this.minValue);
this.rotation = this.lastDeg = this.currentDeg = this.ratioToDegree(ratio);
this.knobTopPointerContainer.css('transform','rotate('+(this.currentDeg)+'deg)');
if (this.canvasBar.value !== value) {
this.canvasBar.value = value;
}
this.updateColor(this.canvasBar.getValueColor());
this.value = this.formatValue(value);
this.checkValueSize();
this.ctx.detectChanges();
}
private updateColor(color: string) {
const glowColor = tinycolor(color).brighten(30).toHexString();
this.knobValue.css({color: glowColor});
const textShadow = `${color} 1px 1px 10px, ${glowColor} 1px 1px 10px`;
this.knobValue.css({textShadow});
this.knobTopPointer.css({backgroundColor: glowColor});
const boxShadow = `inset 1px 0 2px #040404, 1px 1px 8px 2px ${glowColor}`;
this.knobTopPointer.css({boxShadow});
}
private onValue(value: number) {
this.value = this.formatValue(value);
this.checkValueSize();
this.rpcUpdateValue(value);
this.ctx.detectChanges();
}
private formatValue(value: any): string {
return this.ctx.utils.formatValue(value, this.ctx.decimals, this.ctx.units, true);
}
private rpcRequestValue() {
this.error = '';
this.ctx.controlApi.sendTwoWayCommand(this.getValueMethod, null, this.requestTimeout).subscribe(
(responseBody) => {
if (isNumber(responseBody)) {
const numValue = Number(Number(responseBody).toFixed(this.ctx.decimals));
this.setValue(numValue);
} else {
const errorText = `Unable to parse response: ${responseBody}`;
this.onError(errorText);
}
},
() => {
const errorText = this.ctx.defaultSubscription.rpcErrorText;
this.onError(errorText);
}
);
}
private rpcUpdateValue(value: number) {
if (this.executingUpdateValue) {
this.scheduledValue = value;
return;
} else {
this.scheduledValue = null;
this.rpcValue = value;
this.executingUpdateValue = true;
}
this.error = '';
this.ctx.controlApi.sendOneWayCommand(this.setValueMethod, value, this.requestTimeout).subscribe(
() => {
this.executingUpdateValue = false;
if (this.scheduledValue != null && this.scheduledValue !== this.rpcValue) {
this.rpcUpdateValue(this.scheduledValue);
}
},
() => {
this.executingUpdateValue = false;
const errorText = this.ctx.defaultSubscription.rpcErrorText;
this.onError(errorText);
}
);
}
private onError(error: string) {
this.error = error;
this.setFontSize(this.knobError, this.error, this.knobErrorContainer.height(), this.knobErrorContainer.width());
this.ctx.detectChanges();
}
}