mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-27 11:00:31 -04:00 
			
		
		
		
	Web API: /sdrangel/location implementation
This commit is contained in:
		
							parent
							
								
									72615b188e
								
							
						
					
					
						commit
						aa8b02a225
					
				| @ -23,3 +23,4 @@ QString WebAPIAdapterInterface::instanceDevicesURL = "/sdrangel/devices"; | |||||||
| QString WebAPIAdapterInterface::instanceChannelsURL = "/sdrangel/channels"; | QString WebAPIAdapterInterface::instanceChannelsURL = "/sdrangel/channels"; | ||||||
| QString WebAPIAdapterInterface::instanceLoggingURL = "/sdrangel/logging"; | QString WebAPIAdapterInterface::instanceLoggingURL = "/sdrangel/logging"; | ||||||
| QString WebAPIAdapterInterface::instanceAudioURL = "/sdrangel/audio"; | QString WebAPIAdapterInterface::instanceAudioURL = "/sdrangel/audio"; | ||||||
|  | QString WebAPIAdapterInterface::instanceLocationURL = "/sdrangel/location"; | ||||||
|  | |||||||
| @ -29,6 +29,7 @@ namespace Swagger | |||||||
|     class SWGLoggingInfo; |     class SWGLoggingInfo; | ||||||
|     class SWGAudioDevices; |     class SWGAudioDevices; | ||||||
|     class SWGAudioDevicesSelect; |     class SWGAudioDevicesSelect; | ||||||
|  |     class SWGLocationInformation; | ||||||
|     class SWGErrorResponse; |     class SWGErrorResponse; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -102,11 +103,30 @@ public: | |||||||
|             Swagger::SWGErrorResponse& error __attribute__((unused))) |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
|     { return 501; } |     { return 501; } | ||||||
| 
 | 
 | ||||||
