|
33 | 33 |
|
34 | 34 | #include "../MainWindow.h" |
35 | 35 | #include "../AccessClass.h" |
| 36 | +#include "../UI/ProcessorList.h" |
36 | 37 |
|
37 | 38 | #include "Utils.h" |
38 | 39 |
|
@@ -82,6 +83,8 @@ using json = nlohmann::json; |
82 | 83 | * - GET /api/processors/<processor_id>/streams/<stream_index>/parameters/<parameter_name> |
83 | 84 | * - PUT /api/processors/<processor_id>/streams/<stream_index>/parameters/<parameter_name> |
84 | 85 | * - PUT /api/processors/<processor_id>/config |
| 86 | + * - PUT /api/processors/add |
| 87 | + * - PUT /api/processors/delete |
85 | 88 | * - PUT /api/window |
86 | 89 | * |
87 | 90 | * All endpoints are JSON endpoints. The PUT endpoint expects two parameters: "channel" (an integer), and "value", |
@@ -497,6 +500,175 @@ class OpenEphysHttpServer : juce::Thread { |
497 | 500 | res.set_content(ret.dump(), "application/json"); |
498 | 501 | }); |
499 | 502 |
|
| 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 | + |
500 | 672 | svr_->Put(R"(/api/processors/([0-9]+)/parameters/([A-Za-z0-9_\.\-]+))", |
501 | 673 | [this](const httplib::Request& req, httplib::Response& res) { |
502 | 674 | auto processor = find_processor(req.matches[1]); |
|
0 commit comments