-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
393 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# receiver - notification receiver | ||
|
||
This command can receive notifications related with subscriptions that context broker send. | ||
|
||
```console | ||
ngsi receiver [options] | ||
``` | ||
|
||
### Options | ||
|
||
| Options | Description | | ||
| ---------------------- | ------------------------------------------- | | ||
| --port value, -p value | specify port for receiver (default: "1028") | | ||
| --pretty, -P | pretty format (default: false) | | ||
| --verbose, -v | verbose (default: false) | | ||
| --help | show help (default: false) | | ||
|
||
### Example | ||
|
||
```console | ||
ngsi rm --host orion --type EvacuationSpace --run | ||
``` | ||
|
||
``` | ||
{ | ||
"data": [ | ||
{ | ||
"id": "device001", | ||
"temperature": { | ||
"metadata": {}, | ||
"type": "Number", | ||
"value": 24 | ||
}, | ||
"type": "device" | ||
} | ||
], | ||
"subscriptionId": "5fd412e8ecb082767349b975" | ||
} | ||
``` | ||
|
||
### Use case | ||
|
||
#### Start up a receiver | ||
|
||
Run `ngsi receiver` command. | ||
|
||
```console | ||
ngsi receiver --pretty | ||
``` | ||
|
||
Open another terminal and run the following commands on it. | ||
|
||
#### Create an entity | ||
|
||
```console | ||
ngsi create --host orion entity --data '{"type": "device", "id": "device001", "temperature": 26}' | ||
``` | ||
|
||
#### Create a subscription | ||
|
||
```console | ||
ngsi create --host orin subscription --idPattern ".*" --url http://192.168.1.1:1028/ | ||
``` | ||
|
||
```console | ||
5fd412e8ecb082767349b975 | ||
``` | ||
|
||
#### Update an attribute value | ||
|
||
```console | ||
ngsi update --host orion attr --id device001 --attrName temperature --data 22 | ||
``` | ||
|
||
#### Notification message | ||
|
||
You will find the following message on the terminal you run `ngsi receiver` command. | ||
|
||
```json | ||
{ | ||
"data": [ | ||
{ | ||
"id": "device001", | ||
"temperature": { | ||
"metadata": {}, | ||
"type": "Number", | ||
"value": 22 | ||
}, | ||
"type": "device" | ||
} | ||
], | ||
"subscriptionId": "5fd412e8ecb082767349b975" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ ngsi rm [options] | |
|
||
```console | ||
ngsi rm --host orion --type EvacuationSpace --run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2020 Kazuhito Suda | ||
This file is part of NGSI Go | ||
https://github.com/lets-fiware/ngsi-go | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package ngsicmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/lets-fiware/ngsi-go/internal/ngsilib" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type receiverParam struct { | ||
ngsi *ngsilib.NGSI | ||
pretty bool | ||
} | ||
|
||
var receiverGlobal *receiverParam | ||
|
||
func receiver(c *cli.Context) error { | ||
const funcName = "receiver" | ||
|
||
port := c.String("port") | ||
addr := ":" + port | ||
|
||
pretty := c.IsSet("pretty") | ||
|
||
ngsi, err := initCmd(c, funcName, false) | ||
if err != nil { | ||
return &ngsiCmdError{funcName, 1, err.Error(), err} | ||
} | ||
|
||
receiverGlobal = &receiverParam{ngsi: ngsi, pretty: pretty} | ||
|
||
http.HandleFunc("/", http.HandlerFunc(receiverHandler)) | ||
|
||
if c.IsSet("verbose") { | ||
fmt.Fprintf(ngsi.Stderr, "%s\n", addr) | ||
} | ||
http.ListenAndServe(addr, nil) | ||
|
||
return nil | ||
} | ||
|
||
func receiverHandler(w http.ResponseWriter, r *http.Request) { | ||
status := http.StatusNoContent | ||
|
||
switch r.Method { | ||
default: | ||
fmt.Fprint(receiverGlobal.ngsi.Stderr, "Method not allowed.\n") | ||
status = http.StatusMethodNotAllowed | ||
case http.MethodPost: | ||
body := r.Body | ||
defer body.Close() | ||
buf := new(bytes.Buffer) | ||
io.Copy(buf, body) | ||
|
||
b := buf.Bytes() | ||
if receiverGlobal.pretty && ngsilib.IsJSON(b) { | ||
var j interface{} | ||
err := json.Unmarshal(b, &j) | ||
if err != nil { | ||
fmt.Fprintf(receiverGlobal.ngsi.Stderr, "json.Unmarshal error\n") | ||
} | ||
b, _ = json.MarshalIndent(j, "", " ") | ||
} | ||
fmt.Fprint(receiverGlobal.ngsi.StdWriter, string(b)+"\n") | ||
} | ||
w.WriteHeader(status) | ||
} |
Oops, something went wrong.