webthing

package module
v0.0.0-...-30ee7c0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

README

Web of Things


GitHub forks GitHub version GoDoc Codacy Badge travis Go Report Card codebeat badge Build Status

USAGE

This library fully supports Web Thing REST API.You can start building your Web of Thing by looking at single-thing.

Download and import

This package name is called webthing. This project is called webthing-go to keep the naming consistent with the implementation of other languages. You just need to import this package the way golang normally imports a package.

go get -u -v github.com/dravenk/webthing-go
import (
 "github.com/dravenk/webthing-go"
)
Create Thing
// Create a Lamp.
thing := webthing.NewThing("urn:dev:ops:my-thing-1234",
 "Lamp",
 []string{"OnOffSwitch", "Light"},
 "A web connected thing")

For more information on Creating Webthing, please check the wiki Create-Thing

Example
cd $GOPATH/src/github.com/dravenk/webthing-go
go run examples/single-thing/single-thing.go

You can also run a sample with docker:

docker run -ti --name single-thing -p 8888:8888 dravenk/webthing

For more information on Run Example, please check the wiki Run-Example

RESOURCES

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseHandle

func BaseHandle(h interface{}, w http.ResponseWriter, r *http.Request)

BaseHandle Base handler that is initialized with a list of things. func BaseHandle(h BaseHandler, w http.ResponseWriter, r *http.Request) {

func Timestamp

func Timestamp() string

Timestamp Get the current time.

@return The current time in the form YYYY-mm-ddTHH:MM:SS+00.00

Types

type Action

type Action struct {

	// Override this with the code necessary to perform the action.
	PerformAction func() *Action

	// Override this with the code necessary to cancel the action.
	Cancel func()
	// contains filtered or unexported fields
}

Action An Action represents an individual action on a thing.

func NewAction

func NewAction(id string, thing *Thing, name string, input *json.RawMessage, PerformAction func() *Action, Cancel func()) *Action

NewAction Initialize the object. @param id ID of this action @param thing Thing this action belongs to @param name Name of the action @param input Any action inputs

func (*Action) AsActionDescription

func (action *Action) AsActionDescription() []byte

AsActionDescription Get the action description. @return Description of the action as a JSONObject.

func (*Action) Finish

func (action *Action) Finish() *Action

Finish performing the action.

func (*Action) Href

func (action *Action) Href() string

Href Get this action's href. @returns {String} The href.

func (*Action) ID

func (action *Action) ID() string

ID Get this action's ID. @returns {String} The ID.

func (*Action) Input

func (action *Action) Input() *json.RawMessage

Input Get the inputs for this action. @returns {Object} The inputs.

func (*Action) Name

func (action *Action) Name() string

Name Get this action's name. @returns {String} The name.

func (*Action) SetHrefPrefix

func (action *Action) SetHrefPrefix(prefix string)

SetHrefPrefix Set the prefix of any hrefs associated with this action. @param prefix The prefix

func (*Action) SetInput

func (action *Action) SetInput(input *json.RawMessage)

SetInput Set any input to this action. @param input The input

func (*Action) Start

func (action *Action) Start() *Action

Start performing the action.

func (*Action) Status

func (action *Action) Status() string

Status Get this action's status. Get this action's status. @returns {String} The status.

func (*Action) Thing

func (action *Action) Thing() *Thing

Thing Get the thing associated with this action. @returns {Object} The thing.

func (*Action) TimeCompleted

func (action *Action) TimeCompleted() string

TimeCompleted Get the time the action was completed. @returns {String} The time.

func (*Action) TimeRequested

func (action *Action) TimeRequested() string

TimeRequested Get the time the action was requested. @returns {String} The time.

type ActionHandle

type ActionHandle struct {
	*ActionsHandle
	ActionName string
}

ActionHandle Handle a request to /actions/<action_name>.

func (*ActionHandle) Get

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*ActionHandle) Handle

