Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history


Signed-off-by: Daniel Kastl <[email protected]>
  • Loading branch information
dkastl committed Jul 4, 2024
2 parents 4160723 + a207a8a commit 07fe7dd
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.env
17 changes: 10 additions & 7 deletions app/controllers/subscription_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,19 @@ def handle_fiware_action(action)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')

headers = {
'Authorization' => "Bearer #{@fiware_broker_auth_token}"
}

headers['Content-Type'] = 'application/json' if action == 'publish'
headers['Fiware-Service'] = @subscription_template.fiware_service if @subscription_template.fiware_service.present?
headers['Fiware-ServicePath'] = @subscription_template.fiware_servicepath if @subscription_template.fiware_servicepath.present?

request = case action
when 'publish'
Net::HTTP::Post.new(uri.path, initheader = {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@fiware_broker_auth_token}"
}).tap { |req| req.body = @json_payload }
Net::HTTP::Post.new(uri.path, headers).tap { |req| req.body = @json_payload }
when 'unpublish'
Net::HTTP::Delete.new(uri.path, initheader = {
'Authorization' => "Bearer #{@fiware_broker_auth_token}"
})
Net::HTTP::Delete.new(uri.path, headers)
else
Rails.logger.error "Unknown action: #{action}"
@error_message = l(:general_action_error)
Expand Down
6 changes: 4 additions & 2 deletions app/views/subscription_templates/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@
if (link) {
link.addEventListener('click', function(e) {
e.preventDefault();
var geom = JSON.parse(e.target.dataset.geom).geometry.coordinates[0].map(coord => [Number(coord[0].toFixed(5)), Number(coord[1].toFixed(5))]).join(';');
var geom = JSON.parse(e.target.dataset.geom).geometry.coordinates[0]
.map(coord => [Number(coord[1].toFixed(5)), Number(coord[0].toFixed(5))]) // Flip the coordinates
.join(';');
document.querySelector('input[name="subscription_template[expression_coords]"]').value = geom;
document.querySelector('select[name="subscription_template[expression_geometry]"]').value = 'polygon';
document.querySelector('input[name="subscription_template[expression_georel]"]').value = 'coveredBy';
});
}
});
</script>
</script>

<p>
<%= content_tag :label, l(:field_subscription_template_alteration_types) %>
Expand Down
4 changes: 2 additions & 2 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ de:
Suchmaske für die Subscription verwendet'
gtt_fiware_subscription_template_variable_hint: 'Attributwerte können als <code>${Variable}</code>
eingebettet werden.'
field_subscription_template_expression_coords: 'Koordinaten'
field_subscription_template_expression_coords: 'Koordinaten (lat, lon)'
field_subscription_template_standard: 'NGSI-Standard'
field_subscription_template_attachments_string: 'Anlagen (Array)'
field_subscription_template_fiware_servicepath_placeholder: '/environment'
field_subscription_template_expression_coords_placeholder: '40.419,-3.691;40.429,-3.691;40.429,-3.701'
field_subscription_template_expression_coords_placeholder: "lat1, lon1; lat2, lon2; lat3, lon3; lat1, lon1"
field_subscription_template_fiware_service_placeholder: 'smartcity'
field_subscription_template_attachments_placeholder: '[{"filename": "image.jpg",
"url": "https://example.com/image.jpg"}]'
Expand Down
4 changes: 2 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ en:
field_subscription_template_expression_georel_placeholder: "near;maxDistance:1000"
field_subscription_template_expression_geometry: "Geometry"
field_subscription_template_expression_geometry_placeholder: "Select a geometry"
field_subscription_template_expression_coords: "Coordinates"
field_subscription_template_expression_coords_placeholder: "40.419,-3.691;40.429,-3.691;40.429,-3.701"
field_subscription_template_expression_coords: "Coordinates (lat, lon)"
field_subscription_template_expression_coords_placeholder: "lat1, lon1; lat2, lon2; lat3, lon3; lat1, lon1"
field_subscription_template_alteration_types: "Alteration types"
field_subscription_template_notify_on_metadata_change: "Notify on metadata change"

Expand Down
4 changes: 2 additions & 2 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ en:
field_subscription_template_expression_georel_placeholder: "near;maxDistance:1000"
field_subscription_template_expression_geometry: "Geometry"
field_subscription_template_expression_geometry_placeholder: "Select a geometry"
field_subscription_template_expression_coords: "Coordinates"
field_subscription_template_expression_coords_placeholder: "40.419,-3.691;40.429,-3.691;40.429,-3.701"
field_subscription_template_expression_coords: "Coordinates (lat, lon)"
field_subscription_template_expression_coords_placeholder: "lat1, lon1; lat2, lon2; lat3, lon3; lat1, lon1"
field_subscription_template_alteration_types: "Alteration types"
field_subscription_template_notify_on_metadata_change: "Notify on metadata change"

