Skip to content

Commit

Permalink
Fix Point discovery to correctly map units facets
Browse files Browse the repository at this point in the history
  • Loading branch information
afrankvt committed Jul 22, 2021
1 parent 3f95877 commit 126a131
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Version 0.4 (working)
* Fix Point discovery to correctly map `units` facets

## Version 0.3 (17-Jun-2021)
* Add Point discovery support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
package io.novant.point;

import java.util.*;

import javax.baja.control.*;
import javax.baja.nre.util.Array;
import javax.baja.nre.annotations.*;
import javax.baja.registry.TypeInfo;
import javax.baja.units.*;
import javax.baja.status.*;
import javax.baja.sys.*;

import com.tridium.ndriver.discover.BNPointDiscoveryLeaf;
import com.tridium.ndriver.util.SfUtil;

import io.novant.util.*;

/**
* BNovantPointDiscoveryLeaf is container class for point elements to
* display in point discovery pane and pass to new point callback.
Expand Down Expand Up @@ -259,7 +263,7 @@ public void updateTarget(BComponent target)
BNovantProxyExt ext = new BNovantProxyExt();

String unit = getUnit();
if (unit.length() > 0) f = BFacets.make(f, "unit", BString.make(unit));
if (unit.length() > 0) f = BFacets.make(f, "units", NovantUnits.get(unit));

ext.setPointId(getPointId());
p.setProxyExt(ext);
Expand Down
52 changes: 52 additions & 0 deletions novant/novant-rt/src/io/novant/util/NovantUnits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright (c) 2021, Novant LLC
// Licensed under the MIT License
//
// History:
// 21 Jul 2021 Andy Frank Creation
//

package io.novant.util;

import java.util.*;
import javax.baja.units.*;

/**
* NovantUnits provides unit conversion to BUnit.
*/
public final class NovantUnits
{
/** Lookup a BUnit by symbol, or create a new BUnit if not found. */
public static BUnit get(String symbol)
{
if (map == null) loadMap();

BUnit unit = (BUnit)map.get(symbol);
if (unit == null)
{
unit = BUnit.make(symbol, symbol, BDimension.DEFAULT);
map.put(symbol, unit);
}

return unit;
}

/** Load reverse lookup map via UnitDatabase. */
private static void loadMap()
{
map = new HashMap();
UnitDatabase.Quantity[] quants = UnitDatabase.getDefault().getQuantities();
for (int i=0; i<quants.length; i++)
{
UnitDatabase.Quantity q = quants[i];
BUnit[] units = q.getUnits();
for (int j=0; j<units.length; j++)
{
BUnit u = units[j];
map.put(u.getSymbol(), u);
}
}
}

private static HashMap map;
}
57 changes: 57 additions & 0 deletions novant/novant-rt/srcTest/test/io/novant/BUnitsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (c) 2021, Novant LLC
// Licensed under the MIT License
//
// History:
// 22 Jul 2021 Andy Frank Creation
//

package test.io.novant;

import java.io.*;
import java.util.*;

import javax.baja.nre.annotations.*;
import javax.baja.sys.*;
import javax.baja.test.BTestNg;
import javax.baja.units.*;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.novant.util.*;

/** BUnitsTest */
@NiagaraType
public class BUnitsTest extends BTestNg
{

/*+ ------------ BEGIN BAJA AUTO GENERATED CODE ------------ +*/

////////////////////////////////////////////////////////////////
// Type
////////////////////////////////////////////////////////////////

@Override
public Type getType() { return TYPE; }
public static final Type TYPE = Sys.loadType(BUnitsTest.class);

/*+ ------------ END BAJA AUTO GENERATED CODE -------------- +*/

@BeforeMethod public void beforeMethod() {}
@AfterMethod public void afterMethod() {}

@Test public void test() throws IOException
{
// test existing
verifyEq(NovantUnits.get("kW"), BUnit.getUnit("kilowatt"));
verifyEq(NovantUnits.get("Pa"), BUnit.getUnit("pascal"));
verifyEq(NovantUnits.get("N-s"), BUnit.getUnit("newton second"));

// test make new
verifyEq(NovantUnits.get("foo"), BUnit.make("foo", "foo", BDimension.DEFAULT));
verifyEq(NovantUnits.get("bar"), BUnit.make("bar", "bar", BDimension.DEFAULT));
}
}

0 comments on commit 126a131

Please sign in to comment.