Skip to content

Commit

Permalink
Switching to json! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Christiane Ruetten committed Feb 29, 2016
1 parent b52c98a commit b9b11e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
48 changes: 31 additions & 17 deletions src/adapters/philips_hue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ impl<T: Controller> ServiceAdapter for PhilipsHueAdapter<T> {
for _ in 0..120 {
controller.send_event(
EventData::AdapterNotification {
info: format!(
"{{\"adapter\": \"philips_hue\", \"message\": \"NeedsPairing\", \"hub\": \"{}\"}}",
hub.id)
}).unwrap();
info: json!({
adapter: "philips_hue",
message: "NeedsPairing",
hub: hub.id
})
}
).unwrap();
if hub.try_pairing() {
break;
}
Expand All @@ -87,18 +90,24 @@ impl<T: Controller> ServiceAdapter for PhilipsHueAdapter<T> {
info!("Paired with Philips Hue Bridge ID {}", hub.id);
controller.send_event(
EventData::AdapterNotification {
info: format!(
"{{\"adapter\": \"philips_hue\", \"message\": \"PairingSuccess\", \"hub\": \"{}\"}}",
hub.id)
}).unwrap();
info: json!({
adapter: "philips_hue",
message: "PairingSuccess",
hub: hub.id
})
}
).unwrap();
} else {
warn!("Pairing timeout with Philips Hue Bridge ID {}", hub.id);
controller.send_event(
EventData::AdapterNotification {
info: format!(
"{{\"adapter\": \"philips_hue\", \"message\": \"PairingTimeout\", \"hub\": \"{}\"}}",
hub.id)
}).unwrap();
info: json!({
adapter: "philips_hue",
message: "PairingTimeout",
hub: hub.id
})
}
).unwrap();
// Giving up for this Hub.
return;
}
Expand Down Expand Up @@ -165,11 +174,16 @@ impl<T: Controller> HueLightService<T> {
// internal design change.
let status = self.light.get_settings();
let light_state = self.light.get_state();
let json = format!(
"{{\"type\": \"{}\", \"available\": {}, \"on\": {}, \"hue\": {}, \"sat\": {}, \"val\": {}}}",
"device/light/colorlight",
status.state.reachable, status.state.on,
light_state.hue, light_state.sat, light_state.val);
let json = json!(
{
type: "device/light/colorlight",
available: status.state.reachable,
on: status.state.on,
hue: light_state.hue,
sat: light_state.sat,
val: light_state.val
}
);
let mut response = Response::with(json);
response.status = Some(Status::Ok);
response.headers.set(ContentType::json());
Expand Down
3 changes: 1 addition & 2 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ impl Controller for FoxBox {
let services = self.services.lock().unwrap();
match services.get(&id) {
None => {
let mut response = Response::with(format!(
"{{\"result\": \"error\", \"details\": \"No Such Service: {}\"}}", id));
let mut response = Response::with(json!({ error: "NoSuchService", id: id }));
response.status = Some(Status::BadRequest);
response.headers.set(AccessControlAllowOrigin::Any);
response.headers.set(ContentType::json());
Expand Down

0 comments on commit b9b11e4

Please sign in to comment.