-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutationHandler.go
125 lines (114 loc) · 2.9 KB
/
mutationHandler.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
package main
import (
"fmt"
"github.com/Hundemeier/key2sACN/keylogger"
"github.com/graphql-go/graphql"
)
func mutateSacnOutput(p graphql.ResolveParams) (sacn interface{}, err error) {
// marshall and cast the argument value
univInt, _ := p.Args["universe"].(int)
univ, err := checkUniverse(univInt)
if err != nil {
return
}
multicast, _ := p.Args["multicast"].(bool)
destinations := make([]string, 0) //convert []interface{}
if p.Args["destinations"] != nil {
for _, dest := range p.Args["destinations"].([]interface{}) {
destinations = append(destinations, dest.(string))
}
}
if !trans.IsActivated(univ) {
err = activateUniverse(univ)
if err != nil {
return
}
}
trans.SetMulticast(univ, multicast)
errs := trans.SetDestinations(univ, destinations) //only get the first error
if len(errs) != 0 {
err = errs[0]
}
return sACNtype{
Universe: univ,
Multicast: trans.IsMulticast(univ),
Destinations: getDestinations(univ),
}, err
}
func mutateStopSacn(p graphql.ResolveParams) (interface{}, error) {
univInt, _ := p.Args["universe"].(int)
univ, err := checkUniverse(univInt)
if err != nil {
return false, err
}
if _, ok := universeChanMap[univ]; ok {
close(universeChanMap[univ])
universeChanMap[univ] = nil
return true, nil
}
return false, nil
}
func mutateKeyMap(p graphql.ResolveParams) (interface{}, error) {
univInt, _ := p.Args["universe"].(int)
univ, err := checkUniverse(univInt)
if err != nil {
return nil, err
}
chanInt, _ := p.Args["channel"].(int)
channel, err := checkChannel(chanInt)
if err != nil {
return nil, err
}
keyCodeInt, _ := p.Args["keycode"].(int)
keyCode := uint16(keyCodeInt)
keyboardID, _ := p.Args["keyboardID"].(int)
return setMapping(univ, channel, keyCode, keyboardID), nil
}
func mutateKeyListener(p graphql.ResolveParams) (interface{}, error) {
listen := p.Args["listen"].(bool)
deviceID := p.Args["deviceID"].(int)
if listen {
//get the device with the id
devs, err := keylogger.NewDevices()
if err != nil {
return nil, err
}
var dev *keylogger.InputDevice
for _, val := range devs {
if val.Id == deviceID {
dev = val
}
}
if dev == nil {
return nil, fmt.Errorf("could not find device with id %v", deviceID)
}
startKeylogger(dev, keyChan)
return deviceType{
Listening: true,
Id: dev.Id,
Name: dev.Name,
}, nil
}
stopKeylogger(deviceID)
return nil, nil
}
func mutateWriteConfig(p graphql.ResolveParams) (interface{}, error) {
err := writeConfig()
if err == nil {
return true, nil
}
return false, err
}
func mutateDeleteConfig(p graphql.ResolveParams) (interface{}, error) {
err := deleteConfig()
if err == nil {
return true, nil
}
return false, err
}
func mutateDeleteKeyMap(p graphql.ResolveParams) (interface{}, error) {
keycode := p.Args["keycode"].(int)
keyboardID := p.Args["keyboardID"].(int)
ok := deleteMapping(uint16(keycode), keyboardID)
return ok, nil
}