Skip to content
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

add no_hardware to pubber options #340

Merged
merged 19 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
21 changes: 9 additions & 12 deletions bin/pubber
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,26 @@ else
if [[ $option == *"="* ]]; then
k=$(echo $option | cut -d'=' -f1)
v=$(echo $option | cut -d'=' -f2)
options[$k]=$v
if ! [[ $v =~ ^[0-9]+([.][0-9]+)?$ ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't go about it this way, since although it seems "simple" it's actually somewhat of a minefield because, say, what if you want a string that looks like a number? Better to be just simpler (all options are strings) and then deal with the conversion later, if/when necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed so options are either boolean true or string value

v="\"$v\""
fi
else
options[$option]=$option
k=$option
v=true
grafnu marked this conversation as resolved.
Show resolved Hide resolved
fi
printf -v options_json '%s"%s":%s,' "$options_json" "$k" "$v"
done

extra_point="${options[extra_point]}"

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

options_json="{${options_json%?}}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the ? for? If it's a wildcard, can you be explicit about what it's removing (trailing comma, right?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

last character, changed to remove trailing comma

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
2 changes: 2 additions & 0 deletions docs/tools/pubber.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The following parameters are currently supported from the CLI:
(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)
* `no_hardware` - omits the `system.hardware` field from state messages (will
trigger validation error, missing required field)

Values can be assigned to the parameters e.g.
`bin/pubber SITE_PATH PROJECT_ID DEVICE_ID SERIAL_NO extra_point=name_of_point`
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": true
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't extraField be a string, not boolean?

}
}
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 = false;
public Boolean extraPoint = false;
public Boolean extraField = false;

}
25 changes: 19 additions & 6 deletions pubber/src/main/java/daq/pubber/Pubber.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static java.util.stream.Collectors.toMap;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
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 @@ -75,6 +77,7 @@ public class Pubber {
private static final String UDMI_VERSION = "1.3.14";
private static final Logger LOG = LoggerFactory.getLogger(Pubber.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
.enable(SerializationFeature.INDENT_OUTPUT)
.setDateFormat(new ISO8601DateFormat())
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Expand Down Expand Up @@ -146,6 +149,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,24 +362,32 @@ 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;
deviceState.system.serial_no = configuration.serialNo;
deviceState.system.hardware.make = "BOS";
deviceState.system.hardware.model = "pubber";
deviceState.system.software = new HashMap<>();
deviceState.system.software.put("firmware", "v1");
deviceState.system.software.put("firmware", "v1");
Copy link
Collaborator

Choose a reason for hiding this comment

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

tws?

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) {
devicePoints.extraField = "extra_field";
Copy link
Collaborator

Choose a reason for hiding this comment

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

see, now if this were still a string then you have your extra field value!

Copy link
Collaborator

Choose a reason for hiding this comment

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

see, now if this were still a string then you have your extra field value!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

extra field value now taken from string

}

if (configuration.options.extraPoint) {
addPoint(makePoint("extra_point",
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto -- extra_point here would be useful

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

extra point value now taken from string

makePointPointsetModel(true, 50, 50, "Celsius")));
}

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

markStateDirty(0);
}

Expand Down