|  |     /**
 | ||||||
|  |      * Handler of /sdrangel/location (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels | ||||||
|  |      * returns the Http status code (default 501: not implemented) | ||||||
|  |      */ | ||||||
|  |     virtual int instanceLocationGet( | ||||||
|  |             Swagger::SWGLocationInformation& response __attribute__((unused)), | ||||||
|  |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
|  |     { return 501; } | ||||||
|  | 
 | ||||||
|  |     /**
 | ||||||
|  |      * Handler of /sdrangel/location (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels | ||||||
|  |      * returns the Http status code (default 501: not implemented) | ||||||
|  |      */ | ||||||
|  |     virtual int instanceLocationPut( | ||||||
|  |             Swagger::SWGLocationInformation& response __attribute__((unused)), | ||||||
|  |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
|  |     { return 501; } | ||||||
|  | 
 | ||||||
|     static QString instanceSummaryURL; |     static QString instanceSummaryURL; | ||||||
|     static QString instanceDevicesURL; |     static QString instanceDevicesURL; | ||||||
|     static QString instanceChannelsURL; |     static QString instanceChannelsURL; | ||||||
|     static QString instanceLoggingURL; |     static QString instanceLoggingURL; | ||||||
|     static QString instanceAudioURL; |     static QString instanceAudioURL; | ||||||
|  |     static QString instanceLocationURL; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -27,6 +27,7 @@ | |||||||
| #include "SWGInstanceChannelsResponse.h" | #include "SWGInstanceChannelsResponse.h" | ||||||
| #include "SWGAudioDevices.h" | #include "SWGAudioDevices.h" | ||||||
| #include "SWGAudioDevicesSelect.h" | #include "SWGAudioDevicesSelect.h" | ||||||
|  | #include "SWGLocationInformation.h" | ||||||
| #include "SWGErrorResponse.h" | #include "SWGErrorResponse.h" | ||||||
| 
 | 
 | ||||||
| WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) : | WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) : | ||||||
| @ -64,6 +65,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http | |||||||
|             instanceLoggingService(request, response); |             instanceLoggingService(request, response); | ||||||
|         } else if (path == WebAPIAdapterInterface::instanceAudioURL) { |         } else if (path == WebAPIAdapterInterface::instanceAudioURL) { | ||||||
|             instanceAudioService(request, response); |             instanceAudioService(request, response); | ||||||
|  |         } else if (path == WebAPIAdapterInterface::instanceLocationURL) { | ||||||
|  |             instanceLocationService(request, response); | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
| @ -235,6 +238,47 @@ void WebAPIRequestMapper::instanceAudioService(qtwebapp::HttpRequest& request, q | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void WebAPIRequestMapper::instanceLocationService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) | ||||||
|  | { | ||||||
|  |     Swagger::SWGErrorResponse errorResponse; | ||||||
|  | 
 | ||||||
|  |     if (request.getMethod() == "GET") | ||||||
|  |     { | ||||||
|  |         Swagger::SWGLocationInformation normalResponse; | ||||||
|  | 
 | ||||||
|  |         int status = m_adapter->instanceLocationGet(normalResponse, errorResponse); | ||||||
|  |         response.setStatus(status); | ||||||
|  | 
 | ||||||
|  |         if (status == 200) { | ||||||
|  |             response.write(normalResponse.asJson().toUtf8()); | ||||||
|  |         } else { | ||||||
|  |             response.write(errorResponse.asJson().toUtf8()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     else if (request.getMethod() == "PUT") | ||||||
|  |     { | ||||||
|  |         Swagger::SWGLocationInformation normalResponse; | ||||||
|  |         QString jsonStr = request.getBody(); | ||||||
|  | 
 | ||||||
|  |         if (parseJsonBody(jsonStr, response)) | ||||||
|  |         { | ||||||
|  |             normalResponse.fromJson(jsonStr); | ||||||
|  |             int status = m_adapter->instanceLocationPut(normalResponse, errorResponse); | ||||||
|  |             response.setStatus(status); | ||||||
|  | 
 | ||||||
|  |             if (status == 200) { | ||||||
|  |                 response.write(normalResponse.asJson().toUtf8()); | ||||||
|  |             } else { | ||||||
|  |                 response.write(errorResponse.asJson().toUtf8()); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         response.setStatus(405,"Invalid HTTP method"); | ||||||
|  |         response.write("Invalid HTTP method"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response) | bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response) | ||||||
| { | { | ||||||
| @ -248,7 +292,7 @@ bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse | |||||||
| 
 | 
 | ||||||
|         if (error.error != QJsonParseError::NoError) |         if (error.error != QJsonParseError::NoError) | ||||||
|         { |         { | ||||||
|             QString errorMsg = QString("Input JSON error: ") + error.errorString(); |             QString errorMsg = QString("Input JSON error: ") + error.errorString() + QString(" at offset ") + QString::number(error.offset); | ||||||
|             errorResponse.init(); |             errorResponse.init(); | ||||||
|             *errorResponse.getMessage() = errorMsg; |             *errorResponse.getMessage() = errorMsg; | ||||||
|             response.setStatus(400, errorMsg.toUtf8()); |             response.setStatus(400, errorMsg.toUtf8()); | ||||||
|  | |||||||
| @ -44,6 +44,7 @@ private: | |||||||
|     void instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); |     void instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); | ||||||
|     void instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); |     void instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); | ||||||
|     void instanceAudioService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); |     void instanceAudioService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); | ||||||
|  |     void instanceLocationService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); | ||||||
| 
 | 
 | ||||||
|     bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response); |     bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response); | ||||||
| }; | }; | ||||||
|  | |||||||
| @ -39,6 +39,7 @@ | |||||||
| #include "SWGDeviceListItem.h" | #include "SWGDeviceListItem.h" | ||||||
| #include "SWGAudioDevices.h" | #include "SWGAudioDevices.h" | ||||||
| #include "SWGAudioDevicesSelect.h" | #include "SWGAudioDevicesSelect.h" | ||||||
|  | #include "SWGLocationInformation.h" | ||||||
| #include "SWGErrorResponse.h" | #include "SWGErrorResponse.h" | ||||||
| 
 | 
 | ||||||
