-
Notifications
You must be signed in to change notification settings - Fork 0
Add Raspberry Pi sensor observables #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.easternedgerobotics.rov.value; | ||
|
|
||
| class Cpu implements ImmutableValueCompanion<CpuValue> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would work, yeah. How does that sound?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| 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 | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
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 theScheduler.io()thread ?nevermind!