/
activity.go
executable file
·140 lines (117 loc) · 3.15 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gpio
import (
"errors"
"fmt"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/stianeikeland/go-rpio"
)
// log is the default package logger
var log = logger.GetLogger("activity-tibco-rest")
const (
method = "method"
pinNumber = "pinNumber"
directionState = "direction"
state = "state"
direction = "Direction"
setState = "Set State"
readState = "Read State"
pull = "Pull"
input = "Input"
//output = "Output"
high = "High"
//low = "Low"
up = "Up"
down = "Down"
//off = "off"
//ouput
result = "result"
)
type GPIOActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new GPIOActivity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &GPIOActivity{metadata: metadata}
}
// Metadata returns the activity's metadata
func (a *GPIOActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *GPIOActivity) Eval(context activity.Context) (done bool, err error) {
//getmethod
log.Debug("Running gpio activity.")
methodInput := context.GetInput(method)
ivmethod, ok := methodInput.(string)
if !ok {
return true, errors.New("Method field not set.")
}
//get pinNumber
ivPinNumber, ok := context.GetInput(pinNumber).(int)
if !ok {
return true, errors.New("Pin number must exist")
}
log.Debugf("Method '%s' and pin number '%d'", methodInput, ivPinNumber)
//Open pin
openErr := rpio.Open()
defer rpio.Close()
if openErr != nil {
log.Errorf("Open RPIO error: %+v", openErr.Error())
return true, errors.New("Open RPIO error: " + openErr.Error())
}
pin := rpio.Pin(ivPinNumber)
switch ivmethod {
case direction:
ivDirectionField, ok := context.GetInput(directionState).(string)
if !ok {
return true, errors.New("Direction field not set.")
}
if strings.EqualFold(input, ivDirectionField) {
log.Debugf("Set pin %d direction to input", pin)
pin.Input()
} else {
log.Debugf("Set pin %d direction to output", pin)
pin.Output()
}
case setState:
ivState, ok := context.GetInput(state).(string)
if !ok {
return true, errors.New("State field not set.")
}
if strings.EqualFold(high, ivState) {
log.Debugf("Set pin %d state to High", pin)
pin.High()
} else {
log.Debugf("Set pin %d state to low", pin)
pin.Low()
}
case readState:
log.Debugf("Read pin %d state..", pin)
state := pin.Read()
log.Debugf("Read state and state: %s", state)
context.SetOutput(result, int(state))
return true, nil
case pull:
ivPull, ok := context.GetInput(pull).(string)
if !ok {
return true, errors.New("Pull field not set.")
}
if strings.EqualFold(up, ivPull) {
log.Debugf("Pull pin %d to Up", pin)
pin.PullUp()
} else if strings.EqualFold(down, ivPull) {
log.Debugf("Pull pin %d to Down", pin)
pin.PullDown()
} else {
log.Debugf("Pull pin %d to Up", pin)
pin.PullOff()
}
default:
log.Errorf("Cannot find method %s ", ivmethod)
return true, errors.New(fmt.Sprintf("Cannot find method %s", ivmethod))
}
context.SetOutput(result, 0)
return true, nil
}