Skip to content

Commit dff5b3f

Browse files
authored
Merge pull request #527 from anjaldoshi/http-server-updates
[OpenEphysHttpServer] Add support for adding and deleting a processor
2 parents 9b098b4 + 95c1ca7 commit dff5b3f

3 files changed

Lines changed: 204 additions & 0 deletions

File tree

Source/UI/ProcessorList.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,36 @@ Array<String> ProcessorList::getItemList()
716716
return listOfProcessors;
717717
}
718718

719+
720+
Plugin::Description ProcessorList::getItemDescriptionfromList(const String& name)
721+
{
722+
723+
Plugin::Description description;
724+
725+
for (int i = 0; i < 5; i++)
726+
{
727+
int numSubItems = baseItem->getSubItem(i)->getNumSubItems();
728+
729+
ProcessorListItem* subItem = baseItem->getSubItem(i);
730+
731+
for(int j = 0; j < numSubItems ; j++)
732+
{
733+
if(name.equalsIgnoreCase(subItem->getSubItem(j)->getName()))
734+
{
735+
description.fromProcessorList = true;
736+
description.index = subItem->getSubItem(j)->index;
737+
description.name = subItem->getSubItem(j)->getName();
738+
description.type = subItem->getSubItem(j)->pluginType;
739+
description.processorType = subItem->getSubItem(j)->processorType;
740+
741+
break;
742+
}
743+
}
744+
}
745+
746+
return description;
747+
}
748+
719749
// ===================================================================
720750

721751
ProcessorListItem::ProcessorListItem(const String& name_,

Source/UI/ProcessorList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class ProcessorList : public Component,
9090
/** Get list of processors **/
9191
Array<String> getItemList();
9292

93+
Plugin::Description getItemDescriptionfromList(const String& processorName);
94+
9395
/** Set component bounds */
9496
void resized();
9597

Source/Utils/OpenEphysHttpServer.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "../MainWindow.h"
3535
#include "../AccessClass.h"
36+
#include "../UI/ProcessorList.h"
3637

3738
#include "Utils.h"
3839

@@ -82,6 +83,8 @@ using json = nlohmann::json;
8283
* - GET /api/processors/<processor_id>/streams/<stream_index>/parameters/<parameter_name>
8384
* - PUT /api/processors/<processor_id>/streams/<stream_index>/parameters/<parameter_name>
8485
* - PUT /api/processors/<processor_id>/config
86+
* - PUT /api/processors/add
87+
* - PUT /api/processors/delete
8588
* - PUT /api/window
8689
*
8790
* All endpoints are JSON endpoints. The PUT endpoint expects two parameters: "channel" (an integer), and "value",
@@ -497,6 +500,175 @@ class OpenEphysHttpServer : juce::Thread {
497500
res.set_content(ret.dump(), "application/json");
498501
});
499502

