use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class M2MReportDao method getMailingListXid.
/**
* @param pointId
* @return
*/
public String getMailingListXid(int listId) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = this.connection.createStatement();
stmt.execute(MAILING_LIST_XID_SELECT + " where id = " + listId);
rs = stmt.getResultSet();
if (rs.next()) {
return rs.getString(1);
} else
return null;
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
throw new ShouldNeverHappenException(e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
}
}
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class ReportChartCreator method createContent.
/**
* Uses the given parameters to create the data for the fields of this class. Once the content has been created the
* getters for the fields can be used to retrieve.
*
* @param host - Mango's hostname
* @param port - Mango's port
* @param reportInstance
* @param reportDao
* @param inlinePrefix
* if this is non-null, it implies that the content should be inline.
* @param createExportFile
*/
public void createContent(String host, int port, ReportInstance reportInstance, ReportDao reportDao, String inlinePrefix, boolean createExportFile) {
this.inlinePrefix = inlinePrefix;
reportInstance.setTranslations(translations);
// Use a stream handler to get the report data from the database.
StreamHandler handler = new StreamHandler(host, port, reportInstance.getXidMap(), reportInstance.getReportStartTime(), reportInstance.getReportEndTime(), IMAGE_WIDTH, createExportFile, translations);
// Process the report content with the handler.
if (Common.databaseProxy.getNoSQLProxy() == null)
reportDao.reportInstanceDataSQL(reportInstance.getId(), handler);
else
reportDao.reportInstanceDataNoSQL(reportInstance.getId(), handler);
pointStatistics = handler.getPointStatistics();
devices = handler.getDevices();
pointMap = handler.getStatisticsMap();
UsedImagesDirective inlineImages = new UsedImagesDirective();
SubjectDirective subjectDirective = new SubjectDirective(translations);
// Prepare the model for the content rendering.
Map<String, Object> model = new HashMap<String, Object>();
model.put("fmt", new MessageFormatDirective(translations));
model.put("subject", subjectDirective);
model.put("img", inlineImages);
model.put("instance", reportInstance);
model.put("timezone", timeZone.getID());
model.put("points", pointStatistics);
model.put("inline", inlinePrefix == null ? "" : "cid:");
model.put("devices", devices);
model.put("mapped", pointMap);
model.put("ALPHANUMERIC", DataTypes.ALPHANUMERIC);
model.put("BINARY", DataTypes.BINARY);
model.put("MULTISTATE", DataTypes.MULTISTATE);
model.put("NUMERIC", DataTypes.NUMERIC);
model.put("IMAGE", DataTypes.IMAGE);
// Create the individual point charts
for (PointStatistics pointStat : pointStatistics) {
PointTimeSeriesCollection ptsc = new PointTimeSeriesCollection(timeZone);
if (pointStat.getNumericTimeSeries() != null)
ptsc.addNumericTimeSeries(pointStat.getNumericTimeSeries());
else if (pointStat.getDiscreteTimeSeries() != null)
ptsc.addDiscreteTimeSeries(pointStat.getDiscreteTimeSeries());
if (ptsc.hasData()) {
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + pointStat.getChartName());
pointStat.setImageData(ImageChartUtils.getChartData(ptsc, POINT_IMAGE_WIDTH, POINT_IMAGE_HEIGHT, reportInstance.getReportStartTime(), reportInstance.getReportEndTime()));
}
// in the report I'll add it here while we are already iterating over the points that are included in the report
if (pointStat.getDataType() == DataTypes.IMAGE) {
ValueChangeCounter pointStatisticsGenerator = (ValueChangeCounter) pointStat.getStats();
ImageValue img = (ImageValue) (pointStatisticsGenerator.getLastValue());
if (img != null) {
try {
pointStat.setImageData(img.getImageData());
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + pointStat.getChartName());
else {
// serve up the image using the reportImageChart servlet instead of the imageValueServlet that is used on flipbook page
// The path comes from the servlet path definition in web.xml.
model.put("chartName", IMAGE_SERVLET + pointStat.getChartName());
}
} catch (IOException e) {
LOG.error("failed to retrieve image data", e);
}
}
}
}
// consolidated chart
PointTimeSeriesCollection ptsc = handler.getPointTimeSeriesCollection();
if (ptsc.hasData()) {
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + IMAGE_CONTENT_ID);
else {
chartName = "r" + reportInstance.getId() + ".png";
// The path comes from the servlet path definition in web.xml.
model.put("chartName", IMAGE_SERVLET + chartName);
}
imageData = ImageChartUtils.getChartData(ptsc, true, IMAGE_WIDTH, IMAGE_HEIGHT, reportInstance.getReportStartTime(), reportInstance.getReportEndTime());
}
List<EventInstance> events = null;
if (reportInstance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
events = reportDao.getReportInstanceEvents(reportInstance.getId());
model.put("includeEvents", true);
model.put("events", events);
} else
model.put("includeEvents", false);
List<ReportUserComment> comments = null;
if (reportInstance.isIncludeUserComments()) {
comments = reportDao.getReportInstanceUserComments(reportInstance.getId());
// Only provide the list of point comments to the report. The event comments have already be correlated
// into the events list.
List<ReportUserComment> pointComments = new ArrayList<ReportUserComment>();
for (ReportUserComment c : comments) {
if (c.getCommentType() == UserCommentVO.TYPE_POINT)
pointComments.add(c);
}
model.put("includeUserComments", true);
model.put("userComments", pointComments);
} else
model.put("includeUserComments", false);
// Create the template.
Template ftl;
StringWriter writer = new StringWriter();
FileReader reader = null;
try {
File templateFile = ReportCommon.instance.getTemplateFile(reportInstance.getTemplateFile());
reader = new FileReader(templateFile);
ftl = new Template(reportInstance.getName(), reader, Common.freemarkerConfiguration);
ftl.process(model, writer);
} catch (FileNotFoundException e) {
LOG.error("Unable to find report template file: " + reportInstance.getName());
} catch (IOException e) {
// Couldn't load the template?
throw new ShouldNeverHappenException(e);
} catch (Exception e) {
// Error processing the FTL?
throw new ShouldNeverHappenException(e);
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
LOG.error("Error closing template file reader: " + e.getMessage(), e);
}
}
// Save the content
html = writer.toString();
subject = subjectDirective.getSubject();
inlineImageList = inlineImages.getImageList();
// Save the export file (if any)
exportFile = handler.exportFile;
if (createExportFile && events != null) {
try {
eventFile = File.createTempFile("tempEventCSV", ".csv");
new EventCsvStreamer(new PrintWriter(new FileWriter(eventFile)), events, translations);
} catch (IOException e) {
LOG.error("Failed to create temp event file", e);
}
}
if (createExportFile && comments != null) {
try {
commentFile = File.createTempFile("tempCommentCSV", ".csv");
new UserCommentCsvStreamer(new PrintWriter(new FileWriter(commentFile)), comments, translations);
} catch (IOException e) {
LOG.error("Failed to create temp comment file", e);
}
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class ReportJob method scheduleReportJob.
public static void scheduleReportJob(String host, int port, ReportVO report) {
synchronized (JOB_REGISTRY) {
// Ensure that there is no existing job.
unscheduleReportJob(report);
if (report.isSchedule()) {
CronTimerTrigger trigger;
if (report.getSchedulePeriod() == ReportVO.SCHEDULE_CRON) {
try {
trigger = new CronTimerTrigger(report.getScheduleCron());
} catch (ParseException e) {
throw new ShouldNeverHappenException(e);
}
} else
trigger = Common.getCronTrigger(report.getSchedulePeriod(), report.getRunDelayMinutes() * 60);
ReportJob reportJob = new ReportJob(trigger, report, host, port);
JOB_REGISTRY.put(report.getId(), reportJob);
Common.timer.schedule(reportJob);
}
}
}
use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class ScheduledEventRT method createTrigger.
public TimerTrigger createTrigger(boolean activeTrigger) {
if (!activeTrigger && !vo.isReturnToNormal())
return null;
if (vo.getScheduleType() == ScheduledEventVO.TYPE_CRON) {
try {
if (activeTrigger)
return new CronTimerTrigger(vo.getActiveCron());
return new CronTimerTrigger(vo.getInactiveCron());
} catch (ParseException e) {
// Should never happen, so wrap and rethrow
throw new ShouldNeverHappenException(e);
}
}
if (vo.getScheduleType() == ScheduledEventVO.TYPE_ONCE) {
DateTime dt;
if (activeTrigger)
dt = new DateTime(vo.getActiveYear(), vo.getActiveMonth(), vo.getActiveDay(), vo.getActiveHour(), vo.getActiveMinute(), vo.getActiveSecond(), 0);
else
dt = new DateTime(vo.getInactiveYear(), vo.getInactiveMonth(), vo.getInactiveDay(), vo.getInactiveHour(), vo.getInactiveMinute(), vo.getInactiveSecond(), 0);
return new OneTimeTrigger(new Date(dt.getMillis()));
}
int month = vo.getActiveMonth();
int day = vo.getActiveDay();
int hour = vo.getActiveHour();
int minute = vo.getActiveMinute();
int second = vo.getActiveSecond();
if (!activeTrigger) {
month = vo.getInactiveMonth();
day = vo.getInactiveDay();
hour = vo.getInactiveHour();
minute = vo.getInactiveMinute();
second = vo.getInactiveSecond();
}
StringBuilder expression = new StringBuilder();
expression.append(second).append(' ');
expression.append(minute).append(' ');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_HOURLY)
expression.append("* * * ?");
else {
expression.append(hour).append(' ');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_DAILY)
expression.append("* * ?");
else if (vo.getScheduleType() == ScheduledEventVO.TYPE_WEEKLY)
expression.append("? * ").append(weekdays[day]);
else {
if (day > 0)
expression.append(day);
else if (day == -1)
expression.append('L');
else
expression.append(-day).append('L');
if (vo.getScheduleType() == ScheduledEventVO.TYPE_MONTHLY)
expression.append(" * ?");
else
expression.append(' ').append(month).append(" ?");
}
}
CronTimerTrigger cronTrigger;
try {
cronTrigger = new CronTimerTrigger(expression.toString());
} catch (ParseException e) {
// Should never happen, so wrap and rethrow
throw new ShouldNeverHappenException(e);
}
return cronTrigger;
}
use of com.serotonin.ShouldNeverHappenException in project ma-modules-public by infiniteautomation.
the class SerialDataSourceRT method setPointValueImplTransport.
private void setPointValueImplTransport(DataPointRT dataPoint, PointValueTime valueTime) throws IOException {
OutputStream os = this.port.getOutputStream();
if (os == null)
throw new IOException("Port is closed.");
// Create Message from Message Start
SerialPointLocatorRT pl = dataPoint.getPointLocator();
byte[] data;
if (this.vo.isHex()) {
// Convert to Hex
try {
switch(dataPoint.getDataTypeId()) {
case DataTypes.ALPHANUMERIC:
data = convertToHex(valueTime.getStringValue());
break;
case DataTypes.BINARY:
if (valueTime.getBooleanValue())
data = convertToHex("00");
else
data = convertToHex("01");
break;
case DataTypes.MULTISTATE:
String intValue = Integer.toString(valueTime.getIntegerValue());
if (intValue.length() % 2 != 0)
intValue = "0" + intValue;
data = convertToHex(intValue);
break;
case DataTypes.NUMERIC:
String numValue = Integer.toString(valueTime.getIntegerValue());
if (numValue.length() % 2 != 0)
numValue = "0" + numValue;
data = convertToHex(numValue);
break;
default:
throw new ShouldNeverHappenException("Unsupported data type" + dataPoint.getDataTypeId());
}
if (this.vo.isLogIO())
this.ioLog.log(false, data);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
raiseEvent(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.notHex"));
return;
}
} else {
// Pin the terminator on the end
String messageTerminator = ((SerialDataSourceVO) this.getVo()).getMessageTerminator();
// Do we need to or is it already on the end?
String identifier = pl.getVo().getPointIdentifier();
String fullMsg = identifier + valueTime.getStringValue();
if (!fullMsg.endsWith(messageTerminator)) {
fullMsg += messageTerminator;
}
// String output = newValue.getStringValue();
data = fullMsg.getBytes();
if (vo.isLogIO())
this.ioLog.log("O: " + fullMsg);
}
for (byte b : data) {
os.write(b);
}
os.flush();
}
Aggregations