particle

package module
v0.0.0-...-3cdf1ac Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 7 Imported by: 0

README

particle-go

Golang client library to access the Particle Cloud functionalities.

Install the library

Run the go get github.com/matisszilard/particle-go command.

How to use

In the first step the library must be initialized with a valid Particle Token.

// Initialize the particle-go library
// Set a proper Particle token to access the Particle Cloud
// Make sure that you don't share any private token in this repository
p := particle.Load(os.Getenv("PARTICLE_TOKEN"))

The library provide the following functionalities:

// Request all of the claimed particle photons
func (p *Particle) GetPhotons() ([]*Photon, error)

// Request a given Photon by ID
func (p *Particle) GetPhoton(id string) (*Photon, error)

// Subscribe for a specific event. The events are pushed to the "Event" channel set in the function parameters
func (p *Particle) GetEvent(c chan *Event, name string)

// Get a variable information from the Particle Cloud
func (p *Particle) GetVariable(ph *Photon, variable string) (*Variable, error)

// Call a function of a Photon
func (p *Particle) CallFunction(ph *Photon, function string, command string) (*Function, error)

NOTE: The library should work with other hardware devices too, but the functionalities are not tested with a different Particle hardware devices, only with Particle Photon.

Build the library

You can build the library with the provided makefile by running make all command. The makefile will create a clean build, get the dependencies, and run go build.

The library use glide as a package manager. For more information please check the glide official github page: https://github.com/Masterminds/glide.

Makefile targets
# Create a clean build
all: clean build

# Build the library with the dependencies
build: deps
	go build

# Install the library dependencies
deps:
	glide install

# Remove the vendor folder
clean:
	rm -rf vendor

# Run the linter command
lint:
	gometalinter --vendor --deadline=300s ./...

# Get the required tools
get-tools:
	go get -u github.com/alecthomas/gometalinter
	gometalinter --install --update

License

Please check the LICENSE file under the source. (MIT)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Data        string `json:"data"`
	TTL         string `json:"ttl"`
	PublishTime string `json:"published_at"`
	PhotonID    string `json:"coreid"`
}

Data represent the data struct from the Particle Cloud

func ToData

func ToData(j string) (*Data, error)

ToData convert a json string to Data struct

type Event

type Event struct {
	ID   string
	Name string
	Data *Data
}

Event represent an event from the Particle Cloud

func ToEvent

func ToEvent(e eventsource.Event) (*Event, error)

ToEvent convert an eventsource.Event into an Event struct

type Function

type Function struct {
	Name        string
	ReturnValue int
	PhotonID    string
}

Function contains information about a Particle function

type Particle

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

Particle struct to access the Particle Cloud

func Load

func Load(particleToken string) *Particle

Load initialize the particle token

func (*Particle) CallFunction

func (p *Particle) CallFunction(ph *Photon, function string, command string) (*Function, error)

CallFunction call a given Particle function on the Photon

func (*Particle) GetEvent

func (p *Particle) GetEvent(c chan *Event, name string)

GetEvent get a given event from the particle cloud

func (*Particle) GetPhoton

func (p *Particle) GetPhoton(id string) (*Photon, error)

GetPhoton get a photon information from the Particle Cloud

func (*Particle) GetPhotons

func (p *Particle) GetPhotons() ([]*Photon, error)

GetPhotons from the Particle Cloud

func (*Particle) GetVariable

func (p *Particle) GetVariable(ph *Photon, variable string) (*Variable, error)

GetVariable get a given photon variable information from the Particle Cloud

type Photon

type Photon struct {
	// Particle Cloud defined variables
	ID        string `json:"id"`
	Name      string `json:"name"`
	Claimed   bool   `json:"claimed"`
	Connected bool   `son:"connected"`
	Status    string `json:"status"`
	IPAddress string `json:"ipaddress"`

	// Photon software defined variables and functions
	Variables []*Variable `json:"variables"`
	Functions []*Function `json:"functions"`
}

Photon model for storing a Photon

func (*Photon) GetVariable

func (p *Photon) GetVariable(name string) *Variable

GetVariable return the Photon Variable

func (*Photon) HasFunction

func (p *Photon) HasFunction(name string) (bool, int)

HasFunction check if the photon has the given function. Return the result of the search and the index of the function.

func (*Photon) HasVariable

func (p *Photon) HasVariable(name string) (bool, int)

HasVariable check if the photon has the given variable. Return the result of the search and the index of the variable.

func (*Photon) SetVariable

func (p *Photon) SetVariable(v *Variable)

SetVariable set a variable in the photon, if the variable was already added then it is only an update operation, otherwise it is going to add the new variable

type Variable

type Variable struct {
	PhotonID string
	Name     string
	Value    float64
}

Variable contains information about a Particle variable

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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