}
private void fileEvent() {
//Should never happen
if(this.file == null) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailedFileNotSetup"));
return;
}
//The file is modified or we've just started, so read it.
try{
BufferedReader reader = new BufferedReader(new FileReader(this.file));
String msg;
if(!this.dataPoints.isEmpty()) {
//TODO optimize to be better than numLines*numPoints
while( (msg = reader.readLine()) != null) {
//Give all points the chance to find their data
for(DataPointRT dp: this.dataPoints){
AsciiFilePointLocatorRT pl = dp.getPointLocator();
AsciiFilePointLocatorVO plVo = pl.getVo();
Pattern pointValuePattern = Pattern.compile(plVo.getValueRegex());
Matcher pointValueMatcher = pointValuePattern.matcher(msg); //Use the index from the above message
if(pointValueMatcher.find()){
if(plVo.getPointIdentifier().equals(pointValueMatcher.group(plVo.getPointIdentifierIndex()))) {
String value = pointValueMatcher.group(plVo.getValueIndex());
PointValueTime newValue;
Date dt;
if(plVo.getHasTimestamp() && !plVo.getTimestampFormat().equals(".")) {
SimpleDateFormat fmt = new SimpleDateFormat(plVo.getTimestampFormat());
dt = fmt.parse(pointValueMatcher.group(plVo.getTimestampIndex()));
}
else if(plVo.getHasTimestamp()) {
dt = new Date(Long.parseLong(pointValueMatcher.group(plVo.getTimestampIndex())));
}
else {
dt = new Date();
}
//Switch on the type
switch(plVo.getDataTypeId()){
case DataTypes.ALPHANUMERIC:
newValue = new PointValueTime(value, dt.getTime());
break;
case DataTypes.NUMERIC:
newValue = new PointValueTime(Double.parseDouble(value), dt.getTime());
break;
case DataTypes.MULTISTATE:
newValue = new PointValueTime(Integer.parseInt(value), dt.getTime());
break;
case DataTypes.BINARY:
newValue = new PointValueTime(Boolean.parseBoolean(value), dt.getTime());
break;
default:
throw new ShouldNeverHappenException("Uknown Data type for point");
}
if(!plVo.getHasTimestamp())
dp.updatePointValue(newValue);
else
dp.savePointValueDirectToCache(newValue, null, true, true);
}
}
}
}
}
reader.close();
returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
}catch ( FileNotFoundException e ){
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.fileNotFound",e.getMessage()));
} catch (IOException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailed",e.getMessage()));
} catch (NumberFormatException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.notNumber",e.getMessage()));
} catch (ParseException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.dateParseFailed",e.getMessage()));
}
}