Expand Down
4 changes: 4 additions & 0 deletions doc/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BROKER_URL=http://your_broker:1026
BROKER_TOKEN=your_token
FIWARE_SERVICE=your_service
FIWARE_SERVICEPATH=your_servicepath
4 changes: 4 additions & 0 deletions doc/broker_scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ URL specified in the header for each subscription.
`http://app.local:1026` if not set.
- `BROKER_TOKEN`: The bearer token for authentication. If provided, it will be
used in the `Authorization` header for the requests.
- `FIWARE_SERVICE`: The FIWARE service for the subscriptions (optional).
- `FIWARE_SERVICEPATH`: The FIWARE service path for the subscriptions (optional).

**Note:** Make sure to set the `BROKER_URL` and `BROKER_TOKEN` (optional) environment
variables to your context broker URL and authentication token respectively
Expand Down Expand Up @@ -71,6 +73,8 @@ subscriptions, and then makes DELETE requests to the
`http://app.local:1026` if not set.
- `BROKER_TOKEN`: The bearer token for authentication. If provided, it will be
used in the `Authorization` header for the requests.
- `FIWARE_SERVICE`: The FIWARE service for the subscriptions (optional).
- `FIWARE_SERVICEPATH`: The FIWARE service path for the subscriptions (optional).

**Note:** This script does not require any arguments. Make sure to set the
`BROKER_URL` and `BROKER_TOKEN` (optional) environment variables to your context
Expand Down
8 changes: 7 additions & 1 deletion doc/examples/camera_sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Create the subscription template and publish it.
curl -iX POST "${BROKER_URL}/v2/entities" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"id": "urn:ngsi-ld:SpeedCameraSensor:001",
"type": "SpeedCameraSensor",
Expand Down Expand Up @@ -78,6 +80,8 @@ curl -iX PATCH \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:SpeedCameraSensor:001/attrs" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"speed": {
"value": 90.0,
Expand All @@ -91,5 +95,7 @@ curl -iX PATCH \
```bash
curl -iX DELETE \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:SpeedCameraSensor:001" \
-H "Authorization: Bearer ${BROKER_TOKEN}"
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}"
```
8 changes: 7 additions & 1 deletion doc/examples/location_sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Create the subscription template and publish it.
curl -iX POST "${BROKER_URL}/v2/entities" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"id": "urn:ngsi-ld:LocationSensor:001",
"type": "LocationSensor",
Expand All @@ -64,6 +66,8 @@ curl -iX POST "${BROKER_URL}/v2/entities" \
curl -iX PATCH \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:LocationSensor:001/attrs" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-H "Content-Type: application/json" \
-d '{
"location": {
Expand All @@ -85,5 +89,7 @@ within the project boundary.
```bash
curl -iX DELETE \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:LocationSensor:001" \
-H "Authorization: Bearer ${BROKER_TOKEN}"
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}"
```
17 changes: 14 additions & 3 deletions doc/examples/temperature_sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Create the subscription template and publish it.
curl -iX POST "${BROKER_URL}/v2/entities" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"id": "urn:ngsi-ld:TemperatureSensor:001",
"type": "TemperatureSensor",
Expand All @@ -58,6 +60,8 @@ curl -iX POST "${BROKER_URL}/v2/entities" \
curl -iX POST "${BROKER_URL}/v2/entities" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"id": "urn:ngsi-ld:TemperatureSensor:002",
"type": "TemperatureSensor",
Expand All @@ -75,9 +79,11 @@ curl -iX PATCH \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:TemperatureSensor:001/attrs" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}" \
-d '{
"temperature": {
"value": 28.0,
"value": 38.5,
"type": "Number"
}
}'
Expand All @@ -88,8 +94,13 @@ curl -iX PATCH \
```bash
curl -iX DELETE \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:TemperatureSensor:001" \
-H "Authorization: Bearer ${BROKER_TOKEN}"
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}"

curl -iX DELETE \
"${BROKER_URL}/v2/entities/urn:ngsi-ld:TemperatureSensor:002" \
-H "Authorization: Bearer ${BROKER_TOKEN}"
-H "Authorization: Bearer ${BROKER_TOKEN}" \
-H "Fiware-Service: ${FIWARE_SERVICE}" \
-H "Fiware-ServicePath: ${FIWARE_SERVICEPATH}"
```
55 changes: 51 additions & 4 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ FIWARE plugin and its API endpoints.
- For security reasons don't select a user with admin rights for the FIWARE
subscriptions. Instead, create a new user with the necessary permissions.

