Skip to content

Commit

Permalink
mapping added into config
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed Apr 3, 2022
1 parent 9710f81 commit 2295edc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package one.microproject.rpi.powercontroller.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class PortMapping {

private final int address;
private final PortType type;

@JsonCreator
public PortMapping(@JsonProperty("address") int address,
@JsonProperty("type") PortType type) {
this.address = address;
this.type = type;
}

public int getAddress() {
return address;
}

public PortType getType() {
return type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ public class SystemState {

private final Date timeStamp;
private final Map<Integer, Boolean> ports;
private final Map<Integer, PortType> portTypes;
private final Map<Integer, PortMapping> portMapping;

@JsonCreator
public SystemState(@JsonProperty("timeStamp") Date timeStamp,
@JsonProperty("ports") Map<Integer, Boolean> ports,
@JsonProperty("portTypes") Map<Integer, PortType> portTypes) {
@JsonProperty("portMapping") Map<Integer, PortMapping> portMapping) {
this.timeStamp = timeStamp;
this.ports = ports;
this.portTypes = portTypes;
this.portMapping = portMapping;
}

public Map<Integer, Boolean> getPorts() {
return ports;
}

public Map<Integer, PortType> getPortTypes() {
return portTypes;
public Map<Integer, PortMapping> getPortMapping() {
return portMapping;
}

public Date getTimeStamp() {
Expand Down
3 changes: 3 additions & 0 deletions rpi-powercontroller/docs/hardware-bom.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ This is detailed HW equipment Bill of Material for RPi Power Controller.
## Example Assembly
This assembly uses only two output power ports __(Port 0, Port 1)__ and only one input key __(Key 0)__.
![hw01](hardware-image-01.svg)

## Reference device
![reference-device](reference-device.png)
Binary file added rpi-powercontroller/docs/reference-device.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import one.microproject.rpi.powercontroller.dto.PortType;
import one.microproject.rpi.powercontroller.dto.PortMapping;

import java.util.Collection;
import java.util.Date;
Expand All @@ -16,7 +16,7 @@ public class Configuration {
private final String host;
private final int port;
private final boolean hardware;
private final Map<Integer, PortType> portTypes;
private final Map<Integer, PortMapping> portMapping;
private final Collection<JobConfiguration> jobConfigurations;
private final Date started;
private final Map<String, String> credentials;
Expand All @@ -32,7 +32,7 @@ public Configuration(@JsonProperty("id") String id,
@JsonProperty("host") String host,
@JsonProperty("port") int port,
@JsonProperty("hardware") boolean hardware,
@JsonProperty("portTypes") Map<Integer, PortType> portTypes,
@JsonProperty("portMapping") Map<Integer, PortMapping> portMapping,
@JsonProperty("jobConfigurations") Collection<JobConfiguration> jobConfigurations,
@JsonProperty("credentials") Map<String, String> credentials,
@JsonProperty("executeJobsOnStart") Collection<String> executeJobsOnStart,
Expand All @@ -45,7 +45,7 @@ public Configuration(@JsonProperty("id") String id,
this.host = host;
this.port = port;
this.hardware = hardware;
this.portTypes = portTypes;
this.portMapping = portMapping;
this.jobConfigurations = jobConfigurations;
this.started = new Date();
this.credentials = credentials;
Expand Down Expand Up @@ -76,8 +76,8 @@ public boolean isHardware() {
return hardware;
}

public Map<Integer, PortType> getPortsTypes() {
return portTypes;
public Map<Integer, PortMapping> getPortsMapping() {
return portMapping;
}

public Collection<JobConfiguration> getJobConfigurations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
import one.microproject.rpi.powercontroller.dto.PortMapping;
import one.microproject.rpi.powercontroller.dto.PortType;
import one.microproject.rpi.powercontroller.dto.Measurements;
import one.microproject.rpi.powercontroller.dto.SystemState;
Expand All @@ -31,41 +32,47 @@ public class RPiHardwareServiceImpl implements RPiService {
private final HTU21DF htu21DF;
private final Map<Integer, DigitalOutput> outPorts;
private final Map<Integer, DigitalInput> inPorts;
private final Map<Integer, PortType> portTypes;
private final Map<Integer, PortMapping> portMapping;

public RPiHardwareServiceImpl(PortListener portListener, Configuration configuration) {
LOG.info("initializing hardware ...");
LOG.info("Initializing RPi hardware ...");
Context context = Pi4J.newAutoContext();
this.bmp180 = new BMP180(context);
this.htu21DF = new HTU21DF(context);
this.outPorts = new ConcurrentHashMap<>();
this.inPorts = new ConcurrentHashMap<>();
this.portTypes = configuration.getPortsTypes();
this.portTypes.forEach((k, v) -> {
int pinId = mapPin(k);
LOG.info("initializing port {} pinId={} as {}", k, pinId, v);
if (PortType.OUTPUT.equals(v)) {
this.portMapping = configuration.getPortsMapping();
this.portMapping.forEach((k, v) -> {
int pinId = v.getAddress();
LOG.info("Initializing port {} pinId={} as {}", k, pinId, v);
if (PortType.OUTPUT.equals(v.getType())) {
DigitalOutput digitalOutput = context.dout().create(pinId);
digitalOutput.low();
outPorts.put(k, digitalOutput);
LOG.info("OUTPUT port created");
}
if (PortType.INPUT.equals(v)) {
if (PortType.INPUT.equals(v.getType())) {
DigitalInput digitalInput = context.din().create(pinId);
digitalInput.addListener(new PinListener(k, portListener));
inPorts.put(k, digitalInput);
LOG.info("INPUT port created");
}
});
LOG.info("hardware initialization done.");
LOG.info("INPUT {}", inPorts.size());
LOG.info("OUTPUT {}", outPorts.size());
LOG.info("Hardware initialization done.");
}

@Override
public Measurements getMeasurements() {
try {
LOG.debug("getMeasurements");
float temperature = bmp180.readTemperature();
float pressure = bmp180.readPressure() / 1000f;
float relHumidity = htu21DF.readHumidity();
return new Measurements(new Date(), temperature, "celsius", relHumidity, "percent", pressure, "kPa");
} catch (Exception e) {
LOG.error("ERROR: ", e);
return new Measurements(new Date(),null, "celsius", null, "percent", null, "kPa");
}
}
Expand All @@ -79,7 +86,7 @@ public SystemState getSystemState() {
inPorts.forEach((k,v) ->
result.put(k, v.isHigh())
);
return new SystemState(new Date(), result, portTypes);
return new SystemState(new Date(), result, portMapping);
}

@Override
Expand All @@ -102,28 +109,6 @@ public void close() throws Exception {
htu21DF.close();
}

public static Integer mapPin(Integer port) {
if (port == 0) {
return 0;
} else if (port == 1) {
return 1;
} else if (port == 2) {
return 2;
} else if (port == 3) {
return 3;
} else if (port == 4) {
return 4;
} else if (port == 5) {
return 5;
} else if (port == 6) {
return 6;
} else if (port == 7) {
return 7;
} else {
throw new UnsupportedOperationException("Invalid Pin number " + port);
}
}

private class PinListener implements DigitalStateChangeListener {

private final Integer port;
Expand All @@ -136,6 +121,7 @@ private PinListener(Integer port, PortListener portListener) {

@Override
public void onDigitalStateChange(DigitalStateChangeEvent event) {
LOG.info("onDigitalStateChange: {} {}", event.state().getName(), event.state().getValue());
portListener.onStateChange(port, event.state().isHigh());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package one.microproject.rpi.powercontroller.services.impl;

import one.microproject.rpi.powercontroller.config.Configuration;
import one.microproject.rpi.powercontroller.dto.PortMapping;
import one.microproject.rpi.powercontroller.dto.PortType;
import one.microproject.rpi.powercontroller.dto.Measurements;
import one.microproject.rpi.powercontroller.dto.SystemState;
Expand All @@ -19,14 +20,14 @@ public class RPiSimulatedServiceImpl implements RPiService {
private static final Logger LOG = LoggerFactory.getLogger(RPiSimulatedServiceImpl.class);

private final Map<Integer, Boolean> ports;
private final Map<Integer, PortType> portTypes;
private final Map<Integer, PortMapping> portMapping;
private final PortListener portListener;

public RPiSimulatedServiceImpl(PortListener portListener, Configuration configuration) {
this.portListener = portListener;
this.portTypes = configuration.getPortsTypes();
this.portMapping = configuration.getPortsMapping();
this.ports = new ConcurrentHashMap<>();
this.portTypes.forEach((k,v) -> this.ports.put(k, false));
this.portMapping.forEach((k,v) -> this.ports.put(k, false));
}

@Override
Expand All @@ -36,12 +37,12 @@ public Measurements getMeasurements() {

@Override
public SystemState getSystemState() {
return new SystemState(new Date(), ports, portTypes);
return new SystemState(new Date(), ports, portMapping);
}

@Override
public Optional<Boolean> setPortState(Integer port, Boolean state) {
if (ports.containsKey(port) && PortType.OUTPUT.equals(portTypes.get(port))) {
if (ports.containsKey(port) && PortType.OUTPUT.equals(portMapping.get(port).getType())) {
ports.put(port, state);
portListener.onStateChange(port, state);
return Optional.of(state);
Expand Down
18 changes: 9 additions & 9 deletions rpi-powercontroller/src/main/resources/rpi-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"host": "0.0.0.0",
"port": 8080,
"hardware": false,
"portTypes": {
"0": "OUTPUT",
"1": "OUTPUT",
"2": "OUTPUT",
"3": "OUTPUT",
"4": "INPUT",
"5": "INPUT",
"6": "INPUT",
"7": "INPUT"
"portMapping": {
"0": { "address": 17, "type": "OUTPUT" },
"1": { "address": 27, "type": "OUTPUT" },
"2": { "address": 22, "type": "OUTPUT" },
"3": { "address": 12, "type": "OUTPUT" },
"4": { "address": 4, "type": "INPUT" },
"5": { "address": 23, "type": "INPUT" },
"6": { "address": 24, "type": "INPUT" },
"7": { "address": 25, "type": "INPUT" }
},
"credentials": {
"client-001": "ex4ooNgexoo2aeSho7del8caiPh8iegi",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void testSystemState() {
SystemState systemState = powerControllerClient.getSystemState();
assertNotNull(systemState);
assertNotNull(systemState.getTimeStamp());
assertNotNull(systemState.getPortTypes());
assertNotNull(systemState.getPortMapping());
assertNotNull(systemState.getPorts());
}

Expand Down

0 comments on commit 2295edc

Please sign in to comment.