2019-03-10 04:41:23 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/sqshq/sampler/asset"
|
|
|
|
"github.com/sqshq/sampler/config"
|
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TrueIndicator = "1"
|
|
|
|
InitialValue = "0"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Trigger struct {
|
|
|
|
title string
|
|
|
|
condition string
|
2019-03-16 23:59:28 +00:00
|
|
|
actions *Actions
|
|
|
|
consumer *Consumer
|
2019-03-10 04:41:23 +00:00
|
|
|
valuesByLabel map[string]Values
|
2019-03-21 02:23:08 +00:00
|
|
|
options config.Options
|
2019-03-10 04:41:23 +00:00
|
|
|
player *asset.AudioPlayer
|
|
|
|
digitsRegexp *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
type Actions struct {
|
|
|
|
terminalBell bool
|
|
|
|
sound bool
|
|
|
|
visual bool
|
|
|
|
script *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Values struct {
|
|
|
|
current string
|
|
|
|
previous string
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:09:24 +00:00
|
|
|
func NewTriggers(cfgs []config.TriggerConfig, consumer *Consumer, options config.Options, player *asset.AudioPlayer) []*Trigger {
|
2019-03-10 04:41:23 +00:00
|
|
|
|
2019-04-07 15:09:24 +00:00
|
|
|
triggers := make([]*Trigger, 0)
|
2019-03-10 04:41:23 +00:00
|
|
|
|
|
|
|
for _, cfg := range cfgs {
|
2019-03-21 02:23:08 +00:00
|
|
|
triggers = append(triggers, NewTrigger(cfg, consumer, options, player))
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return triggers
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:09:24 +00:00
|
|
|
func NewTrigger(config config.TriggerConfig, consumer *Consumer, options config.Options, player *asset.AudioPlayer) *Trigger {
|
|
|
|
return &Trigger{
|
2019-03-10 04:41:23 +00:00
|
|
|
title: config.Title,
|
|
|
|
condition: config.Condition,
|
|
|
|
consumer: consumer,
|
|
|
|
valuesByLabel: make(map[string]Values),
|
2019-03-21 02:23:08 +00:00
|
|
|
options: options,
|
2019-03-10 04:41:23 +00:00
|
|
|
player: player,
|
|
|
|
digitsRegexp: regexp.MustCompile("[^0-9]+"),
|
2019-03-16 23:59:28 +00:00
|
|
|
actions: &Actions{
|
2019-03-10 04:41:23 +00:00
|
|
|
terminalBell: *config.Actions.TerminalBell,
|
|
|
|
sound: *config.Actions.Sound,
|
|
|
|
visual: *config.Actions.Visual,
|
|
|
|
script: config.Actions.Script,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 23:59:28 +00:00
|
|
|
func (t *Trigger) Execute(sample *Sample) {
|
2019-03-10 04:41:23 +00:00
|
|
|
if t.evaluate(sample) {
|
|
|
|
|
|
|
|
if t.actions.terminalBell {
|
|
|
|
fmt.Print(console.BellCharacter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.actions.sound {
|
|
|
|
t.player.Beep()
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.actions.visual {
|
2019-03-16 23:59:28 +00:00
|
|
|
t.consumer.AlertChannel <- &Alert{
|
2019-03-13 03:15:55 +00:00
|
|
|
Title: t.title,
|
|
|
|
Text: fmt.Sprintf("%s: %v", sample.Label, sample.Value),
|
|
|
|
Color: sample.Color,
|
2019-03-11 03:43:47 +00:00
|
|
|
}
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.actions.script != nil {
|
2019-03-21 02:23:08 +00:00
|
|
|
_, _ = t.runScript(*t.actions.script, sample.Label, t.valuesByLabel[sample.Label])
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 23:59:28 +00:00
|
|
|
func (t *Trigger) evaluate(sample *Sample) bool {
|
2019-03-10 04:41:23 +00:00
|
|
|
|
|
|
|
if values, ok := t.valuesByLabel[sample.Label]; ok {
|
|
|
|
values.previous = values.current
|
|
|
|
values.current = sample.Value
|
|
|
|
t.valuesByLabel[sample.Label] = values
|
|
|
|
} else {
|
|
|
|
t.valuesByLabel[sample.Label] = Values{previous: InitialValue, current: sample.Value}
|
|
|
|
}
|
|
|
|
|
2019-03-21 02:23:08 +00:00
|
|
|
output, err := t.runScript(t.condition, sample.Label, t.valuesByLabel[sample.Label])
|
2019-03-10 04:41:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-03-17 03:13:14 +00:00
|
|
|
t.consumer.AlertChannel <- &Alert{
|
|
|
|
Title: "TRIGGER CONDITION FAILURE",
|
2019-03-22 03:21:00 +00:00
|
|
|
Text: getErrorMessage(err),
|
2019-04-07 17:56:20 +00:00
|
|
|
Color: sample.Color,
|
2019-03-17 03:13:14 +00:00
|
|
|
}
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t.digitsRegexp.ReplaceAllString(string(output), "") == TrueIndicator
|
|
|
|
}
|
|
|
|
|
2019-03-21 02:23:08 +00:00
|
|
|
func (t *Trigger) runScript(script, label string, data Values) ([]byte, error) {
|
2019-03-10 04:41:23 +00:00
|
|
|
|
|
|
|
cmd := exec.Command("sh", "-c", script)
|
|
|
|
cmd.Env = os.Environ()
|
2019-03-21 02:23:08 +00:00
|
|
|
|
|
|
|
for _, variable := range t.options.Variables {
|
|
|
|
cmd.Env = append(cmd.Env, variable)
|
|
|
|
}
|
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
cmd.Env = append(cmd.Env,
|
|
|
|
fmt.Sprintf("prev=%v", data.previous),
|
|
|
|
fmt.Sprintf("cur=%v", data.current),
|
|
|
|
fmt.Sprintf("label=%v", label))
|
|
|
|
|
|
|
|
return cmd.Output()
|
|
|
|
}
|