plugin

package
v2.0.0-...-3280c16 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package plugin provides types used to create plugins.

A plugin is a "code" used to extend functionality of the gakisitor, like new user input. In fact, because the gakisitor must be working with different user interface (like training bike or simple button), we need to easily extend its functionality, working for several user input through GPIO (button, distance sensor, ...).

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// State code formats
	// +---------+-----------------+--------------------------------------------------------------+
	// |  Range  |   Description   |                         Behaviours                           |
	// +---------+-----------------+--------------------------------------------------------------+
	// | 1x ~ 1F | OK states       | Nothing                                                      |
	// | 2x ~ DF | Custom states   | Nothing                                                      |
	// | Ex ~ EF | Error states    | The scheduler notify the server                              |
	// | Fx ~ FF | Critical states | The scheduler try to restart the plugin && notify the server |
	// +---------+-----------------+--------------------------------------------------------------+
	NilState State

	IdleState      = State{0x11, "currently idle", nil}
	InSessionState = State{0x12, "currently in session", nil}
	PausedState    = State{0x13, "currently paused", nil}

	GPIODisconnectionState  = State{0xE1, "GPIO has been disconnected", nil}
	GPIOFailureState        = State{0xE2, "GPIO reading has failed", nil}
	AggregationFailureState = State{0xE3, "data aggregation has failed", nil}
	ConversionFailureState  = State{0xE4, "data conversion has failed", nil}

	GPIOPanicState    = State{0xF1, "GPIO critical error", nil}
	HandledPanicState = State{0xF2, "panic handled", nil}
)

Predefined states.

Functions

This section is empty.

Types

type Chan

type Chan struct {
	Data        chan<- interface{} // Used to send data (only JSON serializable data)
	Status      chan<- State       // Used to send status
	Instruction <-chan Instruction // Used to read instruction from the gakisitor
}

Chan provides container with all channels used by the plugins.

type Instruction

type Instruction byte

Instruction type.

const (
	StatusPluginInstruction Instruction = 0x01 // Send a the current status
	StartSessionInstruction Instruction = 0x10 // Start a game session (you MUST retrieve user input during this session)
	StopSessionInstruction  Instruction = 0x1F // Stop the game session (you MUST stop your retrieving user input)
)

Instructions list.

type Plugin

type Plugin struct {
	// The plugin name. It will be used by the server/game
	// engine to know which plugin the data comes from.
	Name string

	// Start the plugin instance with the plugin profile and channels. You
	// MUST check the profile before starting the process.
	//
	// For more information about plugin, see the package description.
	// For more information about plugin channels, see the Chan structure above.
	Instance func(ctx context.Context, profile profile.Plugin, channels Chan) error
}

Plugin structure used to extend the Gakisitor functionality

Example (Basic)
_ = Plugin{
	Name: "ExamplePlugin",
	Instance: func(ctx context.Context, profile profile.Plugin, channels Chan) error {
		var inSession bool
		var state = IdleState
		dataMarshallable := struct {
			A int     `json:"a"`
			B float64 `json:"b"`
		}{0, 0.0}

		// configuration value shared into the profile (Config > ManyItems > ThisItem)
		_, e := profile.AccessTo("ManyItems", "ThisItem")
		if e != nil {
			return e
		}

		// plugin main loop
		for {
			select {
			// closing context here ... DO NOT FORGET IT
			case <-ctx.Done():
				return nil

				// interpret instruction here
			case instruction, valid := <-channels.Instruction:
				// if channel is closed, you must stop the plugin
				if !valid {
					return nil
				}

				switch instruction {
				case StatusPluginInstruction:
					channels.Status <- state
				case StartSessionInstruction:
					inSession = true
					state = InSessionState
				case StopSessionInstruction:
					inSession = false
					state = IdleState
				}

				// example of data sending
			case <-time.Tick(time.Millisecond):
				if inSession {
					channels.Data <- dataMarshallable
				}
			}
		}
	},
}
Output:

type State

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

State represent the current plugin state. State are immutable (to prevent predefined state edition).

func NewState

func NewState(code byte, desc string, raw ...interface{}) State

NewState creates a new custom state.

func (State) AddRaw

func (state State) AddRaw(raw interface{}) State

AddRaw add a raw to an existing state.

func (State) Code

func (state State) Code() byte

Code return the state code.

func (State) Desc

func (state State) Desc() string

Desc return the state description.

func (State) Equal

func (state State) Equal(o interface{}) bool

Equal compare two states (compare only state codes).

func (State) Raw

func (state State) Raw() interface{}

Raw return the state raw.

Directories

Path Synopsis
Package plugin_test provides tools used to tests plugins.
Package plugin_test provides tools used to tests plugins.

Jump to

Keyboard shortcuts

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