mqttjson

package
v0.0.0-...-4208786 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientIsNotReady = errors.New("[RRPC] client is not reader")
)
View Source
var (
	// ErrRetailedMessage is an error indicating that retain message is not allowed.
	ErrRetailedMessage = errors.New("[RRPC] reatain message is not allowed, please set retaind=false")
)

Functions

func Dial

func Dial(reqTopic, replyTopic string, c mqttadapter.MQTTClientAdapter, qos byte) (*rpc.Client, error)

Dial establishes a connection to the MQTT broker and returns an RPC client. It takes the request topic, reply topic, MQTT client, and quality of service (QoS) as parameters. The function creates a new RPC connection using the provided parameters and returns a reverserpc_json.Client. If an error occurs during the connection establishment, it returns nil and the error.

func NewRPCClient

func NewRPCClient(conn io.ReadWriteCloser) *rpc.Client

NewRPCClient creates a new RPC client using the provided connection. It returns a pointer to an rpc.Client.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(client mqttadapter.MQTTClientAdapter, topicPrefix string) *Client

func (*Client) Call

func (s *Client) Call(ctx context.Context, targetId, serviceMethod string, args interface{}, reply interface{}) error

func (*Client) Client

func (s *Client) Client() mqttadapter.MQTTClientAdapter

func (*Client) Close

func (s *Client) Close() error

func (*Client) IsConnected

func (s *Client) IsConnected() bool

func (*Client) OnConnect

func (s *Client) OnConnect(cb func()) int

func (*Client) Subscribe

func (s *Client) Subscribe(topic string, qos byte, cb mqttadapter.MessageCallback)

type MQTTContext

type MQTTContext struct {
	// contains filtered or unexported fields
}

MQTTContext represents the context of an MQTT request.

func NewMQTTContext

func NewMQTTContext(req *RequestData, service *Server, validator *validator.Validate) *MQTTContext

NewMQTTContext creates a new MQTTContext with the given request, service, and validator. It returns a pointer to the created MQTTContext.

func (*MQTTContext) Bind

func (c *MQTTContext) Bind(request interface{}) error

Bind unmarshals the JSON-encoded request parameters into the provided request object and validates the request using the validator. It returns an error if the unmarshaling or validation fails.

func (*MQTTContext) ID

func (c *MQTTContext) ID() *rrpc.ID

ID returns the ID of the MQTTContext.

func (*MQTTContext) Method

func (c *MQTTContext) Method() string

Method returns the method of the MQTTContext. It retrieves the method from the underlying request.

func (*MQTTContext) PrometheusLabels

func (c *MQTTContext) PrometheusLabels() prometheus.Labels

PrometheusLabels returns the Prometheus labels for the MQTTContext. It includes the method and host labels.

func (*MQTTContext) Reply

func (c *MQTTContext) Reply(res *rrpc.Response) bool

Reply sends a response to the client. If the response has an error, it sends an error response. Otherwise, it sends an OK response with the result. It returns true to indicate that the response was sent successfully.

func (*MQTTContext) ReplyDesc

func (c *MQTTContext) ReplyDesc() string

ReplyDesc returns the reply topic for the MQTTContext.

type Request

type Request struct {
	ID       uint64            `json:"id"`
	Method   string            `json:"method"`
	Metadata map[string]string `json:"metadata,omitempty"`
	Params   json.RawMessage   `json:"params"`
}

Request represents a JSON-RPC request.

type RequestData

type RequestData struct {
	Topic string
	Request
}

RequestData represents a request message for a request.

func (*RequestData) GetResponse

func (r *RequestData) GetResponse() *ResponseData

GetResponse returns the response object for the request. It creates a new Response object with the reply topic and sets the ID and Method fields.

func (*RequestData) MakeErrResponse

func (r *RequestData) MakeErrResponse(status int, err error) *ResponseData

MakeErrResponse creates an error response with the specified status code and error message. It returns a pointer to a Response object.

func (*RequestData) MakeOKResponse

func (r *RequestData) MakeOKResponse(x interface{}) *ResponseData

MakeOKResponse creates a successful response with status code 200 and the provided data. It marshals the data to JSON format and sets it as the response data. The response object is returned.

func (*RequestData) ReplyTopic

func (r *RequestData) ReplyTopic() string

ReplyTopic returns the topic for the response message corresponding to the request. It replaces the word "request" with "response" in the original topic.

type Response

type Response struct {
	ID       uint64            `json:"id"`
	Method   string            `json:"method"`
	Status   int               `json:"status"`
	Metadata map[string]string `json:"metadata,omitempty"`
	Data     json.RawMessage   `json:"data"`
}

Response represents a JSON-RPC response.

type ResponseData

type ResponseData struct {
	Topic string
	Response
}

ResponseData represents a response message for a request.

type Server

type Server struct {
	*rrpc.Server
	// contains filtered or unexported fields
}

Server represents a MQTT service.

func NewServer

func NewServer(client mqttadapter.MQTTClientAdapter, subscribeTopic string, validator *validator.Validate, options ...rrpc.ServerOption) *Server

NewServer creates a new MQTT server with the provided options. It initializes an MQTT client with the given MQTT options and connects to the MQTT broker. The function also sets up the necessary configurations for the reverse RPC server. It returns a pointer to the created Service and an error if any.

func (*Server) Close

func (s *Server) Close() error

Close closes the MQTT service by disconnecting the IoT client. It returns an error if there was a problem disconnecting the client.

func (*Server) IsConnected

func (s *Server) IsConnected() bool

IsConnected returns a boolean value indicating whether the service is connected to the MQTT broker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL