I would like to get ideas for best practice in Perspective for the following scenario:
- every gateway deployment has a unit conversion table
- all tags in the project have their units labeled in the "Engineering Units" field
- like to be able to drop in parameterized views (templates) for numeric display and numeric entry. All values will be displayed in local units
- user can add or modify the unit conversion table at any time from any session and all values in all sessions will be updated automatically to the local units
- (wish) Allow individual sessions to select their unit conversion tables so groups in different geographical areas looking at the same system can view in the units of their choice)
Note: we did this already in Vision by creating a client tag that contained a dictionary of lookup values for the conversion:
Table columns: base unit, local unit, m, b
Where base unit is the text string found in the tags "Engineering units " value *(example meters)
Local unit is the unit to convert to (example: feet )
m is the slope of the conversion (meters to feet is a value of 3.28, C to F is a value of 1.8)
b is the offset for the conversion (meters to feet is a value of 0, C to F is a value of 32
Every time the tag value changes we applied the equation through a property change script:
localValue = baseValue * m + b
Every time the baseUnit changed, or the dictionary changed or the component was loaded we updated the value of m and b then recalculated the localValue by the equation in the previous line.
For speed we loaded the conversion table from a dataset tag into a persistent dictionary variable created in a gateway script. The dictionary on the client side was only updated when the base dictionary was edited.
# local variable - dictionary to hold all unit table item objects _unitDict = dict()
For numeric entry we created a special template that would display a popup. On closing the value would be converted back to the base units and written to the tag. (cumbersome but it works, no direct entry in numeric input component)
All the functions for unit table lookup and editing were put in a single project script. These scripts were called to get the value of m, b and localUnit when various events occurred in our templates.
A few questions:
- Are transforms the best place to do the unit conversions ?
- Will creating a session property with the unit conversion dictionary speed things up in perspective ? My understanding of where Perspective stores values is unclear, but if it is like other web based systems it would seem the dictionary lookup would be done on the server and not provide any value.
- Is there any built in system for doing this conversion ? I don't want to use the engineering unit conversion values in the native tag. We have systems all over the world that share the same tag set so maintaining a different tag set for each system is not reasonable. I don't want to write a script that modifies them either, too hard to validate each time there is a change.
- In some frameworks there is an event called "Validating" and "Validated" which provides a place to do unit conversion after the user has entered a value. Is there anything similar in Perspective ?