Coder Social home page Coder Social logo

webthing-go's Introduction

Web of Things

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

USAGE:

You can start building your Web of Thing by looking at single-thing

Download and import:

 go get -u -v github.com/dravenk/webthing-go

This package is called webthing. You just need to import this package the way golang normally imports a package.

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")

Before creating OnOffProperty you need to create the Forwarder method of OnOff Value. The method that updates the actual value on the thing Example:

func onValueForwarder(i interface{}) {
    fmt.Println("Now on statue: ", i)
}

Create an onValue with default value:

onValue := webthing.NewValue(true, onValueForwarder)
// Adding an OnOffProperty to thing.
onDescription := []byte(`{
    "@type": "OnOffProperty",
    "type": "boolean",
    "title": "On/Off",
    "description": "Whether the lamp is turned on"
    }`)
onValue := webthing.NewValue(true, onValueForwarder)
on := webthing.NewProperty(thing,
	"on",
	onValue,
	onDescription)
thing.AddProperty(on)

Create an action. The methods you have to implement are:

// 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()

The Action Request can be used to retrieve input data in the following way like the fade.Thing().Input(). Here is an example:

type FadeAction struct {
	*webthing.Action
}

func (fade *FadeAction) Generator(thing *webthing.Thing) *webthing.Action {
	fade.Action = webthing.NewAction(uuid.New().String(), thing, "fade", nil, fade.PerformAction, fade.Cancel)
	return fade.Action
}

func (fade *FadeAction) PerformAction() *webthing.Action {
	thing := fade.Thing()
	params, _ := fade.Input().MarshalJSON()

	input := make(map[string]interface{})
	if err := json.Unmarshal(params, &input); err != nil {
		fmt.Println(err)
	}
	if brightness, ok := input["brightness"]; ok {
		thing.Property("brightness").Set(brightness)
	}
	if duration, ok := input["duration"]; ok {
		time.Sleep(time.Duration(int64(duration.(float64))) * time.Millisecond)
	}
	return fade.Action
}

func (fade *FadeAction) Cancel() {}

You can find an example of creating Thing above in single-thing. You can find more Examples in the Examples directory

Run example:

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

All Web Thing REST API are currently supported. The currently built Server supports lookup using Thing's title or index in Things.

# The default address is http://localhost:8888/things
curl --request GET --url http://localhost:8888/things

# Example: Get a description of a Thing
# use the Thing index.
curl --request GET --url http://localhost:8888
# Or use the Thing title.
curl --request GET --url http://localhost:8888/Lamp

Example: Properties

# Example: Get all properties
curl --request GET --url http://localhost:8888/properties
# Or
curl --request GET --url http://localhost:8888/Lamp/properties

# Example: Get a property
curl --request GET --url http://localhost:8888/properties/brightness

# Example: Set a property
curl --request PUT \
  --url http://localhost:8888/properties/brightness \
  --data '{"brightness": 33}'

Example: Actions

 # Example: Action Request
 curl --request POST \
   --url http://localhost:8888/actions \
   --data '{"fade":{"input":{"brightness":55,"duration":2000}}}'

 # Example: Cancel an Action Request
    curl --request DELETE \
      --url http://localhost:8888/actions/fade/{action_id}

 # Example: Action Request
 curl --request POST \
   --url http://localhost:8888/actions \
   --data '{"toggle":{}}'

 # Example: Actions Queue
 curl --request GET \
   --url http://localhost:8888/actions

Example: Events

# Example: Events Request
curl --request GET \
  --url http://localhost:8888/events
  
 # Example: Event Request
 curl --request GET \
   --url http://localhost:8888/events/overheated

RESOURCES

webthing-go's People

Contributors

dravenk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.