sampler-fork/data/trigger.go

136 lines
3.0 KiB
Go
Raw Normal View History

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
valuesByLabel map[string]Values
2019-03-21 02:23:08 +00:00
options config.Options
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-04-07 15:09:24 +00:00
triggers := make([]*Trigger, 0)
for _, cfg := range cfgs {
2019-03-21 02:23:08 +00:00
triggers = append(triggers, NewTrigger(cfg, consumer, options, player))
}
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{
title: config.Title,
condition: config.Condition,
consumer: consumer,
valuesByLabel: make(map[string]Values),
2019-03-21 02:23:08 +00:00
options: options,
player: player,
digitsRegexp: regexp.MustCompile("[^0-9]+"),
2019-03-16 23:59:28 +00:00
actions: &Actions{
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) {
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{
Title: t.title,
Text: fmt.Sprintf("%s: %v", sample.Label, sample.Value),
Color: sample.Color,
Recoverable: false,
2019-03-11 03:43:47 +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-16 23:59:28 +00:00
func (t *Trigger) evaluate(sample *Sample) bool {
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])
if err != nil {
2019-03-17 03:13:14 +00:00
t.consumer.AlertChannel <- &Alert{
Title: "Trigger condition failure",
Text: getErrorMessage(err),
Color: sample.Color,
Recoverable: true,
2019-03-17 03:13:14 +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) {
cmd := exec.Command("sh", "-c", script)
cmd.Env = os.Environ()
2019-03-21 02:23:08 +00:00
2019-05-24 02:58:46 +00:00
for _, variable := range t.options.Environment {
2019-03-21 02:23:08 +00:00
cmd.Env = append(cmd.Env, variable)
}
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()
}