| #include "webapiadaptergui.h" | #include "webapiadaptergui.h" | ||||||
| @ -287,7 +288,7 @@ int WebAPIAdapterGUI::instanceAudioGet( | |||||||
| 
 | 
 | ||||||
| int WebAPIAdapterGUI::instanceAudioPatch( | int WebAPIAdapterGUI::instanceAudioPatch( | ||||||
|             Swagger::SWGAudioDevicesSelect& response, |             Swagger::SWGAudioDevicesSelect& response, | ||||||
|             Swagger::SWGErrorResponse& error) |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
| { | { | ||||||
|     // response input is the query actually
 |     // response input is the query actually
 | ||||||
|     float inputVolume = response.getInputVolume(); |     float inputVolume = response.getInputVolume(); | ||||||
| @ -318,6 +319,35 @@ int WebAPIAdapterGUI::instanceAudioPatch( | |||||||
|     return 200; |     return 200; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int WebAPIAdapterGUI::instanceLocationGet( | ||||||
|  |             Swagger::SWGLocationInformation& response, | ||||||
|  |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
|  | { | ||||||
|  |     response.setLatitude(m_mainWindow.m_settings.getLatitude()); | ||||||
|  |     response.setLongitude(m_mainWindow.m_settings.getLongitude()); | ||||||
|  | 
 | ||||||
|  |     return 200; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int WebAPIAdapterGUI::instanceLocationPut( | ||||||
|  |             Swagger::SWGLocationInformation& response, | ||||||
|  |             Swagger::SWGErrorResponse& error __attribute__((unused))) | ||||||
|  | { | ||||||
|  |     float latitude = response.getLatitude(); | ||||||
|  |     float longitude = response.getLongitude(); | ||||||
|  | 
 | ||||||
|  |     latitude = latitude < -90.0 ? -90.0 : latitude > 90.0 ? 90.0 : latitude; | ||||||
|  |     longitude = longitude < -180.0 ? -180.0 : longitude > 180.0 ? 180.0 : longitude; | ||||||
|  | 
 | ||||||
|  |     m_mainWindow.m_settings.setLatitude(latitude); | ||||||
|  |     m_mainWindow.m_settings.setLongitude(longitude); | ||||||
|  | 
 | ||||||
|  |     response.setLatitude(m_mainWindow.m_settings.getLatitude()); | ||||||
|  |     response.setLongitude(m_mainWindow.m_settings.getLongitude()); | ||||||
|  | 
 | ||||||
|  |     return 200; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| QtMsgType WebAPIAdapterGUI::getMsgTypeFromString(const QString& msgTypeString) | QtMsgType WebAPIAdapterGUI::getMsgTypeFromString(const QString& msgTypeString) | ||||||
| { | { | ||||||
|     if (msgTypeString == "debug") { |     if (msgTypeString == "debug") { | ||||||
|  | |||||||
| @ -61,6 +61,14 @@ public: | |||||||
|             Swagger::SWGAudioDevicesSelect& response, |             Swagger::SWGAudioDevicesSelect& response, | ||||||
|             Swagger::SWGErrorResponse& error); |             Swagger::SWGErrorResponse& error); | ||||||
| 
 | 
 | ||||||
|  |     virtual int instanceLocationGet( | ||||||
|  |             Swagger::SWGLocationInformation& response, | ||||||
|  |             Swagger::SWGErrorResponse& error); | ||||||
|  | 
 | ||||||
|  |     virtual int instanceLocationPut( | ||||||
|  |             Swagger::SWGLocationInformation& response, | ||||||
|  |             Swagger::SWGErrorResponse& error); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     MainWindow& m_mainWindow; |     MainWindow& m_mainWindow; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user