Skip to content

Commit

Permalink
refactor pubber options and add noHardware option
Browse files Browse the repository at this point in the history
  • Loading branch information
noursaidi committed Jun 13, 2022
1 parent 33cf6b2 commit e215679
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
19 changes: 7 additions & 12 deletions bin/pubber
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,25 @@ else
for option in $*; do
if [[ $option == *"="* ]]; then
k=$(echo $option | cut -d'=' -f1)
v=$(echo $option | cut -d'=' -f2)
options[$k]=$v
v="\"$(echo $option | cut -d'=' -f2)\""
else
options[$option]=$option
k=$option
v=true
fi
printf -v options_json '%s"%s":%s,' "$options_json" "$k" "$v"
done
options_json="{${options_json%,}}"

extra_point="${options[extra_point]}"

if [ -n "${options[extra_field]}" ]; then
printf -v extra_field '"extraField": "%s",' "${options[extra_field]}"
fi

cloud_region=`jq -r .cloud_region $site_path/cloud_iot_config.json`

cat <<EOF > /tmp/pubber_config.json
{
$extra_field
"extraPoint": "$extra_point",
"projectId": "$project_id",
"sitePath": "$site_path",
"cloudRegion": "$cloud_region",
"deviceId": "$device_id",
"serialNo": "$serial"
"serialNo": "$serial_no",
"options": $options_json
}
EOF
$ROOT_DIR/pubber/bin/run /tmp/pubber_config.json
Expand Down
12 changes: 6 additions & 6 deletions docs/tools/pubber.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Pubber is run from the CLI within the UDMI directory.
### Options

The following parameters are currently supported from the CLI:
* `extra_field` - adds an extra schema invalidating field to pointset events
* `extraPoint=<name>` - adds an extra point with the given name to the device
which does not exist in device's metadata with a random value (will trigger
validation additional point error)
* `extraField=<name>` - adds an extra schema invalidating field to pointset events
(will trigger validation schema error)
* `extra_point` - adds an extra point to the device which does not exist in
device's metadata (will trigger validation additional point error)

Values can be assigned to the parameters e.g.
`bin/pubber SITE_PATH PROJECT_ID DEVICE_ID SERIAL_NO extra_point=name_of_point`
* `no_hardware` - omits the `system.hardware` field from state messages (will
trigger validation error, missing required field)

More advanced options can be set by by calling pubber directly with the path a
configuration file: `pubber/bin/run path/to/config.json`
Expand Down
6 changes: 4 additions & 2 deletions proxy/pubber.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"projectId": "bos-udmi-hub",
"cloudRegion": "us-central1",
"registryId": "ZZ-TRI-FECTA",
"extraField": 1234,
"serialNo": "test_aux-16132",
"macAddr": "3c5ab41e8f0a",
"keyFile": "./udmi_site_model/devices/GAT-123/ec_private.pkcs8",
"algorithm": "ES256",
"gatewayId": "GAT-123",
"deviceId": "SNS-4"
"deviceId": "SNS-4",
"options": {
"extraField": "1234"
}
}
4 changes: 2 additions & 2 deletions pubber/src/main/java/daq/pubber/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package daq.pubber;


/**
* General bucket of pubber configuration information.
*/
Expand All @@ -15,8 +16,7 @@ public class Configuration {
public String keyFile = "local/rsa_private.pkcs8";
public byte[] keyBytes;
public String algorithm = "RS256";
public Object extraField;
public String serialNo;
public String macAddr;
public String extraPoint;
public ConfigurationOptions options = new ConfigurationOptions();
}
11 changes: 11 additions & 0 deletions pubber/src/main/java/daq/pubber/ConfigurationOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package daq.pubber;

/**
* Pubber configuration options which change default behavior.
*/
public class ConfigurationOptions {
public Boolean noHardware;
public String extraPoint;
public String extraField;

}
21 changes: 16 additions & 5 deletions pubber/src/main/java/daq/pubber/Pubber.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -146,6 +147,8 @@ public Pubber(String configPath) {
File configFile = new File(configPath);
try {
configuration = OBJECT_MAPPER.readValue(configFile, Configuration.class);
} catch (UnrecognizedPropertyException e) {
throw new RuntimeException("Invalid arguments or options: " + e.getMessage());
} catch (Exception e) {
throw new RuntimeException("While reading config " + configFile.getAbsolutePath(), e);
}
Expand Down Expand Up @@ -357,9 +360,8 @@ private void initializeDevice() {
pullDeviceMessage();
}

info(String.format("Starting pubber %s, serial %s, mac %s, extra %s, gateway %s",
info(String.format("Starting pubber %s, serial %s, mac %, gateway %s",
configuration.deviceId, configuration.serialNo, configuration.macAddr,
configuration.extraField,
configuration.gatewayId));

deviceState.system.operational = true;
Expand All @@ -369,12 +371,21 @@ private void initializeDevice() {
deviceState.system.software = new HashMap<>();
deviceState.system.software.put("firmware", "v1");
deviceState.system.last_config = new Date(0);
devicePoints.extraField = configuration.extraField;

if (configuration.extraPoint != null && !configuration.extraPoint.isEmpty()) {
addPoint(makePoint(configuration.extraPoint,
// Pubber runtime options
if (configuration.options.extraField != null) {
devicePoints.extraField = configuration.options.extraField;
}

if (configuration.options.extraPoint != null) {
addPoint(makePoint(configuration.options.extraPoint,
makePointPointsetModel(true, 50, 50, "Celsius")));
}

if (configuration.options.noHardware != null && configuration.options.noHardware) {
deviceState.system.hardware = null;
}

markStateDirty(0);
}

Expand Down

0 comments on commit e215679

Please sign in to comment.