Skip to content

Commit 7ef1493

Browse files
committed
Small refactoring
1 parent 1f3a3d2 commit 7ef1493

3 files changed

Lines changed: 45 additions & 39 deletions

File tree

examples/test/test.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
Copyright 2019 Jan-Eric Schober
33
44
Licensed under the Apache License, Version 2.0 (the "License");
@@ -69,13 +69,13 @@ void cmdHello( String arguments, StreamCommander * instance )
6969

7070
void cmdPointer( String arguments, StreamCommander * instance )
7171
{
72-
instance->sendResponse( "Pointer address is 0x" + String( (unsigned int)instance, HEX ) );
72+
instance->sendResponse( "Pointer address is 0x" + String( (unsigned int)instance, HEX ) );
7373
}
7474

7575
void cmdLed( String arguments, StreamCommander * instance )
7676
{
7777
arguments.trim();
78-
78+
7979
if ( arguments.equals( "on" ) )
8080
{
8181
digitalWrite( LED_BUILTIN, HIGH );

src/StreamCommander.cpp

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,43 @@ StreamCommander::DefaultCallbackFunction StreamCommander::getDefaultCallback()
364364
return this->defaultCallbackFunction;
365365
}
366366

367+
void StreamCommander::executeCommand( String command, String arguments )
368+
{
369+
// Send an Echo
370+
if ( shouldEchoCommands() )
371+
{
372+
if ( arguments.length() )
373+
{
374+
sendEcho( command + " " + arguments );
375+
}
376+
else
377+
{
378+
sendEcho( command );
379+
}
380+
}
381+
382+
// Try to find our input-command and execute it
383+
CommandContainer * container = getCommandContainer( command );
384+
385+
// If a container for this command has been found, try to call the callback
386+
if ( container != nullptr )
387+
{
388+
if ( container->callbackFunction != nullptr )
389+
{
390+
// Call our Callback-Function with the arguments and our object-instance
391+
container->callbackFunction( arguments, this );
392+
}
393+
else
394+
{
395+
sendError( "Command callback function for command '" + command + "' is empty." );
396+
}
397+
}
398+
else
399+
{
400+
getDefaultCallback()( command, arguments, this );
401+
}
402+
}
403+
367404
void StreamCommander::fetchCommand()
368405
{
369406
Stream * streamInstance = getStreamInstance();
@@ -419,39 +456,7 @@ void StreamCommander::fetchCommand()
419456

420457
command = commandBuffer.substring( 0, commandEnd );
421458

422-
// Send an Echo
423-
if ( shouldEchoCommands() )
424-
{
425-
if ( arguments.length() )
426-
{
427-
sendEcho( command + " " + arguments );
428-
}
429-
else
430-
{
431-
sendEcho( command );
432-
}
433-
}
434-
435-
// Try to find our input-command and execute it
436-
CommandContainer * container = getCommandContainer( command );
437-
438-
// If a container for this command has been found, try to call the callback
439-
if ( container != nullptr )
440-
{
441-
if ( container->callbackFunction != nullptr )
442-
{
443-
// Call our Callback-Function with the arguments and our object-instance
444-
container->callbackFunction( arguments, this );
445-
}
446-
else
447-
{
448-
sendError( "Command callback function for command '" + command + "' is empty." );
449-
}
450-
}
451-
else
452-
{
453-
getDefaultCallback()( command, arguments, this );
454-
}
459+
executeCommand( command, arguments );
455460
}
456461

457462
void StreamCommander::sendMessage( String type, String content )

src/StreamCommander.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,18 @@ class StreamCommander
8282
bool shouldAddStandardCommands(); // Returns whether the standard commadns should be added or not.
8383
void saveIdToEeprom( String id ); // Saves and ID to the EEPROM if it differs from the old one.
8484
void loadIdFromEeprom(); // Loads the ID from the EEPROM.
85+
CommandContainer * getCommandContainer( String command ); // Gets the container containing all commands.
86+
int getCommandContainerIndex( String command ); // Returns the index (position) of a specific command in the command container by name.
8587
void deleteCommands(); // Deletes all registered commands.
8688
void setNumCommands( int numCommands ); // Sets the number of the currently registered commands.
8789
void incrementNumCommands(); // Increments the number of the currently registered commands.
88-
CommandContainer * getCommandContainer( String command ); // Gets the container containing all commands.
89-
int getCommandContainerIndex( String command ); // Returns the index (position) of a specific command in the command container by name.
90+
void executeCommand( String command, String arguments ); // Tries to execute a command with given arguments. Arguments can be empty.
9091

9192
static void commandActivate( String arguments, StreamCommander * instance );// Definition of the command COMMAND_ACTIVATE.
9293
static void commandDeactivate( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_DEACTIVATE.
9394
static void commandIsActive( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_ISACTIVE.
9495
static void commandSetEcho( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_SETECHO.
95-
static void commandSetId( String id, StreamCommander * instance ); // Definition of the command COMMAND_SETID.
96+
static void commandSetId( String id, StreamCommander * instance ); // Definition of the command COMMAND_SETID.
9697
static void commandGetId( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_GETID.
9798
static void commandPing( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_PING.
9899
static void commandGetStatus( String arguments, StreamCommander * instance ); // Definition of the command COMMAND_GETSTATUS.

0 commit comments

Comments
 (0)