If you need a test installation of FIWARE, you can use
[FIWARE-Small-Bang](https://github.com/lets-fiware/FIWARE-Small-Bang) (for local
development) or [FIWARE-Big-Bang](https://github.com/lets-fiware/FIWARE-Big-Bang)
(for server deployment).

### Redmine Permissions

![Plugin permissions](permissions.png)
Expand Down Expand Up @@ -37,12 +42,15 @@ To allow **public** access to NGSI-LD context documents, it's necessary to grant
For all examples, the following environment variables are used:

```bash
export BROKER_URL="http://your_broker:1026"
export BROKER_URL=http://your_broker:1026
export BROKER_TOKEN=your_token
export FIWARE_SERVICE=your_service
export FIWARE_SERVICEPATH=your_servicepath
```

Replace `your_broker_url` with the actual URL of your FIWARE broker. After
running this command, the BROKER_URL environment variable will be
available to all subsequent commands in the same terminal session.
Alternatively, you can use `.env` files to set these variables.
Copy the `.env.example` file to `.env` and set the values accordingly.
Then run `source .env` to load the environment variables.

### General FIWARE Broker Commands

Expand All @@ -67,3 +75,42 @@ curl -sX GET "${BROKER_URL}/v2/subscriptions" -H "Accept: application/json" | jq
These cURL commands should help you interact with the FIWARE broker and test the
Redmine GTT FIWARE plugin effectively. If you encounter any issues or need
further assistance, please let us know!

#### CORS Issues

If you encounter CORS issues, for example when you use FIWARE-Big-Bang, you can extend
the Ngix configuration as follows:

```nginx
[snip]
server {
[snip]
# Add CORS Headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT, PATCH' always;
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization, X-Requested-With, fiware-service, fiware-servicepath' always;
add_header 'Access-Control-Expose-Headers' 'location, fiware-correlator' always;
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT, PATCH' always;
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization, X-Requested-With, fiware-service, fiware-servicepath' always;
add_header 'Access-Control-Expose-Headers' 'location, fiware-correlator' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
[snip]
}
[snip]
}
```

In particular `location` and `fiware-service, fiware-servicepath` are important
for the FIWARE broker to work correctly.
9 changes: 9 additions & 0 deletions doc/scripts/delete_all_subscriptions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ if [ -n "$BROKER_TOKEN" ]; then
HEADERS+=(-H "Authorization: Bearer $BROKER_TOKEN")
fi

# Check if FIWARE_SERVICE and FIWARE_SERVICEPATH are provided
if [ -z "$FIWARE_SERVICE" ]; then
HEADERS+=(-H "Fiware-Service: $FIWARE_SERVICE")
fi

if [ -z "$FIWARE_SERVICEPATH" ]; then
HEADERS+=(-H "Fiware-ServicePath: $FIWARE_SERVICEPATH")
fi

# Fetch all subscription IDs using a GET request
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
http_code="$response"
Expand Down
9 changes: 9 additions & 0 deletions doc/scripts/register_all_subscriptions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ if [ -n "$BROKER_TOKEN" ]; then
HEADERS+=(-H "Authorization: Bearer $BROKER_TOKEN")
fi

# Check if FIWARE_SERVICE and FIWARE_SERVICEPATH are provided
if [ -z "$FIWARE_SERVICE" ]; then
HEADERS+=(-H "Fiware-Service: $FIWARE_SERVICE")
fi

if [ -z "$FIWARE_SERVICEPATH" ]; then
HEADERS+=(-H "Fiware-ServicePath: $FIWARE_SERVICEPATH")
fi

# Fetch all subscriptions using a GET request
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
http_code="$response"
Expand Down
3 changes: 2 additions & 1 deletion doc/subscription_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ subscription, including entities, attributes, and (geospatial) queries.
all or none of the following fields:
- **Georel**: Set the geospatial relationship (e.g., `near;maxDistance:1000`).
- **Geometry**: Choose the geometry type (e.g., `Point`).
- **Coordinates**: Enter the coordinates for the geospatial query.
- **Coordinates**: Enter the coordinates (`latitude`,
`longitude`) for the geospatial query (e.g. `34.751,135.22;34.67,135.221;34.671,135.345;34.752,135.344;34.751,135.22`).
- **Insert project boundary**: Automatically insert the project boundary as
valid geospatial query.

Expand Down

0 comments on commit 07fe7dd

Please sign in to comment.