func (h *ActionHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /actions/<action_name>.

func (*ActionHandle) Post

func (h *ActionHandle) Post(w http.ResponseWriter, r *http.Request)

Post Handle a Post request.

@param {Object} r The request object @param {Object} w The response object

type ActionIDHandle

type ActionIDHandle struct {
	*ActionHandle
	*Action
}

ActionIDHandle Handle a request to /actions/<action_name>/<action_id>.

func (*ActionIDHandle) Delete

func (h *ActionIDHandle) Delete(w http.ResponseWriter, r *http.Request)

Delete Handle a Delete request.

@param {Object} r The request object @param {Object} w The response object

func (*ActionIDHandle) Get

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*ActionIDHandle) Handle

func (h *ActionIDHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /actions/<action_name>/<action_id>.

type Actioner

type Actioner interface {
	// Custom Action need create a Generator to generate a action.
	// The application will invoke the Action created by the Generator method.
	// This is very similar to simply constructor.
	// See thing.PerformAction()*Action
	Generator(thing *Thing) *Action

	// Override this with the code necessary to perform the action.
	PerformAction() *Action

	// Override this with the code necessary to cancel the action.
	Cancel()

	Start() *Action
	Finish() *Action

	AsActionDescription() []byte
	SetHrefPrefix(prefix string)
	ID() string
	Name() string
	Href() string
	Status() string
	Thing() *Thing
	TimeRequested() string
	TimeCompleted() string
	Input() *json.RawMessage
	SetInput(input *json.RawMessage)
}

Actioner Customize the methods that the action must implement.

type ActionsHandle

type ActionsHandle struct {
	*ThingHandle
}

ActionsHandle Handle a request to /actions.

func (*ActionsHandle) Get

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*ActionsHandle) Handle

func (h *ActionsHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /actions.

func (*ActionsHandle) Post

func (h *ActionsHandle) Post(w http.ResponseWriter, r *http.Request)

Post Handle a POST request.

@param {Object} req The request object @param {Object} res The response object

type AvailableAction

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

AvailableAction Class to describe an action available to be taken.

func NewAvailableAction

func NewAvailableAction(metadata json.RawMessage, cls Actioner) *AvailableAction

NewAvailableAction Initialize the object.

@param metadata The action metadata @param action Instance for the action

func (*AvailableAction) Action

func (ac *AvailableAction) Action() *Action

Action Get the class to instantiate for the action.

@return The class.

func (*AvailableAction) Metadata

func (ac *AvailableAction) Metadata() []byte

Metadata Get the action metadata.

@return The metadata.

func (*AvailableAction) ValidateActionInput

func (ac *AvailableAction) ValidateActionInput(actionInput interface{}) bool

ValidateActionInput Validate the input for a new action.

@param actionInput The input to validate @return Boolean indicating validation success.

type AvailableEvent

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

AvailableEvent Class to describe an event available for subscription.

func NewAvailableEvent

func NewAvailableEvent(metadata json.RawMessage) *AvailableEvent

NewAvailableEvent Initialize the object.

@param metadata The event metadata

func (*AvailableEvent) Metadata

func (ae *AvailableEvent) Metadata() json.RawMessage

Metadata Get the event metadata.

@return The metadata.

type DeleteInterface

type DeleteInterface interface {
	Delete(w http.ResponseWriter, r *http.Request)
}

DeleteInterface Implementation of http Delete menthod.

type Event

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

Event An Event represents an individual event from a thing.

func NewEvent

func NewEvent(thing *Thing, name string, data json.RawMessage) *Event

NewEvent Initialize the object. @param thing Thing this event belongs to @param name Name of the event @param data Data associated with the event

func (*Event) AsEventDescription

func (event *Event) AsEventDescription() []byte

AsEventDescription Get the event description. @return Description of the event as a JSONObject.

func (*Event) Data

func (event *Event) Data() json.RawMessage

Data Get the event's data. @returns {*} The data.

func (*Event) Name

func (event *Event) Name() string

Name Get the event's name. @returns {String} The name.

func (*Event) Thing

func (event *Event) Thing() *Thing

Thing Get the thing associated with this event. @returns {Object} The thing.

func (*Event) Time

func (event *Event) Time() string

Time Get the event's timestamp. @returns {String} The time.

type EventHandle

type EventHandle struct {
	*EventsHandle
	// contains filtered or unexported fields
}

EventHandle handle a request to /events.

func (*EventHandle) Get

func (h *EventHandle) Get(w http.ResponseWriter, r *http.Request)

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*EventHandle) Handle

