SAP Leonardo IoT – Digital Twin ( How to create Thing model)
const express = require('express');
const fetch = require("node-fetch");
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded( {extended : false } ));
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
app.post('/iotAction', (req,res) => {
console.log("received data = ", req.body);
res.json({
"data": "received"
})
});
app.get('/iotAction', (req, res) => {
console.log("get triggered.")
res.send("get triggered")
});
"${"
+ another letter(s), you will get a context menu with variables that you can use in the notification text. ( ${property_set_name.property_name } )Thing Type
automatically.https://<server address>/geolocation/v1/Spaces
[
{
"Descriptions": [
{
"Label": "string",
"Locale": "string"
}
],
"Geometry": {
"Type": "string"
},
"ParentSpaceId": "string",
"SpaceName": "string"
}
]
This method allows you to create a geolocation entry with details such as Name, Address, Description, Geometry, and Space. You can create single or multiple geolocations in the same call, and this method lets you create upto 20 geolocations in a single call.
Point
Polygon
https://<server address>/geolocation/v1/GeoLocations
[
{
"Type": "Feature",
"Properties": {
"SpaceId": "SPACE_ID_THAT_YOU_HAVE_CREATED",
"ExternalId": null,
"GeoLocationName": "UnitedStates",
"ParentGeoLocationId": null,
"Addresses": null,
"Descriptions": [
{
"Label": "UnitedStates",
"Locale": "en"
}
]
},
"Geometry": {
"type": "Polygon",
"coordinates": [
[
[
-123.31054687499999,
48.86471476180277
],
[
-124.892578125,
39.977120098439634
],
[
-106.5234375,
31.952162238024975
],
[
-81.38671875,
25.799891182088334
],
[
-68.64257812499999,
46.437856895024204
],
[
-82.79296874999999,
40.84706035607122
],
[
-83.14453125,
45.460130637921004
],
[
-95.09765625,
48.69096039092549
],
[
-123.31054687499999,
48.86471476180277
]
]
]
}
}
]
Auth: OAuth2.0
app.post('/iotAction', (req,res) => {
async function geoMatch(req){
let latitude;
let longitude;
console.log("received data = ", req.body);
latitude = req.body.geoDeviceLatitude;
longitude = req.body.geoDeviceLongitude;
let urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "client_credentials");
urlencoded.append("response_type", "token");
urlencoded.append("client_id", "sb-xxxxxx-e6b6-400d-8772-054b69282e76!b6001|iotae_service!b5");
urlencoded.append("client_secret", "xxxxxxx+pk8cSxxxxxxBxjhEs=");
let requestTokenOptions = {
method: 'POST',
headers: {'grant_type':'client_credentials'},
body: urlencoded,
redirect: 'follow'
};
// Requst Oauth2 Token
let token = await fetch("https://xxxxxiotae.authentication.eu10.hana.ondemand.com/oauth/token", requestTokenOptions)
.then(response => response.json())
.then(json => {
console.log("token is ready = ", json.access_token);
return json.access_token
})
.catch(error => res.status(500).json({ error: error.toString() }) );
let requestOptions = {
method: 'GET',
headers: {"Authorization" : `Bearer ${token}`},
redirect: 'follow'
};
let geoLocationId = 'xxxxx-05e3-47b4-81aa-0e9edcefd1e0'
let distance = 0;
// GeoTest GET request
let geoMatchResult = await fetch(`https://sap-iot-noah-live-geolocation-runtime.cfapps.eu10.hana.ondemand.com/geolocation/v1/GeoTest/Check/Within/GeoPosition?Latitude=${latitude}&Longitude=${longitude}&GeoLocationId=${geoLocationId}&Distance=${distance}`, requestOptions)
.then(response => {
return response.json()
})
.then(result => {
console.log("geoMatchResult = ", result);
if(result.Within){
console.log("The customer is in the US")
} else {
console.log("ALERT!!! Customer is outside of US");
}
return
})
.catch(error => console.log('error', error));
}
geoMatch(req);
res.send("done")
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
11 | |
10 | |
10 | |
10 | |
8 | |
8 |