Skip to content

Commit

Permalink
Merge 4d9c2d0 into 7f8afd3
Browse files Browse the repository at this point in the history
  • Loading branch information
fisuda committed Dec 12, 2020
2 parents 7f8afd3 + 4d9c2d0 commit 4f658b7
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 14 deletions.
14 changes: 8 additions & 6 deletions cmd/ngsi/testdata/help/help.ct
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ VERSION:
COMMANDS:
help, h Shows a list of commands or help for one command
CONVENIENCE:
admin admin command for FIWARE Orion
cp copy entities
wc print number of entities, subscriptions, registrations, or types
wc print number of entities, subscriptions, registrations or types
man print urls of document
ls list entities
rm remove entities
receiver notification receiver
template create template of subscription or registration
version print the version of Context Broker
MANAGEMENT:
Expand All @@ -29,12 +31,12 @@ COMMANDS:
NGSI:
append append attributes
create create entity(ies), subscription or registration
delete delete entity(ies), attribute, subscription, or registration
get get entity(ies), attribute(s), subscription, registration, or type
list list types, entities, subscriptions, or registrations
delete delete entity(ies), attribute, subscription or registration
get get entity(ies), attribute(s), subscription, registration or type
list list types, entities, subscriptions or registrations
replace replace entities or attributes
update update entities, attribute(s), or subscription
upsert upsert entities
update update entities, attribute(s) or subscription
upsert upsert entity or entities

GLOBAL OPTIONS:
--syslog LEVEL specify logging LEVEL (off, err, info, debug)
Expand Down
94 changes: 94 additions & 0 deletions docs/convenience/receiver.md
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"
}
```
1 change: 1 addition & 0 deletions docs/convenience/rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ ngsi rm [options]

```console
ngsi rm --host orion --type EvacuationSpace --run
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The NGSI Go is a UNIX command-line tool FIWARE supporting both [NGSI v2](https:/
- [man](convenience/man.md): print URLs of the documents related to the NGSI Go
- [ls](convenience/ls.md): list entities
- [rm](convenience/rm.md): remove entities
- [receiver](convenience/receiver.md): notification receiver
- [template](convenience/template.md): create template of subscription or registration
- [version](convenience/version.md): print the version of Context Broker

Expand Down
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ngsi [global options] command [common options] sub-command [options]
| [man](./convenience/man.md) | - | print urls of document |
| [ls](./convenience/ls.md) | - | list entities |
| [rm](./convenience/rm.md) | - | remove entities |
| [receiver](./convenience/receiver.md) | - | notification receiver |
| [template](./convenience/template.md) | subscription | create template of subscription |
| | registration | create template of registration |
| [version](./convenience/version.md) | - | print the version of Context Broker |
Expand Down
29 changes: 22 additions & 7 deletions internal/ngsicmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ var (
Required: true,
}
uriFlag = &cli.StringFlag{
Name: "uri",
Aliases: []string{"u"},
Usage: "uri/url to be invoked when a notification is generated",
Name: "uri",
Aliases: []string{"u"},
Usage: "uri/url to be invoked when a notification is generated",
}
acceptFlag = &cli.StringFlag{
Name: "accept",
Expand Down Expand Up @@ -476,13 +476,13 @@ var (
Usage: "attributes to be notified",
}
getFlag = &cli.BoolFlag{
Name: "get",
Usage: "get (v2)",
Name: "get",
Usage: "get (v2)",
Hidden: true,
}
notifyURLFlag = &cli.StringFlag{
Name: "url",
Usage: "url to be invoked when a notification is generated (v2)",
Name: "url",
Usage: "url to be invoked when a notification is generated (v2)",
Hidden: true,
}
)
Expand Down Expand Up @@ -544,6 +544,21 @@ var (
}
)

// flag for receiver
var (
portFlag = &cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Value: "1028",
Usage: "port for receiver",
}
prettyFlag = &cli.BoolFlag{
Name: "pretty",
Aliases: []string{"P"},
Usage: "pretty format",
}
)

// flag for context
var (
nameFlag = &cli.StringFlag{
Expand Down
15 changes: 15 additions & 0 deletions internal/ngsicmd/ngsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
&listCmd,
&lsCmd,
&removeCmd,
&receiverCmd,
&replaceCmd,
&settingsCmd,
&templateCmd,
Expand Down Expand Up @@ -364,6 +365,20 @@ var templateCmd = cli.Command{
},
}

var receiverCmd = cli.Command{
Name: "receiver",
Category: "CONVENIENCE",
Usage: "notification receiver",
Flags: []cli.Flag{
portFlag,
prettyFlag,
verboseFlag,
},
Action: func(c *cli.Context) error {
return receiver(c)
},
}

var versionCmd = cli.Command{
Name: "version",
Category: "CONVENIENCE",
Expand Down
1 change: 1 addition & 0 deletions internal/ngsicmd/ngsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestNGSICommand(t *testing.T) {
{args: []string{"wc", "types"}, rc: 1},
{args: []string{"ls"}, rc: 1},
{args: []string{"rm", "--type", "abc"}, rc: 1},
{args: []string{"receiver"}, rc: 1},
{args: []string{"template", "registration"}, rc: 1},
{args: []string{"template", "subscription", "--url", "abc"}, rc: 1},
{args: []string{"version"}, rc: 1},
Expand Down
100 changes: 100 additions & 0 deletions internal/ngsicmd/receiver.go
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)
}

0 comments on commit 4f658b7

Please sign in to comment.