Skip to content

Commit

Permalink
load yamaha-avr and philips-hue cards from providers instead of stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjoehnk committed Aug 8, 2017
1 parent 055f694 commit e225b68
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 95 deletions.
20 changes: 15 additions & 5 deletions frontends/angular-stream/lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Router } = require('express');
const { stream, chips, presence } = require('../stubs');
const { stream: streamStub, chips, presence } = require('../stubs');
const providers = require('../providers');
const stream = require('../stream');

module.exports = config => {
const router = new Router();
Expand All @@ -10,10 +11,19 @@ module.exports = config => {
activateScene
} = providers(config);

router.get('/stream', (req, res) => {
res.status(200);
res.json(stream);
res.end();
const {
fetchStream
} = stream(config);

router.get('/stream', async(req, res, next) => {
try {
const stream = await fetchStream();
res.status(200);
res.json([...stream, ...streamStub]);
res.end();
}catch (err) {
return next(err);
}
});

router.get('/chips', (req, res) => {
Expand Down
27 changes: 26 additions & 1 deletion frontends/angular-stream/lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = config => {
const getProvidersOfType = type =>
config.providers.filter(provider => provider.type === type);

const getProviderByName = name => config.providers.find(provider => provider.name === name);

const _fetchScenes = async() => {
const providers = getProvidersOfType('generic-scenes');
return await Promise.all(providers.map(({ url }) =>
Expand Down Expand Up @@ -46,12 +48,35 @@ module.exports = config => {
}, scene));
};

const fetchInputs = ({ url }) => fetch(`${url}/inputs`)
.then(async res => {
if (res.ok) {
return res;
}
const body = await res.json();
throw new Error(body.message);
})
.then(res => res.json());

const fetchLights = ({ url }) => fetch(`${url}/lights`)
.then(async res => {
if (res.ok) {
return res;
}
const body = await res.json();
throw new Error(body.message);
})
.then(res => res.json());

const activateScene = ({ url }, scene) => fetch(`${url}/scenes/${scene}/activate`);

return {
getProvidersOfType,
getProviderByName,
fetchScenes,
findProviderByScene,
activateScene
activateScene,
fetchInputs,
fetchLights
};
};
79 changes: 79 additions & 0 deletions frontends/angular-stream/lib/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const providers = require('./providers');

module.exports = config => {
const {
getProviderByName,
fetchInputs,
fetchLights
} = providers(config);

const fetchYamahaAvrCard = async(provider, { priority, name }) => {
const payload = {
name,
inputs: await fetchInputs(provider)
};
const card = {
type: 'yamaha-avr',
priority,
payload
};
return card;
};

const fetchPhilipsHueCards = async(provider, { priority, filter }) => {
const lights = await fetchLights(provider);
return lights
.filter(({ id, name }) => {
if (filter) {
return filter.includes(id) || filter.includes(name);
}
return true;
})
.map(light => ({
id: light.id,
type: light.type,
name: light.name,
state: {
power: light.state.on,
brightness: light.state.bri,
hue: light.state.hue,
saturation: light.state.sat,
effect: light.state.effect,
xy: light.state.xy,
ct: light.state.ct,
alert: light.state.alert,
colormode: light.state.colormode,
reachable: light.state.reachable
}
}))
.map(payload => ({
type: 'philips-hue',
payload,
priority
}));
};

const fetchStream = async() => {
const stream = await Promise.all(config.stream.cards.map(async card => {
switch (card.type) {
case 'yamaha-avr': {
const provider = getProviderByName(card.provider);
const avr = await fetchYamahaAvrCard(provider, card);
return [avr];
}
case 'philips-hue': {
const provider = getProviderByName(card.provider);
const lights = await fetchPhilipsHueCards(provider, card);
return lights;
}
default:
throw new Error(`Invalid Card Type ${card.type}`);
}
}));
return stream.reduce((a, b) => a.concat(b));
};

return {
fetchStream
};
};
88 changes: 0 additions & 88 deletions frontends/angular-stream/lib/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,94 +142,6 @@ const stream = [
},
style: {},
priority: 100
},
{
type: 'philips-hue',
payload: {
id: 1,
state: {
power: false,
brightness: 254,
hue: 39392,
saturation: 13,
effect: 'none',
xy: [
0.3691,
0.3719
],
ct: 230,
alert: 'select',
colormode: 'xy',
reachable: true
},
type: 'Extended color light',
name: 'Mitte'
},
priority: 90
},
{
type: 'philips-hue',
payload: {
id: 2,
state: {
power: false,
brightness: 254,
hue: 39392,
saturation: 13,
effect: 'none',
xy: [
0.3691,
0.3719
],
ct: 230,
alert: 'select',
colormode: 'xy',
reachable: true
},
type: 'Extended color light',
name: 'Rechts'
},
priority: 90
},
{
type: 'philips-hue',
payload: {
id: 3,
state: {
power: true,
brightness: 254,
hue: 39392,
saturation: 13,
effect: 'none',
xy: [
0.3691,
0.3719
],
ct: 230,
alert: 'select',
colormode: 'xy',
reachable: true
},
type: 'Extended color light',
name: 'Links'
},
priority: 90
},
{
type: 'yamaha-avr',
payload: {
name: 'Max AV-Receiver',
zones: {
Main_Zone: {
input: 'Chrome',
volume: -300
}
},
inputs: [
'Chrome'
]
},
priority: 2
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</md-card-header>
<md-card-content fxLayout="column">
<md-select placeholder="Input">
<md-option *ngFor="let input of avr.inputs" [value]="input">{{ input }}</md-option>
<md-option *ngFor="let input of avr.inputs" [value]="input">{{ input.display || input.name }}</md-option>
</md-select>
</md-card-content>
</md-card>

0 comments on commit e225b68

Please sign in to comment.