Commit a79f7525 by dongshufeng

refactor(iesplan): slider and switch config support in para card

parent 9c6c838a
...@@ -49,7 +49,8 @@ pub enum ParaType { ...@@ -49,7 +49,8 @@ pub enum ParaType {
Radio, Radio,
Switch, Switch,
Select(Vec<f64>), Select(Vec<f64>),
Slider(f64, f64, f64, bool), // min, max, step
Slider(f64, f64, f64),
TextField, TextField,
} }
...@@ -83,8 +84,20 @@ pub fn create_parameters(content: &[u8]) -> Parameters { ...@@ -83,8 +84,20 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
"RADIO" => ParaType::Radio, "RADIO" => ParaType::Radio,
"SWITCH" => ParaType::Switch, "SWITCH" => ParaType::Switch,
"TEXTFIELD" => ParaType::TextField, "TEXTFIELD" => ParaType::TextField,
// "SLIDE" => ParaType::Slider(), "SLIDER" => {
// "SLIDE" => ParaType::Checkbox, let v = csv_str(&row, 3).unwrap();
let s_vec: Vec<&str> = v.split(";").collect();
let min = s_vec[0].parse().unwrap();
let max = s_vec[1].parse().unwrap();
let step = s_vec[2].parse().unwrap();
ParaType::Slider(min, max, step)
},
"SELECT" => {
let v = csv_str(&row, 3).unwrap();
let floats = v.split(";")
.map(|s| s.parse::<f64>().unwrap()).collect();
ParaType::Select(floats)
}
_ => ParaType::TextField _ => ParaType::TextField
}; };
para_types.push(para_type); para_types.push(para_type);
......
use eig_domain::{MeasureValue, SetPointValue};
use std::collections::HashMap; use std::collections::HashMap;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use web_sys::InputEvent; use web_sys::InputEvent;
use yew::prelude::*; use yew::prelude::*;
use yew_bulma::*;
use yew_bulma::calendar::get_timestamp; use yew_bulma::calendar::get_timestamp;
use eig_domain::{MeasureValue, SetPointValue}; use yew_bulma::*;
use eig_expr::{Expr, Token};
use crate::{get_headers, get_user_id, ParaType, Parameters, PointControl3, QueryWithId}; use crate::{get_headers, get_user_id, ParaType, Parameters, PointControl3, QueryWithId};
pub enum Msg { pub enum Msg {
...@@ -14,6 +14,7 @@ pub enum Msg { ...@@ -14,6 +14,7 @@ pub enum Msg {
SetBool(usize, bool), SetBool(usize, bool),
SetString(usize), SetString(usize),
SetOption(usize, String), SetOption(usize, String),
SetParaSuccess(Vec<u64>),
None, None,
} }
...@@ -47,14 +48,14 @@ impl Component for ParaCard { ...@@ -47,14 +48,14 @@ impl Component for ParaCard {
} }
pos.insert(ctx.props().paras.points[index], index); pos.insert(ctx.props().paras.points[index], index);
} }
Self::query_para(ctx); Self::query_para(ctx, &ctx.props().paras.points);
Self { bools, floats, pos } Self { bools, floats, pos }
} }
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg { match msg {
Msg::Refresh => { Msg::Refresh => {
Self::query_para(ctx); Self::query_para(ctx, &ctx.props().paras.points);
} }
Msg::ParaLoaded(values) => { Msg::ParaLoaded(values) => {
for v in values { for v in values {
...@@ -70,67 +71,28 @@ impl Component for ParaCard { ...@@ -70,67 +71,28 @@ impl Component for ParaCard {
} }
Msg::SetBool(i, b) => { Msg::SetBool(i, b) => {
let point_id = &ctx.props().paras.points[i]; let point_id = &ctx.props().paras.points[i];
let input_type = &ctx.props().paras.para_types[i]; let value = if b {
let v = if ParaType::Checkbox.eq(input_type) "1.0"
|| ParaType::Radio.eq(input_type)
|| ParaType::Switch.eq(input_type) {
if b {
Some(Expr::from_vec(vec![Token::Number(1.0)]))
} else {
Some(Expr::from_vec(vec![Token::Number(0.0)]))
}
} else { } else {
None "0.0"
}; };
if let Some(expr) = v { self.do_set_point(ctx, value, point_id);
let user_id = get_user_id();
let v = SetPointValue {
point_id: *point_id,
sender_id: user_id as u64,
command: expr,
timestamp: get_timestamp(),
};
self.set_point(ctx, PointControl3 { commands: vec![v] });
}
if let Some(v) = self.bools.get_mut(&i) {
*v = b;
return true;
}
} }
Msg::SetString(i) => { Msg::SetString(i) => {
let point_id = &ctx.props().paras.points[i]; let point_id = &ctx.props().paras.points[i];
let name = format!("tf_{}", point_id); let name = format!("tf_{}", point_id);
let value = get_input_value_by_name(&name); let value = get_input_value_by_name(&name);
if let Ok(expr) = value.parse() { self.do_set_point(ctx, &value, point_id);
let user_id = get_user_id();
let v = SetPointValue {
point_id: *point_id,
sender_id: user_id as u64,
command: expr,
timestamp: get_timestamp(),
};
self.set_point(ctx, PointControl3 { commands: vec![v] });
} else {
alert("Wrong input");
}
} }
Msg::SetOption(i, value) => { Msg::SetOption(i, value) => {
if value == "None" { if value == "None" {
return false; return false;
} }
let point_id = &ctx.props().paras.points[i]; let point_id = &ctx.props().paras.points[i];
if let Ok(expr) = value.parse() { self.do_set_point(ctx, &value, point_id);
let user_id = get_user_id(); }
let v = SetPointValue { Msg::SetParaSuccess(points) => {
point_id: *point_id, Self::query_para(ctx, &points);
sender_id: user_id as u64,
command: expr,
timestamp: get_timestamp(),
};
self.set_point(ctx, PointControl3 { commands: vec![v] });
} else {
alert("Wrong input");
}
} }
Msg::None => {} Msg::None => {}
} }
...@@ -172,10 +134,9 @@ impl ParaCard { ...@@ -172,10 +134,9 @@ impl ParaCard {
ParaType::Checkbox => { ParaType::Checkbox => {
let checked = self.bools.get(&i).cloned().unwrap_or(false); let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! { html! {
<Field horizontal={true} > <Field horizontal={true} label={label}>
<Checkbox checked={checked} <Checkbox checked={checked}
update={link.callback(move |b| Msg::SetBool(i, b))}> update={link.callback(move |b| Msg::SetBool(i, b))}>
{label}
</Checkbox> </Checkbox>
</Field> </Field>
} }
...@@ -183,11 +144,10 @@ impl ParaCard { ...@@ -183,11 +144,10 @@ impl ParaCard {
ParaType::Radio => { ParaType::Radio => {
let checked = self.bools.get(&i).cloned().unwrap_or(false); let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! { html! {
<Field horizontal={true} > <Field horizontal={true} label={label}>
<Radio update={link.callback(move |_| Msg::SetBool(i, !checked))} <Radio update={link.callback(move |_| Msg::SetBool(i, !checked))}
checked_value={"selected"} checked_value={"selected"}
value={if checked {"selected"} else {"none"}}> value={if checked {"selected"} else {"none"}}>
<span>{label}</span>
</Radio> </Radio>
</Field> </Field>
} }
...@@ -202,7 +162,7 @@ impl ParaCard { ...@@ -202,7 +162,7 @@ impl ParaCard {
</Field> </Field>
} }
} }
ParaType::Slider(lower, upper, step, is_vertical) => { ParaType::Slider(lower, upper, step) => {
let oninput = link.callback(move |e: InputEvent| { let oninput = link.callback(move |e: InputEvent| {
let target = e.target().unwrap(); let target = e.target().unwrap();
let input = target.dyn_into::<web_sys::HtmlInputElement>().unwrap(); let input = target.dyn_into::<web_sys::HtmlInputElement>().unwrap();
...@@ -210,8 +170,7 @@ impl ParaCard { ...@@ -210,8 +170,7 @@ impl ParaCard {
}); });
html! { html! {
<Field horizontal={true} label={label}> <Field horizontal={true} label={label}>
<input class={"slider is-fullwidth"} type={"range"} <input class={"slider is-fullwidth"} type={"range"} orient={"horizontal"}
orient={if *is_vertical {"vertical"} else {"horizontal"}}
oninput={oninput} step={step.to_string()} min={lower.to_string()} oninput={oninput} step={step.to_string()} min={lower.to_string()}
max={upper.to_string()} value={lower.to_string()} max={upper.to_string()} value={lower.to_string()}
/> />
...@@ -250,11 +209,24 @@ impl ParaCard { ...@@ -250,11 +209,24 @@ impl ParaCard {
} }
} }
} }
}
impl ParaCard { fn do_set_point(&mut self, ctx: &Context<Self>, value: &str, point_id: &u64) {
fn query_para(ctx: &Context<Self>) { if let Ok(expr) = value.parse() {
let ids: Vec<String> = ctx.props().paras.points.iter().map(|s| s.to_string()).collect(); let user_id = get_user_id();
let v = SetPointValue {
point_id: *point_id,
sender_id: user_id as u64,
command: expr,
timestamp: get_timestamp(),
};
Self::set_point(ctx, PointControl3 { commands: vec![v] });
} else {
alert("Wrong input");
}
}
fn query_para(ctx: &Context<Self>, points: &[u64]) {
let ids: Vec<String> = points.iter().map(|s| s.to_string()).collect();
let ids = ids.join(",").to_string(); let ids = ids.join(",").to_string();
let query = QueryWithId { let query = QueryWithId {
id: None, id: None,
...@@ -283,17 +255,18 @@ impl ParaCard { ...@@ -283,17 +255,18 @@ impl ParaCard {
Msg::None Msg::None
}); });
} }
fn set_point(&self, ctx: &Context<Self>, cmd: PointControl3) { fn set_point(ctx: &Context<Self>, cmd: PointControl3) {
let url = "/api/v1/controls_cbor/points_by_expr"; let url = "/api/v1/controls_cbor/points_by_expr";
let points: Vec<u64> = cmd.commands.iter().map(|c| c.point_id).collect();
ctx.link().send_future(async move { ctx.link().send_future(async move {
let content = serde_cbor::to_vec(&cmd).unwrap(); let content = serde_cbor::to_vec(&cmd).unwrap();
let body = js_sys::Uint8Array::from(content.as_ref()).dyn_into().unwrap(); let body = js_sys::Uint8Array::from(content.as_ref()).dyn_into().unwrap();
match async_ws_post_no_resp(url, &get_headers(), Some(body)).await { match async_ws_post_no_resp(url, &get_headers(), Some(body)).await {
Ok(b) => { Ok(b) => {
if b { if !b {
alert("Success"); alert("Fail to set parameter");
} else { } else {
alert("Fail"); return Msg::SetParaSuccess(points);
} }
} }
Err(err) => { Err(err) => {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论