503+
svr_->Put("/api/processors/delete", [this](const httplib::Request& req, httplib::Response& res) {
504+
505+
LOGD( "Received PUT request" );
506+
507+
json request_json;
508+
try {
509+
LOGD( "Trying to decode" );
510+
request_json = json::parse(req.body);
511+
LOGD( "Parsed" );
512+
}
513+
catch (json::exception& e) {
514+
LOGD( "Hit exception" );
515+
res.set_content(e.what(), "text/plain");
516+
res.status = 400;
517+
return;
518+
}
519+
520+
int procId;
521+
if (!request_json.contains("id")) {
522+
LOGD( "No 'id' element found." );
523+
res.set_content("Request must contain processor id.", "text/plain");
524+
res.status = 400;
525+
return;
526+
}
527+
else {
528+
procId = request_json["id"];
529+
LOGD( "Found a processor id." );
530+
}
531+
532+
auto processor = find_processor(String(procId).toStdString());
533+
if (processor == nullptr) {
534+
LOGD( "Could not find processor" );
535+
res.status = 404;
536+
return;
537+
}
538+
539+
String return_msg;
540+
541+
if (!CoreServices::getAcquisitionStatus())
542+
{
543+
Array<GenericProcessor*> processorNodes;
544+
String processorName = processor->getDisplayName();
545+
546+
processorNodes.add(processor);
547+
548+
const MessageManagerLock mml;
549+
graph_->deleteNodes(processorNodes);
550+
551+
return_msg = processorName + " [" + String(procId) + "] deleted successfully";
552+
} else {
553+
return_msg = "Cannot delete processors while acquisition is active.";
554+
}
555+
556+
json ret;
557+
ret["info"] = return_msg.toStdString();
558+
res.set_content(ret.dump(), "application/json");
559+
});
560+
561+
562+
svr_->Put("/api/processors/add", [this](const httplib::Request& req, httplib::Response& res) {
563+
564+
LOGD( "Received PUT request" );
565+
566+
json request_json;
567+
try {
568+
LOGD( "Trying to decode" );
569+
request_json = json::parse(req.body);
570+
LOGD( "Parsed" );
571+
}
572+
catch (json::exception& e) {
573+
LOGD( "Hit exception" );
574+
res.set_content(e.what(), "text/plain");
575+
res.status = 400;
576+
return;
577+
}
578+
579+
std::string procName;
580+
if (!request_json.contains("name")) {
581+
LOGD( "No 'name' element found." );
582+
res.set_content("Request must contain processor name.", "text/plain");
583+
res.status = 400;
584+
return;
585+
}
586+
else {
587+
procName = request_json["name"];
588+
LOGD( "Found processor name: ", procName);
589+
}
590+
591+
int sourceNodeId = 0;
592+
int destNodeId = 0;
593+
if (!request_json.contains("source_id") && !request_json.contains("dest_id")) {
594+
LOGD( "No 'source_id' or 'dest_id' element found." );
595+
res.set_content("Request must contain source or destination processor node id.", "text/plain");
596+
res.status = 400;
597+
return;
598+
}
599+
else {
600+
if(request_json.contains("source_id"))
601+
sourceNodeId = request_json["source_id"];
602+
else
603+
destNodeId = request_json["dest_id"];
604+
605+
LOGD( "Found a source/dest node id." );
606+
}
607+
608+
609+
auto listOfProc = AccessClass::getProcessorList()->getItemList();
610+
bool foundProcessor = false;
611+
for(auto p : listOfProc)
612+
{
613+
if(p.equalsIgnoreCase(String(procName)))
614+
{
615+
foundProcessor = true;
616+
break;
617+
}
618+
}
619+
620+
if (!foundProcessor) {
621+
LOGD( "Could not find processor in the Processor List" );
622+
res.status = 404;
623+
return;
624+
}
625+
626+
String return_msg;
627+
628+
if (!CoreServices::getAcquisitionStatus())
629+
{
630+
auto description = AccessClass::getProcessorList()->getItemDescriptionfromList(procName);
631+
632+
GenericProcessor* sourceProcessor = nullptr;
633+
GenericProcessor* destProcessor = nullptr;
634+
635+
if (sourceNodeId == 0)
636+
{
637+
destProcessor = graph_->getProcessorWithNodeId(destNodeId);
638+
639+
if (destProcessor != nullptr)
640+
sourceProcessor = destProcessor->getSourceNode();
641+
}
642+
else
643+
{
644+
sourceProcessor = graph_->getProcessorWithNodeId(sourceNodeId);
645+
646+
if (sourceProcessor != nullptr)
647+
destProcessor = sourceProcessor->getDestNode();
648+
}
649+
650+
if (sourceProcessor == nullptr && destProcessor == nullptr)
651+
{
652+
return_msg = "Neither source node ID nor dest node ID could be found.";
653+
}
654+
else {
655+
const MessageManagerLock mml;
656+
graph_->createProcessor(description,
657+
sourceProcessor,
658+
destProcessor);
659+
660+
return_msg = procName + " added successfully";
661+
}
662+
663+
} else {
664+
return_msg = "Cannot add processors while acquisition is active.";
665+
}
666+
667+
json ret;
668+
ret["info"] = return_msg.toStdString();
669+
res.set_content(ret.dump(), "application/json");
670+
});
671+
500672
svr_->Put(R"(/api/processors/([0-9]+)/parameters/([A-Za-z0-9_\.\-]+))",
501673
[this](const httplib::Request& req, httplib::Response& res) {
502674
auto processor = find_processor(req.matches[1]);

0 commit comments

Comments
 (0)