Skip to content

Commit

Permalink
Merge pull request HyperIoT-Labs#144 from FedericoTorsello/HYIOTPT-11…
Browse files Browse the repository at this point in the history
…84_linechart-fullscreen

Linechart fullscreen convertion fix
  • Loading branch information
degrelle authored Jan 9, 2025
2 parents ef646ec + 6f9e559 commit fdfbbfe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ export class RealtimeDataService extends BaseDataService implements IDataService
return dataChannel;
}

copyDataChannel(widgetId: number, originalChannelId: number) {
const originalChannel = this.dataChannels[originalChannelId];
if (!originalChannel) {
throw 'channel with id ' + originalChannelId + ' not found';
}
const dataChannel = super.copyDataChannel(widgetId, originalChannelId);
dataChannel.controller = new RealtimeDataChannelController();
(dataChannel.controller.dataStreamOutput$ as Observable<PacketDataChunk>).subscribe(dataChannel.subject);

return dataChannel;
}

private onWsOpen() {
this.isConnected = true;
}
Expand Down
19 changes: 11 additions & 8 deletions projects/widgets/src/lib/base/base-widget/base-widget.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export abstract class BaseWidgetComponent implements OnChanges, OnInit, OnDestro
return +Object.keys(packetFields).find(key => packetFields[key] === fieldName);
}

computePacketData(packetData: PacketData[]) {
computePacketData(packetData: PacketData[], convertOldValues = true) {
if (!this.widget.config.fieldUnitConversions) {
return;
}
Expand All @@ -135,34 +135,37 @@ export abstract class BaseWidgetComponent implements OnChanges, OnInit, OnDestro
// TODO all configs should contain information about field type
fieldType = this.widget.config.fieldTypes[fieldId];
} catch (error) { }
switch(fieldType) {
switch (fieldType) {
case 'DEFAULT':
case 'INTEGER':
case 'DOUBLE':
case 'FLOAT': {
// TODO: inserire qui la conversione dei valori con expression
const unitConversion = this.widget.config.fieldUnitConversions[fieldId];
// temp fix. packet values shouldn't be forcibly converted
// temp fix. packet values shouldn't be forcibly converted

if (!isNaN(parseFloat(pd[packetKey]))) {
pd[packetKey] = parseFloat(pd[packetKey]);
}
if (unitConversion && typeof pd[packetKey] === 'number') {
// applying unit conversion
if (unitConversion.convertFrom !== unitConversion.convertTo) {
pd[packetKey] = convert(pd[packetKey])
.from(unitConversion.convertFrom)
.to(unitConversion.convertTo);
if (convertOldValues) {
pd[packetKey] = convert(pd[packetKey])
.from(unitConversion.convertFrom)
.to(unitConversion.convertTo);
}
}
pd[packetKey] = +pd[packetKey];
// applying approximation
try {
pd[packetKey] = pd[packetKey].toFixed(unitConversion.decimals);
}
catch(error) {
catch (error) {
this.logger.error(error);
}
try {
if(this.widget.config.fieldCustomConversions
if (this.widget.config.fieldCustomConversions
&& this.widget.config.fieldCustomConversions[fieldId]
&& this.widget.config.fieldCustomConversions[fieldId].expression
&& this.widget.config.fieldCustomConversions[fieldId].expression.trim() !== '') {
Expand Down
30 changes: 19 additions & 11 deletions projects/widgets/src/lib/widget/line-chart/line-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
configure() {
this.graph.layout = {};
this.graph.data = [];
this.allData = [];

super.removeSubscriptionsAndDataChannels();
if (!this.serviceType) {
this.logger.error("TYPE SERVICE UNDEFINED");
Expand Down Expand Up @@ -216,7 +218,7 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
this.logger.debug("subscribeAndInit");
this.subscribeDataChannel();
if (this.serviceType === ServiceType.ONLINE) {
this.computePacketData(this.initData);
this.computePacketData(this.initData, this.isToolbarVisible);
}

const resizeObserver = new ResizeObserver((entries) => {
Expand All @@ -241,15 +243,18 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
this.widget.config.packetFields,
true
);

this.channelId = +this.widget.id;
if (!this.isToolbarVisible && this.serviceType === ServiceType.OFFLINE) {

if (this.isToolbarVisible) {
this.dataChannel = this.dataService.addDataChannel(this.channelId, [dataPacketFilter]);
} else {
// setting negative id to fullscreen offline widget to prevent updating original widget
// TODO channelId should be a proper string
this.channelId = -this.channelId;
this.dataChannel = this.dataService.copyDataChannel(this.channelId, -this.channelId);
} else {
this.dataChannel = this.dataService.addDataChannel(this.channelId, [ dataPacketFilter ]);
}

this.dataSubscription = this.dataChannel.subject
.pipe(
map((dataChunk) => dataChunk.data),
Expand All @@ -263,9 +268,7 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
this.offControllerSubscription =
this.dataChannel.controller.$totalCount.subscribe((res) => {
this.totalLength = res;
if (!this.isToolbarVisible) { // if fullscreen
this.computePacketData(this.initData);
} else {
if (this.isToolbarVisible) {
this.allData = [];
this.graph.data.forEach((tsd) => {
(tsd.x = []), (tsd.y = []);
Expand All @@ -277,19 +280,23 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
this.dataRequest();
}
}
} else { // if fullscreen
this.computePacketData(this.initData, this.isToolbarVisible);
}
});
}
}

async computePacketData(packetData: PacketData[]) {
async computePacketData(packetData: PacketData[], convertOldValues = true) {
this.logger.debug("computePacketData", packetData);
super.computePacketData(packetData);

if (packetData.length === 0) {
return;
}
this.allData = this.allData.concat(packetData);

this.allData = this.allData.concat([...packetData]);

super.computePacketData(packetData, convertOldValues);

packetData.forEach((datum) => {
if (
Expand All @@ -300,6 +307,7 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
}
this.convertAndBufferData(datum);
});

await this.renderBufferedData();
this.loadingOfflineData = false;
if (this.serviceType === ServiceType.OFFLINE) {
Expand Down Expand Up @@ -625,7 +633,7 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
this.pause();
break;
case "toolbar:fullscreen":
widgetAction.value = this.allData;
widgetAction.value = [...this.allData];
break;
}

Expand Down

0 comments on commit fdfbbfe

Please sign in to comment.