Compare commits
47 Commits
2540d19b76
...
dev-Pieter
| Author | SHA1 | Date | |
|---|---|---|---|
| d5d078413c | |||
| 17662ef7cb | |||
| f653a1e98c | |||
| 3886277616 | |||
| 83018fabe0 | |||
| e72579e5d0 | |||
| 0fb42865ff | |||
| b2b811e802 | |||
| bde2dcf7d8 | |||
| 76570280bc | |||
| d7017b5d33 | |||
| f93603c182 | |||
| c261335df5 | |||
| a41f053d5d | |||
| 8d7d98f126 | |||
| 3f90685834 | |||
| efc97d6cd1 | |||
|
|
d72bfd5560 | ||
| 6d30e25daa | |||
| 16e202e841 | |||
|
|
241ed1d3cb | ||
| 3876f86530 | |||
| 56be0f1840 | |||
|
|
a30f2c90f4 | ||
| 302e122387 | |||
|
|
50f99fa642 | ||
| 494a688583 | |||
| c512c96636 | |||
|
|
eb15da2a63 | ||
| 6dcd3c3d26 | |||
| 958ec2269c | |||
|
|
83ca429bf5 | ||
|
|
222d0f56fc | ||
|
|
e1c6124cf0 | ||
|
|
e87f9da4bf | ||
| 0bccad05f8 | |||
| 7191e57aea | |||
| aec2d3692d | |||
|
|
7061d6a539 | ||
| 71643375fc | |||
| f13ee68938 | |||
| 475caa90db | |||
| 9aa38f9000 | |||
| 4a6273b037 | |||
| 8c9301b128 | |||
| 7cdfc87c83 | |||
| 839ae2f3da |
98
LICENSE
98
LICENSE
@@ -1,9 +1,97 @@
|
||||
MIT License
|
||||
OPENBARE LICENTIE VAN DE EUROPESE UNIE v. 1.2.
|
||||
EUPL © Europese Unie 2007, 2016
|
||||
Deze openbare licentie van de Europese Unie („EUPL”) is van toepassing op het werk (zoals hieronder gedefinieerd) dat onder de voorwaarden van deze licentie wordt verstrekt. Elk gebruik van het werk dat niet door deze licentie is toegestaan, is verboden (voor zover dit gebruik valt onder een recht van de houder van het auteursrecht op het werk). Het werk wordt verstrekt onder de voorwaarden van deze licentie wanneer de licentiegever (zoals hieronder gedefinieerd), direct volgend op de kennisgeving inzake het auteursrecht op het werk, de volgende kennisgeving opneemt:
|
||||
In licentie gegeven krachtens de EUPL
|
||||
of op een andere wijze zijn bereidheid te kennen heeft gegeven krachtens de EUPL in licentie te geven.
|
||||
|
||||
Copyright (c) 2025 RnD
|
||||
1.Definities
|
||||
In deze licentie wordt verstaan onder:
|
||||
— „de licentie”:de onderhavige licentie;
|
||||
— „het oorspronkelijke werk”:het werk dat of de software die door de licentiegever krachtens deze licentie wordt verspreid of medegedeeld, en dat/die beschikbaar is als broncode en, in voorkomend geval, ook als uitvoerbare code;
|
||||
— „bewerkingen”:de werken of software die de licentiehouder kan creëren op grond van het oorspronkelijke werk of wijzigingen ervan. In deze licentie wordt niet gedefinieerd welke mate van wijziging of afhankelijkheid van het oorspronkelijke werk vereist is om een werk als een bewerking te kunnen aanmerken; dat wordt bepaald conform het auteursrecht dat van toepassing is in de in artikel 15 bedoelde staat;
|
||||
— „het werk”:het oorspronkelijke werk of de bewerkingen ervan;
|
||||
— „de broncode”:de voor mensen leesbare vorm van het werk, die het gemakkelijkste door mensen kan worden bestudeerd en gewijzigd;
|
||||
— „de uitvoerbare code”:elke code die over het algemeen is gecompileerd en is bedoeld om door een computer als een programma te worden uitgevoerd;
|
||||
— „de licentiegever”:de natuurlijke of rechtspersoon die het werk krachtens de licentie verspreidt of mededeelt;
|
||||
— „bewerker(s)”:elke natuurlijke of rechtspersoon die het werk krachtens de licentie wijzigt of op een andere wijze bijdraagt tot de totstandkoming van een bewerking;
|
||||
— „de licentiehouder” of „u”:elke natuurlijke of rechtspersoon die het werk onder de voorwaarden van de licentie gebruikt; — „verspreiding” of „mededeling”:het verkopen, geven, uitlenen, verhuren, verspreiden, mededelen, doorgeven, of op een andere wijze online of offline beschikbaar stellen van kopieën van het werk of het verlenen van toegang tot de essentiële functies ervan ten behoeve van andere natuurlijke of rechtspersonen.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
2.Draagwijdte van de uit hoofde van de licentie verleende rechten
|
||||
De licentiegever verleent u hierbij een wereldwijde, royaltyvrije, niet-exclusieve, voor een sublicentie in aanmerking komende licentie, om voor de duur van het aan het oorspronkelijke werk verbonden auteursrecht, het volgende te doen:
|
||||
— het werk in alle omstandigheden en voor ongeacht welk doel te gebruiken;
|
||||
— het werk te verveelvoudigen;
|
||||
— het werk te wijzigen en op grond van het werk bewerkingen te ontwikkelen;
|
||||
— het werk aan het publiek mede te delen, waaronder het recht om het werk of kopieën ervan aan het publiek ter beschikking te stellen of te vertonen, en het werk, in voorkomend geval, in het openbaar uit te voeren;
|
||||
— het werk of kopieën ervan te verspreiden;
|
||||
— het werk of kopieën ervan uit te lenen en te verhuren;
|
||||
— de rechten op het werk of op kopieën ervan in sublicentie te geven.
|
||||
Deze rechten kunnen worden uitgeoefend met gebruikmaking van alle thans bekende of nog uit te vinden media, dragers en formaten, voor zover het toepasselijke recht dit toestaat. In de landen waar immateriële rechten van toepassing zijn, doet de licentiegever afstand van zijn recht op uitoefening van zijn immateriële rechten in de mate die door het toepasselijke recht wordt toegestaan teneinde een doeltreffende uitoefening van de bovenvermelde in licentie gegeven economische rechten mogelijk te maken. De licentiegever verleent de licentiehouder een royaltyvrij, niet-exclusief gebruiksrecht op alle octrooien van de licentiegever, voor zover dit noodzakelijk is om de uit hoofde van deze licentie verleende rechten op het werk te gebruiken.
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
3.Mededeling van de broncode
|
||||
De licentiegever kan het werk verstrekken in zijn broncode of als uitvoerbare code. Indien het werk als uitvoerbare code wordt verstrekt, verstrekt de licentiegever bij elke door hem verspreide kopie van het werk tevens een machinaal leesbare kopie van de broncode van het werk of geeft hij in een mededeling, volgende op de bij het werk gevoegde auteursrechtelijke kennisgeving, de plaats aan waar de broncode gemakkelijk en vrij toegankelijk is, zolang de licentiegever het werk blijft verspreiden of mededelen.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
4.Beperkingen van het auteursrecht
|
||||
Geen enkele bepaling in deze licentie heeft ten doel de licentiehouder het recht te ontnemen een beroep te doen op een uitzondering op of een beperking van de exclusieve rechten van de rechthebbenden op het werk, of op de uitputting van die rechten of andere toepasselijke beperkingen daarvan.
|
||||
|
||||
5.Verplichtingen van de licentiehouder
|
||||
De verlening van de bovenvermelde rechten is onderworpen aan een aantal aan de licentiehouder opgelegde beperkingen en verplichtingen. Het gaat om de onderstaande verplichtingen.
|
||||
|
||||
Attributierecht: de licentiehouder moet alle auteurs-, octrooi- of merkenrechtelijke kennisgevingen onverlet laten alsook alle kennisgevingen die naar de licentie en de afwijzing van garanties verwijzen. De licentiehouder moet een afschrift van deze kennisgevingen en een afschrift van de licentie bij elke kopie van het werk voegen die hij verspreidt of mededeelt. De licentiehouder moet in elke bewerking duidelijk aangeven dat het werk is gewijzigd, en eveneens de datum van wijziging vermelden.
|
||||
|
||||
Copyleftclausule: wanneer de licentiehouder kopieën van het oorspronkelijke werk of bewerkingen verspreidt of mededeelt, geschiedt die verspreiding of mededeling onder de voorwaarden van deze licentie of van een latere versie van deze licentie, tenzij het oorspronkelijke werk uitdrukkelijk alleen onder deze versie van de licentie wordt verspreid — bijvoorbeeld door de mededeling „alleen EUPL v. 1.2”. De licentiehouder (die licentiegever wordt) kan met betrekking tot het werk of de bewerkingen geen aanvullende bepalingen of voorwaarden opleggen of stellen die de voorwaarden van de licentie wijzigen of beperken.
|
||||
|
||||
Verenigbaarheidsclausule: wanneer de licentiehouder bewerkingen of kopieën ervan verspreidt of mededeelt die zijn gebaseerd op het werk en op een ander werk dat uit hoofde van een verenigbare licentie in licentie is gegeven, kan die verspreiding of mededeling geschieden onder de voorwaarden van deze verenigbare licentie. Voor de toepassing van deze clausule wordt onder „verenigbare licentie” verstaan, de licenties die in het aanhangsel bij deze licentie zijn opgesomd. Indien de verplichtingen van de licentiehouder uit hoofde van de verenigbare licentie in strijd zijn met diens verplichtingen uit hoofde van deze licentie, hebben de verplichtingen van de verenigbare licentie voorrang.
|
||||
|
||||
Verstrekking van de broncode: bij de verspreiding of mededeling van kopieën van het werk verstrekt de licentiehouder een machinaal leesbare kopie van de broncode of geeft hij aan waar deze broncode gemakkelijk en vrij toegankelijk is, zolang de licentiehouder het werk blijft verspreiden of mededelen.
|
||||
|
||||
Juridische bescherming: deze licentie verleent geen toestemming om handelsnamen, handelsmerken, dienstmerken of namen van de licentiegever te gebruiken, behalve wanneer dit op grond van een redelijk en normaal gebruik noodzakelijk is om de oorsprong van het werk te beschrijven en de inhoud van de auteursrechtelijke kennisgeving te herhalen.
|
||||
|
||||
6.Auteursketen
|
||||
De oorspronkelijke licentiegever garandeert dat hij houder is van het hierbij verleende auteursrecht op het oorspronkelijke werk dan wel dat dit hem in licentie is gegeven en dat hij de bevoegdheid heeft de licentie te verlenen. Elke bewerker garandeert dat hij houder is van het auteursrecht op de door hem aan het werk aangebrachte wijzigingen dan wel dat dit hem in licentie is gegeven en dat hij de bevoegdheid heeft de licentie te verlenen. Telkens wanneer u de licentie aanvaardt, verlenen de oorspronkelijke licentiegever en de opeenvolgende bewerkers u een licentie op hun bijdragen aan het werk onder de voorwaarden van deze licentie.
|
||||
|
||||
7.Uitsluiting van garantie
|
||||
Het werk is een werk in ontwikkeling, dat voortdurend door vele bewerkers wordt verbeterd. Het is een onvoltooid werk, dat bijgevolg nog tekortkomingen of programmeerfouten („bugs”) kan vertonen, die onlosmakelijk verbonden zijn met dit soort ontwikkeling. Om die reden wordt het werk op grond van de licentie verstrekt „zoals het is” en zonder enige garantie met betrekking tot het werk te geven, met inbegrip van, maar niet beperkt tot garanties met betrekking tot de verhandelbaarheid, de geschiktheid voor een specifiek doel, de afwezigheid van tekortkomingen of fouten, de nauwkeurigheid, de eerbiediging van andere intellectuele-eigendomsrechten dan het in artikel 6 van deze licentie bedoelde auteursrecht. Deze uitsluiting van garantie is een essentieel onderdeel van de licentie en een voorwaarde voor de verlening van rechten op het werk.
|
||||
|
||||
8.Uitsluiting van aansprakelijkheid
|
||||
Behoudens in het geval van een opzettelijke fout of directe schade aan natuurlijke personen, is de licentiegever in geen enkel geval aansprakelijk voor ongeacht welke directe of indirecte, materiële of immateriële schade die voortvloeit uit de licentie of het gebruik van het werk, met inbegrip van, maar niet beperkt tot schade als gevolg van het verlies van goodwill, verloren werkuren, een computerdefect of computerfout, het verlies van gegevens, of enige andere commerciële schade, zelfs indien de licentiegever werd gewezen op de mogelijkheid van dergelijke schade. De licentiegever is echter aansprakelijk op grond van de wetgeving inzake productaansprakelijkheid, voor zover deze wetgeving op het werk van toepassing is.
|
||||
|
||||
9.Aanvullende overeenkomsten
|
||||
Bij de verspreiding van het werk kunt u ervoor kiezen een aanvullende overeenkomst te sluiten, waarin de verplichtingen of diensten overeenkomstig deze licentie worden omschreven. Indien deze verplichtingen worden aanvaard, kunt u echter alleen in eigen naam en onder eigen verantwoordelijkheid handelen, en dus niet in naam van de oorspronkelijke licentiegever of een bewerker, en kunt u voorts alleen handelen indien u ermee instemt alle bewerkers schadeloos te stellen, te verdedigen of te vrijwaren met betrekking tot de aansprakelijkheid van of vorderingen tegen deze bewerkers op grond van het feit dat u een garantie of aanvullende aansprakelijkheid hebt aanvaard.
|
||||
|
||||
10.Aanvaarding van de licentie
|
||||
De bepalingen van deze licentie kunnen worden aanvaard door te klikken op het pictogram „Ik ga akkoord”, dat zich bevindt onderaan het venster waarin de tekst van deze licentie is weergegeven, of door overeenkomstig de toepasselijke wetsbepalingen op een soortgelijke wijze met de licentie in te stemmen. Door op dat pictogram te klikken geeft u aan dat u deze licentie en alle voorwaarden ervan ondubbelzinnig en onherroepelijk aanvaardt. Evenzo aanvaardt u onherroepelijk deze licentie en alle voorwaarden ervan door uitoefening van de rechten die u in artikel 2 van deze licentie zijn verleend, zoals het gebruik van het werk, het creëren door u van een bewerking of de verspreiding of mededeling door u van het werk of kopieën ervan.
|
||||
|
||||
11.Voorlichting van het publiek
|
||||
Indien u het werk verspreidt of mededeelt door middel van elektronische communicatiemiddelen (bijvoorbeeld door voor te stellen het werk op afstand te downloaden), moet het distributiekanaal of het medium (bijvoorbeeld een website) het publiek ten minste de gegevens verschaffen die door het toepasselijke recht zijn voorgeschreven met betrekking tot de licentiegever, de licentie en de wijze waarop deze kan worden geraadpleegd, gesloten, opgeslagen en gereproduceerd door de licentiehouder.
|
||||
|
||||
12.Einde van de licentie
|
||||
De licentie en de uit hoofde daarvan verleende rechten eindigen automatisch bij elke inbreuk door de licentiehouder op de voorwaarden van de licentie. Dit einde beëindigt niet de licenties van personen die het werk van de licentiehouder krachtens de licentie hebben ontvangen, mits deze personen zich volledig aan de licentie houden.
|
||||
|
||||
13.Overige
|
||||
Onverminderd artikel 9 vormt de licentie de gehele overeenkomst tussen de partijen met betrekking tot het werk. Indien een bepaling van de licentie volgens het toepasselijke recht ongeldig is of niet uitvoerbaar is, doet dit geen afbreuk aan de geldigheid of uitvoerbaarheid van de licentie in haar geheel. Deze bepaling dient zodanig te worden uitgelegd of gewijzigd dat zij geldig en uitvoerbaar wordt. De Europese Commissie kan, voor zover dit noodzakelijk en redelijk is, versies in andere talen of nieuwe versies van deze licentie of geactualiseerde versies van dit aanhangsel publiceren, zonder de draagwijdte van de uit hoofde van de licentie verleende rechten te beperken. Nieuwe versies van de licentie zullen worden gepubliceerd met een uniek versienummer. Alle door de Europese Commissie goedgekeurde taalversies van deze licentie hebben dezelfde waarde. De partijen kunnen zich beroepen op de taalversie van hun keuze.
|
||||
|
||||
14.Bevoegd gerecht
|
||||
Onverminderd specifieke overeenkomsten tussen de partijen,
|
||||
— vallen alle geschillen tussen de instellingen, organen en instanties van de Europese Unie, als licentiegeefster, en een licentiehouder in verband met de uitlegging van deze licentie onder de bevoegdheid van het Hof van Justitie van de Europese Unie, conform artikel 272 van het Verdrag betreffende de werking van de Europese Unie,
|
||||
— vallen alle geschillen tussen andere partijen in verband met de uitlegging van deze licentie onder de uitsluitende bevoegdheid van het bevoegde gerecht van de plaats waar de licentiegever is gevestigd of zijn voornaamste activiteit uitoefent.
|
||||
|
||||
15.Toepasselijk recht
|
||||
Onverminderd specifieke overeenkomsten tussen de partijen,
|
||||
— wordt deze licentie beheerst door het recht van de lidstaat van de Europese Unie waar de licentiegever zijn statutaire zetel, verblijfplaats of hoofdkantoor heeft,
|
||||
— wordt deze licentie beheerst door het Belgische recht indien de licentiegever geen statutaire zetel, verblijfplaats of hoofdkantoor heeft in een lidstaat van de Europese Unie.
|
||||
|
||||
|
||||
Aanhangsel
|
||||
„Verenigbare licenties” in de zin van artikel 5 EUPL zijn:
|
||||
— GNU General Public License (GPL) v. 2, v. 3
|
||||
— GNU Affero General Public License (AGPL) v. 3
|
||||
— Open Software License (OSL) v. 2.1, v. 3.0
|
||||
— Eclipse Public License (EPL) v. 1.0
|
||||
— CeCILL v. 2.0, v. 2.1
|
||||
— Mozilla Public Licence (MPL) v. 2
|
||||
— GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
|
||||
— Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) voor andere werken dan software
|
||||
— European Union Public Licence (EUPL) v. 1.1, v. 1.2
|
||||
— Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) of Strong Reciprocity (LiLiQ-R+).
|
||||
De Europese Commissie kan dit aanhangsel actualiseren in geval van latere versies van de bovengenoemde licenties zonder dat er een nieuwe EUPL-versie wordt ontwikkeld, zolang die versies de uit hoofde van artikel 2 van deze licentie verleende rechten verlenen en ze de betrokken broncode beschermen tegen exclusieve toe-eigening.
|
||||
Voor alle andere wijzigingen van of aanvullingen op dit aanhangsel is de ontwikkeling van een nieuwe EUPL-versie vereist.
|
||||
@@ -59,15 +59,20 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Level",
|
||||
"name": "Quantity (oxygen)",
|
||||
"models": [
|
||||
{
|
||||
"name": "VegaLevel 10",
|
||||
"units": ["m", "ft", "mm"]
|
||||
},
|
||||
"name": "VegaOxySense 10",
|
||||
"units": ["g/m³", "mol/m³"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Quantity (TSS)",
|
||||
"models": [
|
||||
{
|
||||
"name": "VegaLevel 20",
|
||||
"units": ["m", "ft", "mm"]
|
||||
"name": "VegaSolidsProbe",
|
||||
"units": ["g/m³"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -202,6 +202,28 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "7",
|
||||
"name": "Vegabar 10",
|
||||
"product_model_subtype_id": "3",
|
||||
"product_model_description": null,
|
||||
"vendor_id": "4",
|
||||
"product_model_status": "Actief",
|
||||
"vendor_name": "vega",
|
||||
"product_subtype_name": "pressure",
|
||||
"product_model_meta": []
|
||||
},
|
||||
{
|
||||
"id": "8",
|
||||
"name": "VegaFlow 10",
|
||||
"product_model_subtype_id": "4",
|
||||
"product_model_description": null,
|
||||
"vendor_id": "4",
|
||||
"product_model_status": "Actief",
|
||||
"vendor_name": "vega",
|
||||
"product_subtype_name": "flow",
|
||||
"product_model_meta": []
|
||||
}
|
||||
]
|
||||
}
|
||||
6
index.js
6
index.js
@@ -12,11 +12,12 @@ const outputUtils = require('./src/helper/outputUtils.js');
|
||||
const logger = require('./src/helper/logger.js');
|
||||
const validation = require('./src/helper/validationUtils.js');
|
||||
const configUtils = require('./src/helper/configUtils.js');
|
||||
const assertions = require('./src/helper/assertionUtils.js')
|
||||
|
||||
// Domain-specific modules
|
||||
const { MeasurementContainer } = require('./src/measurements/index.js');
|
||||
const configManager = require('./src/configs/index.js');
|
||||
const nrmse = require('./src/nrmse/ErrorMetrics.js');
|
||||
const nrmse = require('./src/nrmse/errorMetrics.js');
|
||||
const state = require('./src/state/state.js');
|
||||
const convert = require('./src/convert/index.js');
|
||||
const MenuManager = require('./src/menu/index.js');
|
||||
@@ -34,6 +35,7 @@ module.exports = {
|
||||
configUtils,
|
||||
logger,
|
||||
validation,
|
||||
assertions,
|
||||
MeasurementContainer,
|
||||
nrmse,
|
||||
state,
|
||||
@@ -41,4 +43,4 @@ module.exports = {
|
||||
MenuManager,
|
||||
childRegistrationUtils,
|
||||
loadCurve
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"general": {
|
||||
"name": {
|
||||
"default": "Measurement Configuration",
|
||||
"default": "Sensor",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "A human-readable name or label for this measurement configuration."
|
||||
@@ -91,6 +91,13 @@
|
||||
],
|
||||
"description": "Defines the position of the measurement relative to its parent equipment or system."
|
||||
}
|
||||
},
|
||||
"distance":{
|
||||
"default": null,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"description": "Defines the position of the measurement relative to its parent equipment or system."
|
||||
}
|
||||
}
|
||||
},
|
||||
"asset": {
|
||||
|
||||
@@ -412,6 +412,14 @@
|
||||
],
|
||||
"description": "The frequency at which calculations are performed."
|
||||
}
|
||||
},
|
||||
"flowNumber": {
|
||||
"default": 1,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"nullable": false,
|
||||
"description": "Defines which effluent flow of the parent node to handle."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
371
src/configs/valveGroupControl.json
Normal file
371
src/configs/valveGroupControl.json
Normal file
@@ -0,0 +1,371 @@
|
||||
{
|
||||
"general": {
|
||||
"name": {
|
||||
"default": "ValveGroupControl",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "A human-readable name or label for this valveGroupControl configuration."
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"default": null,
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "A unique identifier for this configuration. If not provided, defaults to null."
|
||||
}
|
||||
},
|
||||
"unit": {
|
||||
"default": "unitless",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "The default measurement unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
"logging": {
|
||||
"logLevel": {
|
||||
"default": "info",
|
||||
"rules": {
|
||||
"type": "enum",
|
||||
"values": [
|
||||
{
|
||||
"value": "debug",
|
||||
"description": "Log messages are printed for debugging purposes."
|
||||
},
|
||||
{
|
||||
"value": "info",
|
||||
"description": "Informational messages are printed."
|
||||
},
|
||||
{
|
||||
"value": "warn",
|
||||
"description": "Warning messages are printed."
|
||||
},
|
||||
{
|
||||
"value": "error",
|
||||
"description": "Error messages are printed."
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"enabled": {
|
||||
"default": true,
|
||||
"rules": {
|
||||
"type": "boolean",
|
||||
"description": "Indicates whether logging is active. If true, log messages will be generated."
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"functionality": {
|
||||
"softwareType": {
|
||||
"default": "valveGroupControl",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "Specified software type for this configuration."
|
||||
}
|
||||
},
|
||||
"role": {
|
||||
"default": "ValveGroupController",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "Indicates the role this configuration plays within the system."
|
||||
}
|
||||
}
|
||||
},
|
||||
"asset": {
|
||||
"uuid": {
|
||||
"default": null,
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "A universally unique identifier for this asset. May be null if not assigned."
|
||||
}
|
||||
},
|
||||
"geoLocation": {
|
||||
"default": {},
|
||||
"rules": {
|
||||
"type": "object",
|
||||
"description": "An object representing the asset's physical coordinates or location.",
|
||||
"schema": {
|
||||
"x": {
|
||||
"default": 0,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"description": "X coordinate of the asset's location."
|
||||
}
|
||||
},
|
||||
"y": {
|
||||
"default": 0,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"description": "Y coordinate of the asset's location."
|
||||
}
|
||||
},
|
||||
"z": {
|
||||
"default": 0,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"description": "Z coordinate of the asset's location."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"supplier": {
|
||||
"default": "Unknown",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "The supplier or manufacturer of the asset."
|
||||
}
|
||||
},
|
||||
"type": {
|
||||
"default": "valve",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "A general classification of the asset tied to the specific software. This is not chosen from the asset dropdown menu."
|
||||
}
|
||||
},
|
||||
"subType": {
|
||||
"default": "Unknown",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "A more specific classification within 'type'. For example, 'centrifugal' for a centrifugal pump."
|
||||
}
|
||||
},
|
||||
"model": {
|
||||
"default": "Unknown",
|
||||
"rules": {
|
||||
"type": "string",
|
||||
"description": "A user-defined or manufacturer-defined model identifier for the asset."
|
||||
}
|
||||
},
|
||||
"accuracy": {
|
||||
"default": null,
|
||||
"rules": {
|
||||
"type": "number",
|
||||
"nullable": true,
|
||||
"description": "The accuracy of the valve or sensor, typically as a percentage or absolute value."
|
||||
}
|
||||
}
|
||||
},
|
||||
"mode": {
|
||||
"current": {
|
||||
"default": "auto",
|
||||
"rules": {
|
||||
"type": "enum",
|
||||
"values": [
|
||||
{
|
||||
"value": "auto",
|
||||
"description": "ValveGroupController accepts inputs from a parents and childs and runs autonomously."
|
||||
},
|
||||
{
|
||||
"value": "virtualControl",
|
||||
"description": "Controlled via GUI setpoints; ignores parent commands."
|
||||
},
|
||||
{
|
||||
"value": "fysicalControl",
|
||||
"description": "Controlled via physical buttons or switches; ignores external automated commands."
|
||||
},
|
||||
{
|
||||
"value": "maintenance",
|
||||
"description": "No active control from auto, virtual, or fysical sources."
|
||||
}
|
||||
],
|
||||
"description": "The operational mode of the valveGroupControl."
|
||||
}
|
||||
},
|
||||
"allowedActions":{
|
||||
"default":{},
|
||||
"rules": {
|
||||
"type": "object",
|
||||
"schema":{
|
||||
"auto": {
|
||||
"default": ["statusCheck", "execSequence", "emergencyStop", "valvePositionChange", "totalFlowChange", "valveDeltaPchange"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Actions allowed in auto mode."
|
||||
}
|
||||
},
|
||||
"virtualControl": {
|
||||
"default": ["statusCheck", "execSequence", "emergencyStop", "valvePositionChange", "totalFlowChange", "valveDeltaPchange"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Actions allowed in virtualControl mode."
|
||||
}
|
||||
},
|
||||
"fysicalControl": {
|
||||
"default": ["statusCheck", "emergencyStop"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Actions allowed in fysicalControl mode."
|
||||
}
|
||||
},
|
||||
"maintenance": {
|
||||
"default": ["statusCheck"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Actions allowed in maintenance mode."
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Information about valid command sources recognized by the valve."
|
||||
}
|
||||
},
|
||||
"allowedSources":{
|
||||
"default": {},
|
||||
"rules": {
|
||||
"type": "object",
|
||||
"schema":{
|
||||
"auto": {
|
||||
"default": ["parent", "GUI", "fysical"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sources allowed in auto mode."
|
||||
}
|
||||
},
|
||||
"virtualControl": {
|
||||
"default": ["GUI", "fysical"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sources allowed in virtualControl mode."
|
||||
}
|
||||
},
|
||||
"fysicalControl": {
|
||||
"default": ["fysical"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sources allowed in fysicalControl mode."
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Information about valid command sources recognized by the valveGroupControl."
|
||||
}
|
||||
}
|
||||
},
|
||||
"source": {
|
||||
"default": "parent",
|
||||
"rules": {
|
||||
"type": "enum",
|
||||
"values": [
|
||||
{
|
||||
"value": "parent",
|
||||
"description": "Commands are received from a parent controller."
|
||||
},
|
||||
{
|
||||
"value": "GUI",
|
||||
"description": "Commands are received from a graphical user interface."
|
||||
},
|
||||
{
|
||||
"value": "fysical",
|
||||
"description": "Commands are received from physical buttons or switches."
|
||||
}
|
||||
],
|
||||
"description": "Information about valid command sources recognized by the valveGroupControl."
|
||||
}
|
||||
},
|
||||
"action": {
|
||||
"default": "statusCheck",
|
||||
"rules": {
|
||||
"type": "enum",
|
||||
"values": [
|
||||
{
|
||||
"value": "statusCheck",
|
||||
"description": "Checks the valveGroupControl's state (mode, submode, operational status)."
|
||||
},
|
||||
{
|
||||
"value": "valvePositionChange",
|
||||
"description": "If child valve position change, the new flow for each child valve is determined"
|
||||
},
|
||||
{
|
||||
"value": "execSequence",
|
||||
"description": "Allows execution of sequences through auto or GUI controls."
|
||||
},
|
||||
{
|
||||
"value": "totalFlowChange",
|
||||
"description": "If total flow change, the new flow for each child valve is determined"
|
||||
},
|
||||
{
|
||||
"value": "valveDeltaPchange",
|
||||
"description": "If deltaP change, the deltaPmax is determined"
|
||||
},
|
||||
{
|
||||
"value": "emergencyStop",
|
||||
"description": "Overrides all commands and stops the valveGroupControl immediately (safety scenarios)."
|
||||
}
|
||||
],
|
||||
"description": "Defines the possible actions that can be performed on the valveGroupControl."
|
||||
}
|
||||
},
|
||||
"sequences":{
|
||||
"default":{},
|
||||
"rules": {
|
||||
"type": "object",
|
||||
"schema": {
|
||||
"startup": {
|
||||
"default": ["starting","warmingup","operational"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sequence of states for starting up the valve."
|
||||
}
|
||||
},
|
||||
"shutdown": {
|
||||
"default": ["stopping","coolingdown","idle"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sequence of states for shutting down the valveGroupControl."
|
||||
}
|
||||
},
|
||||
"emergencystop": {
|
||||
"default": ["emergencystop","off"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sequence of states for an emergency stop."
|
||||
}
|
||||
},
|
||||
"boot": {
|
||||
"default": ["idle","starting","warmingup","operational"],
|
||||
"rules": {
|
||||
"type": "set",
|
||||
"itemType": "string",
|
||||
"description": "Sequence of states for booting up the valveGroupControl."
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Predefined sequences of states for the valveGroupControl."
|
||||
|
||||
},
|
||||
"calculationMode": {
|
||||
"default": "medium",
|
||||
"rules": {
|
||||
"type": "enum",
|
||||
"values": [
|
||||
{
|
||||
"value": "low",
|
||||
"description": "Calculations run at fixed intervals (time-based)."
|
||||
},
|
||||
{
|
||||
"value": "medium",
|
||||
"description": "Calculations run when new setpoints arrive or measured changes occur (event-driven)."
|
||||
},
|
||||
{
|
||||
"value": "high",
|
||||
"description": "Calculations run on all event-driven info, including every movement."
|
||||
}
|
||||
],
|
||||
"description": "The frequency at which calculations are performed."
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,253 +1,98 @@
|
||||
// ChildRegistrationUtils.js
|
||||
class ChildRegistrationUtils {
|
||||
constructor(mainClass) {
|
||||
this.mainClass = mainClass; // Reference to the main class
|
||||
this.mainClass = mainClass;
|
||||
this.logger = mainClass.logger;
|
||||
this.registeredChildren = new Map();
|
||||
}
|
||||
|
||||
async registerChild(child, positionVsParent) {
|
||||
|
||||
this.logger.debug(`Registering child: ${child.id} with position=${positionVsParent}`);
|
||||
const { softwareType } = child.config.functionality;
|
||||
const { name, id, unit } = child.config.general;
|
||||
const { category = "", type = "" } = child.config.asset || {};
|
||||
console.log(`Registering child: ${name}, id: ${id}, softwareType: ${softwareType}, category: ${category}, type: ${type}, positionVsParent: ${positionVsParent}` );
|
||||
const emitter = child.emitter;
|
||||
const { name, id } = child.config.general;
|
||||
|
||||
//define position vs parent in child
|
||||
this.logger.debug(`Registering child: ${name} (${id}) as ${softwareType} at ${positionVsParent}`);
|
||||
|
||||
// Enhanced child setup - multiple parents
|
||||
if (Array.isArray(child.parent)) {
|
||||
child.parent.push(this.mainClass);
|
||||
} else {
|
||||
child.parent = [this.mainClass];
|
||||
}
|
||||
child.positionVsParent = positionVsParent;
|
||||
child.parent = this.mainClass;
|
||||
|
||||
if (!this.mainClass.child) this.mainClass.child = {};
|
||||
if (!this.mainClass.child[softwareType])
|
||||
this.mainClass.child[softwareType] = {};
|
||||
if (!this.mainClass.child[softwareType][category])
|
||||
this.mainClass.child[softwareType][category] = {};
|
||||
if (!this.mainClass.child[softwareType][category][type])
|
||||
this.mainClass.child[softwareType][category][type] = {};
|
||||
|
||||
// Use an array to handle multiple categories
|
||||
if (!Array.isArray(this.mainClass.child[softwareType][category][type])) {
|
||||
this.mainClass.child[softwareType][category][type] = [];
|
||||
// Enhanced measurement container with rich context
|
||||
if (child.measurements) {
|
||||
child.measurements.setChildId(id);
|
||||
child.measurements.setChildName(name);
|
||||
child.measurements.setParentRef(this.mainClass);
|
||||
}
|
||||
|
||||
// Store child in your expected structure
|
||||
this._storeChild(child, softwareType);
|
||||
|
||||
// Push the new child to the array of the mainclass so we can track the childs
|
||||
this.mainClass.child[softwareType][category][type].push({
|
||||
name,
|
||||
id,
|
||||
unit,
|
||||
emitter,
|
||||
});
|
||||
|
||||
//then connect the child depending on the type type etc..
|
||||
this.connectChild(
|
||||
id,
|
||||
softwareType,
|
||||
emitter,
|
||||
category,
|
||||
// Track registration for utilities
|
||||
this.registeredChildren.set(id, {
|
||||
child,
|
||||
type,
|
||||
positionVsParent
|
||||
);
|
||||
}
|
||||
|
||||
connectChild(
|
||||
id,
|
||||
softwareType,
|
||||
emitter,
|
||||
category,
|
||||
child,
|
||||
type,
|
||||
positionVsParent
|
||||
) {
|
||||
this.logger.debug(
|
||||
`Connecting child id=${id}: desc=${softwareType}, category=${category},type=${type}, position=${positionVsParent}`
|
||||
);
|
||||
|
||||
switch (softwareType) {
|
||||
case "measurement":
|
||||
this.logger.debug(
|
||||
`Registering measurement child: ${id} with category=${category}`
|
||||
);
|
||||
this.connectMeasurement(child, type, positionVsParent);
|
||||
break;
|
||||
|
||||
case "machine":
|
||||
this.logger.debug(`Registering complete machine child: ${id}`);
|
||||
this.connectMachine(child);
|
||||
break;
|
||||
|
||||
case "valve":
|
||||
this.logger.debug(`Registering complete valve child: ${id}`);
|
||||
this.connectValve(child);
|
||||
break;
|
||||
|
||||
case "machineGroup":
|
||||
this.logger.debug(`Registering complete machineGroup child: ${id}`);
|
||||
this.connectMachineGroup(child);
|
||||
break;
|
||||
|
||||
case "actuator":
|
||||
this.logger.debug(`Registering linear actuator child: ${id}`);
|
||||
this.connectActuator(child,positionVsParent);
|
||||
break;
|
||||
|
||||
default:
|
||||
this.logger.error(`Child registration unrecognized desc: ${desc}`);
|
||||
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
||||
}
|
||||
}
|
||||
|
||||
connectMeasurement(child, type, position) {
|
||||
this.logger.debug(
|
||||
`Connecting measurement child: ${type} with position=${position}`
|
||||
);
|
||||
|
||||
// Check if type is valid
|
||||
if (!type) {
|
||||
this.logger.error(`Invalid type for measurement: ${type}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize the measurement to a number - logging each step for debugging
|
||||
try {
|
||||
this.logger.debug(
|
||||
`Initializing measurement: ${type}, position: ${position} value: 0`
|
||||
);
|
||||
const typeResult = this.mainClass.measurements.type(type);
|
||||
const variantResult = typeResult.variant("measured");
|
||||
const positionResult = variantResult.position(position);
|
||||
positionResult.value(0);
|
||||
|
||||
this.logger.debug(
|
||||
`Subscribing on mAbs event for measurement: ${type}, position: ${position}`
|
||||
);
|
||||
// Listen for the mAbs event and update the measurement
|
||||
|
||||
this.logger.debug(
|
||||
`Successfully initialized measurement: ${type}, position: ${position}`
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to initialize measurement: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
child.emitter.on("mAbs", (value) => {
|
||||
// Use the same method chaining approach that worked during initialization
|
||||
this.mainClass.measurements
|
||||
.type(type)
|
||||
.variant("measured")
|
||||
.position(position)
|
||||
.value(value);
|
||||
this.mainClass.updateMeasurement("measured", type, value, position);
|
||||
//this.logger.debug(`--------->>>>>>>>>Updated measurement: ${type}, value: ${value}, position: ${position}`);
|
||||
softwareType,
|
||||
position: positionVsParent,
|
||||
registeredAt: Date.now()
|
||||
});
|
||||
|
||||
// IMPORTANT: Only call parent registration - no automatic handling and if parent has this function then try to register this child
|
||||
if (typeof this.mainClass.registerChild === 'function') {
|
||||
this.mainClass.registerChild(child, softwareType);
|
||||
}
|
||||
|
||||
this.logger.info(`✅ Child ${name} registered successfully`);
|
||||
}
|
||||
|
||||
connectMachine(machine) {
|
||||
if (!machine) {
|
||||
this.logger.error("Invalid machine provided.");
|
||||
return;
|
||||
_storeChild(child, softwareType) {
|
||||
// Maintain your existing structure
|
||||
if (!this.mainClass.child) this.mainClass.child = {};
|
||||
if (!this.mainClass.child[softwareType]) this.mainClass.child[softwareType] = {};
|
||||
|
||||
const { category = "sensor" } = child.config.asset || {};
|
||||
if (!this.mainClass.child[softwareType][category]) {
|
||||
this.mainClass.child[softwareType][category] = [];
|
||||
}
|
||||
|
||||
const machineId = Object.keys(this.mainClass.machines).length + 1;
|
||||
this.mainClass.machines[machineId] = machine;
|
||||
|
||||
this.logger.info(
|
||||
`Setting up pressureChange listener for machine ${machineId}`
|
||||
);
|
||||
|
||||
machine.emitter.on("pressureChange", () =>
|
||||
this.mainClass.handlePressureChange(machine)
|
||||
);
|
||||
|
||||
//update of child triggers the handler
|
||||
this.mainClass.handleChildChange();
|
||||
|
||||
this.logger.info(`Machine ${machineId} registered successfully.`);
|
||||
|
||||
this.mainClass.child[softwareType][category].push(child);
|
||||
}
|
||||
|
||||
connectValve(valve) {
|
||||
if (!valve) {
|
||||
this.logger.warn("Invalid valve provided.");
|
||||
return;
|
||||
// NEW: Utility methods for parent to use
|
||||
getChildrenOfType(softwareType, category = null) {
|
||||
if (!this.mainClass.child[softwareType]) return [];
|
||||
|
||||
if (category) {
|
||||
return this.mainClass.child[softwareType][category] || [];
|
||||
}
|
||||
const valveId = Object.keys(this.mainClass.valves).length + 1;
|
||||
this.mainClass.valves[valveId] = valve; // Gooit valve object in de valves attribute met valve objects
|
||||
|
||||
valve.state.emitter.on("positionChange", (data) => {
|
||||
//ValveGroupController abboneren op klepstand verandering
|
||||
this.mainClass.logger.debug(`Position change of valve detected: ${data}`);
|
||||
this.mainClass.calcValveFlows();
|
||||
}); //bepaal nieuwe flow per valve
|
||||
valve.emitter.on("deltaPChange", () => {
|
||||
this.mainClass.logger.debug("DeltaP change of valve detected");
|
||||
this.mainClass.calcMaxDeltaP();
|
||||
}); //bepaal nieuwe max deltaP
|
||||
|
||||
this.logger.info(`Valve ${valveId} registered successfully.`);
|
||||
|
||||
// Return all children of this software type
|
||||
return Object.values(this.mainClass.child[softwareType]).flat();
|
||||
}
|
||||
|
||||
connectMachineGroup(machineGroup) {
|
||||
if (!machineGroup) {
|
||||
this.logger.warn("Invalid machineGroup provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const machineGroupId = Object.keys(this.mainClass.machineGroups).length + 1;
|
||||
this.mainClass.machineGroups[machineGroupId] = machineGroup;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Skip machinegroup connnection: ${error.message}`);
|
||||
}
|
||||
|
||||
machineGroup.emitter.on("totalFlowChange", (data) => {
|
||||
this.mainClass.logger.debug('Total flow change of machineGroup detected');
|
||||
this.mainClass.handleInput("parent", "totalFlowChange", data)}); //Geef nieuwe totale flow door aan valveGrouControl
|
||||
|
||||
this.logger.info(`MachineGroup ${machineGroup.config.general.name} registered successfully.`);
|
||||
}
|
||||
|
||||
connectActuator(actuator, positionVsParent) {
|
||||
if (!actuator) {
|
||||
this.logger.warn("Invalid actuator provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
//Special case gateGroupControl
|
||||
if (
|
||||
this.mainClass.config.functionality.softwareType == "gateGroupControl"
|
||||
) {
|
||||
if (Object.keys(this.mainClass.actuators).length < 2) {
|
||||
if (positionVsParent == "downstream") {
|
||||
this.mainClass.actuators[0] = actuator;
|
||||
}
|
||||
|
||||
if (positionVsParent == "upstream") {
|
||||
this.mainClass.actuators[1] = actuator;
|
||||
}
|
||||
//define emitters
|
||||
actuator.state.emitter.on("positionChange", (data) => {
|
||||
this.mainClass.logger.debug(`Position change of actuator detected: ${data}`);
|
||||
this.mainClass.eventUpdate();
|
||||
});
|
||||
|
||||
//define emitters
|
||||
actuator.state.emitter.on("stateChange", (data) => {
|
||||
this.mainClass.logger.debug(`State change of actuator detected: ${data}`);
|
||||
this.mainClass.eventUpdate();
|
||||
});
|
||||
|
||||
} else {
|
||||
this.logger.error(
|
||||
"Too many actuators registered. Only two are allowed."
|
||||
);
|
||||
}
|
||||
}
|
||||
getChildById(childId) {
|
||||
return this.registeredChildren.get(childId)?.child || null;
|
||||
}
|
||||
|
||||
//wanneer hij deze ontvangt is deltaP van een van de valves veranderd (kan ook zijn niet child zijn, maar dat maakt niet uit)
|
||||
getAllChildren() {
|
||||
return Array.from(this.registeredChildren.values()).map(r => r.child);
|
||||
}
|
||||
|
||||
// NEW: Debugging utilities
|
||||
logChildStructure() {
|
||||
this.logger.debug('Current child structure:', JSON.stringify(
|
||||
Object.keys(this.mainClass.child).reduce((acc, softwareType) => {
|
||||
acc[softwareType] = Object.keys(this.mainClass.child[softwareType]).reduce((catAcc, category) => {
|
||||
catAcc[category] = this.mainClass.child[softwareType][category].map(c => ({
|
||||
id: c.config.general.id,
|
||||
name: c.config.general.name
|
||||
}));
|
||||
return catAcc;
|
||||
}, {});
|
||||
return acc;
|
||||
}, {}), null, 2
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChildRegistrationUtils;
|
||||
module.exports = ChildRegistrationUtils;
|
||||
260
src/helper/childRegistrationUtils_DEPRECATED.js
Normal file
260
src/helper/childRegistrationUtils_DEPRECATED.js
Normal file
@@ -0,0 +1,260 @@
|
||||
// ChildRegistrationUtils.js
|
||||
class ChildRegistrationUtils {
|
||||
constructor(mainClass) {
|
||||
this.mainClass = mainClass; // Reference to the main class
|
||||
this.logger = mainClass.logger;
|
||||
}
|
||||
|
||||
async registerChild(child, positionVsParent) {
|
||||
|
||||
this.logger.debug(`Registering child: ${child.id} with position=${positionVsParent}`);
|
||||
const { softwareType } = child.config.functionality;
|
||||
const { name, id, unit } = child.config.general;
|
||||
const { category = "", type = "" } = child.config.asset || {};
|
||||
console.log(`Registering child: ${name}, id: ${id}, softwareType: ${softwareType}, category: ${category}, type: ${type}, positionVsParent: ${positionVsParent}` );
|
||||
const emitter = child.emitter;
|
||||
|
||||
//define position vs parent in child
|
||||
child.positionVsParent = positionVsParent;
|
||||
child.parent = this.mainClass;
|
||||
|
||||
if (!this.mainClass.child) this.mainClass.child = {};
|
||||
if (!this.mainClass.child[softwareType])
|
||||
this.mainClass.child[softwareType] = {};
|
||||
if (!this.mainClass.child[softwareType][category])
|
||||
this.mainClass.child[softwareType][category] = {};
|
||||
if (!this.mainClass.child[softwareType][category][type])
|
||||
this.mainClass.child[softwareType][category][type] = {};
|
||||
|
||||
// Use an array to handle multiple categories
|
||||
if (!Array.isArray(this.mainClass.child[softwareType][category][type])) {
|
||||
this.mainClass.child[softwareType][category][type] = [];
|
||||
}
|
||||
|
||||
// Push the new child to the array of the mainclass so we can track the childs
|
||||
this.mainClass.child[softwareType][category][type].push({
|
||||
name,
|
||||
id,
|
||||
unit,
|
||||
emitter,
|
||||
});
|
||||
|
||||
//then connect the child depending on the type type etc..
|
||||
this.connectChild(
|
||||
id,
|
||||
softwareType,
|
||||
emitter,
|
||||
category,
|
||||
child,
|
||||
type,
|
||||
positionVsParent
|
||||
);
|
||||
}
|
||||
|
||||
connectChild(
|
||||
id,
|
||||
softwareType,
|
||||
emitter,
|
||||
category,
|
||||
child,
|
||||
type,
|
||||
positionVsParent
|
||||
) {
|
||||
this.logger.debug(
|
||||
`Connecting child id=${id}: desc=${softwareType}, category=${category},type=${type}, position=${positionVsParent}`
|
||||
);
|
||||
|
||||
switch (softwareType) {
|
||||
case "measurement":
|
||||
this.logger.debug(
|
||||
`Registering measurement child: ${id} with category=${category}`
|
||||
);
|
||||
this.connectMeasurement(child, type, positionVsParent);
|
||||
break;
|
||||
|
||||
case "machine":
|
||||
this.logger.debug(`Registering complete machine child: ${id}`);
|
||||
this.connectMachine(child);
|
||||
break;
|
||||
|
||||
case "valve":
|
||||
this.logger.debug(`Registering complete valve child: ${id}`);
|
||||
this.connectValve(child);
|
||||
break;
|
||||
|
||||
case "machineGroup":
|
||||
this.logger.debug(`Registering complete machineGroup child: ${id}`);
|
||||
this.connectMachineGroup(child);
|
||||
break;
|
||||
|
||||
case "actuator":
|
||||
this.logger.debug(`Registering linear actuator child: ${id}`);
|
||||
this.connectActuator(child,positionVsParent);
|
||||
break;
|
||||
|
||||
default:
|
||||
this.logger.error(`Child registration unrecognized desc: ${desc}`);
|
||||
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
||||
}
|
||||
}
|
||||
|
||||
connectMeasurement(child, type, position) {
|
||||
this.logger.debug(
|
||||
`Connecting measurement child: ${type} with position=${position}`
|
||||
);
|
||||
|
||||
// Check if type is valid
|
||||
if (!type) {
|
||||
this.logger.error(`Invalid type for measurement: ${type}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize the measurement to a number - logging each step for debugging
|
||||
try {
|
||||
this.logger.debug(
|
||||
`Initializing measurement: ${type}, position: ${position} value: 0`
|
||||
);
|
||||
const typeResult = this.mainClass.measurements.type(type);
|
||||
const variantResult = typeResult.variant("measured");
|
||||
const positionResult = variantResult.position(position);
|
||||
positionResult.value(0);
|
||||
|
||||
this.logger.debug(
|
||||
`Subscribing on mAbs event for measurement: ${type}, position: ${position}`
|
||||
);
|
||||
// Listen for the mAbs event and update the measurement
|
||||
|
||||
this.logger.debug(
|
||||
`Successfully initialized measurement: ${type}, position: ${position}`
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to initialize measurement: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
//testing new emitter strategy
|
||||
child.measurements.emitter.on("newValue", (data) => {
|
||||
this.logger.warn(
|
||||
`Value change event received for measurement: ${type}, position: ${position}, value: ${data.value}`
|
||||
);
|
||||
});
|
||||
|
||||
child.emitter.on("mAbs", (value) => {
|
||||
// Use the same method chaining approach that worked during initialization
|
||||
this.mainClass.measurements
|
||||
.type(type)
|
||||
.variant("measured")
|
||||
.position(position)
|
||||
.value(value);
|
||||
this.mainClass.updateMeasurement("measured", type, value, position);
|
||||
//this.logger.debug(`--------->>>>>>>>>Updated measurement: ${type}, value: ${value}, position: ${position}`);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
connectMachine(machine) {
|
||||
if (!machine) {
|
||||
this.logger.error("Invalid machine provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
const machineId = Object.keys(this.mainClass.machines).length + 1;
|
||||
this.mainClass.machines[machineId] = machine;
|
||||
|
||||
this.logger.info(
|
||||
`Setting up pressureChange listener for machine ${machineId}`
|
||||
);
|
||||
|
||||
machine.emitter.on("pressureChange", () =>
|
||||
this.mainClass.handlePressureChange(machine)
|
||||
);
|
||||
|
||||
//update of child triggers the handler
|
||||
this.mainClass.handleChildChange();
|
||||
|
||||
this.logger.info(`Machine ${machineId} registered successfully.`);
|
||||
}
|
||||
|
||||
connectValve(valve) {
|
||||
if (!valve) {
|
||||
this.logger.warn("Invalid valve provided.");
|
||||
return;
|
||||
}
|
||||
const valveId = Object.keys(this.mainClass.valves).length + 1;
|
||||
this.mainClass.valves[valveId] = valve; // Gooit valve object in de valves attribute met valve objects
|
||||
|
||||
valve.state.emitter.on("positionChange", (data) => {
|
||||
//ValveGroupController abboneren op klepstand verandering
|
||||
this.mainClass.logger.debug(`Position change of valve detected: ${data}`);
|
||||
this.mainClass.calcValveFlows();
|
||||
}); //bepaal nieuwe flow per valve
|
||||
valve.emitter.on("deltaPChange", () => {
|
||||
this.mainClass.logger.debug("DeltaP change of valve detected");
|
||||
this.mainClass.calcMaxDeltaP();
|
||||
}); //bepaal nieuwe max deltaP
|
||||
|
||||
this.logger.info(`Valve ${valveId} registered successfully.`);
|
||||
}
|
||||
|
||||
connectMachineGroup(machineGroup) {
|
||||
if (!machineGroup) {
|
||||
this.logger.warn("Invalid machineGroup provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const machineGroupId = Object.keys(this.mainClass.machineGroups).length + 1;
|
||||
this.mainClass.machineGroups[machineGroupId] = machineGroup;
|
||||
} catch (error) {
|
||||
this.logger.warn(`Skip machinegroup connnection: ${error.message}`);
|
||||
}
|
||||
|
||||
machineGroup.emitter.on("totalFlowChange", (data) => {
|
||||
this.mainClass.logger.debug('Total flow change of machineGroup detected');
|
||||
this.mainClass.handleInput("parent", "totalFlowChange", data)}); //Geef nieuwe totale flow door aan valveGrouControl
|
||||
|
||||
this.logger.info(`MachineGroup ${machineGroup.config.general.name} registered successfully.`);
|
||||
}
|
||||
|
||||
connectActuator(actuator, positionVsParent) {
|
||||
if (!actuator) {
|
||||
this.logger.warn("Invalid actuator provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
//Special case gateGroupControl
|
||||
if (
|
||||
this.mainClass.config.functionality.softwareType == "gateGroupControl"
|
||||
) {
|
||||
if (Object.keys(this.mainClass.actuators).length < 2) {
|
||||
if (positionVsParent == "downstream") {
|
||||
this.mainClass.actuators[0] = actuator;
|
||||
}
|
||||
|
||||
if (positionVsParent == "upstream") {
|
||||
this.mainClass.actuators[1] = actuator;
|
||||
}
|
||||
//define emitters
|
||||
actuator.state.emitter.on("positionChange", (data) => {
|
||||
this.mainClass.logger.debug(`Position change of actuator detected: ${data}`);
|
||||
this.mainClass.eventUpdate();
|
||||
});
|
||||
|
||||
//define emitters
|
||||
actuator.state.emitter.on("stateChange", (data) => {
|
||||
this.mainClass.logger.debug(`State change of actuator detected: ${data}`);
|
||||
this.mainClass.eventUpdate();
|
||||
});
|
||||
|
||||
} else {
|
||||
this.logger.error(
|
||||
"Too many actuators registered. Only two are allowed."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//wanneer hij deze ontvangt is deltaP van een van de valves veranderd (kan ook zijn niet child zijn, maar dat maakt niet uit)
|
||||
}
|
||||
|
||||
module.exports = ChildRegistrationUtils;
|
||||
@@ -131,7 +131,6 @@ class Measurement {
|
||||
const downIndex = downValues.timestamps.indexOf(upTimestamp);
|
||||
|
||||
if (downIndex !== -1) {
|
||||
|
||||
const diff = upValues.values[i] - downValues.values[downIndex];
|
||||
diffMeasurement.setValue(diff, upTimestamp);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ class MeasurementBuilder {
|
||||
this.position,
|
||||
this.windowSize
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
const MeasurementBuilder = require('./MeasurementBuilder');
|
||||
const EventEmitter = require('events');
|
||||
const convertModule = require('../convert/index');
|
||||
|
||||
class MeasurementContainer {
|
||||
constructor(options = {}, logger) {
|
||||
this.logger = logger;
|
||||
constructor(options = {}) {
|
||||
this.emitter = new EventEmitter();
|
||||
this.measurements = {};
|
||||
this.windowSize = options.windowSize || 10; // Default window size
|
||||
|
||||
@@ -10,6 +12,63 @@ class MeasurementContainer {
|
||||
this._currentType = null;
|
||||
this._currentVariant = null;
|
||||
this._currentPosition = null;
|
||||
this._unit = null;
|
||||
|
||||
// Default units for each measurement type
|
||||
this.defaultUnits = {
|
||||
pressure: 'mbar',
|
||||
flow: 'm3/h',
|
||||
power: 'kW',
|
||||
temperature: 'C',
|
||||
volume: 'm3',
|
||||
length: 'm',
|
||||
...options.defaultUnits // Allow override
|
||||
};
|
||||
|
||||
// Auto-conversion settings
|
||||
this.autoConvert = options.autoConvert !== false; // Default to true
|
||||
this.preferredUnits = options.preferredUnits || {}; // Per-measurement overrides
|
||||
|
||||
// For chaining context
|
||||
this._currentType = null;
|
||||
this._currentVariant = null;
|
||||
this._currentPosition = null;
|
||||
this._unit = null;
|
||||
|
||||
// NEW: Enhanced child identification
|
||||
this.childId = null;
|
||||
this.childName = null;
|
||||
this.parentRef = null;
|
||||
|
||||
}
|
||||
|
||||
// NEW: Methods to set child context
|
||||
setChildId(childId) {
|
||||
this.childId = childId;
|
||||
return this;
|
||||
}
|
||||
|
||||
setChildName(childName) {
|
||||
this.childName = childName;
|
||||
return this;
|
||||
}
|
||||
|
||||
setParentRef(parent) {
|
||||
this.parentRef = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
// New method to set preferred units
|
||||
setPreferredUnit(measurementType, unit) {
|
||||
this.preferredUnits[measurementType] = unit;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Get the target unit for a measurement type
|
||||
_getTargetUnit(measurementType) {
|
||||
return this.preferredUnits[measurementType] ||
|
||||
this.defaultUnits[measurementType] ||
|
||||
null;
|
||||
}
|
||||
|
||||
// Chainable methods
|
||||
@@ -29,28 +88,85 @@ class MeasurementContainer {
|
||||
return this;
|
||||
}
|
||||
|
||||
position(positionName) {
|
||||
position(positionValue) {
|
||||
if (!this._currentVariant) {
|
||||
throw new Error('Variant must be specified before position');
|
||||
}
|
||||
this._currentPosition = positionName;
|
||||
|
||||
// Turn string positions into numeric values
|
||||
if (typeof positionValue == "string") {
|
||||
positionValue = this._convertPositionStr2Num(positionValue);
|
||||
}
|
||||
|
||||
this._currentPosition = positionValue;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// Core methods that complete the chain
|
||||
value(val, timestamp = Date.now()) {
|
||||
if (!this._ensureChainIsValid()) return this;
|
||||
|
||||
const measurement = this._getOrCreateMeasurement();
|
||||
measurement.setValue(val, timestamp);
|
||||
return this;
|
||||
// ENHANCED: Update your existing value method
|
||||
value(val, timestamp = Date.now(), sourceUnit = null) {
|
||||
if (!this._ensureChainIsValid()) return this;
|
||||
|
||||
const measurement = this._getOrCreateMeasurement();
|
||||
const targetUnit = this._getTargetUnit(this._currentType);
|
||||
|
||||
let convertedValue = val;
|
||||
let finalUnit = sourceUnit || targetUnit;
|
||||
|
||||
// Auto-convert if enabled and units are specified
|
||||
if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) {
|
||||
try {
|
||||
convertedValue = convertModule(val).from(sourceUnit).to(targetUnit);
|
||||
finalUnit = targetUnit;
|
||||
|
||||
if (this.logger) {
|
||||
this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`);
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.warn(`Auto-conversion failed from ${sourceUnit} to ${targetUnit}: ${error.message}`);
|
||||
}
|
||||
convertedValue = val;
|
||||
finalUnit = sourceUnit;
|
||||
}
|
||||
}
|
||||
|
||||
measurement.setValue(convertedValue, timestamp);
|
||||
|
||||
if (finalUnit && !measurement.unit) {
|
||||
measurement.setUnit(finalUnit);
|
||||
}
|
||||
|
||||
// ENHANCED: Emit event with rich context
|
||||
const eventData = {
|
||||
value: convertedValue,
|
||||
originalValue: val,
|
||||
unit: finalUnit,
|
||||
sourceUnit: sourceUnit,
|
||||
timestamp,
|
||||
position: this._currentPosition,
|
||||
variant: this._currentVariant,
|
||||
type: this._currentType,
|
||||
// NEW: Enhanced context
|
||||
childId: this.childId,
|
||||
childName: this.childName,
|
||||
parentRef: this.parentRef
|
||||
};
|
||||
|
||||
// Emit the exact event your parent expects
|
||||
this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
|
||||
//console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
unit(unitName) {
|
||||
if (!this._ensureChainIsValid()) return this;
|
||||
|
||||
const measurement = this._getOrCreateMeasurement();
|
||||
measurement.setUnit(unitName);
|
||||
this._unit = unitName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -60,14 +176,52 @@ class MeasurementContainer {
|
||||
return this._getOrCreateMeasurement();
|
||||
}
|
||||
|
||||
getCurrentValue() {
|
||||
getCurrentValue(requestedUnit = null) {
|
||||
const measurement = this.get();
|
||||
return measurement ? measurement.getCurrentValue() : null;
|
||||
if (!measurement) return null;
|
||||
|
||||
const value = measurement.getCurrentValue();
|
||||
if (value === null) return null;
|
||||
|
||||
// Return as-is if no unit conversion requested
|
||||
if (!requestedUnit) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Convert if needed
|
||||
if (measurement.unit && requestedUnit !== measurement.unit) {
|
||||
try {
|
||||
return convertModule(value).from(measurement.unit).to(requestedUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
return value; // Return original value if conversion fails
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
getAverage() {
|
||||
getAverage(requestedUnit = null) {
|
||||
const measurement = this.get();
|
||||
return measurement ? measurement.getAverage() : null;
|
||||
if (!measurement) return null;
|
||||
|
||||
const avgValue = measurement.getAverage();
|
||||
if (avgValue === null) return null;
|
||||
|
||||
if (!requestedUnit || !measurement.unit || requestedUnit === measurement.unit) {
|
||||
return avgValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return convertModule(avgValue).from(measurement.unit).to(requestedUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
return avgValue;
|
||||
}
|
||||
}
|
||||
|
||||
getMin() {
|
||||
@@ -85,47 +239,45 @@ class MeasurementContainer {
|
||||
return measurement ? measurement.getAllValues() : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Difference calculations between positions
|
||||
difference() {
|
||||
difference(requestedUnit = null) {
|
||||
if (!this._currentType || !this._currentVariant) {
|
||||
throw new Error('Type and variant must be specified for difference calculation');
|
||||
}
|
||||
|
||||
// Save position to restore chain state after operation
|
||||
const savedPosition = this._currentPosition;
|
||||
|
||||
// Get upstream measurement
|
||||
this._currentPosition = 'upstream';
|
||||
// Get upstream and downstream measurements
|
||||
const positions = this.getPositions();
|
||||
|
||||
this._currentPosition = Math.min(...positions);
|
||||
const upstream = this.get();
|
||||
|
||||
// Get downstream measurement
|
||||
this._currentPosition = 'downstream';
|
||||
this._currentPosition = Math.max(...positions);
|
||||
const downstream = this.get();
|
||||
|
||||
// Restore chain state
|
||||
this._currentPosition = savedPosition;
|
||||
|
||||
if (!upstream || !downstream || upstream.values.length === 0 || downstream.values.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Ensure units match
|
||||
let downstreamForCalc = downstream;
|
||||
if (upstream.unit && downstream.unit && upstream.unit !== downstream.unit) {
|
||||
try {
|
||||
downstreamForCalc = downstream.convertTo(upstream.unit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`Unit conversion failed: ${error.message}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// Get target unit for conversion
|
||||
const targetUnit = requestedUnit || upstream.unit || downstream.unit;
|
||||
|
||||
// Get values in the same unit
|
||||
const upstreamValue = this._convertValueToUnit(upstream.getCurrentValue(), upstream.unit, targetUnit);
|
||||
const downstreamValue = this._convertValueToUnit(downstream.getCurrentValue(), downstream.unit, targetUnit);
|
||||
|
||||
const upstreamAvg = this._convertValueToUnit(upstream.getAverage(), upstream.unit, targetUnit);
|
||||
const downstreamAvg = this._convertValueToUnit(downstream.getAverage(), downstream.unit, targetUnit);
|
||||
|
||||
return {
|
||||
value: downstreamForCalc.getCurrentValue() - upstream.getCurrentValue() ,
|
||||
avgDiff: downstreamForCalc.getAverage() - upstream.getAverage() ,
|
||||
unit: upstream.unit
|
||||
value: downstreamValue - upstreamValue,
|
||||
avgDiff: downstreamAvg - upstreamAvg,
|
||||
unit: targetUnit
|
||||
};
|
||||
}
|
||||
|
||||
@@ -176,7 +328,7 @@ class MeasurementContainer {
|
||||
Object.keys(this.measurements[this._currentType]) : [];
|
||||
}
|
||||
|
||||
getPositions() {
|
||||
getPositions(asNumber = false) {
|
||||
if (!this._currentType || !this._currentVariant) {
|
||||
throw new Error('Type and variant must be specified before listing positions');
|
||||
}
|
||||
@@ -185,8 +337,12 @@ class MeasurementContainer {
|
||||
!this.measurements[this._currentType][this._currentVariant]) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Object.keys(this.measurements[this._currentType][this._currentVariant]);
|
||||
|
||||
if (asNumber) {
|
||||
return Object.keys(this.measurements[this._currentType][this._currentVariant]);
|
||||
}
|
||||
|
||||
return Object.keys(this.measurements[this._currentType][this._currentVariant]).map(this._convertPositionNum2Str);
|
||||
}
|
||||
|
||||
clear() {
|
||||
@@ -195,6 +351,105 @@ class MeasurementContainer {
|
||||
this._currentVariant = null;
|
||||
this._currentPosition = null;
|
||||
}
|
||||
|
||||
// Helper method for value conversion
|
||||
_convertValueToUnit(value, fromUnit, toUnit) {
|
||||
if (!value || !fromUnit || !toUnit || fromUnit === toUnit) {
|
||||
return value;
|
||||
}
|
||||
|
||||
try {
|
||||
return convertModule(value).from(fromUnit).to(toUnit);
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.warn(`Conversion failed from ${fromUnit} to ${toUnit}: ${error.message}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// Get available units for a measurement type
|
||||
getAvailableUnits(measurementType = null) {
|
||||
const type = measurementType || this._currentType;
|
||||
if (!type) return [];
|
||||
|
||||
// Map measurement types to convert module measures
|
||||
const measureMap = {
|
||||
pressure: 'pressure',
|
||||
flow: 'volumeFlowRate',
|
||||
power: 'power',
|
||||
temperature: 'temperature',
|
||||
volume: 'volume',
|
||||
length: 'length',
|
||||
mass: 'mass',
|
||||
energy: 'energy'
|
||||
};
|
||||
|
||||
const convertMeasure = measureMap[type];
|
||||
if (!convertMeasure) return [];
|
||||
|
||||
try {
|
||||
return convertModule().possibilities(convertMeasure);
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Get best unit for current value
|
||||
getBestUnit(excludeUnits = []) {
|
||||
const measurement = this.get();
|
||||
if (!measurement || !measurement.unit) return null;
|
||||
|
||||
const currentValue = measurement.getCurrentValue();
|
||||
if (currentValue === null) return null;
|
||||
|
||||
try {
|
||||
const best = convertModule(currentValue)
|
||||
.from(measurement.unit)
|
||||
.toBest({ exclude: excludeUnits });
|
||||
|
||||
return best;
|
||||
} catch (error) {
|
||||
if (this.logger) {
|
||||
this.logger.error(`getBestUnit failed: ${error.message}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
_convertPositionStr2Num(positionString) {
|
||||
switch(positionString) {
|
||||
case "atEquipment":
|
||||
return 0;
|
||||
case "upstream":
|
||||
return Number.POSITIVE_INFINITY;
|
||||
case "downstream":
|
||||
return Number.NEGATIVE_INFINITY;
|
||||
|
||||
default:
|
||||
if (this.logger) {
|
||||
this.logger.error(`Invalid positionVsParent provided: ${positionString}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_convertPositionNum2Str(positionValue) {
|
||||
if (positionValue === 0) {
|
||||
return "atEquipment";
|
||||
}
|
||||
if (positionValue < 0) {
|
||||
return "upstream";
|
||||
}
|
||||
if (positionValue > 0) {
|
||||
return "downstream";
|
||||
}
|
||||
|
||||
if (this.logger) {
|
||||
this.logger.error(`Invalid position provided: ${positionValue}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = MeasurementContainer;
|
||||
|
||||
@@ -1,58 +1,255 @@
|
||||
const { MeasurementContainer } = require('./index');
|
||||
|
||||
// Create a container
|
||||
const container = new MeasurementContainer({ windowSize: 20 });
|
||||
console.log('=== MEASUREMENT CONTAINER EXAMPLES ===\n');
|
||||
console.log('This guide shows how to use the MeasurementContainer for storing,');
|
||||
console.log('retrieving, and converting measurement data with automatic unit handling.\n');
|
||||
|
||||
// Example 1: Setting values with chaining
|
||||
console.log('--- Example 1: Setting values ---');
|
||||
container.type('pressure').variant('measured').position('upstream').value(100).unit('psi');
|
||||
container.type('pressure').variant('measured').position('downstream').value(95).unit('psi');
|
||||
container.type('pressure').variant('measured').position('downstream').value(80);
|
||||
// ====================================
|
||||
// BASIC SETUP EXAMPLES
|
||||
// ====================================
|
||||
console.log('--- Example 1: Basic Setup & Event Subscription ---');
|
||||
|
||||
// Example 2: Getting values with chaining
|
||||
console.log('--- Example 2: Getting values ---');
|
||||
const upstreamValue = container.type('pressure').variant('measured').position('upstream').getCurrentValue();
|
||||
const upstreamUnit = container.type('pressure').variant('measured').position('upstream').get().unit;
|
||||
// Create a basic container
|
||||
const basicContainer = new MeasurementContainer({ windowSize: 20 });
|
||||
|
||||
// Subscribe to flow events to monitor changes
|
||||
basicContainer.emitter.on('flow.predicted.upstream', (data) => {
|
||||
console.log(`📡 Event: Flow predicted upstream update: ${data.value} at ${new Date(data.timestamp).toLocaleTimeString()}`);
|
||||
});
|
||||
|
||||
//show all flow values from variant measured
|
||||
basicContainer.emitter.on('flow.measured.*', (data) => {
|
||||
console.log(`📡 Event---------- I DID IT: Flow measured ${data.position} update: ${data.value}`)
|
||||
});
|
||||
|
||||
// Basic value setting with chaining
|
||||
console.log('Setting basic pressure values...');
|
||||
basicContainer.type('pressure').variant('measured').position('upstream').value(100).unit('psi');
|
||||
basicContainer.type('pressure').variant('measured').position('downstream').value(95).unit('psi');
|
||||
basicContainer.type('pressure').variant('measured').position('downstream').value(80); // Additional value
|
||||
|
||||
console.log('✅ Basic setup complete\n');
|
||||
|
||||
// ====================================
|
||||
// AUTO-CONVERSION SETUP EXAMPLES
|
||||
// ====================================
|
||||
console.log('--- Example 2: Auto-Conversion Setup ---');
|
||||
console.log('Setting up a container with automatic unit conversion...\n');
|
||||
|
||||
// Create container with auto-conversion enabled
|
||||
const autoContainer = new MeasurementContainer({
|
||||
autoConvert: true,
|
||||
windowSize: 50,
|
||||
defaultUnits: {
|
||||
pressure: 'bar', // Default pressure unit
|
||||
flow: 'l/min', // Default flow unit
|
||||
power: 'kW', // Default power unit
|
||||
temperature: 'C' // Default temperature unit
|
||||
},
|
||||
preferredUnits: {
|
||||
pressure: 'psi' // Override: store pressure in PSI instead of bar
|
||||
}
|
||||
});
|
||||
|
||||
// Values are automatically converted to preferred units
|
||||
console.log('Adding pressure data with auto-conversion:');
|
||||
autoContainer.type('pressure').variant('measured').position('upstream')
|
||||
.value(1.5, Date.now(), 'bar'); // Input: 1.5 bar → Auto-stored as ~21.76 psi
|
||||
|
||||
autoContainer.type('pressure').variant('measured').position('downstream')
|
||||
.value(20, Date.now(), 'psi'); // Input: 20 psi → Stored as 20 psi (already in preferred unit)
|
||||
|
||||
// Check what was actually stored
|
||||
const storedPressure = autoContainer.type('pressure').variant('measured').position('upstream').get();
|
||||
console.log(` Stored upstream pressure: ${storedPressure.getCurrentValue()} ${storedPressure.unit}`);
|
||||
console.log(' Auto-conversion setup complete\n');
|
||||
|
||||
// ====================================
|
||||
// UNIT CONVERSION EXAMPLES
|
||||
// ====================================
|
||||
console.log('--- Example 3: Unit Conversion on Retrieval ---');
|
||||
console.log('Getting values in different units without changing stored data...\n');
|
||||
|
||||
// Add flow data in different units
|
||||
autoContainer.type('flow').variant('predicted').position('upstream')
|
||||
.value(100, Date.now(), 'l/min'); // Stored in l/min (default)
|
||||
|
||||
autoContainer.type('flow').variant('predicted').position('downstream')
|
||||
.value(6, Date.now(), 'm3/h'); // Auto-converted from m3/h to l/min
|
||||
|
||||
// Retrieve the same data in different units
|
||||
const flowLPM = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('l/min');
|
||||
const flowM3H = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('m3/h');
|
||||
const flowGPM = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('gal/min');
|
||||
|
||||
console.log(`Flow in l/min: ${flowLPM}`);
|
||||
console.log(`Flow in m³/h: ${flowM3H.toFixed(2)}`);
|
||||
console.log(`Flow in gal/min: ${flowGPM.toFixed(2)}`);
|
||||
console.log('Unit conversion examples complete\n');
|
||||
|
||||
// ====================================
|
||||
// SMART UNIT SELECTION
|
||||
// ====================================
|
||||
console.log('--- Example 4: Smart Unit Selection ---');
|
||||
console.log('Automatically finding the best unit for readability...\n');
|
||||
|
||||
// Add a very small pressure value
|
||||
autoContainer.type('pressure').variant('test').position('sensor')
|
||||
.value(0.001, Date.now(), 'bar');
|
||||
|
||||
// Get the best unit for this small value
|
||||
const bestUnit = autoContainer.type('pressure').variant('test').position('sensor').getBestUnit();
|
||||
if (bestUnit) {
|
||||
console.log(`Best unit representation: ${bestUnit.val} ${bestUnit.unit}`);
|
||||
}
|
||||
|
||||
// Get all available units for pressure
|
||||
const availableUnits = autoContainer.getAvailableUnits('pressure');
|
||||
console.log(`Available pressure units: ${availableUnits.slice(0, 8).join(', ')}... (${availableUnits.length} total)`);
|
||||
console.log('Smart unit selection complete\n');
|
||||
|
||||
// ====================================
|
||||
// BASIC RETRIEVAL AND CALCULATIONS
|
||||
// ====================================
|
||||
console.log('--- Example 5: Basic Value Retrieval ---');
|
||||
console.log('Getting individual values and their units...\n');
|
||||
|
||||
// Using basic container for clear examples
|
||||
const upstreamValue = basicContainer.type('pressure').variant('measured').position('upstream').getCurrentValue();
|
||||
const upstreamUnit = basicContainer.type('pressure').variant('measured').position('upstream').get().unit;
|
||||
console.log(`Upstream pressure: ${upstreamValue} ${upstreamUnit}`);
|
||||
const downstreamValue = container.type('pressure').variant('measured').position('downstream').getCurrentValue();
|
||||
const downstreamUnit = container.type('pressure').variant('measured').position('downstream').get().unit;
|
||||
|
||||
const downstreamValue = basicContainer.type('pressure').variant('measured').position('downstream').getCurrentValue();
|
||||
const downstreamUnit = basicContainer.type('pressure').variant('measured').position('downstream').get().unit;
|
||||
console.log(`Downstream pressure: ${downstreamValue} ${downstreamUnit}`);
|
||||
console.log('Basic retrieval complete\n');
|
||||
|
||||
// Example 3: Calculations using chained methods
|
||||
console.log('--- Example 3: Calculations ---');
|
||||
container.type('flow').variant('predicted').position('upstream').value(200).unit('gpm');
|
||||
container.type('flow').variant('predicted').position('downstream').value(195).unit('gpm');
|
||||
// ====================================
|
||||
// CALCULATIONS AND STATISTICS
|
||||
// ====================================
|
||||
console.log('--- Example 6: Calculations & Statistics ---');
|
||||
console.log('Using built-in calculation methods...\n');
|
||||
|
||||
const flowAvg = container.type('flow').variant('predicted').position('upstream').getAverage();
|
||||
// Add flow data for calculations
|
||||
basicContainer.type('flow').variant('predicted').position('upstream').value(200).unit('gpm');
|
||||
basicContainer.type('flow').variant('predicted').position('downstream').value(195).unit('gpm');
|
||||
|
||||
const flowAvg = basicContainer.type('flow').variant('predicted').position('upstream').getAverage();
|
||||
console.log(`Average upstream flow: ${flowAvg} gpm`);
|
||||
|
||||
// Example 4: Getting pressure difference
|
||||
console.log('--- Example 4: Difference calculations ---');
|
||||
const pressureDiff = container.type('pressure').variant('measured').difference();
|
||||
// Calculate pressure difference between upstream and downstream
|
||||
const pressureDiff = basicContainer.type('pressure').variant('measured').difference();
|
||||
console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}`);
|
||||
console.log('Calculations complete\n');
|
||||
|
||||
// Example 5: Adding multiple values to track history
|
||||
console.log('--- Example 5: Multiple values ---');
|
||||
// Add several values to upstream flow
|
||||
container.type('flow').variant('measured').position('upstream')
|
||||
.value(210).value(215).value(205).unit('gpm');
|
||||
// ====================================
|
||||
// ADVANCED STATISTICS
|
||||
// ====================================
|
||||
console.log('--- Example 7: Advanced Statistics & History ---');
|
||||
console.log('Adding multiple values and getting comprehensive statistics...\n');
|
||||
|
||||
// Then get statistics
|
||||
console.log('Flow statistics:');
|
||||
console.log(`- Current: ${container.type('flow').variant('measured').position('upstream').getCurrentValue()} gpm`);
|
||||
console.log(`- Average: ${container.type('flow').variant('measured').position('upstream').getAverage()} gpm`);
|
||||
console.log(`- Min: ${container.type('flow').variant('measured').position('upstream').getMin()} gpm`);
|
||||
console.log(`- Max: ${container.type('flow').variant('measured').position('upstream').getMax()} gpm`);
|
||||
console.log(`Show all values : ${JSON.stringify(container.type('flow').variant('measured').position('upstream').getAllValues())}`);
|
||||
// Add several flow measurements to build history
|
||||
basicContainer.type('flow').variant('measured').position('upstream')
|
||||
.value(210).value(215).value(205).value(220).value(200).unit('m3/h');
|
||||
basicContainer.type('flow').variant('measured').position('downstream')
|
||||
.value(190).value(195).value(185).value(200).value(180).unit('m3/h');
|
||||
|
||||
// Example 6: Listing available data
|
||||
console.log('--- Example 6: Listing available data ---');
|
||||
console.log('Types:', container.getTypes());
|
||||
console.log('Pressure variants:', container.type('pressure').getVariants());
|
||||
console.log('Measured pressure positions:', container.type('pressure').variant('measured').getPositions());
|
||||
// Get comprehensive statistics
|
||||
const measurement = basicContainer.type('flow').variant('measured').position('upstream');
|
||||
console.log('Flow Statistics:');
|
||||
console.log(`- Current value: ${measurement.getCurrentValue()} ${measurement.get().unit}`);
|
||||
console.log(`- Average: ${measurement.getAverage().toFixed(1)} ${measurement.get().unit}`);
|
||||
console.log(`- Minimum: ${measurement.getMin()} ${measurement.get().unit}`);
|
||||
console.log(`- Maximum: ${measurement.getMax()} ${measurement.get().unit}`);
|
||||
|
||||
// Show all values with timestamps
|
||||
const allValues = measurement.getAllValues();
|
||||
console.log(`- Total samples: ${allValues.values.length}`);
|
||||
console.log(`- Value history: [${allValues.values.join(', ')}]`);
|
||||
console.log('Advanced statistics complete\n');
|
||||
|
||||
// ====================================
|
||||
// DYNAMIC UNIT MANAGEMENT
|
||||
// ====================================
|
||||
console.log('--- Example 8: Dynamic Unit Management ---');
|
||||
console.log('Changing preferred units at runtime...\n');
|
||||
|
||||
// Change preferred unit for flow measurements
|
||||
autoContainer.setPreferredUnit('flow', 'm3/h');
|
||||
console.log('Changed preferred flow unit to m³/h');
|
||||
|
||||
// Add new flow data - will auto-convert to new preferred unit
|
||||
autoContainer.type('flow').variant('realtime').position('inlet')
|
||||
.value(150, Date.now(), 'l/min'); // Input in l/min, stored as m³/h
|
||||
|
||||
const realtimeFlow = autoContainer.type('flow').variant('realtime').position('inlet');
|
||||
console.log(`Stored as: ${realtimeFlow.getCurrentValue()} ${realtimeFlow.get().unit}`);
|
||||
console.log(`Original unit: ${realtimeFlow.getCurrentValue('l/min')} l/min`);
|
||||
console.log('Dynamic unit management complete\n');
|
||||
|
||||
// ====================================
|
||||
// DATA EXPLORATION
|
||||
// ====================================
|
||||
console.log('--- Example 9: Data Exploration ---');
|
||||
console.log('Discovering what data is available in the container...\n');
|
||||
|
||||
console.log('Available measurement types:', basicContainer.getTypes());
|
||||
console.log('Pressure variants:', basicContainer.type('pressure').getVariants());
|
||||
console.log('Measured pressure positions:', basicContainer.type('pressure').variant('measured').getPositions());
|
||||
|
||||
// Show data structure overview
|
||||
console.log('\nData Structure Overview:');
|
||||
basicContainer.getTypes().forEach(type => {
|
||||
console.log(`${type.toUpperCase()}:`);
|
||||
const variants = basicContainer.type(type).getVariants();
|
||||
variants.forEach(variant => {
|
||||
const positions = basicContainer.type(type).variant(variant).getPositions();
|
||||
positions.forEach(position => {
|
||||
const measurement = basicContainer.type(type).variant(variant).position(position).get();
|
||||
if (measurement && measurement.values.length > 0) {
|
||||
console.log(` └── ${variant}.${position}: ${measurement.values.length} values (${measurement.unit || 'no unit'})`);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
console.log('Data exploration complete\n');
|
||||
|
||||
// ====================================
|
||||
// BEST PRACTICES SUMMARY
|
||||
// ====================================
|
||||
console.log('--- Best Practices Summary ---');
|
||||
console.log('BEST PRACTICES FOR NEW USERS:\n');
|
||||
|
||||
console.log('1. SETUP:');
|
||||
console.log(' • Enable auto-conversion for consistent units');
|
||||
console.log(' • Define default units for your measurement types');
|
||||
console.log(' • Set appropriate window size for your data needs\n');
|
||||
|
||||
console.log('2. STORING DATA:');
|
||||
console.log(' • Always use the full chain: type().variant().position().value()');
|
||||
console.log(' • Specify source unit when adding values: .value(100, timestamp, "psi")');
|
||||
console.log(' • Set units immediately after first value: .value(100).unit("psi")\n');
|
||||
|
||||
console.log('3. RETRIEVING DATA:');
|
||||
console.log(' • Use .getCurrentValue("unit") to get values in specific units');
|
||||
console.log(' • Use .getBestUnit() for automatic unit selection');
|
||||
console.log(' • Use .difference() for automatic upstream/downstream calculations\n');
|
||||
|
||||
console.log('4. MONITORING:');
|
||||
console.log(' • Subscribe to events for real-time updates');
|
||||
console.log(' • Use .emitter.on("type.variant.position", callback)');
|
||||
console.log(' • Explore available data with .getTypes(), .getVariants(), .getPositions()\n');
|
||||
|
||||
console.log('All examples complete! Ready to use MeasurementContainer');
|
||||
|
||||
// Export for programmatic use
|
||||
module.exports = {
|
||||
runExamples: () => {
|
||||
console.log('Examples of the measurement chainable API');
|
||||
}
|
||||
};
|
||||
console.log('Measurement Container Examples - Complete Guide for New Users');
|
||||
console.log('This file demonstrates all features with practical examples.');
|
||||
},
|
||||
|
||||
// Export containers for testing
|
||||
basicContainer,
|
||||
autoContainer
|
||||
};
|
||||
@@ -1,23 +1,26 @@
|
||||
const AssetMenu = require('./asset.js');
|
||||
const { TagcodeApp, DynamicAssetMenu } = require('./tagcodeApp.js');
|
||||
const LoggerMenu = require('./logger.js');
|
||||
const PhysicalPositionMenu = require('./physicalPosition.js');
|
||||
|
||||
class MenuManager {
|
||||
|
||||
constructor() {
|
||||
this.registeredMenus = new Map(); // Store menu type instances
|
||||
this.registerMenu('asset', new AssetMenu()); // Register asset menu by default
|
||||
this.registerMenu('logger', new LoggerMenu()); // Register logger menu by default
|
||||
this.registerMenu('position', new PhysicalPositionMenu()); // Register position menu by default
|
||||
this.registeredMenus = new Map();
|
||||
// Register factory functions
|
||||
this.registerMenu('asset', () => new AssetMenu()); // static menu to be replaced by dynamic one but later
|
||||
//this.registerMenu('asset', (nodeName) => new DynamicAssetMenu(nodeName, new TagcodeApp()));
|
||||
this.registerMenu('logger', () => new LoggerMenu());
|
||||
this.registerMenu('position', () => new PhysicalPositionMenu());
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a menu type with its handler instance
|
||||
* Register a menu type with its handler factory function
|
||||
* @param {string} menuType - The type of menu (e.g., 'asset', 'logging')
|
||||
* @param {object} menuHandler - The menu handler instance
|
||||
* @param {function} menuFactory - The menu factory function
|
||||
*/
|
||||
registerMenu(menuType, menuHandler) {
|
||||
this.registeredMenus.set(menuType, menuHandler);
|
||||
registerMenu(menuType, menuFactory) {
|
||||
this.registeredMenus.set(menuType, menuFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,58 +30,145 @@ class MenuManager {
|
||||
* @returns {string} Complete JavaScript code to serve
|
||||
*/
|
||||
createEndpoint(nodeName, menuTypes) {
|
||||
// 1. Collect all menu data
|
||||
const menuData = {};
|
||||
menuTypes.forEach(menuType => {
|
||||
const handler = this.registeredMenus.get(menuType);
|
||||
if (handler && typeof handler.getAllMenuData === 'function') {
|
||||
menuData[menuType] = handler.getAllMenuData();
|
||||
try {
|
||||
// ✅ Create instances using factory functions with proper error handling
|
||||
const instantiatedMenus = new Map();
|
||||
|
||||
menuTypes.forEach(menuType => {
|
||||
try {
|
||||
const factory = this.registeredMenus.get(menuType);
|
||||
if (typeof factory === 'function') {
|
||||
const instance = factory(nodeName);
|
||||
instantiatedMenus.set(menuType, instance);
|
||||
} else {
|
||||
console.warn(`No factory function found for menu type: ${menuType}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error creating instance for ${menuType}:`, error);
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ Collect all menu data with error handling
|
||||
const menuData = {};
|
||||
menuTypes.forEach(menuType => {
|
||||
try {
|
||||
const handler = instantiatedMenus.get(menuType);
|
||||
if (handler && typeof handler.getAllMenuData === 'function') {
|
||||
menuData[menuType] = handler.getAllMenuData();
|
||||
} else {
|
||||
// Provide default empty data if method doesn't exist
|
||||
menuData[menuType] = {};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error getting menu data for ${menuType}:`, error);
|
||||
menuData[menuType] = {};
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ Generate HTML injection code with error handling
|
||||
const htmlInjections = menuTypes.map(type => {
|
||||
try {
|
||||
const menu = instantiatedMenus.get(type);
|
||||
if (menu && typeof menu.getHtmlInjectionCode === 'function') {
|
||||
return menu.getHtmlInjectionCode(nodeName);
|
||||
}
|
||||
return '';
|
||||
} catch (error) {
|
||||
console.error(`Error generating HTML injection for ${type}:`, error);
|
||||
return `// Error generating HTML injection for ${type}: ${error.message}`;
|
||||
}
|
||||
}).join('\n');
|
||||
|
||||
// ✅ Collect all client initialization code with error handling
|
||||
const initFunctions = [];
|
||||
menuTypes.forEach(menuType => {
|
||||
try {
|
||||
const handler = instantiatedMenus.get(menuType);
|
||||
if (handler && typeof handler.getClientInitCode === 'function') {
|
||||
initFunctions.push(handler.getClientInitCode(nodeName));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error generating init code for ${menuType}:`, error);
|
||||
initFunctions.push(`// Error in ${menuType} initialization: ${error.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Convert menu data to JSON
|
||||
const menuDataJSON = JSON.stringify(menuData, null, 2);
|
||||
|
||||
// ✅ Assemble the complete script with comprehensive error handling
|
||||
return `
|
||||
try {
|
||||
// Create the namespace structure with safety checks
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
|
||||
// Initialize menu namespaces
|
||||
${menuTypes.map(type => `window.EVOLV.nodes.${nodeName}.${type}Menu = window.EVOLV.nodes.${nodeName}.${type}Menu || {};`).join('\n ')}
|
||||
|
||||
// Inject the pre-loaded menu data directly into the namespace
|
||||
window.EVOLV.nodes.${nodeName}.menuData = ${menuDataJSON};
|
||||
|
||||
// HTML injections with error handling
|
||||
try {
|
||||
${htmlInjections}
|
||||
} catch (htmlError) {
|
||||
console.error('Error in HTML injections for ${nodeName}:', htmlError);
|
||||
}
|
||||
|
||||
// Initialize functions with error handling
|
||||
try {
|
||||
${initFunctions.join('\n\n ')}
|
||||
} catch (initError) {
|
||||
console.error('Error in initialization functions for ${nodeName}:', initError);
|
||||
}
|
||||
|
||||
// Main initialization function that calls all menu initializers
|
||||
window.EVOLV.nodes.${nodeName}.initEditor = function(node) {
|
||||
try {
|
||||
${menuTypes.map(type => `
|
||||
try {
|
||||
if (window.EVOLV.nodes.${nodeName}.${type}Menu && window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor) {
|
||||
window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor(node);
|
||||
}
|
||||
} catch (${type}Error) {
|
||||
console.error('Error initializing ${type} menu for ${nodeName}:', ${type}Error);
|
||||
}`).join('')}
|
||||
} catch (editorError) {
|
||||
console.error('Error in main editor initialization for ${nodeName}:', editorError);
|
||||
}
|
||||
};
|
||||
|
||||
console.log('${nodeName} menu data and initializers loaded for: ${menuTypes.join(', ')}');
|
||||
|
||||
} catch (globalError) {
|
||||
console.error('Critical error in ${nodeName} menu initialization:', globalError);
|
||||
|
||||
// Fallback initialization
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
window.EVOLV.nodes.${nodeName}.initEditor = function(node) {
|
||||
console.warn('Using fallback editor initialization for ${nodeName}');
|
||||
};
|
||||
}
|
||||
});
|
||||
`;
|
||||
|
||||
// Generate HTML injection code
|
||||
const htmlInjections = menuTypes.map(type => {
|
||||
const menu = this.registeredMenus.get(type);
|
||||
if (menu && menu.getHtmlInjectionCode) {
|
||||
return menu.getHtmlInjectionCode(nodeName);
|
||||
}
|
||||
return '';
|
||||
}).join('\n');
|
||||
|
||||
// 2. Collect all client initialization code
|
||||
const initFunctions = [];
|
||||
menuTypes.forEach(menuType => {
|
||||
const handler = this.registeredMenus.get(menuType);
|
||||
if (handler && typeof handler.getClientInitCode === 'function') {
|
||||
initFunctions.push(handler.getClientInitCode(nodeName));
|
||||
}
|
||||
});
|
||||
|
||||
// 3. Convert menu data to JSON
|
||||
const menuDataJSON = JSON.stringify(menuData, null, 2);
|
||||
|
||||
// 4. Assemble the complete script
|
||||
return `
|
||||
// Create the namespace structure
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
|
||||
// Inject the pre-loaded menu data directly into the namespace
|
||||
window.EVOLV.nodes.${nodeName}.menuData = ${menuDataJSON};
|
||||
|
||||
${initFunctions.join('\n\n')}
|
||||
|
||||
// Main initialization function that calls all menu initializers
|
||||
window.EVOLV.nodes.${nodeName}.initEditor = function(node) {
|
||||
${menuTypes.map(type => `
|
||||
if (window.EVOLV.nodes.${nodeName}.${type}Menu && window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor) {
|
||||
window.EVOLV.nodes.${nodeName}.${type}Menu.initEditor(node);
|
||||
}`).join('')}
|
||||
};
|
||||
|
||||
console.log('${nodeName} menu data and initializers loaded for: ${menuTypes.join(', ')}');
|
||||
`;
|
||||
} catch (error) {
|
||||
console.error(`Critical error creating endpoint for ${nodeName}:`, error);
|
||||
|
||||
// Return minimal fallback script
|
||||
return `
|
||||
window.EVOLV = window.EVOLV || {};
|
||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
||||
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
|
||||
window.EVOLV.nodes.${nodeName}.initEditor = function(node) {
|
||||
console.error('Menu system failed to initialize for ${nodeName}');
|
||||
};
|
||||
console.error('Menu system failed for ${nodeName}:', '${error.message}');
|
||||
`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,25 @@ class PhysicalPositionMenu {
|
||||
{ value: 'downstream', label: '→ Downstream' , icon: '→' }
|
||||
]
|
||||
}
|
||||
]
|
||||
],
|
||||
// Distance contexts for each position
|
||||
distanceContexts: {
|
||||
upstream: {
|
||||
description: 'Distance from parent inlet',
|
||||
placeholder: 'e.g., 2.5 (meters before parent)',
|
||||
helpText: 'How far upstream from the parent equipment'
|
||||
},
|
||||
downstream: {
|
||||
description: 'Distance from parent outlet',
|
||||
placeholder: 'e.g., 3.0 (meters after parent)',
|
||||
helpText: 'How far downstream from the parent equipment'
|
||||
},
|
||||
atEquipment: {
|
||||
description: 'Distance from parent start',
|
||||
placeholder: 'e.g., 1.2 (meters from start)',
|
||||
helpText: 'Position within the parent equipment boundaries'
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,6 +44,24 @@ class PhysicalPositionMenu {
|
||||
<!-- optgroups will be injected -->
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Distance section -->
|
||||
<div class="form-row">
|
||||
<label> </label>
|
||||
<input type="checkbox" id="node-input-hasDistance" style="display:inline-block; width:auto; margin-right:5px;">
|
||||
<label for="node-input-hasDistance" style="width:auto;">Specify 1D Distance</label>
|
||||
</div>
|
||||
|
||||
<div id="distance-section" class="form-row" style="display:none;">
|
||||
<label for="node-input-distance"><i class="fa fa-ruler"></i>Distance</label>
|
||||
<div style="display:flex; align-items:center; width:70%;">
|
||||
<input type="number" id="node-input-distance" step="0.1" min="0" style="width:60%;" placeholder="0.0">
|
||||
<span style="margin-left:5px; margin-right:5px;">meters</span>
|
||||
</div>
|
||||
<div id="distance-help" class="form-tips" style="margin-left:105px; font-size:11px; color:#666;">
|
||||
Select a position to see distance context
|
||||
</div>
|
||||
</div>
|
||||
<hr />
|
||||
`;
|
||||
}
|
||||
@@ -52,22 +88,40 @@ class PhysicalPositionMenu {
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.loadData = function(node) {
|
||||
const data = window.EVOLV.nodes.${nodeName}.menuData.position;
|
||||
const sel = document.getElementById('node-input-positionVsParent');
|
||||
if (!sel) return;
|
||||
sel.innerHTML = '';
|
||||
(data.positionGroups||[]).forEach(grp => {
|
||||
const optg = document.createElement('optgroup');
|
||||
optg.label = grp.group;
|
||||
grp.options.forEach(o=>{
|
||||
const opt = document.createElement('option');
|
||||
opt.value = o.value;
|
||||
opt.textContent = o.label;
|
||||
opt.setAttribute('data-icon', o.icon);
|
||||
optg.appendChild(opt);
|
||||
const hasDistanceCheck = document.getElementById('node-input-hasDistance');
|
||||
const distanceInput = document.getElementById('node-input-distance');
|
||||
const distanceSection = document.getElementById('distance-section');
|
||||
|
||||
//Load position options
|
||||
if (sel) {
|
||||
sel.innerHTML = '';
|
||||
(data.positionGroups||[]).forEach(grp => {
|
||||
const optg = document.createElement('optgroup');
|
||||
optg.label = grp.group;
|
||||
grp.options.forEach(o=>{
|
||||
const opt = document.createElement('option');
|
||||
opt.value = o.value;
|
||||
opt.textContent = o.label;
|
||||
opt.setAttribute('data-icon', o.icon);
|
||||
optg.appendChild(opt);
|
||||
});
|
||||
sel.appendChild(optg);
|
||||
});
|
||||
sel.appendChild(optg);
|
||||
});
|
||||
// default to “atEquipment” if not set
|
||||
sel.value = node.positionVsParent || 'atEquipment';
|
||||
sel.value = node.positionVsParent || 'atEquipment';
|
||||
}
|
||||
|
||||
//Load distance values
|
||||
if (hasDistanceCheck) {
|
||||
hasDistanceCheck.checked = node.hasDistance || false;
|
||||
distanceSection.style.display = hasDistanceCheck.checked ? 'block' : 'none';
|
||||
}
|
||||
|
||||
if (distanceInput) {
|
||||
distanceInput.value = node.distance || '';
|
||||
}
|
||||
|
||||
// Update distance context for current position
|
||||
this.updateDistanceContext(node.positionVsParent || 'atEquipment', data.distanceContexts);
|
||||
};
|
||||
`;
|
||||
}
|
||||
@@ -77,24 +131,86 @@ class PhysicalPositionMenu {
|
||||
return `
|
||||
// PhysicalPosition events for ${nodeName}
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.wireEvents = function(node) {
|
||||
// no dynamic behavior
|
||||
const positionSel = document.getElementById('node-input-positionVsParent');
|
||||
const hasDistanceCheck = document.getElementById('node-input-hasDistance');
|
||||
const distanceSection = document.getElementById('distance-section');
|
||||
const data = window.EVOLV.nodes.${nodeName}.menuData.position;
|
||||
|
||||
// Toggle distance section visibility
|
||||
if (hasDistanceCheck && distanceSection) {
|
||||
hasDistanceCheck.addEventListener('change', function() {
|
||||
distanceSection.style.display = this.checked ? 'block' : 'none';
|
||||
|
||||
// Clear distance if unchecked
|
||||
if (!this.checked) {
|
||||
const distanceInput = document.getElementById('node-input-distance');
|
||||
if (distanceInput) {
|
||||
distanceInput.value = '';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Update distance context when position changes
|
||||
if (positionSel) {
|
||||
positionSel.addEventListener('change', function() {
|
||||
const position = this.value;
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.updateDistanceContext(position, data.distanceContexts);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to update distance context
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.updateDistanceContext = function(position, contexts) {
|
||||
const distanceInput = document.getElementById('node-input-distance');
|
||||
const distanceHelp = document.getElementById('distance-help');
|
||||
|
||||
const context = contexts && contexts[position];
|
||||
|
||||
if (context && distanceInput && distanceHelp) {
|
||||
distanceInput.placeholder = context.placeholder || '0.0';
|
||||
distanceHelp.textContent = context.helpText || 'Enter distance in meters';
|
||||
}
|
||||
};
|
||||
`;
|
||||
}
|
||||
|
||||
// 6) Save-logic injector
|
||||
getSaveInjectionCode(nodeName) {
|
||||
return `
|
||||
// PhysicalPosition Save injection for ${nodeName}
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.saveEditor = function(node) {
|
||||
const sel = document.getElementById('node-input-positionVsParent');
|
||||
node.positionVsParent = sel? sel.value : 'atEquipment';
|
||||
node.positionLabel = sel? sel.options[sel.selectedIndex].textContent : 'At Equipment';
|
||||
node.positionIcon = sel? sel.options[sel.selectedIndex].getAttribute('data-icon') : 'fa fa-cog';
|
||||
return true;
|
||||
};
|
||||
`;
|
||||
}
|
||||
getSaveInjectionCode(nodeName) {
|
||||
return `
|
||||
// PhysicalPosition Save injection for ${nodeName}
|
||||
window.EVOLV.nodes.${nodeName}.positionMenu.saveEditor = function(node) {
|
||||
const sel = document.getElementById('node-input-positionVsParent');
|
||||
const hasDistanceCheck = document.getElementById('node-input-hasDistance');
|
||||
const distanceInput = document.getElementById('node-input-distance');
|
||||
|
||||
// Save existing position data
|
||||
node.positionVsParent = sel ? sel.value : 'atEquipment';
|
||||
node.positionLabel = sel ? sel.options[sel.selectedIndex].textContent : 'At Equipment';
|
||||
node.positionIcon = sel ? sel.options[sel.selectedIndex].getAttribute('data-icon') : 'fa fa-cog';
|
||||
|
||||
// Save distance data (NEW)
|
||||
node.hasDistance = hasDistanceCheck ? hasDistanceCheck.checked : false;
|
||||
|
||||
if (node.hasDistance && distanceInput && distanceInput.value) {
|
||||
node.distance = parseFloat(distanceInput.value) || 0;
|
||||
node.distanceUnit = 'm'; // Fixed to meters for now
|
||||
|
||||
// Generate distance description based on position
|
||||
const contexts = window.EVOLV.nodes.${nodeName}.menuData.position.distanceContexts;
|
||||
const context = contexts && contexts[node.positionVsParent];
|
||||
node.distanceDescription = context ? context.description : 'Distance from parent';
|
||||
} else {
|
||||
// Clear distance data if not specified
|
||||
delete node.distance;
|
||||
delete node.distanceUnit;
|
||||
delete node.distanceDescription;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
`;
|
||||
}
|
||||
|
||||
// 7) Compose everything into one client bundle
|
||||
getClientInitCode(nodeName) {
|
||||
|
||||
606
src/menu/tagcodeApp.js
Normal file
606
src/menu/tagcodeApp.js
Normal file
@@ -0,0 +1,606 @@
|
||||
/**
|
||||
* taggcodeApp.js
|
||||
* Dynamische AssetMenu implementatie met TagcodeApp API
|
||||
* Vervangt de statische assetData met calls naar REST-endpoints.
|
||||
*/
|
||||
|
||||
class TagcodeApp {
|
||||
constructor(baseURL = 'https://pimmoerman.nl/rdlab/tagcode.app/v2.1/api') {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
async fetchData(path, params = {}) {
|
||||
const url = new URL(`${this.baseURL}/${path}`);
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
url.searchParams.append(key, value);
|
||||
});
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
const json = await response.json();
|
||||
if (!json.success) throw new Error(json.error || json.message);
|
||||
return json.data;
|
||||
}
|
||||
|
||||
// Asset endpoints
|
||||
getAllAssets() {
|
||||
return this.fetchData('asset/get_all_assets.php');
|
||||
}
|
||||
|
||||
getAssetDetail(tag_code) {
|
||||
return this.fetchData('asset/get_detail_asset.php', { tag_code });
|
||||
}
|
||||
|
||||
getAssetHistory(asset_tag_number) {
|
||||
return this.fetchData('asset/get_history_asset.php', { asset_tag_number });
|
||||
}
|
||||
|
||||
getAssetHierarchy(asset_tag_number) {
|
||||
return this.fetchData('asset/get_asset_hierarchy.php', { asset_tag_number });
|
||||
}
|
||||
|
||||
createOrUpdateAsset(params) {
|
||||
return this.fetchData('asset/create_asset.php', params);
|
||||
}
|
||||
|
||||
// Product & vendor endpoints
|
||||
getVendors() {
|
||||
return this.fetchData('vendor/get_vendors.php');
|
||||
}
|
||||
|
||||
getSubtypes(vendor_name) {
|
||||
return this.fetchData('product/get_subtypesFromVendor.php', { vendor_name });
|
||||
}
|
||||
|
||||
getSubtypesForCategory(vendor_name, category) {
|
||||
return this.fetchData('product/get_subtypesFromVendorAndCategory.php', {
|
||||
vendor_name,
|
||||
category
|
||||
});
|
||||
}
|
||||
|
||||
getProductModels(vendor_name, product_subtype_name) {
|
||||
return this.fetchData('product/get_product_models.php', { vendor_name, product_subtype_name });
|
||||
}
|
||||
|
||||
getLocations() {
|
||||
return this.fetchData('location/get_locations.php');
|
||||
}
|
||||
}
|
||||
|
||||
class DynamicAssetMenu {
|
||||
constructor(nodeName, api = new TagcodeApp()) {
|
||||
|
||||
this.nodeName = nodeName;
|
||||
this.api = api;
|
||||
|
||||
//temp translation table for nodeName to API
|
||||
// Mapping van nodeName naar softwareType
|
||||
this.softwareTypeMapping = {
|
||||
'measurement': 'Sensor',
|
||||
'rotatingMachine': 'machine',
|
||||
'valve': 'valve',
|
||||
'pump': 'machine',
|
||||
'heatExchanger': 'machine',
|
||||
// Voeg meer mappings toe als nodig
|
||||
};
|
||||
|
||||
// Bepaal automatisch de softwareType
|
||||
this.softwareType = this.softwareTypeMapping[nodeName] || nodeName;
|
||||
|
||||
|
||||
this.data = {
|
||||
vendors: [],
|
||||
subtypes: {},
|
||||
models: {}
|
||||
};
|
||||
}
|
||||
|
||||
//Added missing getAllMenuData method
|
||||
|
||||
getAllMenuData() {
|
||||
return {
|
||||
vendors: this.data.vendors || [],
|
||||
locations: this.data.locations || [],
|
||||
htmlTemplate: this.getHtmlTemplate()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialiseer: haal alleen de vendor-lijst en locaties op
|
||||
*/
|
||||
async init() {
|
||||
try {
|
||||
this.data.suppliers = await this.api.getVendors();
|
||||
this.data.locations = await this.api.getLocations();
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize DynamicAssetMenu:', error);
|
||||
this.data.suppliers = [];
|
||||
this.data.locations = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Complete getClientInitCode method with full TagcodeApp definition
|
||||
|
||||
getClientInitCode(nodeName) {
|
||||
return `
|
||||
// --- DynamicAssetMenu voor ${nodeName} ---
|
||||
|
||||
// ✅ Define COMPLETE TagcodeApp class in browser context
|
||||
window.TagcodeApp = window.TagcodeApp || class {
|
||||
constructor(baseURL = 'https://pimmoerman.nl/rdlab/tagcode.app/v2.1/api') {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
async fetchData(path, params = {}) {
|
||||
const url = new URL(this.baseURL + '/' + path);
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
url.searchParams.append(key, value);
|
||||
});
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error('HTTP ' + response.status + ': ' + response.statusText);
|
||||
const json = await response.json();
|
||||
if (!json.success) throw new Error(json.error || json.message);
|
||||
return json.data;
|
||||
}
|
||||
|
||||
// ✅ ALL API methods defined here
|
||||
getAllAssets() {
|
||||
return this.fetchData('asset/get_all_assets.php');
|
||||
}
|
||||
|
||||
getAssetDetail(tag_code) {
|
||||
return this.fetchData('asset/get_detail_asset.php', { tag_code });
|
||||
}
|
||||
|
||||
getVendors() {
|
||||
return this.fetchData('vendor/get_vendors.php');
|
||||
}
|
||||
|
||||
getSubtypes(vendor_name, category = null) {
|
||||
const params = { vendor_name };
|
||||
if (category) params.category = category;
|
||||
return this.fetchData('product/get_subtypesFromVendor.php', params);
|
||||
}
|
||||
|
||||
getProductModels(vendor_name, product_subtype_name) {
|
||||
return this.fetchData('product/get_product_models.php', { vendor_name, product_subtype_name });
|
||||
}
|
||||
|
||||
getLocations() {
|
||||
return this.fetchData('location/get_locations.php');
|
||||
}
|
||||
};
|
||||
|
||||
// ✅ Initialize the API instance BEFORE it's needed
|
||||
window.assetAPI = window.assetAPI || new window.TagcodeApp();
|
||||
|
||||
// Helper populate function
|
||||
function populate(el, opts, sel) {
|
||||
if (!el) return;
|
||||
const old = el.value;
|
||||
el.innerHTML = '<option value="">Select…</option>';
|
||||
(opts||[]).forEach(o=>{
|
||||
const opt = document.createElement('option');
|
||||
opt.value = o;
|
||||
opt.textContent = o;
|
||||
el.appendChild(opt);
|
||||
});
|
||||
el.value = sel || '';
|
||||
if (el.value !== old) el.dispatchEvent(new Event('change'));
|
||||
}
|
||||
|
||||
// ✅ Ensure namespace exists and initialize properly
|
||||
if (!window.EVOLV.nodes.${nodeName}.assetMenu) {
|
||||
window.EVOLV.nodes.${nodeName}.assetMenu = {};
|
||||
}
|
||||
|
||||
// ✅ Complete initEditor function
|
||||
window.EVOLV.nodes.${nodeName}.assetMenu.initEditor = async function(node) {
|
||||
try {
|
||||
console.log('🚀 Starting asset menu initialization for ${nodeName}');
|
||||
console.log('🎯 Automatic softwareType: ${this.softwareType}');
|
||||
|
||||
// ✅ Verify API is available
|
||||
if (!window.assetAPI) {
|
||||
console.error('❌ window.assetAPI not available');
|
||||
return;
|
||||
}
|
||||
|
||||
// ✅ Wait for DOM to be ready and inject HTML with retry
|
||||
const waitForDialogAndInject = () => {
|
||||
return new Promise((resolve) => {
|
||||
let attempts = 0;
|
||||
const maxAttempts = 20;
|
||||
|
||||
const tryInject = () => {
|
||||
attempts++;
|
||||
console.log('Injection attempt ' + attempts + '/' + maxAttempts);
|
||||
|
||||
const injectionSuccess = this.injectHtml ? this.injectHtml() : false;
|
||||
|
||||
if (injectionSuccess) {
|
||||
console.log('✅ HTML injection successful on attempt:', attempts);
|
||||
resolve(true);
|
||||
} else if (attempts < maxAttempts) {
|
||||
setTimeout(tryInject, 100);
|
||||
} else {
|
||||
console.warn('⚠️ HTML injection failed after ' + maxAttempts + ' attempts');
|
||||
resolve(false);
|
||||
}
|
||||
};
|
||||
|
||||
setTimeout(tryInject, 200);
|
||||
});
|
||||
};
|
||||
|
||||
// Wait for HTML injection
|
||||
const htmlReady = await waitForDialogAndInject();
|
||||
|
||||
if (!htmlReady) {
|
||||
console.error('❌ Could not inject HTML, continuing without asset menu');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔧 Setting up asset menu functionality');
|
||||
|
||||
// ✅ Load vendor list with error handling
|
||||
try {
|
||||
console.log('📡 Loading vendors...');
|
||||
const vendors = await window.assetAPI.getVendors();
|
||||
console.log('✅ Vendors loaded:', vendors.length);
|
||||
|
||||
// ✅ Handle both string arrays and object arrays
|
||||
const vendorNames = vendors.map(v => v.name || v);
|
||||
populate(document.getElementById('node-input-supplier'), vendorNames, node.supplier);
|
||||
} catch (vendorError) {
|
||||
console.error('❌ Error loading vendors:', vendorError);
|
||||
}
|
||||
|
||||
// ✅ Get form elements
|
||||
const elems = {
|
||||
supplier: document.getElementById('node-input-supplier'),
|
||||
category: document.getElementById('node-input-category'),
|
||||
type: document.getElementById('node-input-assetType'),
|
||||
model: document.getElementById('node-input-model'),
|
||||
unit: document.getElementById('node-input-unit')
|
||||
};
|
||||
|
||||
// ✅ Set automatic category value
|
||||
if (elems.category) {
|
||||
elems.category.value = '${this.softwareType}';
|
||||
console.log('✅ Automatic category set to:', elems.category.value);
|
||||
}
|
||||
|
||||
// ✅ Supplier change: load subtypes for automatic category
|
||||
if (elems.supplier) {
|
||||
elems.supplier.addEventListener('change', async () => {
|
||||
const vendor = elems.supplier.value;
|
||||
const category = '${this.softwareType}';
|
||||
|
||||
if (!vendor) {
|
||||
populate(elems.type, [], '');
|
||||
populate(elems.model, [], '');
|
||||
populate(elems.unit, [], '');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('📡 Loading subtypes for vendor:', vendor, 'category:', category);
|
||||
const subtypes = await window.assetAPI.getSubtypes(vendor, category);
|
||||
console.log('✅ Subtypes loaded:', subtypes.length);
|
||||
|
||||
const subtypeNames = subtypes.map(s => s.name || s.subtype_name || s);
|
||||
populate(elems.type, subtypeNames, node.assetType);
|
||||
|
||||
populate(elems.model, [], '');
|
||||
populate(elems.unit, [], '');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error loading subtypes:', error);
|
||||
populate(elems.type, [], '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ✅ Type change: load models for vendor + selected subtype
|
||||
if (elems.type) {
|
||||
elems.type.addEventListener('change', async () => {
|
||||
const vendor = elems.supplier.value;
|
||||
const selectedSubtype = elems.type.value;
|
||||
|
||||
if (!vendor || !selectedSubtype) {
|
||||
populate(elems.model, [], '');
|
||||
populate(elems.unit, [], '');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('📡 Loading models for vendor:', vendor, 'subtype:', selectedSubtype);
|
||||
const models = await window.assetAPI.getProductModels(vendor, selectedSubtype);
|
||||
console.log('✅ Models loaded:', models.length);
|
||||
|
||||
window._currentModels = models;
|
||||
const modelNames = models.map(m => m.name || m.model_name || m);
|
||||
populate(elems.model, modelNames, node.model);
|
||||
|
||||
populate(elems.unit, [], '');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error loading models:', error);
|
||||
populate(elems.model, [], '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ✅ Model change: show units for selected model
|
||||
if (elems.model) {
|
||||
elems.model.addEventListener('change', () => {
|
||||
const selectedModelName = elems.model.value;
|
||||
const models = window._currentModels || [];
|
||||
const selectedModel = models.find(m =>
|
||||
(m.name || m.model_name) === selectedModelName
|
||||
);
|
||||
|
||||
const units = selectedModel && selectedModel.product_model_meta ?
|
||||
Object.keys(selectedModel.product_model_meta) : [];
|
||||
populate(elems.unit, units, node.unit);
|
||||
});
|
||||
}
|
||||
|
||||
// ✅ Trigger supplier change if there's a saved value
|
||||
if (node.supplier && elems.supplier) {
|
||||
setTimeout(() => {
|
||||
elems.supplier.dispatchEvent(new Event('change'));
|
||||
}, 100);
|
||||
}
|
||||
|
||||
console.log('✅ Asset menu initialization complete for ${nodeName}');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error in asset menu initialization:', error);
|
||||
}
|
||||
};
|
||||
`;
|
||||
}
|
||||
|
||||
getHtmlTemplate() {
|
||||
return `
|
||||
<!-- Asset Properties -->
|
||||
<hr />
|
||||
<h3>Asset selection (${this.softwareType})</h3>
|
||||
<div class="form-row">
|
||||
<label for="node-input-supplier"><i class="fa fa-industry"></i> Supplier</label>
|
||||
<select id="node-input-supplier" style="width:70%;"></select>
|
||||
</div>
|
||||
<!-- ✅ Toon softwareType als readonly info -->
|
||||
<div class="form-row">
|
||||
<label><i class="fa fa-sitemap"></i> Category</label>
|
||||
<input type="text" value="${this.softwareType}" readonly style="width:70%; background-color: #f5f5f5;" />
|
||||
<input type="hidden" id="node-input-category" value="${this.softwareType}" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-assetType"><i class="fa fa-puzzle-piece"></i> Type</label>
|
||||
<select id="node-input-assetType" style="width:70%;"></select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-model"><i class="fa fa-wrench"></i> Model</label>
|
||||
<select id="node-input-model" style="width:70%;"></select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-unit"><i class="fa fa-balance-scale"></i> Unit</label>
|
||||
<select id="node-input-unit" style="width:70%;"></select>
|
||||
</div>
|
||||
<hr />
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixed getHtmlInjectionCode method
|
||||
*/
|
||||
/**
|
||||
* Fixed getHtmlInjectionCode method with better element detection
|
||||
*/
|
||||
getHtmlInjectionCode(nodeName) {
|
||||
const htmlTemplate = this.getHtmlTemplate().replace(/`/g, '\\`').replace(/\${/g, '\\${');
|
||||
|
||||
return `
|
||||
// Enhanced HTML injection with multiple fallback strategies
|
||||
window.EVOLV.nodes.${nodeName}.assetMenu.injectHtml = function() {
|
||||
try {
|
||||
// Strategy 1: Find the dialog form container
|
||||
let targetContainer = document.querySelector('#red-ui-editor-dialog .red-ui-editDialog-content');
|
||||
|
||||
// Strategy 2: Fallback to the main dialog form
|
||||
if (!targetContainer) {
|
||||
targetContainer = document.querySelector('#dialog-form');
|
||||
}
|
||||
|
||||
// Strategy 3: Fallback to any form in the editor dialog
|
||||
if (!targetContainer) {
|
||||
targetContainer = document.querySelector('#red-ui-editor-dialog form');
|
||||
}
|
||||
|
||||
// Strategy 4: Find by Red UI classes
|
||||
if (!targetContainer) {
|
||||
targetContainer = document.querySelector('.red-ui-editor-dialog .editor-tray-content');
|
||||
}
|
||||
|
||||
if (targetContainer) {
|
||||
// Remove any existing asset menu to prevent duplicates
|
||||
const existingAssetMenu = targetContainer.querySelector('.asset-menu-section');
|
||||
if (existingAssetMenu) {
|
||||
existingAssetMenu.remove();
|
||||
}
|
||||
|
||||
// Create container div
|
||||
const assetMenuDiv = document.createElement('div');
|
||||
assetMenuDiv.className = 'asset-menu-section';
|
||||
assetMenuDiv.innerHTML = \`${htmlTemplate}\`;
|
||||
|
||||
// Insert at the beginning of the form
|
||||
targetContainer.insertBefore(assetMenuDiv, targetContainer.firstChild);
|
||||
|
||||
console.log(' Asset menu HTML injected successfully into:', targetContainer.className || targetContainer.tagName);
|
||||
return true;
|
||||
} else {
|
||||
console.warn('⚠️ Could not find dialog form container. Available elements:');
|
||||
console.log('Available dialogs:', document.querySelectorAll('[id*="dialog"], [class*="dialog"]'));
|
||||
console.log('Available forms:', document.querySelectorAll('form'));
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Error injecting HTML:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
// Exporteer voor gebruik in Node-RED
|
||||
module.exports = { TagcodeApp, DynamicAssetMenu };
|
||||
|
||||
/*
|
||||
// --- Test CLI ---
|
||||
// Voer deze test uit met `node tagcodeApp.js` om de API-client en menu-init logica te controleren
|
||||
if (require.main === module) {
|
||||
(async () => {
|
||||
const api = new TagcodeApp();
|
||||
console.log('=== Test: getVendors() ===');
|
||||
let vendors;
|
||||
try {
|
||||
vendors = await api.getVendors();
|
||||
console.log('Vendors:', vendors);
|
||||
} catch (e) {
|
||||
console.error('getVendors() error:', e.message);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('=== Test: getLocations() ===');
|
||||
try {
|
||||
const locations = await api.getLocations();
|
||||
console.log('Locations:', locations);
|
||||
} catch (e) {
|
||||
console.error('getLocations() error:', e.message);
|
||||
return;
|
||||
}
|
||||
|
||||
// ✅ Test verschillende nodeNames met automatische softwareType mapping
|
||||
const testNodes = [
|
||||
{ nodeName: 'measurement', expectedSoftwareType: 'Sensor' },
|
||||
{ nodeName: 'rotatingMachine', expectedSoftwareType: 'machine' },
|
||||
{ nodeName: 'valve', expectedSoftwareType: 'valve' }
|
||||
];
|
||||
|
||||
for (const testNode of testNodes) {
|
||||
console.log(`\n=== Test: ${testNode.nodeName} → ${testNode.expectedSoftwareType} ===`);
|
||||
|
||||
// Initialize DynamicAssetMenu met automatische softwareType
|
||||
const menu = new DynamicAssetMenu(testNode.nodeName, api);
|
||||
console.log(`✅ Automatic softwareType for ${testNode.nodeName}:`, menu.softwareType);
|
||||
|
||||
try {
|
||||
await menu.init();
|
||||
console.log('Preloaded suppliers:', menu.data.suppliers.map(v=>v.name || v));
|
||||
} catch (e) {
|
||||
console.error(`DynamicAssetMenu.init() error for ${testNode.nodeName}:`, e.message);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`=== Sequential dropdown simulation for ${testNode.nodeName} ===`);
|
||||
|
||||
// 1. Select supplier
|
||||
const supplier = menu.data.suppliers[0];
|
||||
const supplierName = supplier.name || supplier;
|
||||
console.log('Selected supplier:', supplierName);
|
||||
|
||||
// 2. ✅ Gebruik automatische softwareType in plaats van dropdown
|
||||
const automaticCategory = menu.softwareType;
|
||||
console.log('Automatic category (softwareType):', automaticCategory);
|
||||
|
||||
// 3. ✅ Direct naar models met supplier + automatische category
|
||||
let models;
|
||||
try {
|
||||
console.log(`📡 Loading models for supplier: "${supplierName}", category: "${automaticCategory}"`);
|
||||
models = await api.getProductModels(supplierName, automaticCategory);
|
||||
console.log('Fetched models:', models.map(m=>m.name || m));
|
||||
|
||||
if (models.length === 0) {
|
||||
console.warn(`⚠️ No models found for ${supplierName} + ${automaticCategory}`);
|
||||
continue;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`getProductModels error for ${supplierName} + ${automaticCategory}:`, e.message);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 4. Extract unique types from models
|
||||
const types = Array.from(new Set(models.map(m => m.product_model_type || m.type || 'Unknown')));
|
||||
console.log('Available types:', types);
|
||||
|
||||
if (types.length === 0) {
|
||||
console.warn('⚠️ No types found in models');
|
||||
continue;
|
||||
}
|
||||
|
||||
// 5. Choose first type
|
||||
const selectedType = types[0];
|
||||
console.log('Selected type:', selectedType);
|
||||
|
||||
// 6. Filter models by type
|
||||
const filteredModels = models.filter(m =>
|
||||
(m.product_model_type || m.type) === selectedType
|
||||
);
|
||||
console.log('Models for selected type:', filteredModels.map(m => m.name || m));
|
||||
|
||||
if (filteredModels.length === 0) {
|
||||
console.warn('⚠️ No models found for selected type');
|
||||
continue;
|
||||
}
|
||||
|
||||
// 7. Choose first model and show units
|
||||
const model = filteredModels[0];
|
||||
console.log('Selected model:', model.name || model);
|
||||
|
||||
const units = model.product_model_meta ? Object.keys(model.product_model_meta) : [];
|
||||
console.log('Available units:', units);
|
||||
const unit = units[0] || 'N/A';
|
||||
console.log('Selected unit:', unit);
|
||||
|
||||
console.log(`✅ Complete flow for ${testNode.nodeName}:`);
|
||||
console.log(` Supplier: ${supplierName}`);
|
||||
console.log(` Category: ${automaticCategory} (automatic)`);
|
||||
console.log(` Type: ${selectedType}`);
|
||||
console.log(` Model: ${model.name || model}`);
|
||||
console.log(` Unit: ${unit}`);
|
||||
}
|
||||
|
||||
console.log('\n=== Test verschillende softwareTypes ===');
|
||||
|
||||
// Test of de API verschillende categories ondersteunt
|
||||
const testCategories = ['Sensor', 'machine', 'valve', 'pump'];
|
||||
const testSupplier = 'Vega'; // Bijvoorbeeld
|
||||
|
||||
for (const category of testCategories) {
|
||||
try {
|
||||
console.log(`\n📡 Testing category: ${category} with supplier: ${testSupplier}`);
|
||||
const models = await api.getProductModels(testSupplier, category);
|
||||
console.log(`✅ Found ${models.length} models for ${testSupplier} + ${category}`);
|
||||
|
||||
if (models.length > 0) {
|
||||
const sampleModel = models[0];
|
||||
console.log(` Sample model:`, sampleModel.name || sampleModel);
|
||||
|
||||
const types = Array.from(new Set(models.map(m => m.product_model_type || m.type)));
|
||||
console.log(` Available types:`, types);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`⚠️ No models found for ${testSupplier} + ${category}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n=== Klaar met alle tests ===');
|
||||
})();
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user