diff --git a/activity/gpio-output/activity.go b/activity/gpio-output/activity.go new file mode 100644 index 00000000..69714b0f --- /dev/null +++ b/activity/gpio-output/activity.go @@ -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 +} diff --git a/activity/gpio-output/activity_test.go b/activity/gpio-output/activity_test.go new file mode 100644 index 00000000..0d6c02dd --- /dev/null +++ b/activity/gpio-output/activity_test.go @@ -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) +} diff --git a/activity/gpio-output/descriptor.json b/activity/gpio-output/descriptor.json new file mode 100644 index 00000000..dc486a0f --- /dev/null +++ b/activity/gpio-output/descriptor.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/activity/gpio-output/go.mod b/activity/gpio-output/go.mod new file mode 100644 index 00000000..dd7b6f5d --- /dev/null +++ b/activity/gpio-output/go.mod @@ -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 diff --git a/activity/gpio-output/go.sum b/activity/gpio-output/go.sum new file mode 100644 index 00000000..211d7b1e --- /dev/null +++ b/activity/gpio-output/go.sum @@ -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= diff --git a/activity/sleep/activity.go b/activity/sleep/activity.go new file mode 100644 index 00000000..acbaf473 --- /dev/null +++ b/activity/sleep/activity.go @@ -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 +} diff --git a/activity/sleep/activity_test.go b/activity/sleep/activity_test.go new file mode 100644 index 00000000..aeb64fc5 --- /dev/null +++ b/activity/sleep/activity_test.go @@ -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) +} diff --git a/activity/sleep/descriptor.json b/activity/sleep/descriptor.json new file mode 100644 index 00000000..6bc444e5 --- /dev/null +++ b/activity/sleep/descriptor.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/activity/sleep/go.mod b/activity/sleep/go.mod new file mode 100644 index 00000000..50af2565 --- /dev/null +++ b/activity/sleep/go.mod @@ -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 diff --git a/activity/sleep/go.sum b/activity/sleep/go.sum new file mode 100644 index 00000000..b17aee7d --- /dev/null +++ b/activity/sleep/go.sum @@ -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=