Browse Source

Turning lights on/off

translation
Colin Reeder 4 years ago
parent
commit
454ced614e
4 changed files with 84 additions and 16 deletions
  1. +1
    -1
      Cargo.toml
  2. +39
    -13
      qml/Main.qml
  3. +9
    -2
      src/adapters.rs
  4. +35
    -0
      src/connection_connector.rs

+ 1
- 1
Cargo.toml View File

@@ -10,7 +10,7 @@ qmetaobject = "0.0.5"
cstr = "0.1.0"
cpp = "0.5"
tokio = "0.1.15"
huey = { git = "https://git.vpzom.click/vpzom/huey", rev = "305a5261" }
huey = { git = "https://git.vpzom.click/vpzom/huey", rev = "9e3b6673" }
# huey = { path = "../hueclient" }
futures = "0.1.25"
try_future = "0.1.3"


+ 39
- 13
qml/Main.qml View File

@@ -100,15 +100,23 @@ ApplicationWindow {
ListView {
height: parent.height
width: parent.width
spacing: 10
model: ListModel {
id: lightListModel
}
delegate: Component {
Item {
RowLayout {
height: 20
Row {
Label {
text: name
width: parent.width
Label {
text: name
}
Item { Layout.fillWidth: true }
Switch {
id: onSwitch
checked: model.state.on
onToggled: {
connector.start_set_light_on(model.id, checked);
}
}
}
@@ -118,18 +126,36 @@ ApplicationWindow {
BridgeConnectionConnector {
id: connector

function fetchLight(id) {
const light = connector.get_light(id);
lights[id] = light;

for(var i = 0; i < lightListModel.count; i++) {
const item = lightListModel.get(i);
if(item.id == id) {
console.log("replacing");
item.name = light.name.toString();
item.state = light.state;
return;
}
}

console.log("new item");

const newItem = {name: light.name.toString(), state: light.state, id: id};
console.log(JSON.stringify(newItem));
lightListModel.append(newItem);
}

onLights_changed: {
const ids = connector.get_lights();
const newLights = {};
ids.forEach(function(id) {
newLights[id] = connector.get_light(id);
});
lights = newLights;

lights = {};
lightListModel.clear();
Object.keys(lights).forEach(function(id) {
lightListModel.append({name: lights[id].name.toString()});
});
ids.forEach(fetchLight);
}

onLight_changed: {
fetchLight(id);
}
}



+ 9
- 2
src/adapters.rs View File

@@ -1,15 +1,22 @@
use qmetaobject::*;

#[derive(QObject, Default, SimpleListItem)]
#[derive(QObject, Default)]
pub struct RadiateLight {
base: qt_base_class!(trait QObject),
pub name: qt_property!(String),
name: qt_property!(String),
state: qt_property!(RadiateLightState),
}

#[derive(QGadget, Default, Clone)]
pub struct RadiateLightState {
on: qt_property!(bool),
}

impl From<&huey::connection::Light> for RadiateLight {
fn from(light: &huey::connection::Light) -> Self {
let mut result: Self = Default::default();
result.name = light.name.clone();
result.state.on = light.state.on;

result
}


+ 35
- 0
src/connection_connector.rs View File

@@ -34,7 +34,9 @@ pub struct BridgeConnectionConnector {
init: qt_method!(fn(&self, address: String, username: String)),
get_lights: qt_method!(fn(&self) -> QVariant),
get_light: qt_method!(fn(&self, id: String) -> QVariant),
start_set_light_on: qt_method!(fn(&self, id: String, on: bool)),
lights_changed: qt_signal!(),
light_changed: qt_signal!(id: String),
}

impl BridgeConnectionConnector {
@@ -97,4 +99,37 @@ impl BridgeConnectionConnector {

Default::default()
}

fn start_set_light_on(&self, id: String, on: bool) {
let callback = {
let ptr = QPointer::from(&*self);
let id = id.clone();
queued_callback(move |_| {
ptr.as_ref().map(|x| x.light_changed(id.clone()));
})
};
if let Some(ref state) = *STATE.read().unwrap() {
let future = state.connection.set_light_on(&id, on);
std::mem::drop(state);
tokio::spawn(future
.then(move |res| {
match res {
Ok(_) => {
if let Some(ref mut state) = *STATE.write().unwrap() {
if let Some(ref mut lights) = state.lights {
if let Some(ref mut light) = lights.get_mut(&id) {
light.state.on = on;
}
}
}
},
Err(err) => {
eprintln!("{:?}", err);
}
}
callback(());
Ok(())
}));
}
}
}

Loading…
Cancel
Save