# README

Eclipse hawkBit™ - Client library for Golang

This repository contains the Golang implementation of hawkBit SoftwareUpdatable feature.

The Eclipse Ditto Client for Golang is used for communication.

Table of Contents

Installation

go get github.com/eclipse/hawkbit-clients-golang

Creating and activating a software updatable feature

Each software updatable feature instance requires a ditto.Client and hawkbit.Configuration.

var su *hawkbit.SoftwareUpdatable

// Activate the SoftwareUpdatable feature after Ditto Client is connected.
configDitto := ditto.NewConfiguration().
	WithBroker("mqtt-host:1883").
	WithConnectHandler(func(dittoClient *ditto.Client) {
		if err := su.Activate(); err != nil {
			panic(fmt.Errorf("cannot activate software updatable feature: %v", err))
		}
	})

// Create new Ditto Client instance.
dittoClient := ditto.NewClient(configDitto)

// Create hawkBit SoftwareUpdatable feature configuration.
config := hawkbit.NewConfiguration().
	WithDittoClient(dittoClient).
	WithThingID(model.NewNamespacedIDFrom("my.namespace:thing.id")).
	WithSoftwareType("my-type").
	WithInstallHandler(installHandler)

// Create new hawkBit SoftwareUpdatable instance.
su, err = hawkbit.NewSoftwareUpdatable(config)
if err != nil {
	panic(fmt.Errorf("failed to create software updatable: %v", err))
}

NOTE: All feature propertires can be modified before the feature activation. This will change the initial feature property values.

After you have configured and created software updatable instance, the Ditto client is ready to be connected.

if err := dittoClient.Connect(); err != nil {
    panic(fmt.Errorf("cannot connect to broker: %v", err))
}

It's a good practice to deactivate the feature on client disconnect.

su.Deactivate()
dittoClient.Disconnect()

Working with software updatable feature

Update installed and context dependencies

// Create new DependencyDescription.
dependency := &hawkbit.DependencyDescription{Group: "My Group", Name: "App #1", Version: "1.0.0", Type: "my-type"}

// Update installed dependencies property.
if err := su.SetInstalledDependencies(dependency); err != nil {
	fmt.Println(fmt.Errorf("could not update installed dependencies property: %v", err))
}

// Update context dependencies property.
if err := su.SetContextDependencies(dependency); err != nil {
	fmt.Println(fmt.Errorf("could not update context dependencies property: %v", err))
}

Handle install operation

func installHandler(update *hawkbit.SoftwareUpdateAction, su *hawkbit.SoftwareUpdatable) {
    // Install provided software modules.
	for _, module := range update.SoftwareModules {
		status := hawkbit.NewOperationStatusUpdate(update.CorrelationID, hawkbit.StatusStarted, module.SoftwareModule).
			WithProgress(0).WithMessage("install operation just started")
		if err := su.SetLastOperation(status); err != nil {
			fmt.Println(fmt.Errorf("could not update the last operation: %v", err))
		}
		// Do the installation here.
	}
    // Finally update the installed dependencies.
}

NOTE: The last failed operation will be updated automatically, if needed.

Logging

Various levels of logs are provided by assigning the logging endpoints, ERROR, WARN, INFO and DEBUG. For example:

type LogLevel int

const (
	ERROR LogLevel = 1 + iota
	WARN
	INFO
	DEBUG
)

var level = INFO

func init() {
	hawkbit.ERROR = &wrapper{level: ERROR, prefix: "ERROR  "}
	hawkbit.WARN = &wrapper{level: WARN, prefix: "WARN   "}
	hawkbit.INFO = &wrapper{level: INFO, prefix: "INFO   "}
	hawkbit.DEBUG = &wrapper{level: DEBUG, prefix: "DEBUG  "}
}

type wrapper struct {
	level  LogLevel
	prefix string
}

func (w *wrapper) Println(v ...interface{}) {
	if level >= w.level {
		fmt.Println(w.prefix, fmt.Sprint(v...))
	}
}

func (w *wrapper) Printf(format string, v ...interface{}) {
	if level >= w.level {
		fmt.Printf(fmt.Sprint(w.prefix, " ", format), v...)
	}
}

# Functions

NewConfiguration returns a SoftwareUpdatable Configuration with the default feature ID.
NewOperationStatusRemove returns an OperationStatus with the mandatory fields needed for software module remove operation.
NewOperationStatusUpdate returns an OperationStatus with the mandatory fields needed for software module update operation.
NewSoftwareUpdatable creates new SoftwareUpdatable instance, which will manage the SoftwareUdpate feature with the specified type to the provided Thing via Ditto client connection.

# Constants

Supported transport protocols.
Supported transport protocols.
Supported transport protocols.
Supported hash algorithms.
Supported transport protocols.
Supported hash algorithms.
Supported hash algorithms.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.
Supported operation statuses.

# Variables

Levels of the library's output that can be configured during package initialization in init().
Levels of the library's output that can be configured during package initialization in init().
Levels of the library's output that can be configured during package initialization in init().
Levels of the library's output that can be configured during package initialization in init().

# Structs

Configuration provides the SoftwareUpdatable's configuration.
DependencyDescription describes an installed software or other dependencies for a device.
Links represents the datatype for the artifact links.
No description provided by the author
OperationStatus represents the status of an operation (install/remove) called on a device.
SoftwareArtifactAction specifications which should be installed.
SoftwareModuleAction representing a software module - a collection of artifacts to be downloaded and installed.
SoftwareModuleID represents an unique identifier for software modules.
SoftwareRemoveAction is used for instructing the device to remove a defined set of software.
SoftwareUpdatable is the HawkBit's library actual implementation.
SoftwareUpdateAction is used for instructing the device to install or download one or more software.

# Interfaces

No description provided by the author

# Type aliases

CancelHandler represents a callback handler that is called on each received cancel message.
CancelRemoveHandler represents a callback handler that is called on each received cancel remove message.
DownloadHandler represents a callback handler that is called on each received download message.
Hash is a representation of the defined by SoftwareUpdatable feature hash algorithms.
InstallHandler represents a callback handler that is called on each received install message.
Protocol is a representation of the defined by SoftwareUpdatable feature transport protocols.
RemoveHandler represents a callback handler that is called on each received remove message.
Status is representing the failure, progress or sucess of the operation.