Categorygithub.com/vaelen/iot
modulepackage
0.0.0-20191108183227-4859fc7ef436
Repository: https://github.com/vaelen/iot.git
Documentation: pkg.go.dev

# README

IoT

A simple framework for implementing a Google IoT device.

This package makes use of the context package to handle request cancelation, timeouts, and deadlines.

gocover.io Go Report Card Go Docs Mentioned in Awesome Go

Copyright 2018, Andrew C. Young <[email protected]>

License: MIT

Here is an example showing how to use this library:

package main

import (
	"context"
	"log"
	"github.com/vaelen/iot"
	// Your client must include the paho package
	// to use the default Eclipse Paho MQTT client.
	_ "github.com/vaelen/iot/paho"
)

func main() {
	ctx := context.Background()

	id := &iot.ID{
		DeviceID:  "deviceName",
		Registry:  "my-registry",
		Location:  "asia-east1",
		ProjectID: "my-project",
	}

	credentials, err := iot.LoadRSACredentials("rsa_cert.pem", "rsa_private.pem")
	if err != nil {
		panic("Couldn't load credentials")
	}

	options := iot.DefaultOptions(id, credentials)
	options.DebugLogger = log.Println
	options.InfoLogger = log.Println
	options.ErrorLogger = log.Println
	options.ConfigHandler = func(thing iot.Thing, config []byte) {
		// Do something here to process the updated config and create an updated state string
		state := []byte("ok")
		thing.PublishState(ctx, state)
	}

	thing := iot.New(options)

	err = thing.Connect(ctx, "ssl://mqtt.googleapis.com:443")
	if err != nil {
		panic("Couldn't connect to server")
	}
	defer thing.Disconnect(ctx)

	// This publishes to /events
	thing.PublishEvent(ctx, []byte("Top level telemetry event"))
	// This publishes to /events/a
	thing.PublishEvent(ctx, []byte("Sub folder telemetry event"), "a")
	// This publishes to /events/a/b
	thing.PublishEvent(ctx, []byte("Sub folder telemetry event"), "a", "b")
}

Thanks to Infostellar for supporting my development of this project.

# Packages

No description provided by the author
Package paho provides an iot.MQTTClient implementation that uses the Eclipse Paho MQTT client.

# Functions

DefaultOptions returns the default set of options.
LoadECCredentials creates a Credentials struct from the given EC private key and certificate.
LoadRSACredentials creates a Credentials struct from the given RSA private key and certificate.
New returns a new Thing using the given options.
NewMockClient returns an instance of MockMQTTClient The MockMQTTClient documentation explains how to use this method when writing tests.

# Constants

CredentialTypeEC specifies that the credentials use Eliptic Curve keys.
CredentialTypeRSA specfies that the credentials use RSA keys.
DefaultAuthTokenExpiration is the default value for Thing.AuthTokenExpiration.

# Variables

ErrCancelled is returned when a context is canceled or times out.
ErrConfigurationError is returned from Connect() if either the ID or Credentials have not been set.
ErrNotConnected is returned if a message is published but the client is not connected.
ErrPublishFailed is returned if the client was unable to send the message.
NewClient is the ClientConstructor used to create MQTT client instances Override this value during testing to provide an MQTT client mock implementation.

# Structs

Credentials wraps the public and private key for a device.
ID represents the various components that uniquely identify this device.
MockMQTTClient implements a mock MQTT client for use in testing To use this client, use code like the following: set iot.NewClient = iot.NewMockClient.
ThingOptions holds the options that are used to create a Thing.

# Interfaces

The MQTTClient interface represents an underlying MQTT client implementation in an abstract way.
Thing represents an IoT device.

# Type aliases

ClientConstructor defines a function for creating an MQTT client instance.
ConfigHandler handles configuration updates received from the server.
CredentialType defines the key type of the credential key pair.
Logger is used to write log output.
MQTTCredentialsProvider should return the current username and password for the MQTT client to use.
MQTTOnConnectHandler will be called after the client connects.