Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/com/easternedgerobotics/rov/io/CpuInformation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.easternedgerobotics.rov.io;

import com.easternedgerobotics.rov.value.CpuValue;

import com.pi4j.system.SystemInfo;
import rx.Observable;
import rx.exceptions.Exceptions;
import rx.schedulers.Schedulers;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public final class CpuInformation {
private final Observable<Long> interval;

/**
* Constructs a CpuInformation instance that polls CPU properties at the specified interval.
*
* @param interval the interval at which to poll the CPU properties.
* @param timeUnit the {@code TimeUnit} the interval is specified in.
*/
public CpuInformation(final int interval, final TimeUnit timeUnit) {
this.interval = Observable.interval(interval, timeUnit);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put this on the Scheduler.io() thread ?

nevermind!

}

/**
* Returns an observable stream of CPU values.
*
* @return a stream of CPU values.
*/
public final Observable<CpuValue> observe() {
return interval.observeOn(Schedulers.io())
.map(this::pollCpu)
.observeOn(Schedulers.immediate());
}

private CpuValue pollCpu(final long tick) {
try {
return CpuValue.create(
SystemInfo.getClockFrequencyArm(), SystemInfo.getCpuTemperature(), SystemInfo.getCpuVoltage());
} catch (final InterruptedException | IOException e) {
throw Exceptions.propagate(e);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/easternedgerobotics/rov/value/Cpu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.easternedgerobotics.rov.value;

class Cpu implements ImmutableValueCompanion<CpuValue> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to observe multiple raspi we will need a name field

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where the images don't have their hostnames set to anything meaningful, would it be better to attach a name externally instead? That is, in each entry point, when wiring up this information, we can map machines to the streams (somehow).

(Which might be doubly useful if we need to map machines to other information.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make our own composite class to wrap names with an object? Essentially a clone of the timestamped class you mentioned the other day.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, yeah. How does that sound?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/**
* The ARM clock frequency.
*/
public long frequency;

/**
* The core temperature of the main SoC in celsius.
*/
public float temperature;

/**
* The core voltage.
*/
public float voltage;

@Override
public CpuValue asImmutable() {
return new CpuValue(this);
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/easternedgerobotics/rov/value/CpuValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.easternedgerobotics.rov.value;

import java.util.Objects;

public class CpuValue implements MutableValueCompanion<Cpu> {
/**
* Constructs a CPU value with the given frequency, temperature, and voltage.
*
* @param frequency the ARM clock frequency.
* @param temperature The core temperature of the main SoC in celsius.
* @param voltage the core voltage.
* @return a CPU value with the given frequency, temperature, and voltage.
*/
public static CpuValue create(final long frequency, final float temperature, final float voltage) {
final Cpu cpu = new Cpu();
cpu.frequency = frequency;
cpu.temperature = temperature;
cpu.voltage = voltage;
return new CpuValue(cpu);
}

private final Cpu cpu;

CpuValue(final Cpu cpu) {
this.cpu = cpu;
}

@Override
public final Cpu asMutable() {
return cpu;
}

@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final CpuValue cpuValue = (CpuValue) o;
return Objects.equals(cpu, cpuValue.cpu);
}

@Override
public final int hashCode() {
return Objects.hash(cpu);
}

@Override
public final String toString() {
return String.format(
"CpuValue{frequency=%d, temperature=%f, voltage=%f}",
cpu.frequency,
cpu.temperature,
cpu.voltage
);
}
}