add networkEffect

This commit is contained in:
Felix Klenner 2021-02-28 03:37:17 +01:00
parent a921f38645
commit 390dff442f
7 changed files with 155 additions and 74 deletions

View file

@ -4,30 +4,41 @@ import de.zuim.ledcontrol.effects.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class EffectManager { public class EffectManager {
int activeId = 4; private boolean sweep = false;
private final LEDEffect[] effects = new LEDEffect[]{new ClockEffect(),new AudioVolume(), new TemperatureEffect(), new SineEffect(), new AudioFFT(), new ColorSweep()};
private final LEDEffect[] effects = new LEDEffect[]{new ClockEffect(),new AudioVolumeEffect(), new NetworkSpeedEffect(), new SineEffect(), new AudioFFTEffect(), new ColorSweepEffect()};
private int activeId = 0;
private final TrayIcon trayIcon;
public EffectManager() { public EffectManager() {
final SystemTray tray = SystemTray.getSystemTray(); final SystemTray tray = SystemTray.getSystemTray();
final TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icon.png")).getImage(), "LEDEffects"); trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icon.png")).getImage(), "LEDEffects");
trayIcon.addMouseListener(new MouseListener() { trayIcon.addMouseListener(new MouseListener() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) switch (e.getButton()){
System.exit(0); case MouseEvent.BUTTON1:
getActiveEffect().unload(); loadNext();
activeId++; break;
if (activeId == effects.length) case MouseEvent.BUTTON2:
activeId = 0; sweep = !sweep;
getActiveEffect().load(); loadNext();
trayIcon.setToolTip("LEDEffects - " + getActiveEffect().getDescription()); break;
case MouseEvent.BUTTON3:
System.exit(0);
}
} }
@Override @Override
@ -46,13 +57,28 @@ public class EffectManager {
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
} }
}); });
try { try {
tray.add(trayIcon); tray.add(trayIcon);
} catch (AWTException e) { } catch (AWTException e) {
e.printStackTrace(); e.printStackTrace();
} }
new Timer(30 * 1000, e -> {
if(sweep)
loadNext();
}).start();
loadNext();
}
private void loadNext() {
getActiveEffect().unload();
activeId++;
if (activeId == effects.length)
activeId = 0;
getActiveEffect().load(); getActiveEffect().load();
trayIcon.setToolTip("LEDEffects - " + getActiveEffect().getDescription()+ (sweep?" (autoswitch)":""));
} }
public LEDEffect getActiveEffect() { public LEDEffect getActiveEffect() {

View file

@ -2,7 +2,6 @@ package de.zuim.ledcontrol.effects;
import com.tagtraum.jipes.math.FFTFactory; import com.tagtraum.jipes.math.FFTFactory;
import com.tagtraum.jipes.math.Transform; import com.tagtraum.jipes.math.Transform;
import de.zuim.ledcontrol.LEDControl;
import de.zuim.ledcontrol.LEDEffect; import de.zuim.ledcontrol.LEDEffect;
import javax.sound.sampled.*; import javax.sound.sampled.*;
@ -13,7 +12,7 @@ import java.util.List;
import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.HEIGHT;
import static de.zuim.ledcontrol.LEDControl.WIDTH; import static de.zuim.ledcontrol.LEDControl.WIDTH;
public class AudioFFT implements LEDEffect { public class AudioFFTEffect implements LEDEffect {
@Override @Override
public String getDescription() { public String getDescription() {
return "Audio FFT"; return "Audio FFT";

View file

@ -7,7 +7,7 @@ import java.util.List;
import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.HEIGHT;
import static de.zuim.ledcontrol.LEDControl.WIDTH; import static de.zuim.ledcontrol.LEDControl.WIDTH;
public class AudioVolume extends AudioFFT { public class AudioVolumeEffect extends AudioFFTEffect {
@Override @Override
public String getDescription() { public String getDescription() {

View file

@ -6,8 +6,7 @@ import java.awt.*;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import static de.zuim.ledcontrol.LEDControl.WIDTH; import static de.zuim.ledcontrol.LEDControl.HEIGHT;
import static java.awt.image.ImageObserver.HEIGHT;
public class ClockEffect implements LEDEffect { public class ClockEffect implements LEDEffect {
private Font font = new Font("Calibri", Font.BOLD, 12*getScale()); private Font font = new Font("Calibri", Font.BOLD, 12*getScale());
@ -39,9 +38,9 @@ public class ClockEffect implements LEDEffect {
g.setFont(font); g.setFont(font);
g.setColor(new Color(0,10-date.getMinutes()/6,date.getMinutes()/6)); g.setColor(new Color(0,10-date.getMinutes()/6,date.getMinutes()/6));
posOffset-=(4*getScale()*timeDelta)/1000000000.0; posOffset-=(4*getScale()*timeDelta)/1000000000.0;
g.drawString(text, (int) posOffset, 8*getScale()); g.drawString(text, (int) posOffset, HEIGHT/2*getScale());
g.setColor(new Color(10-date.getSeconds()/6,date.getSeconds()/6,0)); g.setColor(new Color(10-date.getSeconds()/6,date.getSeconds()/6,0));
g.drawString(text2, 2, 16*getScale()); g.drawString(text2, 2, HEIGHT*getScale());
if(posOffset<-g.getFontMetrics().stringWidth(text)) if(posOffset<-g.getFontMetrics().stringWidth(text))
posOffset=0; posOffset=0;
} }

View file

@ -7,7 +7,7 @@ import java.awt.*;
import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.HEIGHT;
import static de.zuim.ledcontrol.LEDControl.WIDTH; import static de.zuim.ledcontrol.LEDControl.WIDTH;
public class ColorSweep implements LEDEffect { public class ColorSweepEffect implements LEDEffect {
@Override @Override
public String getDescription() { public String getDescription() {
return "Solid Color"; return "Solid Color";

View file

@ -0,0 +1,112 @@
package de.zuim.ledcontrol.effects;
import de.zuim.ledcontrol.LEDEffect;
import oshi.SystemInfo;
import oshi.hardware.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import static de.zuim.ledcontrol.LEDControl.HEIGHT;
import static de.zuim.ledcontrol.LEDControl.WIDTH;
public class NetworkSpeedEffect implements LEDEffect {
private static final int MAX_UPLOAD_MBIT_S = 33;
private static final int MAX_DOWNLOAD_MBIT_S = 330;
private static final long NANOS_PER_PIXEL = 1000000000;
@Override
public String getDescription() {
return "Network Speed";
}
NetworkIF network = null;
@Override
public void load() {
if(network == null){
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
network = hal.getNetworkIFs().get(0);
}
}
private final Font font = new Font("Calibri", Font.BOLD, 9*getScale());
long lastDownBytes = 0, lastUpBytes = 0;
private final List<Measurement> history = new ArrayList<>();
private long nanosElapsed = 0;
private String uploadText = "", downloadText = "";
@Override
public void render(long timeDelta, Graphics g) {
nanosElapsed+=timeDelta;
if(network != null && nanosElapsed > NANOS_PER_PIXEL){
network.updateAttributes();
double factor_MBit = (8*1000000000.0)/(1000*1000*nanosElapsed);
double up_speed = ((network.getBytesSent() - lastUpBytes)*(factor_MBit));
double down_speed = ((network.getBytesRecv() - lastDownBytes)*(factor_MBit));
lastUpBytes = network.getBytesSent();
lastDownBytes = network.getBytesRecv();
history.add(new Measurement((int) (Math.round((HEIGHT*up_speed) / MAX_UPLOAD_MBIT_S)), (int) (Math.round((HEIGHT*down_speed) / MAX_DOWNLOAD_MBIT_S))));
if(history.size()>WIDTH){
history.remove(0);
}
uploadText = Math.round(up_speed)+"";
downloadText = Math.round(down_speed)+"";
//System.out.println(up_speed+" "+down_speed);
nanosElapsed = 0;
}
if(history.size() > 1){
g.setFont(font);
g.setColor(new Color(1, 0, 0));
g.drawString(downloadText, WIDTH-g.getFontMetrics().stringWidth(downloadText), HEIGHT*getScale()-3);
g.setColor(new Color(0, 1, 0));
g.drawString(uploadText, WIDTH-g.getFontMetrics().stringWidth(uploadText), HEIGHT/2*getScale()-2);
for(int x = 0; x < WIDTH && x < history.size(); x++){
boolean firstUp = history.get(x).upY > history.get(x).downY;
drawMeasurement(g, x, firstUp);
drawMeasurement(g, x, !firstUp);
}
}
}
private void drawMeasurement(Graphics g, int x, boolean up){
int offset = WIDTH - history.size();
if(up){
g.setColor(new Color(0, 1, 0));
g.drawLine(x + offset, HEIGHT - history.get(x).upY, x + offset, HEIGHT - 1);
g.setColor(new Color(0, 7, 0));
g.drawRect(x + offset, HEIGHT - 1 - history.get(x).upY, 0, 0);
//g.drawLine(x + offset, HEIGHT - 1 - history.get(x).upY, x + offset + 1, HEIGHT - 1 - history.get(x+1).upY);
}
else{
g.setColor(new Color(1, 0, 0));
g.drawLine(x + offset, HEIGHT - history.get(x).downY, x + offset, HEIGHT - 1);
g.setColor(new Color(7, 0, 0));
g.drawRect(x + offset, HEIGHT - 1 - history.get(x).downY, 0, 0);
//g.drawLine(x + offset, HEIGHT - 1 - history.get(x).downY, x + offset + 1, HEIGHT - 1 - history.get(x+1).downY);
}
}
static class Measurement{
public int upY;
public int downY;
public Measurement(int upY, int downY) {
this.upY = upY;
this.downY = downY;
}
}
}

View file

@ -1,55 +0,0 @@
package de.zuim.ledcontrol.effects;
import com.tagtraum.jipes.math.FFTFactory;
import com.tagtraum.jipes.math.Transform;
import de.zuim.ledcontrol.LEDEffect;
import oshi.SystemInfo;
import oshi.hardware.*;
import javax.sound.sampled.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import static de.zuim.ledcontrol.LEDControl.WIDTH;
import static java.awt.image.ImageObserver.HEIGHT;
public class TemperatureEffect implements LEDEffect {
@Override
public String getDescription() {
return "Temperatur Sensoren";
}
NetworkIF network = null;
CentralProcessor cpu = null;
Sensors sensors = null;
@Override
public void load() {
if(sensors == null){
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
network = hal.getNetworkIFs().get(0);
cpu = hal.getProcessor();
sensors = hal.getSensors();
}
}
private Font font = new Font("Calibri", Font.BOLD, 12*getScale());
@Override
public void render(long timeDelta, Graphics g) {
String text = "---";
if(sensors!=null){
text = Math.round(cpu.getSystemLoadAverage(1)[0]*100)+"%\n";
text+= sensors.getCpuTemperature();
}
g.setFont(font);
g.setColor(new Color(26, 9, 9));
g.drawString(text, 0, 8*getScale());
}
}