Commands backend, saving to file

This commit is contained in:
James Barnsley
2018-10-12 16:10:29 +13:00
parent 74a77ed5b0
commit 49aa6df033
12 changed files with 568 additions and 311 deletions

View File

@ -197,6 +197,12 @@ export function addQueueMetadata(tlids = [], from_uri = null){
* Commands (buttons)
**/
export function getCommands(){
return {
type: 'PUSHER_GET_COMMANDS'
}
}
export function setCommand(command){
return {
type: 'PUSHER_SET_COMMAND',
@ -211,10 +217,11 @@ export function removeCommand(id){
}
}
export function sendCommand(id){
export function sendCommand(id, notify = false){
return {
type: 'PUSHER_SEND_COMMAND',
id: id
id: id,
notify: notify
}
}

View File

@ -108,6 +108,9 @@ const PusherMiddleware = (function(){
case 'radio_stopped':
store.dispatch(pusherActions.radioStopped());
break;
case 'commands_changed':
store.dispatch(pusherActions.commandsUpdated(message.params.commands));
break;
case 'reload':
window.location.reload(true);
break;
@ -168,6 +171,8 @@ const PusherMiddleware = (function(){
}
return store => next => action => {
var pusher = store.getState().pusher;
switch(action.type){
case 'PUSHER_CONNECT':
@ -227,6 +232,7 @@ const PusherMiddleware = (function(){
store.dispatch(pusherActions.getConfig());
store.dispatch(pusherActions.getRadio());
store.dispatch(pusherActions.getCommands());
store.dispatch(pusherActions.getQueueMetadata());
// Give things a few moments to setup before we check for version.
@ -303,7 +309,7 @@ const PusherMiddleware = (function(){
request(store, 'add_queue_metadata', {
tlids: action.tlids,
added_from: action.from_uri,
added_by: store.getState().pusher.username
added_by: pusher.username
})
break;
@ -392,8 +398,24 @@ const PusherMiddleware = (function(){
* Commands
**/
case 'PUSHER_GET_COMMANDS':
request(store, 'get_commands')
.then(
response => {
store.dispatch(pusherActions.commandsUpdated(response.commands));
},
error => {
store.dispatch(coreActions.handleException(
'Could not get commands',
error
));
}
);
next(action);
break
case 'PUSHER_SET_COMMAND':
var commands_index = Object.assign({}, store.getState().pusher.commands);
var commands_index = Object.assign({}, pusher.commands);
if (commands_index[action.command.id]){
var command = Object.assign({}, commands_index[action.command.id], action.command);
@ -402,36 +424,77 @@ const PusherMiddleware = (function(){
}
commands_index[action.command.id] = command;
store.dispatch(pusherActions.commandsUpdated(commands_index));
request(store, 'set_commands', {commands: commands_index})
.then(
response => {
// No action required, the change will be broadcast
},
error => {
store.dispatch(coreActions.handleException(
'Could not remove command',
error
));
}
);
next(action);
break
case 'PUSHER_REMOVE_COMMAND':
var commands_index = Object.assign({}, store.getState().pusher.commands);
var commands_index = Object.assign({}, pusher.commands);
delete commands_index[action.id];
store.dispatch(pusherActions.commandsUpdated(commands_index));
request(store, 'set_commands', {commands: commands_index})
.then(
response => {
store.dispatch(pusherActions.commandsUpdated(response.commands));
},
error => {
store.dispatch(coreActions.handleException(
'Could not remove command',
error
));
}
);
next(action);
break
case 'PUSHER_SEND_COMMAND':
var commands_index = Object.assign({}, pusher.commands);
// Prepare our command
// We try and make it cross-origin compatible
var command = JSON.parse(commands_index[action.id]);
command.crossDomain = true;
command.dataType = "jsonp";
command.success = function(response){
store.dispatch(uiActions.createNotification({type: 'info', content: 'Command sent', description: command.url}));
var command = Object.assign({}, pusher.commands[action.id]);
var notification_key = 'command_'+action.id;
if (action.notify){
store.dispatch(uiActions.startProcess(notification_key, 'Sending command'));
}
command.fail = function(xhr, status, error){
store.dispatch(uiActions.createNotification({type: 'bad', content: 'Command failed', description: error}));
try {
var ajax_settings = JSON.parse(command.command);
} catch(error){
store.dispatch(uiActions.createNotification({key: notification_key, type: 'bad', content: 'Command failed', description: error}));
break;
}
// We try and make it cross-origin compatible
ajax_settings.crossDomain = true;
ajax_settings.dataType = "jsonp";
// Handle success and failure
ajax_settings.success = function(response){
console.log("Command sent, response was:",response);
if (action.notify){
store.dispatch(uiActions.processFinished(notification_key));
store.dispatch(uiActions.createNotification({key: notification_key, type: 'info', content: 'Command sent'}));
}
}
ajax_settings.fail = function(xhr, status, error){
store.dispatch(uiActions.processFinished(notification_key));
store.dispatch(uiActions.createNotification({key: notification_key, type: 'bad', content: 'Command failed', description: error}));
}
// Actually send the request
$.ajax(command);
$.ajax(ajax_settings);
break
@ -498,7 +561,7 @@ const PusherMiddleware = (function(){
{
notification: {
type: 'info',
content: store.getState().pusher.username + ' is starting radio mode'
content: pusher.username + ' is starting radio mode'
}
}
));
@ -535,7 +598,7 @@ const PusherMiddleware = (function(){
{
notification: {
type: 'info',
content: store.getState().pusher.username + ' stopped radio mode'
content: pusher.username + ' stopped radio mode'
}
}
));

View File

@ -161,19 +161,25 @@ class Settings extends React.Component {
}
return (
<div className="list">
<div className="list commands">
{
commands.map(command => {
return (
<Link className="list__item command__item commands-setup__item" key={command.id} to={global.baseURL+'edit-command/'+command.id}>
<span className="col col--w30">
<div className="list__item commands__item commands-setup__item" key={command.id}>
<span className="col col--w90">
<Icon name={command.icon} />
{command.icon}
</span>
<span className="col col--w70">
&nbsp;&nbsp;
{command.command && command.command.url ? command.command.url : "-"}
</span>
</Link>
<div className="commands__item__actions">
<a className="commands__item__trigger-button action" onClick={e => this.props.pusherActions.sendCommand(command.id, true)}>
<Icon name="send" />
</a>
<Link className="commands__item__edit-button action" to={global.baseURL+'edit-command/'+command.id}>
<Icon name="edit" />
</Link>
</div>
</div>
);
})
}

View File

@ -18,7 +18,7 @@ class EditCommand extends React.Component{
this.state = {
id: helpers.generateGuid(),
icon: 'power_settings_new',
command: '{"url":"https://myserver.local:8080/sendCommand/power"}'
command: '{"url":"https://'+window.location.hostname+'/sendCommand/power"}'
}
}

View File

@ -108,7 +108,7 @@ main {
text-decoration: none;
cursor: pointer;
&:not(.control) {
&:not(.control):not(.action) {
border-bottom: 1px solid transparent;
&:hover {

View File

@ -32,6 +32,24 @@
}
}
.commands {
padding-bottom: 20px;
&__item {
position: relative;
&__actions {
position: absolute;
top: 12px;
right: 0;
.action {
padding: 12px 8px;
}
}
}
}
.field.pusher-connections {
.connection.me {
color: $turquoise;