/
commands.go
71 lines (56 loc) · 1.83 KB
/
commands.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
package commands
import (
"time"
)
var CommandsAvailable map[int]DisplayCommand
type JsonObject map[string]interface{}
type DisplayCommand struct {
ExecutableCommand Command
ID int
Name, Description, Type string
IsBasic bool
IsToggle bool
AllowedUsers []string
}
type Command interface {
Execute(parameter interface{}) string
}
func ParseCommands(config JsonObject) {
CommandsAvailable = make(map[int]DisplayCommand)
id := 0
for _, cmd := range config["commands"].([]interface{}) {
c := cmd.(map[string]interface {})
command := DisplayCommand {
Name: c["name"].(string),
Description: c["description"].(string),
Type: c["type"].(string) }
data := c["data"].(map[string]interface {})
switch command.Type {
case BasicCommandTypeString:
command.ExecutableCommand = CreateBasicCommand(data)
command.IsBasic = true
case ToggleCommandTypeString:
command.ExecutableCommand = CreateToggleCommand(data)
command.IsToggle = true
}
command.AllowedUsers = make([]string, 0)
for _, username := range c["users"].([]interface{}) {
command.AllowedUsers = append(command.AllowedUsers, username.(string))
}
if periodic, ok := c["periodic_exec"]; ok {
RegisterCommandForPeriodicExecution(command, int(periodic.(float64)))
}
command.ID = id
id++
CommandsAvailable[command.ID] = command
}
}
func RegisterCommandForPeriodicExecution(cmd DisplayCommand, secs int) {
ticker := time.NewTicker(time.Duration(secs) * time.Second)
go func() {
for {
<-ticker.C
cmd.ExecutableCommand.Execute("on")
}
}()
}