136 lines
2.7 KiB
Java
136 lines
2.7 KiB
Java
package org.hwo.servicelink;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Hashtable;
|
|
|
|
public class ServiceRegisterCache {
|
|
|
|
public class BaseCacheItem
|
|
{
|
|
Integer ax,
|
|
node,
|
|
register;
|
|
|
|
long lastReadTime;
|
|
|
|
public BaseCacheItem(int ax,int node,int register)
|
|
{
|
|
this.ax = ax;
|
|
this.node = node;
|
|
this.register = register;
|
|
}
|
|
|
|
long age()
|
|
{
|
|
return System.currentTimeMillis() - lastReadTime;
|
|
}
|
|
|
|
boolean isOld()
|
|
{
|
|
return age() > 250;
|
|
}
|
|
}
|
|
|
|
public class IntegerCacheItem extends BaseCacheItem
|
|
{
|
|
public IntegerCacheItem(int ax,int node,int register)
|
|
{
|
|
super(ax,node,register);
|
|
intValue = null;
|
|
}
|
|
Integer intValue;
|
|
|
|
Integer intValue()
|
|
{
|
|
if (isOld() && serviceLink.isOpen())
|
|
{
|
|
try {
|
|
intValue = serviceLink.readInt(ax.byteValue(),node.byteValue(),register);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
intValue = null;
|
|
}
|
|
lastReadTime = System.currentTimeMillis();
|
|
}
|
|
return intValue;
|
|
}
|
|
}
|
|
|
|
public class FloatCacheItem extends BaseCacheItem
|
|
{
|
|
public FloatCacheItem(int ax,int node,int register)
|
|
{
|
|
super(ax,node,register);
|
|
floatValue = 0.0f;
|
|
}
|
|
Float floatValue;
|
|
|
|
Float floatValue()
|
|
{
|
|
if (isOld() && serviceLink.isOpen())
|
|
{
|
|
try {
|
|
floatValue = serviceLink.readFloat(ax.byteValue(),node.byteValue(),register);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
floatValue = null;
|
|
}
|
|
lastReadTime = System.currentTimeMillis();
|
|
}
|
|
return floatValue;
|
|
}
|
|
|
|
}
|
|
|
|
int calcHash(int ax,int node,int register)
|
|
{
|
|
return (ax << 20)|(node << 16)|register;
|
|
}
|
|
|
|
ServiceLink serviceLink;
|
|
|
|
Hashtable<Integer, IntegerCacheItem>
|
|
integerCache;
|
|
Hashtable<Integer, FloatCacheItem>
|
|
floatCache;
|
|
|
|
ServiceRegisterCache(ServiceLink serviceLink)
|
|
{
|
|
this.serviceLink = serviceLink;
|
|
this.integerCache = new Hashtable<Integer, ServiceRegisterCache.IntegerCacheItem>();
|
|
this.floatCache = new Hashtable<Integer, ServiceRegisterCache.FloatCacheItem>();
|
|
}
|
|
|
|
public synchronized Integer getCachedInteger(int ax,int node,int register)
|
|
{
|
|
int hash = calcHash(ax, node, register);
|
|
IntegerCacheItem ici = integerCache.get(hash);
|
|
if (ici == null)
|
|
{
|
|
ici = new IntegerCacheItem(ax,node,register);
|
|
integerCache.put(hash, ici);
|
|
}
|
|
return ici.intValue();
|
|
}
|
|
|
|
public synchronized Float getCachedFloat(int ax,int node,int register)
|
|
{
|
|
int hash = calcHash(ax, node, register);
|
|
FloatCacheItem ici = floatCache.get(hash);
|
|
if (ici == null)
|
|
{
|
|
ici = new FloatCacheItem(ax,node,register);
|
|
floatCache.put(hash, ici);
|
|
}
|
|
return ici.floatValue();
|
|
}
|
|
|
|
public synchronized void invalidate(int ax,int node,int register)
|
|
{
|
|
int hash = calcHash(ax, node, register);
|
|
integerCache.remove(hash);
|
|
floatCache.remove(hash);
|
|
}
|
|
|
|
}
|