Skip to content

Commit

Permalink
Added formatSensorValue to API and loosened IP address constaint on c…
Browse files Browse the repository at this point in the history
…ookie
  • Loading branch information
1060NetKernel committed Jul 28, 2018
1 parent 5721bd7 commit c2abdc0
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 72 deletions.
6 changes: 6 additions & 0 deletions module/urn.io.polestar/src/io/polestar/api/IPolestarAPI.java
Expand Up @@ -16,6 +16,12 @@ public interface IPolestarAPI
*/
Object getSensorValue(String aSensorId) throws NKFException;

/** Get the current value of a sensor and format as string according to specification for this sensor
* @param aSensorId the sensor to get the value of
* @throws NKFException if sensor doesn't exist
*/
String formatSensorValue(String aSensorId) throws NKFException;

/** Get the current error of a sensor
* @param aSensorId the sensor to get the error for
* @return null if no error is current for sensor or a java.lang.String error message
Expand Down
Expand Up @@ -18,6 +18,7 @@
import io.polestar.api.IPolestarContext;
import io.polestar.api.IPolestarQuery;
import io.polestar.api.QueryType;
import io.polestar.view.sensors.SensorViewAccessor;

public class PolestarContext implements InvocationHandler, IPolestarAPI
{
Expand Down Expand Up @@ -59,6 +60,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl

private IHDSReader mSensorState;
private IHDSReader mScriptState;
private IHDSReader mSensorConfig;

private IHDSReader getSensorState() throws NKFException
{ if (mSensorState==null)
Expand All @@ -67,6 +69,21 @@ private IHDSReader getSensorState() throws NKFException
return mSensorState;
}

private IHDSReader getSensorDefinitions() throws NKFException
{ if (mSensorConfig==null)
{ mSensorConfig=mContext.source("active:polestarSensorConfig",IHDSDocument.class).getReader();
}
return mSensorConfig;
}

private IHDSReader getSensorDefinition(String aSensor) throws NKFException
{ IHDSReader sensor=getSensorDefinitions().getFirstNodeOrNull("key('byId','"+aSensor+"')");
if (sensor==null)
{ throw new NKFException("Unknown Sensor","sensor ["+aSensor+"] does not exist");
}
return sensor;
}

public boolean sensorExists(String aSensorId) throws NKFException
{ IHDSReader sensor=getSensorState().getFirstNodeOrNull("key('byId','"+aSensorId+"')");
return sensor!=null;
Expand All @@ -86,6 +103,20 @@ public Object getSensorValue(String aSensorId) throws NKFException
{ IHDSReader sensorState=getSensorState(aSensorId);
return sensorState.getFirstValue("value");
}

@Override
public String formatSensorValue(String aSensorId) throws NKFException
{ Object value=getSensorValue(aSensorId);
IHDSReader sensorDef=getSensorDefinition(aSensorId);
String format=(String)sensorDef.getFirstValueOrNull("format");
String s=SensorViewAccessor.getHumanReadable(value, format)[0];
String units=(String)sensorDef.getFirstValueOrNull("units");
if (units!=null && units.length()>0)
{ s=s+" "+units;
}
return s;
}


@Override
public String getSensorError(String aSensorId) throws NKFException
Expand Down
Expand Up @@ -239,7 +239,7 @@ public void onReadingCheck(INKFRequestContext aContext) throws Exception
}
}
if (anyErrorChange)
{ aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE onReadingCheck() anyErrorChange");
{ //aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE onReadingCheck() anyErrorChange");
mChanges.put(SENSOR_ERROR, SENSOR_ERROR);
}

Expand Down Expand Up @@ -353,7 +353,7 @@ public void onUpdate(IHDSReader aState,INKFRequestContext aContext) throws Excep
storeSensorState(sensorId,newValue,now,col,aContext);
}
if (ss.getErrorLastModified()==now)
{ aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE onUpdate() "+(ss.getError()!=null)+" "+sensorDef.getFirstValue("name"));
{ //aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE onUpdate() "+(ss.getError()!=null)+" "+sensorDef.getFirstValue("name"));
mChanges.put(SENSOR_ERROR, SENSOR_ERROR);
}
}
Expand Down
Expand Up @@ -158,12 +158,12 @@ public void poll(IHDSReader sensorDef, long aNow, INKFRequestContext aContext)
{ mErrorLastModifiedPublic=aNow;
mErrorPublic=null;
//System.out.println("error cleared");
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear "+sensorDef.getFirstValue("name"));
}
else
{ mErrorLastModified=mErrorLastModifiedPublic;
//System.out.println("error cleared but not different");
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear but not modified "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() clear but not modified "+sensorDef.getFirstValue("name"));
}
}
else
Expand All @@ -173,18 +173,18 @@ public void poll(IHDSReader sensorDef, long aNow, INKFRequestContext aContext)
{ mErrorLastModifiedPublic=aNow;
mErrorPublic=errorInternal;
//System.out.println("error made public");
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error made public "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error made public "+sensorDef.getFirstValue("name"));
}
else
{ mErrorLastModified=mErrorLastModifiedPublic;
//System.out.println("error made public but not different");
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() public but not different "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() public but not different "+sensorDef.getFirstValue("name"));
}

}
else
{ //System.out.println("error but not yet");
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error but not yet public "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error but not yet public "+sensorDef.getFirstValue("name"));
}
}
}
Expand All @@ -194,7 +194,7 @@ public void poll(IHDSReader sensorDef, long aNow, INKFRequestContext aContext)
if (isDifferent(errorInternal, mErrorPublic))
{ mErrorLastModifiedPublic=mErrorLastModified;
mErrorPublic=getErrorInternal();
aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error "+(mErrorPublic!=null)+" "+sensorDef.getFirstValue("name"));
//aContext.logRaw(INKFLocale.LEVEL_INFO, "ERR_CHANGE poll() error "+(mErrorPublic!=null)+" "+sensorDef.getFirstValue("name"));
}

}
Expand Down
Expand Up @@ -97,6 +97,17 @@ public static void assertAdmin(INKFRequestContext aContext) throws NKFException
}
}

public static Cookie createRememberMeCookie(String aUsername, String aPasswordHash, long aDuration, INKFRequestContext aContext) throws Exception
{ String hostname=getHostname(aContext);
Cookie c=new RememberMeCookie("/polestar/", System.currentTimeMillis()+aDuration, aUsername, aPasswordHash, hostname, aContext);
return c;
}

private static String getHostname(INKFRequestContext aContext) throws Exception
{ //return aContext.source("httpRequest:/remote-host",String.class);
return "whocares";
}


public static String onRememberMeLogin(INKFRequestContext aContext) throws Exception
{ String result=null;
Expand All @@ -107,7 +118,7 @@ public static String onRememberMeLogin(INKFRequestContext aContext) throws Excep
IHDSNode user=getUserDetails(result, aContext);
if (user!=null)
{ String password=(String)user.getFirstValue("password");
String hostname=aContext.source("httpRequest:/remote-host",String.class);
String hostname=getHostname(aContext);
if (rmc.isValid(password,hostname,aContext))
{ //valid remember me login
aContext.sink("session:/username",result);
Expand Down
Expand Up @@ -79,14 +79,12 @@ private void onLogin(INKFRequestContext aContext) throws Exception
{ aContext.sink("session:/username", username);
String role=(String)user.getFirstValue("role");
aContext.sink("session:/role",role);


if (remember)
{ //setup remember me cookie
long duration=7*24*60*60000; // 1week
String hostname = aContext.source("httpRequest:/remote-host",String.class);
String passwordHash = (String)user.getFirstValue("password");
Cookie c=new RememberMeCookie("/polestar/", System.currentTimeMillis()+duration, username, passwordHash, hostname, aContext);
Cookie c=MonitorUtils.createRememberMeCookie(username, passwordHash, duration, aContext);
aContext.sink("httpResponse:/cookie", c);
}

Expand Down
Expand Up @@ -240,74 +240,84 @@ private IHDSMutator getFilteredList(String aFilter, INKFRequestContext aContext)

String format=(String)sensorDef.getFirstValueOrNull("format");
Object value=sensorNode.getFirstValue("value");
String valueHuman;
String valueHTML=null;
try
{
if (format!=null && value!=null)
{ if ("open".equals(format))
{ valueHuman=((Boolean)value)?"open":"closed";
}
else if ("on".equals(format))
{ valueHuman=((Boolean)value)?"on":"off";
}
else if ("rgb".equals(format))
{ valueHuman=null;
Map m=(Map)value;
double r=((Number)m.get("r")).doubleValue();
double g=((Number)m.get("g")).doubleValue();
double b=((Number)m.get("b")).doubleValue();
String rgb="rgb("+((int)(r*255.9))+","+((int)(g*255.9))+","+((int)(b*255.9))+")";
valueHTML="<div class='rgb' style='background: "+rgb+"'/>";
}
else if (format.contains("%"))
{
if (value instanceof Number)
{
try
{ valueHuman=String.format(format,value);
} catch (IllegalFormatConversionException e)
{ valueHuman=value.toString();
}
}
else if (value instanceof Map)
{ StringBuilder sb=new StringBuilder();
Map<Object,Object> bdo=(Map)value;
boolean first=true;
for (Map.Entry entry : bdo.entrySet())
{ if (first) first=false; else sb.append(", ");
sb.append(entry.getKey());
sb.append(": ");
sb.append(String.format(format,entry.getValue()));
}
valueHuman=sb.toString();
}
else
{ valueHuman="?"+value.getClass();
}
}
else

String[] vhh=getHumanReadable(value, format);
String valueHuman=vhh[0];
String valueHTML=vhh[1];

sensorNode.addNode("valueHuman", valueHuman);
if (valueHTML!=null) sensorNode.addNode("valueHTML", valueHTML);
}
}
return state;
}

public static String[] getHumanReadable(Object value, String format)
{
String valueHuman=null;
String valueHTML=null;
try
{
if (format!=null && value!=null)
{ if ("open".equals(format))
{ valueHuman=((Boolean)value)?"open":"closed";
}
else if ("on".equals(format))
{ valueHuman=((Boolean)value)?"on":"off";
}
else if ("rgb".equals(format))
{ valueHuman=null;
Map m=(Map)value;
double r=((Number)m.get("r")).doubleValue();
double g=((Number)m.get("g")).doubleValue();
double b=((Number)m.get("b")).doubleValue();
String rgb="rgb("+((int)(r*255.9))+","+((int)(g*255.9))+","+((int)(b*255.9))+")";
valueHTML="<div class='rgb' style='background: "+rgb+"'/>";
}
else if (format.contains("%"))
{
if (value instanceof Number)
{
try
{ valueHuman=String.format(format,value);
} catch (IllegalFormatConversionException e)
{ valueHuman=value.toString();
}
}
else
{ if (value==null)
{ valueHuman="NULL";
}
else
{ valueHuman=value.toString();
else if (value instanceof Map)
{ StringBuilder sb=new StringBuilder();
Map<Object,Object> bdo=(Map)value;
boolean first=true;
for (Map.Entry entry : bdo.entrySet())
{ if (first) first=false; else sb.append(", ");
sb.append(entry.getKey());
sb.append(": ");
sb.append(String.format(format,entry.getValue()));
}
valueHuman=sb.toString();
}
else
{ valueHuman="?"+value.getClass();
}
}
catch (Exception e)
{ valueHuman= value.toString();
e.printStackTrace();
else
{ valueHuman=value.toString();
}
}
else
{ if (value==null)
{ valueHuman="NULL";
}
else
{ valueHuman=value.toString();
}
sensorNode.addNode("valueHuman", valueHuman);
if (valueHTML!=null) sensorNode.addNode("valueHTML", valueHTML);
}
}
return state;
catch (Exception e)
{ valueHuman= value.toString();
e.printStackTrace();
}
return new String[] {valueHuman,valueHTML};
}

public void onList(INKFRequestContext aContext) throws Exception
Expand Down
2 changes: 1 addition & 1 deletion module/urn.io.polestar/src/module.xml
Expand Up @@ -17,7 +17,7 @@
<meta>
<identity>
<uri>urn:io:polestar</uri>
<version>0.5.2</version>
<version>0.5.3</version>
</identity>
<info>
<name>Polestar IoT Hub</name>
Expand Down

0 comments on commit c2abdc0

Please sign in to comment.