Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Flogo Sleep Activity and GPIO Activity #155

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions activity/gpio-output/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package gpio_output

import (
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/support/log"
"github.com/stianeikeland/go-rpio/v4"
)

const (
actionTurnOn = "TurnOn"
actionTurnOff = "TurnOff"
actionToggle = "Toggle"
)

const (
jsonGpioPin = "gpioPin"
jsonAction = "action"
)

type Input struct {
GpioPin int `md:"GPIOPin, required"`
Action string `md:"Action, required, allowed(TurnOn, TurnOff, Toggle)"`
}

type Activity struct {
}

func init() {
_ = activity.Register(&Activity{})
}

func (i *Input) ToMap() map[string]interface{} {
return map[string]interface{}{
jsonGpioPin: i.GpioPin,
jsonAction: i.Action,
}
}

func (i *Input) FromMap(values map[string]interface{}) error {
var err error
i.GpioPin, err = coerce.ToInt(values[jsonGpioPin])
if err != nil {
return err
}

i.Action, err = coerce.ToString(values[jsonAction])

return err
}

func (a *Activity) Metadata() *activity.Metadata {
return activity.ToMetadata(&Input{})
}

func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {

inputParams := &Input{}
ctx.GetInputObject(inputParams)

pinNumber := inputParams.GpioPin
pin := rpio.Pin(pinNumber)
action := inputParams.Action

log.RootLogger().Debug("Action %s, pin %d", action, pinNumber)

openError := rpio.Open()
if openError != nil {
log.RootLogger().Error("Failed to open pin")
return false, openError
}
pin.Output()

if action == actionTurnOn {
pin.Write(rpio.High)
} else if action == actionTurnOff {
pin.Write(rpio.Low)
} else if action == actionToggle {
pin.Toggle()
} else {
log.RootLogger().Error("Unknown action %s", action)
return false, nil
}

return true, nil
}
37 changes: 37 additions & 0 deletions activity/gpio-output/activity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gpio_output

import (
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/support/test"
"github.com/stretchr/testify/assert"
"testing"
)

func TestRegister(t *testing.T) {

ref := activity.GetRef(&Activity{})
act := activity.Get(ref)

assert.NotNil(t, act)
}

func TestEval(t *testing.T) {

act := &Activity{}
tc := test.NewActivityContext(act.Metadata())

input := &Input{Action: actionTurnOn, GpioPin: 10}
tc.SetInputObject(input)

act.Eval(tc)
}

func TestAddToFlow(t *testing.T) {

act := &Activity{}
tc := test.NewActivityContext(act.Metadata())
//setup attrs
tc.SetInput("action", actionTurnOn)
tc.SetInput("gpioPin", 10)
act.Eval(tc)
}
22 changes: 22 additions & 0 deletions activity/gpio-output/descriptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "flogo-gpio-output",
"type": "flogo:activity",
"version": "0.0.1",
"title": "GPIO Output",
"description": "Turn on, turn off or toggle GPIO pin",
"input": [
{
"name": "action",
"type": "string",
"value": "TurnOn",
"allowed" : ["TurnOn", "TurnOff", "Toggle"],
"description": "Action"
},
{
"name": "gpioPin",
"type": "int",
"value": "10",
"description": "Pin number"
}
]
}
10 changes: 10 additions & 0 deletions activity/gpio-output/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/maksim-khiuttiulia/contrib/activity/gpio-output

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/project-flogo/core v0.9.4-hf.1
github.com/stianeikeland/go-rpio/v4 v4.6.0
github.com/stretchr/testify v1.4.0 // indirect
)

go 1.12
25 changes: 25 additions & 0 deletions activity/gpio-output/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-flogo/core v0.9.4-hf.1 h1:qjwXVpTL38yAgMFDDhb+SdOzye0qGoR2jOhkZiWgLh0=
github.com/project-flogo/core v0.9.4-hf.1/go.mod h1:QGWi7TDLlhGUaYH3n/16ImCuulbEHGADYEXyrcHhX7U=
github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY=
github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
50 changes: 50 additions & 0 deletions activity/sleep/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sleep

import (
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/coerce"

"time"
)

type Input struct {
SleepTime int `md:"SleepTime,required"`
}

type Activity struct {
}

func init() {
_ = activity.Register(&Activity{})
}

func (i *Input) ToMap() map[string]interface{} {
return map[string]interface{}{
"sleepTime": i.SleepTime,
}
}

func (i *Input) FromMap(values map[string]interface{}) error {
var err error
i.SleepTime, err = coerce.ToInt(values["sleepTime"])
if err != nil {
return err
}

return nil
}

func (a *Activity) Metadata() *activity.Metadata {
return activity.ToMetadata(&Input{})
}

func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {

input := &Input{}
ctx.GetInputObject(input)

interval := input.SleepTime
time.Sleep(time.Duration(interval) * time.Second)

return true, nil
}
37 changes: 37 additions & 0 deletions activity/sleep/activity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sleep

import (
"github.com/project-flogo/core/support/test"
"testing"

"github.com/project-flogo/core/activity"
"github.com/stretchr/testify/assert"
)

func TestRegister(t *testing.T) {

ref := activity.GetRef(&Activity{})
act := activity.Get(ref)

assert.NotNil(t, act)
}

func TestEval(t *testing.T) {

act := &Activity{}
tc := test.NewActivityContext(act.Metadata())

input := &Input{SleepTime: 1}
tc.SetInputObject(input)

act.Eval(tc)
}

func TestAddToFlow(t *testing.T) {

act := &Activity{}
tc := test.NewActivityContext(act.Metadata())
//setup attrs
tc.SetInput("sleepTime", 1)
act.Eval(tc)
}
16 changes: 16 additions & 0 deletions activity/sleep/descriptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "flogo-sleep",
"type": "flogo:activity",
"version": "0.0.1",
"title": "Sleep",
"description": "Sleep some period",
"homepage": "https://github.com/maksim-khiuttiulia/flogo-sleep-activity",
"input": [
{
"name": "sleepTime",
"type": "int",
"value": "0",
"description": "Sleep time in seconds"
}
]
}
9 changes: 9 additions & 0 deletions activity/sleep/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/maksim-khiuttiulia/contrib/activity/sleep

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/project-flogo/core v0.9.4-hf.1
github.com/stretchr/testify v1.4.0
)

go 1.12
24 changes: 24 additions & 0 deletions activity/sleep/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-flogo/core v0.9.4-hf.1 h1:qjwXVpTL38yAgMFDDhb+SdOzye0qGoR2jOhkZiWgLh0=
github.com/project-flogo/core v0.9.4-hf.1/go.mod h1:QGWi7TDLlhGUaYH3n/16ImCuulbEHGADYEXyrcHhX7U=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=