Permalink
Browse files

Added support for sensors widget in Jfrontend plugin

  • Loading branch information...
mcicolella committed Jan 9, 2018
1 parent 297e6de commit d32465a24507d802f27e2f07f541f4c69e789fbf
@@ -36,14 +36,17 @@
public class ImageDrawer extends PlainDrawer {
private Protocol masterProtocol;
private String enableSensorsWidget;
/**
*
* @param masterProcotol
* @param masterProtocol
*/
public ImageDrawer(JavaDesktopFrontend masterProcotol) {
super(masterProcotol);
this.masterProtocol = masterProcotol;
public ImageDrawer(JavaDesktopFrontend masterProtocol, String enableSensorsWidget) {
super(masterProtocol);
this.masterProtocol = masterProtocol;
this.enableSensorsWidget = enableSensorsWidget;
}
/**
@@ -54,7 +57,7 @@ public void renderWalls() {
/*
* This method should not be invoked from ImageDrawer class.
* Throwing an UnsupportedOperationException, however, is not recommended due to backwards compatibility
*/
*/
}
/**
@@ -80,7 +83,12 @@ private void renderSingleObject(EnvObjectLogic obj) {
if ((obj.getPojo().getCurrentRepresentation().getIcon() != null)
&& !obj.getPojo().getCurrentRepresentation().getIcon().equalsIgnoreCase("")) {
try {
paintImage(obj.getPojo());
if ("true".equalsIgnoreCase(enableSensorsWidget)) {
Widget widget = new Widget(obj);
paintImage(widget.draw());
} else {
paintImage(obj.getPojo());
}
} catch (RuntimeException e) {
drawPlainObject(obj);
} finally {
@@ -141,7 +149,7 @@ private void paintObjectDescription(EnvObjectLogic obj) {
int y = (int) box.getY() + 10;
Callout callout
= new Callout(obj.getPojo().getName(), "object.description",
description.toString(), x, y, 0.0f, 2000);
description.toString(), x, y, 0.0f, 2000);
createCallout(callout);
setNeedRepaint(true);
}
@@ -53,12 +53,14 @@
private final Map<String, GraphPanel> graphs = new HashMap<>();
private SplashLogin sl;
private boolean init = false;
public final String ENABLE_SENSORS_WIDGET = configuration.getStringProperty("enable-sensors-widget", "false");
/**
*
*/
public JavaDesktopFrontend() {
super("Desktop Frontend", "/frontend-java/desktop-frontend.xml");
super("JFrontend", "/frontend-java/desktop-frontend.xml");
setPollingWait(-1); //disable threaded execution of onRun()
}
@@ -140,10 +142,10 @@ public MainWindow getMainWindow() {
protected Drawer createRenderer(EnvironmentLogic env) {
try {
if ("photo".equalsIgnoreCase(env.getPojo().getRenderer())) {
drawer = new PhotoDrawer(this);
drawer = new PhotoDrawer(this, ENABLE_SENSORS_WIDGET);
drawer.setCurrEnv(env);
} else if ("image".equalsIgnoreCase(env.getPojo().getRenderer())) {
drawer = new ImageDrawer(this);
drawer = new ImageDrawer(this, ENABLE_SENSORS_WIDGET);
drawer.setCurrEnv(env);
} else if ("plain".equalsIgnoreCase(env.getPojo().getRenderer())) {
drawer = new PlainDrawer(this);
@@ -153,7 +155,7 @@ protected Drawer createRenderer(EnvironmentLogic env) {
drawer.setCurrEnv(env);
}
} catch (Exception e) {
LOG.error("Error while initializing a drawer in desktop frontend ", Freedomotic.getStackTraceInfo(e));
LOG.error("Error while initializing a drawer in Jfrontend ", Freedomotic.getStackTraceInfo(e));
}
if (drawer instanceof Renderer) {
@@ -40,8 +40,8 @@
*
* @param master
*/
public PhotoDrawer(JavaDesktopFrontend master) {
super(master);
public PhotoDrawer(JavaDesktopFrontend master, String enableSensorsWidget) {
super(master, enableSensorsWidget);
}
/**
@@ -0,0 +1,109 @@
/**
*
* Copyright (c) 2009-2016 Freedomotic team http://freedomotic.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Freedomotic; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
package com.freedomotic.jfrontend;
import com.freedomotic.app.Freedomotic;
import com.freedomotic.core.ResourcesManager;
import com.freedomotic.things.EnvObjectLogic;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
/**
*
* @author Enrico Nicoletti
*/
public class Widget {
private final EnvObjectLogic obj;
/**
*
* @param obj
*/
public Widget(EnvObjectLogic obj) {
this.obj = obj;
}
/**
*
* @return
*/
public synchronized BufferedImage draw() {
try {
String name = obj.getPojo().getCurrentRepresentation().getIcon();
BufferedImage resource;
Graphics2D canvas;
switch (obj.getPojo().getSimpleType()) {
case "barometer":
resource = ResourcesManager.getResource(name, 96, 96);
canvas = resource.createGraphics();
canvas.setColor(Color.GRAY);
canvas.fillRect(0, 0, (int) 96, (int) 96);
canvas.setFont(new Font("Arial", Font.BOLD, 22));
canvas.setColor(Color.WHITE);
canvas.drawString(obj.getBehavior("pressure").getValueAsString() + " Pa", 7, 55);
break;
case "hygrometer":
resource = ResourcesManager.getResource(name, 96, 96);
canvas = resource.createGraphics();
canvas.setColor(Color.BLUE);
canvas.fillRect(0, 0, (int) 96, (int) 96);
canvas.setFont(new Font("Arial", Font.BOLD, 22));
canvas.setColor(Color.WHITE);
canvas.drawString(obj.getBehavior("humidity").getValueAsString() + " %", 7, 55);
break;
case "powermeter":
resource = ResourcesManager.getResource(name, 96, 96);
canvas = resource.createGraphics();
canvas.setColor(Color.ORANGE);
canvas.fillRect(0, 0, (int) 96, (int) 96);
canvas.setFont(new Font("Arial", Font.BOLD, 22));
canvas.setColor(Color.WHITE);
canvas.drawString(obj.getBehavior("voltage").getValueAsString() + " V", 7, 30);
canvas.drawString(obj.getBehavior("power").getValueAsString() + " W", 7, 58);
canvas.drawString(obj.getBehavior("current").getValueAsString() + " A", 7, 85);
break;
case "thermometer":
resource = ResourcesManager.getResource(name, 96, 96);
canvas = resource.createGraphics();
canvas.setColor(Color.RED);
canvas.fillRect(0, 0, (int) 96, (int) 96);
canvas.setFont(new Font("Arial", Font.BOLD, 22));
canvas.setColor(Color.WHITE);
canvas.drawString(obj.getBehavior("temperature").getValueAsString() + " C", 7, 55);
break;
default:
resource = ResourcesManager.getResource(name);
}
return resource;
} catch (Exception e) {
Freedomotic.getStackTraceInfo(e);
}
return null;
}
}
@@ -1,10 +1,11 @@
<config>
<properties>
<property name="description" value="A Java desktop frontend"/>
<property name="category" value="frontend"/>
<property name="short-name" value="javadesktop"/>
<property name="startup-time" value="on load"/>
<property name="automatic-reply-to-commands" value="false"/>
<property name="enable-i18n" value="true"/>
<property name="description" value="A Java desktop frontend"/>
<property name="category" value="frontend"/>
<property name="short-name" value="javadesktop"/>
<property name="startup-time" value="on load"/>
<property name="automatic-reply-to-commands" value="false"/>
<property name="enable-i18n" value="true"/>
<property name="enable-sensors-widget" value="true"/>
</properties>
</config>

0 comments on commit d32465a

Please sign in to comment.