1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Mainflux.mainflux/cli/message.go
Ivan Milošević effade00aa MF-325 - Add SPDX license and copyright headers (#362)
* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Change mainflux version from 0.4.0 to 0.5.0

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-08-26 13:15:48 +02:00

60 lines
1.2 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package cli
import (
"net/http"
"strings"
"github.com/spf13/cobra"
)
const contentTypeSenml = "application/senml+json"
var cmdMessages = []cobra.Command{
cobra.Command{
Use: "send",
Short: "send <channel_id> <JSON_string> <client_token>",
Long: `Sends message on the channel`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
LogUsage(cmd.Short)
return
}
SendMsg(args[0], args[1], args[2])
},
},
}
func NewMessagesCmd() *cobra.Command {
cmd := cobra.Command{
Use: "msg",
Short: "Send or retrieve messages",
Long: `Send or retrieve messages: control message flow on the channel`,
}
for i, _ := range cmdMessages {
cmd.AddCommand(&cmdMessages[i])
}
return &cmd
}
// SendMsg - publishes SenML message on the channel
func SendMsg(id, msg, token string) {
url := serverAddr + "/http/channels/" + id + "/messages"
req, err := http.NewRequest("POST", url, strings.NewReader(msg))
LogError(err)
req.Header.Set("Authorization", token)
req.Header.Add("Content-Type", contentTypeSenml)
resp, err := httpClient.Do(req)
FormatResLog(resp, err)
}