func (h *EventHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /events.

type EventObject

type EventObject struct {
	AtType      string `json:"@type,omitempty"`
	Title       string `json:"title,omitempty"`
	ObjectType  string `json:"type,omitempty"`
	Description string `json:"description,omitempty"`
	Unit        string `json:"unit,omitempty"`
	Links       []Link `json:"links,omitempty"`
}

EventObject An event object describes a kind of event which may be emitted by a device. See https://iot.mozilla.org/wot/#event-object

type EventsHandle

type EventsHandle struct {
	*ThingHandle
}

EventsHandle a request to /events.

func (*EventsHandle) Get

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*EventsHandle) Handle

func (h *EventsHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /events.

type GetInterface

type GetInterface interface {
	Get(w http.ResponseWriter, r *http.Request)
}

GetInterface Implementation of http Get menthod.

type Link struct {
	Href      string `json:"href,omitempty"`
	Rel       string `json:"rel,omitempty"`
	MediaType string `json:"mediaType,omitempty"`
}

Link base link struct

type MultipleThings

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

MultipleThings A container for multiple things.

func NewMultipleThings

func NewMultipleThings(things []*Thing, name string) *MultipleThings

NewMultipleThings Initialize the container.

@param {Object} things The things to store @param {String} name The mDNS server name

func (*MultipleThings) Name

func (mt *MultipleThings) Name() string

Name Get the mDNS server name.

func (*MultipleThings) Thing

func (mt *MultipleThings) Thing(idx int) *Thing

Thing Get the thing at the given index.

@param {Number|String} idx The index

func (*MultipleThings) Things

func (mt *MultipleThings) Things() []*Thing

Things Get the list of things.

type PostInterface

type PostInterface interface {
	Post(w http.ResponseWriter, r *http.Request)
}

PostInterface Implementation of http Post menthod.

type PropertiesHandle

type PropertiesHandle struct {
	*ThingHandle
}

PropertiesHandle Handle a request to /properties.

func (*PropertiesHandle) Get

Get Handle a Get request.

@param {Object} r The request object @param {Object} w The response object

func (*PropertiesHandle) Handle

func (h *PropertiesHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle Handle a request to /properties.

type Property

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

Property Initialize the object.

@param thing Thing this property belongs to @param name Name of the property @param value Value object to hold the property value @param metadata Property metadata, i.e. type, description, unit, etc., as

a Map

func NewProperty

func NewProperty(thing *Thing, name string, value Value, metadata json.RawMessage) *Property

NewProperty Initialize the object.

@param {Object} thing Thing this property belongs to @param {String} name Name of the property @param {Value} value Value object to hold the property value @param {Object} metadata Property metadata, i.e. type, description, unit,

etc., as an object.

func (*Property) AsPropertyDescription

func (property *Property) AsPropertyDescription() []byte

AsPropertyDescription Get the property description.

@returns {Object} Description of the property as an object.

func (*Property) Href

func (property *Property) Href() string

Href Get the href of this property.

@returns {String} The href

func (*Property) Metadata

func (property *Property) Metadata() json.RawMessage

Metadata Get the metadata associated with this property

@returns {Object} The metadata

func (*Property) Name

func (property *Property) Name() string

Name Get the name of this property.

@returns {String} The property name.

func (*Property) SetHrefPrefix

func (property *Property) SetHrefPrefix(prefix string)

SetHrefPrefix Set the prefix of any hrefs associated with this property.

@param {String} prefix The prefix

func (*Property) SetValue

func (property *Property) SetValue(value *Value) error

SetValue Set the current value of the property.

@param {*} value The value to set

func (*Property) Thing

func (property *Property) Thing() *Thing

Thing Get the thing associated with this property.

@returns {Object} The thing.

func (*Property) ValidateValue

func (property *Property) ValidateValue(value interface{}) error

ValidateValue Validate new property value before setting it.

@param {*} value - New value

func (*Property) Value

func (property *Property) Value() *Value

Value Get the current property value.

@returns {*} The current value

type PropertyHandle

type PropertyHandle struct {
	*PropertiesHandle
	*Property
}

PropertyHandle a request to /properties/<property>.

func (*PropertyHandle) Get

Get Handle a GET request.

@param {Object} r The request object @param {Object} w The response object

func (*PropertyHandle) Handle

func (h *PropertyHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /properties/<property>.

func (*PropertyHandle) Put

Put Handle a PUT request.

@param {Object} r The request object @param {Object} w The response object

type PropertyObject

type PropertyObject struct {
	AtType      string      `json:"@type,omitempty"`
	Title       string      `json:"title,omitempty"`
	Type        string      `json:"type,omitempty"`
	Description string      `json:"description,omitempty"`
	Unit        string      `json:"unit,omitempty"`
	ReadOnly    bool        `json:"readOnly,omitempty"`
	Minimum     json.Number `json:"minimum,omitempty"`
	Maximum     json.Number `json:"maximum,omitempty"`
	Links       []Link      `json:"links,omitempty"`
}

PropertyObject A property object describes an attribute of a Thing and is indexed by a property id. See https://iot.mozilla.org/wot/#property-object

type PutInterface

type PutInterface interface {
	Put(w http.ResponseWriter, r *http.Request)
}

PutInterface Implementation of http Put menthod.

type SingleThing

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

SingleThing A container for a single thing.

func NewSingleThing

func NewSingleThing(thing *Thing) *SingleThing

NewSingleThing Initialize the container.

@param {Object} thing The thing to store

func (*SingleThing) Name

func (st *SingleThing) Name() string

Name Get the mDNS server name.

func (*SingleThing) Thing

func (st *SingleThing) Thing(idx int) *Thing

Thing Get the thing at the given index.

func (*SingleThing) Things

func (st *SingleThing) Things() []*Thing

Things Get the list of things.

type Thing

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

Thing A Web Thing struct.

func NewThing

func NewThing(id, title string, atType []string, description string) *Thing

NewThing create a thing.

func (*Thing) Action

func (thing *Thing) Action(actionName, actionID string) (action *Action)

Action Get an action.

@param actionName Name of the action @param actionId ID of the action @return The requested action if found, else null.

func (*Thing) ActionDescriptions

func (thing *Thing) ActionDescriptions(actionName string) (descriptions []json.RawMessage)

ActionDescriptions Get the thing's actions as an array.

@param {String?} actionName Optional action name to get descriptions for @returns {Object} Action descriptions.

func (*Thing) ActionNotify

func (thing *Thing) ActionNotify(action *Action) error

ActionNotify Notify all subscribers of an action status change.

@param action The action whose status changed

func (*Thing) AddAvailableAction

func (thing *Thing) AddAvailableAction(name string, metadata json.RawMessage, action Actioner)

AddAvailableAction Add an available action.

@param name Name of the action @param metadata Action metadata, i.e. type, description, etc., as a

JSONObject

@param action Instantiate for this action

func (*Thing) AddAvailableEvent

func (thing *Thing) AddAvailableEvent(name string, metadata json.RawMessage)

AddAvailableEvent Add an available event.

@param name Name of the event @param metadata Event metadata, i.e. type, description, etc., as a

JSONObject

func (*Thing) AddEvent

func (thing *Thing) AddEvent(event *Event)

AddEvent Add a new event and notify subscribers.

@param event The event that occurred.

func (*Thing) AddEventSubscriber

func (thing *Thing) AddEventSubscriber()

AddEventSubscriber Add a new websocket subscriber to an event.

@param name Name of the event @param ws The websocket

func (*Thing) AddProperty

func (thing *Thing) AddProperty(property *Property)

AddProperty Add a property to this thing.

@param property Property to add.

func (*Thing) AddSubscriber

func (thing *Thing) AddSubscriber(wsID string, ws *websocket.Conn)

AddSubscriber Add a new websocket subscriber.

@param ws The websocket

func (*Thing) AsThingDescription

func (thing *Thing) AsThingDescription() []byte

AsThingDescription retrun []byte data of thing struct. Return the thing state as a Thing Description. @returns {Object} Current thing state

func (*Thing) Context

func (thing *Thing) Context() string

Context Get the type context of the thing.

@returns {String} The contexthing.

func (*Thing) Description

func (thing *Thing) Description() string

Description Get the description of the thing.

@returns {String} The description.

func (*Thing) EventDescriptions

func (thing *Thing) EventDescriptions(eventName string) []byte

EventDescriptions Get the thing's events as an array.

@param {String?} eventName Optional event name to get descriptions for

@returns {Object} Event descriptions.

func (*Thing) EventNotify

func (thing *Thing) EventNotify(event *Event) error

EventNotify Notify all subscribers of an event.

@param event The event that occurred

func (*Thing) HasProperty

func (thing *Thing) HasProperty(propertyName string) bool

Determine whether or not this thing has a given property.

@param propertyName The property to look for @return Indication of property presence.

func (*Thing) Href

func (thing *Thing) Href() string

Href Get this thing's href.

@returns {String} The href.

func (*Thing) ID

func (thing *Thing) ID() string

ID Get the ID of the thing.

@returns {String} The ID.

func (*Thing) PerformAction

func (thing *Thing) PerformAction(actionName string, input *json.RawMessage) (*Action, error)

PerformAction Perform an action on the thing.

@param actionName Name of the action @param input Any action inputs @return The action that was created.

func (*Thing) Properties

func (thing *Thing) Properties() map[string]interface{}

Properties et a mapping of all properties and their values.

@return JSON object of propertyName -&gt; value.

func (*Thing) Property

func (thing *Thing) Property(propertyName string) *Value

Property Get a property's value.

@param propertyName Name of the property to get the value of @param <T> Type of the property value @return Current property value if found, else null.

func (*Thing) PropertyDescriptions

func (thing *Thing) PropertyDescriptions() string

PropertyDescriptions Get the thing's properties as an object.

@returns {Object} Properties, i.e. name -> description

func (*Thing) PropertyNotify

func (thing *Thing) PropertyNotify(property Property) error

PropertyNotify Notify all subscribers of a property change.

@param property The property that changed

func (*Thing) RemoveAction

func (thing *Thing) RemoveAction(actionName, actionID string) bool

RemoveAction Remove an existing action.

@param actionName name of the action @param actionId ID of the action @return Boolean indicating the presence of the action.

func (*Thing) RemoveEventSubscriber

func (thing *Thing) RemoveEventSubscriber(name string, ws *websocket.Conn) error

RemoveEventSubscriber Remove a websocket subscriber from an event.

@param name Name of the event @param ws The websocket

func (*Thing) RemoveProperty

func (thing *Thing) RemoveProperty(property Property)

RemoveProperty Remove a property from this thing.

@param property Property to remove.

func (*Thing) RemoveSubscriber

func (thing *Thing) RemoveSubscriber(name string, ws *websocket.Conn)

RemoveSubscriber Remove a websocket subscriber.

@param ws The websocket

func (*Thing) SetHrefPrefix

func (thing *Thing) SetHrefPrefix(prefix string)

SetHrefPrefix Set the prefix of any hrefs associated with this thing.

@param {String} prefix The prefix

func (*Thing) SetProperty

func (thing *Thing) SetProperty(propertyName string, value *Value) error

SetProperty Set a property value.

@param propertyName Name of the property to set @param value Value to set @param <T> Type of the property value @throws PropertyError If value could not be set.

func (*Thing) SetUIHref

func (thing *Thing) SetUIHref(href string)

SetUIHref Set the href of this thing's custom UI.

@param {String} href The href

func (*Thing) Title

func (thing *Thing) Title() string

Title Get the title of the thing.

@returns {String} The title.

func (*Thing) Type

func (thing *Thing) Type() []string

Type Get the type(s) of the thing.

@returns {String[]} The type(s).

func (*Thing) UIHref

func (thing *Thing) UIHref() string

UIHref Get this thing's UI href.

@returns {String|null} The href.

type ThingHandle

type ThingHandle struct {
	*Thing
}

ThingHandle Handle a request to thing.

func (*ThingHandle) Get

func (h *ThingHandle) Get(w http.ResponseWriter, r *http.Request)

Get Handle a Get request.

@param {Object} r The request object @param {Object} w The response object

func (*ThingHandle) Handle

func (h *ThingHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle a request to /thing.

type ThingMember

type ThingMember struct {
	ID          string                     `json:"id"`
	Context     string                     `json:"@context"`
	AtType      []string                   `json:"@type"`
	Title       string                     `json:"title"`
	Description string                     `json:"description,omitempty"`
	Properties  json.RawMessage            `json:"properties,omitempty"`
	Actions     map[string]json.RawMessage `json:"actions,omitempty"`
	Events      map[string]json.RawMessage `json:"events,omitempty"`
	Links       []Link                     `json:"links"`
}

ThingMember thingmember

func NewThingMember

func NewThingMember(thing *Thing) *ThingMember

type ThingServer

type ThingServer struct {
	*http.Server
	Things   []*Thing
	Name     string
	BasePath string
}

ThingServer Web Thing Server.

func NewWebThingServer

func NewWebThingServer(thingType ThingsType, httpServer *http.Server, basePath string) *ThingServer

NewWebThingServer Initialize the WebThingServer.

@param thingType List of Things managed by this server @param basePath Base URL path to use, rather than '/'

func (*ThingServer) Start

func (server *ThingServer) Start() error

Start Start listening for incoming connections.

@return Error on failure to listen on port

func (*ThingServer) Stop

func (server *ThingServer) Stop() error

Stop Stop listening.

type ThingsHandle

type ThingsHandle struct {
	Things []*Thing
	// contains filtered or unexported fields
}

ThingsHandle things struct.

func (*ThingsHandle) Get

Get Handle a Get request.

@param {Object} r The request object @param {Object} w The response object

func (*ThingsHandle) Handle

func (h *ThingsHandle) Handle(w http.ResponseWriter, r *http.Request)

Handle handle request.

type ThingsType

type ThingsType interface {

	// Thing Get the thing at the given index.
	//
	// @param idx Index of thing.
	// @return The thing, or null.
	Thing(idx int) *Thing

	// Things Get the list of things.
	//
	// @return The list of things.
	Things() []*Thing

	// Name Get the mDNS server name.
	//
	// @return The server name.
	Name() string
}

ThingsType Container of Things Type

type Value

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

Value A property value.

This is used for communicating between the Thing representation and the actual physical thing implementation.

Notifies all observers when the underlying value changes through an external update (command to turn the light off) or if the underlying sensor reports a new value.

func NewValue

func NewValue(initialValue interface{}, valueForwarder ...func(interface{})) Value

NewValue Initialize the object.

@param {*} initialValue The initial value @param {function?} valueForwarder The method that updates the actual value

on the thing

func (*Value) Get

func (v *Value) Get() interface{}

Get Return the last known value from the underlying thing.

@returns the value.

func (*Value) NotifyOfExternalUpdate

func (v *Value) NotifyOfExternalUpdate(value interface{})

NotifyOfExternalUpdate Notify observers of a new value.

@param {*} value New value

func (*Value) Set

func (v *Value) Set(value interface{})

Set a new value for this thing.

@param {*} value Value to set

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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