Matlab
You can download the following code examples for an (a)synchronous call to the MongoDB Query Service of the Bosch IoT Insights backend as cloud-examples-matlab.zip and import it to your Matlab installation.
Make sure to set your proxy configuration in Matlab if you are behind a corporate proxy (i.e. within the Bosch network). Therefore go to HOME - Preferences - Web and enter your proxy settings.
Within the German Bosch network set the Proxy host to rb-proxy-de.bosch.com and the Proxy port to 8080. The Proxy username and Proxy password are the same as for your Windows login.
Please notice that this examples only work with Matlab 2016b or later.
For the basic authentication, use the credentials of the API user. To create an API user, refer to Creating an API user or Creating an API user via API.
We recommend you to use preemptive authentication. That way, the basic authentication request is sent before the server returns an unauthorized response. Also refer to the Apache documentation.
Sending data
The following Matlab code example shows an example call to the HTTP Data Recorder Service of the Insights backend from within Matlab.
function [] = MongoDBDataRecorderServiceExample( jsondata )
% Send data to the iot-insights DataRecorderService REST-API
% --------- IMPORTANT --------------
% Don't forget to configure your Matlab with the Bosch proxy server
% Go to Preferences, Web Proxy:rb-proxy-de.bosch.com on port
8080
% authentifcation is required with your ordinary Windows credentials
% If you are inside your company network, a proxy authentication may be required. Otherwise, you can remove
this
from the example. This is an example
for
a Bosch internal proxy.
%====CONFIGURATION SECTION ======================================================
projectID =
"YOURPROJECT"
;
% iot-insights Backend URL
MongoConfig.server =
"https://bosch-iot-insights.com"
;
% iot-insights service
MongoConfig.serviceUrl =
"/data-recorder-service/v2/"
+ projectID;
% Project Username and Password
MongoConfig.username =
"YOURUSERNAME"
;
MongoConfig.userpassword =
"YOURUSERPASSWORD"
;
MongoConfig.authString =
"Basic "
+ ...
matlab.net.base64encode(MongoConfig.username +
":"
+ MongoConfig.userpassword);
% Data to send in JSON format
MongoConfig.data = jsondata; % as string or
char
array, example: jsondata=
'{"demo":"test"}'
%================================================================================
iotInsightsBaseUrl = MongoConfig.server + MongoConfig.serviceUrl;
options = weboptions(
'MediaType'
,
'application/json'
,
'ContentType'
,
'json'
,...
'HeaderFields'
, {
'Authorization'
,
char
(MongoConfig.authString)});
% Place the POST request by using webwrite
dataStruct = webwrite( iotInsightsBaseUrl,
char
(MongoConfig.data), options);
disp(dataStruct);
end
Synchronous query execution
The following code snippet shows an example synchronous call to the MongoDB Query Service of the Insights backend from within Matlab.
function data = MongoDBQueryServiceSynchronousExample( )
% Run a synchronous MongoDB Query at the iot-insights REST-API
% --------- IMPORTANT --------------
% Don't forget to configure your Matlab with the Bosch proxy server
% Go to Preferences, Web Proxy:rb-proxy-de.bosch.com on port
8080
% authentifcation is required with your ordinary Windows credentials
% If you are inside your company network, a proxy authentication may be required. Otherwise, you can remove
this
from the example. This is an example
for
a Bosch internal proxy.
%====CONFIGURATION SECTION ======================================================
projectID =
"YOURPROJECT"
;
% iot-insights Backend URL
MongoConfig.server =
"https://bosch-iot-insights.com"
;
% iot-insights service
MongoConfig.serviceUrl =
"/mongodb-query-service/v2/"
+ projectID +
"/execute-aggregation-query"
;
% Project Username and Password
MongoConfig.username =
"YOURUSERNAME"
;
MongoConfig.userpassword =
"YOURUSERPASSWORD"
;
MongoConfig.authString =
"Basic "
+...
matlab.net.base64encode(MongoConfig.username +
":"
+ MongoConfig.userpassword);
% Aggregation query to execute as db.collection.aggregate(...)
MongoConfig.query =
'[{"$limit":100}]'
;
% Collection the query should run
MongoConfig.collection = projectID +
"_processed_data"
;
%================================================================================
iotInsightsBaseUrl = MongoConfig.server + MongoConfig.serviceUrl;
options = weboptions(
'MediaType'
,
'application/json'
,
'ContentType'
,
'json'
,...
'HeaderFields'
, {
'Authorization'
,
char
(MongoConfig.authString)});
% Create the string PostParam, that contains the query itself
PostParam =
'{"collection":"'
+ MongoConfig.collection+
'","query":'
+ MongoConfig.query +
' }'
;
% Place the POST request by using webwrite
dataStruct = webwrite( iotInsightsBaseUrl,
char
(PostParam), options);
disp(dataStruct);
% YOUR CODE HERE
data = dataStruct;
end
Asynchronous query execution
The following code snippet shows an example asynchronous call to the MongoDB Query Service of the Insights backend from within Matlab.
function data = MongoDBQueryServiceAsyncExample( )
% Run a asyncronous MongoDB Query at the iot-insights REST-API
% --------- IMPORTANT --------------
% Don't forget to configure your Matlab with the Bosch proxy server
% Go to Preferecenes, Web Proxy:rb-proxy-de.bosch.com on port
8080
% authentifcation is required with your ordinary Windows credentials
% If you are inside your company network, a proxy authentication may be required. Otherwise, you can remove
this
from the example. This is an example
for
a Bosch internal proxy.
%====CONFIGURATION SECTION ======================================================
projectID =
"YOURPROJECT"
;
% iot-insights Backend URL
MongoConfig.server =
"https://bosch-iot-insights.com"
;
% iot-insights service
MongoConfig.serviceUrl =
"/mongodb-query-service/v2/"
+ projectID;
% Timeout to wait
for
asyncronous response in seconds
MongoConfig.MaxTimeOutTime =
30
;
% Project Username and Password
MongoConfig.username =
"YOURUSERNAME"
;
MongoConfig.userpassword =
"YOURPASSWORD"
;
MongoConfig.authString =
"Basic "
+ ...
matlab.net.base64encode(MongoConfig.username +
":"
+ MongoConfig.userpassword);
% Aggregation query to execute as db.collection.aggregate(...)
MongoConfig.query =
'[{"$limit":10}]'
;
% Collection the query should run
MongoConfig.collection = projectID +
"_processed_data"
;
%================================================================================
iotInsightsBaseUrl = MongoConfig.server + MongoConfig.serviceUrl;
options = weboptions(
'MediaType'
,
'application/json'
,
'ContentType'
,
'json'
,...
'HeaderFields'
, {
'Authorization'
,
char
(MongoConfig.authString)});
% Create the string PostParam, that contains the query itself
PostParam =
'{"collection":"'
+ MongoConfig.collection+
'","query":'
+ MongoConfig.query +
' }'
;
% Place the POST request by using webwrite
dataStruct = webwrite( iotInsightsBaseUrl +
"/submit-aggregation-query"
,
char
(PostParam), options);
disp(dataStruct);
queryId = dataStruct.queryId;
tic
% loop until MaxTimeOutTime is reached
while
toc <= MongoConfig.MaxTimeOutTime
%Place GET request to check
if
the query result is available
iotInsightsUrl = iotInsightsBaseUrl +
"/queries/"
+ queryId;
dataStruct = webread( iotInsightsUrl, options);
disp(dataStruct);
% Check
for
"SUCCESSFULL"
if
dataStruct.status ==
"SUCCESSFUL"
%%Send the
final
GET request to get results
iotInsightsUrl = iotInsightsUrl +
"/result"
;
tryRead =
true
;
while
tryRead
try
dataStruct = webread( iotInsightsUrl, options);
tryRead =
false
;
catch
pause(
1
);
end
end
break
else
%If result is not availble wait
1
second
pause(
1
);
end
end
% YOUR CODE HERE
data = dataStruct;
end