Skip to content

Commit

Permalink
Dev/pid widget (#15)
Browse files Browse the repository at this point in the history
* Added PID widget

* Added PID widget

* Fix formatting issue

* Fixed placeholder values for var inputs

* PIDF widget editing owkrs

* Add PID widget

* Version change
  • Loading branch information
JudeKratzer authored and andycate committed Feb 12, 2019
1 parent decf9af commit d8be29a
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
}

group = 'org.team5499'
version = '0.3.0' /* Change this when deploying a new version */
version = '0.4.0' /* Change this when deploying a new version */

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@
<script src="/javascript/widgets/string-editor.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/int-editor.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/double-editor.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/pidf-widget.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
<script src="/javascript/widgets/pid-widget.jsx" data-plugins="transform-es2015-modules-umd" type="text/babel"></script>
{% endblock %}
116 changes: 116 additions & 0 deletions src/main/resources/static/javascript/widgets/pid-widget.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import PageUtils from 'page-utils';
import Widget from 'widget';

class PIDWidget {}

PIDWidget.Body = class extends Widget.Body {
init() {
let newPValue = (this.getString(this.widgetConfig.variables.ptarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.ptarget);
let newIValue = (this.getString(this.widgetConfig.variables.itarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.itarget);
let newDValue = (this.getString(this.widgetConfig.variables.dtarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.dtarget);
this.state = {
pTargetName: this.widgetConfig.variables.ptarget,
iTargetName: this.widgetConfig.variables.itarget,
dTargetName: this.widgetConfig.variables.dtarget,
pTargetValue: newPValue,
iTargetValue: newIValue,
dTargetValue: newDValue

};
this.pCallbackId = this.registerVarListener(this.state.pTargetName, (key, value) => this.updateState(key, value));
this.iCallbackId = this.registerVarListener(this.state.iTargetName, (key, value) => this.updateState(key, value));
this.dCallbackId = this.registerVarListener(this.state.dTargetName, (key, value) => this.updateState(key, value));
}

updateState(key, value) {
let newState = {};
for(var i in this.state) {
if(this.state[i] === key) {
newState[i.substring(0, 1) + 'TargetValue'] = value;
break;
}
}
this.setState(newState);
}

settingsSave(newConfig) {
this.removeVarListener(this.state.pTargetName, this.pCallbackId);
this.removeVarListener(this.state.iTargetName, this.iCallbackId);
this.removeVarListener(this.state.dTargetName, this.dCallbackId);
let newPValue = (this.getString(newConfig.variables.ptarget) === undefined) ? '' : this.getString(newConfig.variables.ptarget);
let newIValue = (this.getString(newConfig.variables.itarget) === undefined) ? '' : this.getString(newConfig.variables.itarget);
let newDValue = (this.getString(newConfig.variables.dtarget) === undefined) ? '' : this.getString(newConfig.variables.dtarget);
this.setState({
pTargetName: newConfig.variables.ptarget,
iTargetName: newConfig.variables.itarget,
dTargetName: newConfig.variables.dtarget,
pTargetValue: newPValue,
iTargetValue: newIValue,
dTargetValue: newDValue
});
this.pCallbackId = this.registerVarListener(newConfig.variables.ptarget, (key, value) => this.updateState(key, value));
this.iCallbackId = this.registerVarListener(newConfig.variables.itarget, (key, value) => this.updateState(key, value));
this.dCallbackId = this.registerVarListener(newConfig.variables.dtarget, (key, value) => this.updateState(key, value));
}

onVarSave() {
this.setDouble(this.state.pTargetName, this.state.pTargetValue);
this.setDouble(this.state.iTargetName, this.state.iTargetValue);
this.setDouble(this.state.dTargetName, this.state.dTargetValue);
}

onFieldEdit(e) {
let newValue = (e.target.value === "") ? "" : parseFloat(e.target.value);
let newState = {};
newState[e.target.name + 'TargetValue'] = newValue;
this.setState(newState);
}

render() {
return (
<div>
<input className='form-control mb-2' name='p' type='number' placeholder='value' value={this.state.pTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<input className='form-control mb-2' name='i' type='number' placeholder='value' value={this.state.iTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<input className='form-control mb-2' name='d' type='number' placeholder='value' value={this.state.dTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<button className='btn btn-primary' onClick={() => this.onVarSave()}>Submit</button>
</div>
);
}
}

PIDWidget.Settings = class extends Widget.Settings {
init() {
this.state = {
pTargetName: this.widgetConfig.variables.ptarget,
iTargetName: this.widgetConfig.variables.itarget,
dTargetName: this.widgetConfig.variables.dtarget
};
}

settingsData(config) {
let newConfig = config;
newConfig.variables.ptarget = this.state.pTargetName;
newConfig.variables.itarget = this.state.iTargetName;
newConfig.variables.dtarget = this.state.dTargetName;
return newConfig;
}

onSettingsEdit(e) {
let newState = {};
newState[e.target.name + 'TargetName'] = e.target.value;
this.setState(newState);
}

render() {
return [
<input className='form-control mb-2' key='p' type='text' name='p' placeholder='variable' value={this.state.pTargetName} onChange={(e) => this.onSettingsEdit(e)} />,
<input className='form-control mb-2' key='i' type='text' name='i' placeholder='variable' value={this.state.iTargetName} onChange={(e) => this.onSettingsEdit(e)} />,
<input className='form-control mb-2' key='d' type='text' name='d' placeholder='variable' value={this.state.dTargetName} onChange={(e) => this.onSettingsEdit(e)} />
];
}
}

export default PIDWidget;

// make sure to do this for every widget
PageUtils.addWidgetClass('PIDWidget', PIDWidget);
130 changes: 130 additions & 0 deletions src/main/resources/static/javascript/widgets/pidf-widget.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import PageUtils from 'page-utils';
import Widget from 'widget';

class PIDFWidget {}

PIDFWidget.Body = class extends Widget.Body {
init() {
let newPValue = (this.getString(this.widgetConfig.variables.ptarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.ptarget);
let newIValue = (this.getString(this.widgetConfig.variables.itarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.itarget);
let newDValue = (this.getString(this.widgetConfig.variables.dtarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.dtarget);
let newFValue = (this.getString(this.widgetConfig.variables.ftarget) === undefined) ? '' : this.getString(this.widgetConfig.variables.ftarget);
this.state = {
pTargetName: this.widgetConfig.variables.ptarget,
iTargetName: this.widgetConfig.variables.itarget,
dTargetName: this.widgetConfig.variables.dtarget,
fTargetName: this.widgetConfig.variables.ftarget,
pTargetValue: newPValue,
iTargetValue: newIValue,
dTargetValue: newDValue,
fTargetValue: newFValue

};
this.pCallbackId = this.registerVarListener(this.state.pTargetName, (key, value) => this.updateState(key, value));
this.iCallbackId = this.registerVarListener(this.state.iTargetName, (key, value) => this.updateState(key, value));
this.dCallbackId = this.registerVarListener(this.state.dTargetName, (key, value) => this.updateState(key, value));
this.fCallbackId = this.registerVarListener(this.state.fTargetName, (key, value) => this.updateState(key, value));
}

updateState(key, value) {
let newState = {};
for(var i in this.state) {
if(this.state[i] === key) {
newState[i.substring(0, 1) + 'TargetValue'] = value;
break;
}
}
this.setState(newState);
}

settingsSave(newConfig) {
this.removeVarListener(this.state.pTargetName, this.pCallbackId);
this.removeVarListener(this.state.iTargetName, this.iCallbackId);
this.removeVarListener(this.state.dTargetName, this.dCallbackId);
this.removeVarListener(this.state.fTargetName, this.fCallbackId);
let newPValue = (this.getString(newConfig.variables.ptarget) === undefined) ? '' : this.getString(newConfig.variables.ptarget);
let newIValue = (this.getString(newConfig.variables.itarget) === undefined) ? '' : this.getString(newConfig.variables.itarget);
let newDValue = (this.getString(newConfig.variables.dtarget) === undefined) ? '' : this.getString(newConfig.variables.dtarget);
let newFValue = (this.getString(newConfig.variables.ftarget) === undefined) ? '' : this.getString(newConfig.variables.ftarget);
this.setState({
pTargetName: newConfig.variables.ptarget,
iTargetName: newConfig.variables.itarget,
dTargetName: newConfig.variables.dtarget,
fTargetName: newConfig.variables.ftarget,
pTargetValue: newPValue,
iTargetValue: newIValue,
dTargetValue: newDValue,
fTargetValue: newFValue
});
this.pCallbackId = this.registerVarListener(newConfig.variables.ptarget, (key, value) => this.updateState(key, value));
this.iCallbackId = this.registerVarListener(newConfig.variables.itarget, (key, value) => this.updateState(key, value));
this.dCallbackId = this.registerVarListener(newConfig.variables.dtarget, (key, value) => this.updateState(key, value));
this.fCallbackId = this.registerVarListener(newConfig.variables.ftarget, (key, value) => this.updateState(key, value));
}

onVarSave() {
this.setDouble(this.state.pTargetName, this.state.pTargetValue);
this.setDouble(this.state.iTargetName, this.state.iTargetValue);
this.setDouble(this.state.dTargetName, this.state.dTargetValue);
this.setDouble(this.state.fTargetName, this.state.fTargetValue);
}

onFieldEdit(e) {
let newValue = (e.target.value === "") ? "" : parseFloat(e.target.value);
let newState = {};
newState[e.target.name + 'TargetValue'] = newValue;
this.setState(newState);
}

render() {
return (
<div>
<input className='form-control mb-2' name='p' type='number' placeholder='value' value={this.state.pTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<input className='form-control mb-2' name='i' type='number' placeholder='value' value={this.state.iTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<input className='form-control mb-2' name='d' type='number' placeholder='value' value={this.state.dTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<input className='form-control mb-2' name='f' type='number' placeholder='value' value={this.state.fTargetValue} onChange={(e) => this.onFieldEdit(e)} />
<button className='btn btn-primary' onClick={() => this.onVarSave()}>Submit</button>
</div>
);
}
}

PIDFWidget.Settings = class extends Widget.Settings {
init() {
this.state = {
pTargetName: this.widgetConfig.variables.ptarget,
iTargetName: this.widgetConfig.variables.itarget,
dTargetName: this.widgetConfig.variables.dtarget,
fTargetName: this.widgetConfig.variables.ftarget
};
}

settingsData(config) {
let newConfig = config;
newConfig.variables.ptarget = this.state.pTargetName;
newConfig.variables.itarget = this.state.iTargetName;
newConfig.variables.dtarget = this.state.dTargetName;
newConfig.variables.ftarget = this.state.fTargetName;
return newConfig;
}

onSettingsEdit(e) {
let newState = {};
newState[e.target.name + 'TargetName'] = e.target.value;
this.setState(newState);
}

render() {
return [
<input className='form-control mb-2' key='p' type='text' name='p' placeholder='variable' value={this.state.pTargetName} onChange={(e) => this.onSettingsEdit(e)} />,
<input className='form-control mb-2' key='i' type='text' name='i' placeholder='variable' value={this.state.iTargetName} onChange={(e) => this.onSettingsEdit(e)} />,
<input className='form-control mb-2' key='d' type='text' name='d' placeholder='variable' value={this.state.dTargetName} onChange={(e) => this.onSettingsEdit(e)} />,
<input className='form-control mb-2' key='f' type='text' name='f' placeholder='variable' value={this.state.fTargetName} onChange={(e) => this.onSettingsEdit(e)} />
];
}
}

export default PIDFWidget;

// make sure to do this for every widget
PageUtils.addWidgetClass('PIDFWidget', PIDFWidget);
44 changes: 44 additions & 0 deletions src/test/kotlin/tests/PlaygroundTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.team5499.dashboard.Dashboard
class PlaygroundTest {

@Test
@Suppress("LongMethod")
fun runServer() {
println("starting server...")
println("go to http://localhost:5800/")
Expand All @@ -34,6 +35,49 @@ class PlaygroundTest {
key: String, value: Any? ->
println("$key : $value")
})

Dashboard.setVariable("KP", 0.0)
Dashboard.setVariable("KI", 0.0)
Dashboard.setVariable("KD", 0.0)
Dashboard.setVariable("KF", 0.0)
Dashboard.addVarListener("KP", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KI", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KD", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KF", {
key: String, value: Any? ->
println("$key : $value")
})

Dashboard.setVariable("KP2", 0.1)
Dashboard.setVariable("KI2", 0.1)
Dashboard.setVariable("KD2", 0.1)
Dashboard.setVariable("KF2", 0.1)
Dashboard.addVarListener("KP2", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KI2", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KD2", {
key: String, value: Any? ->
println("$key : $value")
})
Dashboard.addVarListener("KF2", {
key: String, value: Any? ->
println("$key : $value")
})

println("server started")
while (true) { // find a better way to wait?
@Suppress("MagicNumber")
Expand Down
27 changes: 27 additions & 0 deletions src/test/resources/playgroundConf.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@
"target": "DOUBLE"
},
"width": "12rem"
},
{
"height": "auto",
"id": "pidfEditor",
"kwargs": {},
"title": "PIDF Variable",
"type": "PIDFWidget",
"variables": {
"dtarget": "KD",
"ftarget": "KF",
"itarget": "KI",
"ptarget": "KP"
},
"width": "12rem"
},
{
"height": "auto",
"id": "pidEditor",
"kwargs": {},
"title": "PID Variable",
"type": "PIDWidget",
"variables": {
"dtarget": "KD2",
"itarget": "KI2",
"ptarget": "KP2"
},
"width": "12rem"
}
]
}
Expand Down

0 comments on commit d8be29a

Please sign in to comment.