|
| 1 | +#ifndef MOCK_COMMANDHANDLER_H |
| 2 | +#define MOCK_COMMANDHANDLER_H |
| 3 | + |
| 4 | +#include "MockAmplifier.h" |
| 5 | +#include <asio.hpp> |
| 6 | +#include <functional> |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <unordered_map> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace mock { |
| 13 | + |
| 14 | +class NotificationHandler; |
| 15 | + |
| 16 | +using NotificationCallback = std::function<void(const std::string&)>; |
| 17 | + |
| 18 | +class CommandHandler : public std::enable_shared_from_this<CommandHandler> { |
| 19 | +public: |
| 20 | + CommandHandler(asio::io_context& io_context, uint16_t port, |
| 21 | + std::shared_ptr<MockAmplifier> amplifier, |
| 22 | + std::shared_ptr<NotificationHandler> notificationHandler); |
| 23 | + ~CommandHandler(); |
| 24 | + |
| 25 | + void start(); |
| 26 | + void stop(); |
| 27 | + |
| 28 | +private: |
| 29 | + void acceptConnections(); |
| 30 | + void handleConnection(std::shared_ptr<asio::ip::tcp::socket> socket); |
| 31 | + void readCommand(std::shared_ptr<asio::ip::tcp::socket> socket); |
| 32 | + |
| 33 | + std::string processCommand(const std::string& command); |
| 34 | + std::string parseAndExecuteCommand(const std::string& cmdLine); |
| 35 | + |
| 36 | + // Command implementations |
| 37 | + std::string cmdNone(int64_t ampId, int16_t channel, const std::string& value); |
| 38 | + std::string cmdStart(int64_t ampId, int16_t channel, const std::string& value); |
| 39 | + std::string cmdStop(int64_t ampId, int16_t channel, const std::string& value); |
| 40 | + std::string cmdSetPower(int64_t ampId, int16_t channel, const std::string& value); |
| 41 | + std::string cmdReset(int64_t ampId, int16_t channel, const std::string& value); |
| 42 | + |
| 43 | + std::string cmdTurnAll10KOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 44 | + std::string cmdTurnChannel10KOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 45 | + std::string cmdSetCOM10KOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 46 | + std::string cmdSetReference10KOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 47 | + |
| 48 | + std::string cmdTurnAllDriveSignals(int64_t ampId, int16_t channel, const std::string& value); |
| 49 | + std::string cmdTurnChannelDriveSignals(int64_t ampId, int16_t channel, const std::string& value); |
| 50 | + std::string cmdSetCOMDriveSignal(int64_t ampId, int16_t channel, const std::string& value); |
| 51 | + std::string cmdSetReferenceDriveSignal(int64_t ampId, int16_t channel, const std::string& value); |
| 52 | + |
| 53 | + std::string cmdSetSubjectGround(int64_t ampId, int16_t channel, const std::string& value); |
| 54 | + std::string cmdSetCurrentSource(int64_t ampId, int16_t channel, const std::string& value); |
| 55 | + std::string cmdSetCalibrationSignalFreq(int64_t ampId, int16_t channel, const std::string& value); |
| 56 | + std::string cmdSetBufferedReference(int64_t ampId, int16_t channel, const std::string& value); |
| 57 | + std::string cmdSetOscillatorGate(int64_t ampId, int16_t channel, const std::string& value); |
| 58 | + std::string cmdSetWaveShape(int64_t ampId, int16_t channel, const std::string& value); |
| 59 | + std::string cmdSetDrivenCommon(int64_t ampId, int16_t channel, const std::string& value); |
| 60 | + std::string cmdSetCalibrationSignalAmplitude(int64_t ampId, int16_t channel, const std::string& value); |
| 61 | + |
| 62 | + std::string cmdSetDigitalOutputData(int64_t ampId, int16_t channel, const std::string& value); |
| 63 | + std::string cmdSetDigitalInOutDirection(int64_t ampId, int16_t channel, const std::string& value); |
| 64 | + |
| 65 | + std::string cmdSetNativeRate(int64_t ampId, int16_t channel, const std::string& value); |
| 66 | + std::string cmdSetDecimatedRate(int64_t ampId, int16_t channel, const std::string& value); |
| 67 | + |
| 68 | + std::string cmdGetStartTime(int64_t ampId, int16_t channel, const std::string& value); |
| 69 | + std::string cmdGetAmpDetails(int64_t ampId, int16_t channel, const std::string& value); |
| 70 | + std::string cmdGetAmpStatus(int64_t ampId, int16_t channel, const std::string& value); |
| 71 | + |
| 72 | + std::string cmdTurnChannelZeroOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 73 | + std::string cmdTurnAllZeroOhms(int64_t ampId, int16_t channel, const std::string& value); |
| 74 | + |
| 75 | + std::string cmdSetPIBChannelGain(int64_t ampId, int16_t channel, const std::string& value); |
| 76 | + std::string cmdGetPhysioConnectionStatus(int64_t ampId, int16_t channel, const std::string& value); |
| 77 | + |
| 78 | + std::string cmdNumberOfAmps(int64_t ampId, int16_t channel, const std::string& value); |
| 79 | + std::string cmdNumberOfActiveAmps(int64_t ampId, int16_t channel, const std::string& value); |
| 80 | + std::string cmdListenToAmp(int64_t ampId, int16_t channel, const std::string& value); |
| 81 | + std::string cmdStopListeningToAmp(int64_t ampId, int16_t channel, const std::string& value); |
| 82 | + std::string cmdReceiveNotifications(int64_t ampId, int16_t channel, const std::string& value); |
| 83 | + std::string cmdStopReceivingNotifications(int64_t ampId, int16_t channel, const std::string& value); |
| 84 | + std::string cmdExit(int64_t ampId, int16_t channel, const std::string& value); |
| 85 | + |
| 86 | + std::string cmdDefaultAcquisitionState(int64_t ampId, int16_t channel, const std::string& value); |
| 87 | + std::string cmdDefaultSignalGeneration(int64_t ampId, int16_t channel, const std::string& value); |
| 88 | + |
| 89 | + // GTEN commands |
| 90 | + std::string cmdGTENSetTrain(int64_t ampId, int16_t channel, const std::string& value); |
| 91 | + std::string cmdGTENSetWaveforms(int64_t ampId, int16_t channel, const std::string& value); |
| 92 | + std::string cmdGTENSetBlocks(int64_t ampId, int16_t channel, const std::string& value); |
| 93 | + std::string cmdGTENStartTrain(int64_t ampId, int16_t channel, const std::string& value); |
| 94 | + std::string cmdGTENAbortTrain(int64_t ampId, int16_t channel, const std::string& value); |
| 95 | + std::string cmdGTENGetStatus(int64_t ampId, int16_t channel, const std::string& value); |
| 96 | + std::string cmdGTENResetAlarm(int64_t ampId, int16_t channel, const std::string& value); |
| 97 | + |
| 98 | + // Helper |
| 99 | + std::string successResponse(); |
| 100 | + std::string errorResponse(); |
| 101 | + std::string responseWithData(const std::string& data); |
| 102 | + |
| 103 | + void sendNotification(const std::string& notification); |
| 104 | + |
| 105 | + asio::io_context& io_context_; |
| 106 | + asio::ip::tcp::acceptor acceptor_; |
| 107 | + std::shared_ptr<MockAmplifier> amplifier_; |
| 108 | + std::shared_ptr<NotificationHandler> notificationHandler_; |
| 109 | + std::atomic<bool> running_{false}; |
| 110 | + std::atomic<bool> exitRequested_{false}; |
| 111 | + int nextClientId_ = 0; |
| 112 | + |
| 113 | + using CommandFunc = std::function<std::string(CommandHandler*, int64_t, int16_t, const std::string&)>; |
| 114 | + std::unordered_map<std::string, CommandFunc> commandMap_; |
| 115 | + |
| 116 | + void initCommandMap(); |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace mock |
| 120 | + |
| 121 | +#endif // MOCK_COMMANDHANDLER_H |
0 commit comments