Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion asyncapi-template/filters/DeviceFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function filterProps(props) {

function aAn(str) {
//Credit: https://stackoverflow.com/questions/37896484/multiple-conditions-for-javascript-includes-method
return ['a', 'e', 'i', 'o', 'u'].some(el => str.includes(el));
return ['a', 'e', 'i', 'o', 'u'].some(el => str.includes(el)) ? "an" : "a";
}

function formatName(type) {
Expand Down
10 changes: 9 additions & 1 deletion asyncapi-template/filters/PropertyFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ function formatPropInitialValue(prop) {
}
}

module.exports = { formatPropName, cap1, formatPropType, formatPropTypeArr, formatPropPrimType, formatPropInitialValue }
function isRobotInput(prop) {
return prop.startsWith(">") || prop.startsWith("<>");
}

function isRobotOutput(prop) {
return prop.startsWith("<");
}

module.exports = { formatPropName, cap1, formatPropType, formatPropTypeArr, formatPropPrimType, formatPropInitialValue, isRobotInput, isRobotOutput }
2 changes: 2 additions & 0 deletions asyncapi-template/partials/initVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
{% set parrtype = prop | formatPropTypeArr %}
{% set pprimtype = prop | formatPropPrimType %}
{% set pinit = prop | formatPropInitialValue | safe %}
{% set isRobotInput = pfname | isRobotInput %}
{% set isRobotOutput = pfname | isRobotOutput %}
93 changes: 52 additions & 41 deletions asyncapi-template/template/$$schema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
// EDIT THAT FILE INSTEAD.
package org.team199.wpiws.devices;

import java.util.List;
{% if hasId -%}
import java.util.HashMap;
import java.util.Map;
{% endif -%}
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
{% if hasId -%}
import java.util.concurrent.ConcurrentHashMap;
{% endif -%}
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand All @@ -36,32 +39,33 @@
*/
{% if hasId -%}
public class {{ name }}Sim extends StateDevice<{{ name }}Sim.State> {
{% else -%}
{%- else %}
public class {{ name }}Sim {
{% endif -%}
{% if hasInit %}
private static final CopyOnWriteArrayList<String> INITIALIZED_DEVICES = new CopyOnWriteArrayList<>();
private static final CopyOnWriteArrayList<BooleanCallback> STATIC_INITIALIZED_CALLBACKS = new CopyOnWriteArrayList<>();
{%- endif %}

{% if hasInit -%}
private static final Set<String> INITIALIZED_DEVICES = new ConcurrentSkipListSet<>();
private static final Set<BooleanCallback> STATIC_INITIALIZED_CALLBACKS = new CopyOnWriteArraySet<>();
{% endif -%}
{% if hasId %}
private static final HashMap<String, {{ name }}Sim.State> STATE_MAP = new HashMap<>();
{%- else %}
{% if hasId -%}
private static final Map<String, {{ name }}Sim.State> STATE_MAP = new ConcurrentHashMap<>();
{% else -%}
private static final {{ name }}Sim.State STATE = new State();
{% endif -%}
private static final Set<String> UNKNOWN_KEYS = new ConcurrentSkipListSet<>();

{% if hasId %}

{% if hasId -%}
/**
* Creates a new {{ name }}Sim
* @param id the device identifier of this {{ name }}Sim
* @param id the device identifier of this {{ name }}Sim
*/
public {{ name }}Sim(String id) {
super(id, STATE_MAP);
}
{% endif -%}
{%- endif -%}

{%- if hasInit %}

{% if hasInit %}
/**
* Registers a BooleanCallback to be called whenever {{ a }} {{ name }}Sim device is initialized or uninitialized
* @param callback the callback function to call
Expand All @@ -71,7 +75,7 @@ public class {{ name }}Sim {
* @see #registerInitializedCallback(BooleanCallback, boolean)
*/
public static ScopedObject<BooleanCallback> registerStaticInitializedCallback(BooleanCallback callback, boolean initialNotify) {
STATIC_INITIALIZED_CALLBACKS.addIfAbsent(callback);
STATIC_INITIALIZED_CALLBACKS.add(callback);
if(initialNotify) {
INITIALIZED_DEVICES.forEach(device -> callback.callback(device, true));
}
Expand All @@ -90,7 +94,7 @@ public static ScopedObject<BooleanCallback> registerStaticInitializedCallback(Bo
public static void cancelStaticInitializedCallback(BooleanCallback callback) {
STATIC_INITIALIZED_CALLBACKS.remove(callback);
}

/**
* Registers a BooleanCallback to be called whenever this device is initialized or uninitialized
* @param callback the callback function to call
Expand All @@ -100,7 +104,7 @@ public static void cancelStaticInitializedCallback(BooleanCallback callback) {
* @see #registerStaticInitializedCallback(BooleanCallback, boolean)
*/
public ScopedObject<BooleanCallback> registerInitializedCallback(BooleanCallback callback, boolean initialNotify) {
getState().INITIALIZED_CALLBACKS.addIfAbsent(callback);
getState().INITIALIZED_CALLBACKS.add(callback);
if(initialNotify) {
callback.callback(id, getState().init);
}
Expand Down Expand Up @@ -144,7 +148,7 @@ private void setInitialized(boolean initialized, boolean notifyRobot) {
if(initialized) {
STATIC_INITIALIZED_CALLBACKS.forEach(CALL_INITIALIZED_CALLBACK);
getState().INITIALIZED_CALLBACKS.forEach(CALL_INITIALIZED_CALLBACK);
INITIALIZED_DEVICES.addIfAbsent(id);
INITIALIZED_DEVICES.add(id);
} else {
INITIALIZED_DEVICES.remove(id);
}
Expand All @@ -159,19 +163,21 @@ private void setInitialized(boolean initialized, boolean notifyRobot) {
public static String[] enumerateDevices() {
return INITIALIZED_DEVICES.toArray(new String[INITIALIZED_DEVICES.size()]);
}
{% endif -%}
{%- endif -%}

{%- if hasId %}

{% if hasId %}
@Override
protected State generateState() {
return new State();
}

{% else %}

{% else -%}

protected static {{ name }}Sim.State getState() {
return STATE;
}

{% endif -%}

{% for propName, prop in props -%}
Expand All @@ -184,7 +190,7 @@ protected State generateState() {
* @see #cancel{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback)
*/
public{{ cstatic }}ScopedObject<{{ varInfo.ptype }}Callback> register{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback callback, boolean initialNotify) {
getState().{{ varInfo.pnameu }}_CALLBACKS.addIfAbsent(callback);
getState().{{ varInfo.pnameu }}_CALLBACKS.add(callback);
if(initialNotify) {
callback.callback({{ cid }}, getState().{{ varInfo.pnamel }});
}
Expand All @@ -205,20 +211,23 @@ protected State generateState() {
}

/**
* @return {{ prop.description() | lower }}
* @return {{ prop.description() | lower | trim }}
* @see #set{{ varInfo.pname }}({{ varInfo.pprimtype }})
*/
public{{ cstatic }}{{ varInfo.pprimtype }} get{{ varInfo.pname }}() {
return getState().{{ varInfo.pnamel }};
}

{%- if varInfo.isRobotInput %}

/**
* Set {{ prop.description() | lower }}
* Set {{ prop.description() | lower | trim }}
* @see #get{{ varInfo.pname }}()
*/
public{{ cstatic }}void set{{ varInfo.pname }}({{ varInfo.pprimtype }} {{ varInfo.pnamel }}) {
set{{ varInfo.pname }}({{ varInfo.pnamel }}, true);
}
{%- endif %}

/**
* A Consumer which calls the given callback with the current {{ varInfo.pnamel }} value of this PWMSim
Expand All @@ -230,6 +239,9 @@ protected State generateState() {
getState().{{ varInfo.pnameu }}_CALLBACKS.forEach(CALL_{{ varInfo.pnameu }}_CALLBACK);
}
if(notifyRobot) {
{%- if not varInfo.isRobotInput %}
System.err.println("WARNING: {{ name }}Sim#set{{ varInfo.pname }}({{ varInfo.pprimtype }}, true) was called, but {{ varInfo.pfname }} is not a robot input!");
{%- endif %}
ConnectionProcessor.broadcastMessage({{ cid }}, "{{ type }}", new WSValue("{{ varInfo.pfname }}", {{ varInfo.pnamel }}));
}
}
Expand Down Expand Up @@ -279,7 +291,7 @@ public static void processMessage(String device, List<WSValue> data) {
private{{ cstatic }}void processValue(WSValue value) {
if(value.getKey() instanceof String && value.getValue() != null) {
switch((String)value.getKey()) {
{% if hasInit -%}
{%- if hasInit %}
case "<init": {
filterMessageAndIgnoreRobotState(value.getValue(), Boolean.class, SET_INITIALIZED);
break;
Expand All @@ -297,32 +309,31 @@ public static void processMessage(String device, List<WSValue> data) {
}
{%- endfor %}
default: {
if (!UNKNOWN_KEYS.contains(value.getKey())) {
if (UNKNOWN_KEYS.add(value.getKey())) {
System.err.println("{{ name }}Sim received value '" + value.getKey() + ":" + value.getValue() + "' but does not recognize '" + value.getKey() + "'. Values with that key will be ignored.");
UNKNOWN_KEYS.add(value.getKey());
}
}
}
}
}

/**
* Contains all information about the state of a {{ name }}Sim
* Contains all information about the state of {{ a }} {{ name }}Sim
*/
public static class State {
{% if hasInit -%}
public boolean init = false;
{% endif -%}
{% for propName, prop in props -%}
{% import "../partials/initVars.java" as varInfo with context -%}
{%- endif -%}
{%- for propName, prop in props -%}
{%- import "../partials/initVars.java" as varInfo with context %}
public {{ varInfo.pprimtype }} {{ varInfo.pnamel }} = {{ varInfo.pinit }};
{% endfor -%}
{%- endfor %}
{% if hasInit -%}
public final CopyOnWriteArrayList<BooleanCallback> INITIALIZED_CALLBACKS = new CopyOnWriteArrayList<>();
{% endif -%}
{% for propName, prop in props -%}
{% import "../partials/initVars.java" as varInfo with context -%}
public final CopyOnWriteArrayList<{{ varInfo.ptype }}Callback> {{ varInfo.pnameu }}_CALLBACKS = new CopyOnWriteArrayList<>();
public final Set<BooleanCallback> INITIALIZED_CALLBACKS = new CopyOnWriteArraySet<>();
{%- endif -%}
{%- for propName, prop in props -%}
{%- import "../partials/initVars.java" as varInfo with context %}
public final Set<{{ varInfo.ptype }}Callback> {{ varInfo.pnameu }}_CALLBACKS = new CopyOnWriteArraySet<>();
{%- endfor %}
}

Expand Down
Loading