Skip to content

Commit

Permalink
refactoring done
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed Apr 3, 2022
1 parent 29f8972 commit 9710f81
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions rpi-powercontroller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Detailed [hardware bill of material](docs/hardware-bom.md).
![sw-arch](docs/rpi-powercontroller-software-architecture.svg)

* Tiny software stack, 32 MB of heap space to run.
* Java 8 and Java 11 compatible.
* Java 11 compatible.
* [undertow.io](http://undertow.io/) as web server.
* [com.fasterxml.jackson](https://github.com/FasterXML/jackson) for JSON processing.
* [Raspbian Lite](https://www.raspberrypi.org/downloads/raspbian/), [WiringPi](http://wiringpi.com/), [pi4j](https://pi4j.com/1.2/index.html)
Expand Down Expand Up @@ -100,8 +100,8 @@ curl -u <client-id>:<client-secret> http://<server>:<port>/uri
RPi Power Controller is designed to be build and tested on PC. In this case
``RPiSimulatedServiceImpl`` is used instead of real RPi hardware.
### Development Environment
* [OpenJDK 8 or 11](https://adoptopenjdk.net/).
* [Gradle 6.0](https://gradle.org/install/) or later.
* [OpenJDK 11](https://adoptopenjdk.net/).
* [Gradle 7.0](https://gradle.org/install/) or later.
```
# build and test
gradle clean build test installDist distZip
Expand Down Expand Up @@ -134,14 +134,14 @@ gradle clean build test installDist distZip
7. Build distribution zip and copy the zip and init scripts to target RPi device.
```
gradle clean build test installDist distZip
scp build/distributions/rpi-powercontroller-1.4.0.zip pi@<ip-address>:/opt/rpi-powercontroller/
scp build/distributions/rpi-powercontroller-2.0.0.zip pi@<ip-address>:/opt/rpi-powercontroller/
scp -r scripts/* pi@<ip-address>:/opt/rpi-powercontroller/
scp src/main/resources/rpi-configuration.json pi@<ip-address>:/opt/rpi-powercontroller/
```
8. Finish installation on target RPi device.
```
cd /opt/rpi-powercontroller
unzip rpi-powercontroller-1.4.0.zip
unzip rpi-powercontroller-2.0.0.zip
chmod 755 controller-start.sh
chmod 755 controller-stop.sh
sudo cp rpi-powercontroller.service /etc/systemd/system/
Expand Down
2 changes: 1 addition & 1 deletion rpi-powercontroller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

group = 'one.microproject.rpi'
version = '1.4.2'
version = '2.0.0'
mainClassName = 'one.microproject.rpi.powercontroller.PowerControllerApp'
applicationDefaultJvmArgs = ['-Xms32m', '-Xmx32m', '-XX:MaxMetaspaceSize=32m']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.pi4j.Pi4J;
import com.pi4j.context.Context;
import com.pi4j.io.gpio.digital.DigitalInput;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalStateChangeEvent;
import com.pi4j.io.gpio.digital.DigitalStateChangeListener;
import one.microproject.rpi.hardware.gpio.sensors.sensors.BMP180;
import one.microproject.rpi.hardware.gpio.sensors.sensors.HTU21DF;
import one.microproject.rpi.powercontroller.config.Configuration;
Expand All @@ -25,28 +29,31 @@ public class RPiHardwareServiceImpl implements RPiService {

private final BMP180 bmp180;
private final HTU21DF htu21DF;
private final Map<Integer, GpioPinDigitalMultipurpose> ports;
private final Map<Integer, DigitalOutput> outPorts;
private final Map<Integer, DigitalInput> inPorts;
private final Map<Integer, PortType> portTypes;

public RPiHardwareServiceImpl(PortListener portListener, Configuration configuration) {
LOG.info("initializing hardware ...");
Context context = Pi4J.newAutoContext();
this.bmp180 = new BMP180(context);
this.htu21DF = new HTU21DF(context);
this.ports = new ConcurrentHashMap<>();
this.outPorts = new ConcurrentHashMap<>();
this.inPorts = new ConcurrentHashMap<>();
this.portTypes = configuration.getPortsTypes();
this.portTypes.forEach((k, v) -> {
LOG.info("initializing port {} as {}", k, v);
Pin pin = mapPin(k);
GpioPinDigitalMultipurpose gpioPinDigitalMultipurpose = gpio.provisionDigitalMultipurposePin(pin, mapPinMode(v));
gpioPinDigitalMultipurpose.addListener(new PinListener(k, portListener));
int pinId = mapPin(k);
LOG.info("initializing port {} pinId={} as {}", k, pinId, v);
if (PortType.OUTPUT.equals(v)) {
gpioPinDigitalMultipurpose.setState(false);
DigitalOutput digitalOutput = context.dout().create(pinId);
digitalOutput.low();
outPorts.put(k, digitalOutput);
}
if (PortType.INPUT.equals(v)) {

DigitalInput digitalInput = context.din().create(pinId);
digitalInput.addListener(new PinListener(k, portListener));
inPorts.put(k, digitalInput);
}
ports.put(k, gpioPinDigitalMultipurpose);
});
LOG.info("hardware initialization done.");
}
Expand All @@ -66,7 +73,10 @@ public Measurements getMeasurements() {
@Override
public SystemState getSystemState() {
Map<Integer, Boolean> result = new HashMap<>();
ports.forEach((k,v) ->
outPorts.forEach((k,v) -> {
result.put(k, v.isHigh());
});
inPorts.forEach((k,v) ->
result.put(k, v.isHigh())
);
return new SystemState(new Date(), result, portTypes);
Expand All @@ -76,8 +86,8 @@ public SystemState getSystemState() {
public Optional<Boolean> setPortState(Integer port, Boolean state) {
try {
LOG.info("setting state port={} state={}", port, state);
GpioPinDigitalMultipurpose gpioPinDigitalMultipurpose = ports.get(port);
gpioPinDigitalMultipurpose.setState(state);
DigitalOutput digitalOutput = outPorts.get(port);
digitalOutput.setState(state);
return Optional.of(state);
} catch (Exception e) {
LOG.error("Exception: ", e);
Expand All @@ -92,39 +102,29 @@ public void close() throws Exception {
htu21DF.close();
}

public static Pin mapPin(Integer port) {
public static Integer mapPin(Integer port) {
if (port == 0) {
return RaspiPin.GPIO_00;
return 0;
} else if (port == 1) {
return RaspiPin.GPIO_01;
return 1;
} else if (port == 2) {
return RaspiPin.GPIO_02;
return 2;
} else if (port == 3) {
return RaspiPin.GPIO_03;
return 3;
} else if (port == 4) {
return RaspiPin.GPIO_04;
return 4;
} else if (port == 5) {
return RaspiPin.GPIO_05;
return 5;
} else if (port == 6) {
return RaspiPin.GPIO_06;
return 6;
} else if (port == 7) {
return RaspiPin.GPIO_07;
return 7;
} else {
throw new UnsupportedOperationException("Invalid Pin number " + port);
}
}

public static PinMode mapPinMode(PortType portType) {
if (PortType.INPUT.equals(portType)) {
return PinMode.DIGITAL_INPUT;
} else if (PortType.OUTPUT.equals(portType)) {
return PinMode.DIGITAL_OUTPUT;
} else {
throw new UnsupportedOperationException("Invalid PortType number " + portType);
}
}

private class PinListener implements GpioPinListenerDigital {
private class PinListener implements DigitalStateChangeListener {

private final Integer port;
private final PortListener portListener;
Expand All @@ -135,8 +135,8 @@ private PinListener(Integer port, PortListener portListener) {
}

@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
portListener.onStateChange(port, event.getState().isHigh());
public void onDigitalStateChange(DigitalStateChangeEvent event) {
portListener.onStateChange(port, event.state().isHigh());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public SystemInfo<ControllerInfo> getSystemInfo() {
long uptime = timeStamp - configuration.getStarted().getTime();
ControllerInfo info = new ControllerInfo(configuration.isHardware(), configuration.getStarted(), ServiceUtils.calculateUptimeDays(uptime));
return new SystemInfo<>(configuration.getId(), "power-controller",
configuration.getName(), "1.4.0", timeStamp, uptime, info);
configuration.getName(), "2.0.0", timeStamp, uptime, info);
}

}

0 comments on commit 9710f81

Please sign in to comment.