import React, { PropTypes } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { Link, hashHistory } from 'react-router'; import VolumeControl from './Fields/VolumeControl'; import LatencyControl from './Fields/LatencyControl'; import TextField from './Fields/TextField'; import DropdownField from './Fields/DropdownField'; import Icon from './Icon'; import * as helpers from '../helpers'; import * as coreActions from '../services/core/actions'; import * as uiActions from '../services/ui/actions'; import * as pusherActions from '../services/pusher/actions'; import * as snapcastActions from '../services/snapcast/actions'; class Snapcast extends React.Component{ constructor(props){ super(props); this.state = { clients_expanded: [] } } componentDidMount(){ if (this.props.pusher_connected && this.props.snapcast_enabled){ this.props.snapcastActions.getServer(); } } componentWillReceiveProps(newProps){ // Just connected if (newProps.snapcast_enabled && !this.props.pusher_connected && newProps.pusher_connected){ this.props.snapcastActions.getServer(); // Just enabled // This is the more probable scenario as we don't know if we're enabled until pusher connects // and then gets the config from the server } else if (!this.props.snapcast_enabled && newProps.snapcast_enabled && newProps.pusher_connected){ this.props.snapcastActions.getServer(); } } toggleClientExpanded(client_id){ var clients_expanded = this.state.clients_expanded; var index = clients_expanded.indexOf(client_id); if (index >= 0){ clients_expanded.splice(index, 1); } else { clients_expanded.push(client_id); } this.setState({clients_expanded: clients_expanded}); } renderClientsList(group, groups){ if (!group.clients || group.clients.length <= 0){ return (
No clients
); } return (
{ group.clients.map(client => { var name = client.config.name ? client.config.name : client.host.name; var class_name = "list__item snapcast__client"; if (client.connected){ class_name += " snapcast__client--connected"; } else { class_name += " snapcast__client--disconnected"; } if (this.state.clients_expanded.includes(client.id)){ return (
this.toggleClientExpanded(client.id)}> {name}
{!client.connected ? : null}
Name
this.props.snapcastActions.setClientName(client.id, value)} value={name} />
Group
Volume
this.props.snapcastActions.setClientVolume(client.id, percent)} onMuteChange={mute => this.props.snapcastActions.setClientMute(client.id, mute)} />
Latency
this.props.snapcastActions.setClientLatency(client.id, parseInt(value))} /> this.props.snapcastActions.setClientLatency(client.id, parseInt(value))} value={String(client.config.latency)} />
Power on command Fire this HTTP request to turn power on
console.log(value)} value="http://localhost:8080/sendCommand/power_on" />
Power off command Fire this HTTP request to turn power off
console.log(value)} value="http://localhost:8080/sendCommand/power_off" />
); } else { return (
this.toggleClientExpanded(client.id)}> {name}
{!client.connected ? : null}
); } }) }
); } render(){ if (!this.props.snapcast_enabled){ return (

To enable Snapcast, edit your mopidy.conf file

) } if (!this.props.clients || !this.props.groups){ return (
) } var streams = []; for (var key in this.props.streams){ if (this.props.streams.hasOwnProperty(key)){ streams.push(this.props.streams[key]); } } // Construct a simple array of our groups index var groups = []; for (var group_id in this.props.groups){ if (this.props.groups.hasOwnProperty(group_id)){ var group = this.props.groups[group_id]; // Merge the group's clients into this group (also as a simple array) var clients = []; for (var i = 0; i < group.clients_ids.length; i++){ if (this.props.clients.hasOwnProperty(group.clients_ids[i])){ var client = this.props.clients[group.clients_ids[i]]; if (client.connected || this.props.show_disconnected_clients){ clients.push(client); } } } groups.push(Object.assign( {}, group, { clients: clients } )); } } return (
Display
{ groups.map(group => { // Average our clients' volume for an overall group volume var group_volume = 0; for (var i = 0; i < group.clients.length; i++){ var client = group.clients[i]; group_volume += client.config.volume.percent; } group_volume = group_volume / group.clients.length; return (
Name
{group.name ? group.name : 'Group '+group.id.substring(0,3)}   ({group.id})
Stream
Volume
this.props.snapcastActions.setGroupVolume(group.id, percent, old_percent)} onMuteChange={mute => this.props.snapcastActions.setGroupMute(group.id, mute)} />
Clients
{this.renderClientsList(group, groups)}
); }) }
); } } const mapStateToProps = (state, ownProps) => { return { snapcast_enabled: state.pusher.config.snapcast_enabled, pusher_connected: state.pusher.connected, show_disconnected_clients: (state.ui.snapcast_show_disconnected_clients !== undefined ? state.ui.snapcast_show_disconnected_clients : false), streams: (state.snapcast.streams ? state.snapcast.streams : null), groups: (state.snapcast.groups ? state.snapcast.groups : null), clients: (state.snapcast.clients ? state.snapcast.clients : null) } } const mapDispatchToProps = (dispatch) => { return { coreActions: bindActionCreators(coreActions, dispatch), uiActions: bindActionCreators(uiActions, dispatch), snapcastActions: bindActionCreators(snapcastActions, dispatch), pusherActions: bindActionCreators(pusherActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(Snapcast)