2019-01-28 23:09:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-31 04:36:00 +00:00
|
|
|
"fmt"
|
2019-03-14 03:01:44 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-03-10 04:41:23 +00:00
|
|
|
"github.com/sqshq/sampler/asset"
|
2019-05-24 02:58:46 +00:00
|
|
|
"github.com/sqshq/sampler/client"
|
2019-02-24 04:49:09 +00:00
|
|
|
"github.com/sqshq/sampler/component"
|
|
|
|
"github.com/sqshq/sampler/component/asciibox"
|
|
|
|
"github.com/sqshq/sampler/component/barchart"
|
2019-02-26 04:36:23 +00:00
|
|
|
"github.com/sqshq/sampler/component/gauge"
|
2019-03-08 04:04:46 +00:00
|
|
|
"github.com/sqshq/sampler/component/layout"
|
2019-02-24 04:49:09 +00:00
|
|
|
"github.com/sqshq/sampler/component/runchart"
|
2019-03-24 22:46:39 +00:00
|
|
|
"github.com/sqshq/sampler/component/sparkline"
|
2019-04-10 02:30:21 +00:00
|
|
|
"github.com/sqshq/sampler/component/textbox"
|
2019-02-02 14:45:53 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"github.com/sqshq/sampler/data"
|
|
|
|
"github.com/sqshq/sampler/event"
|
2019-05-24 02:58:46 +00:00
|
|
|
"github.com/sqshq/sampler/metadata"
|
2019-05-31 04:36:00 +00:00
|
|
|
"runtime/debug"
|
2019-05-14 03:24:29 +00:00
|
|
|
"time"
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
|
|
|
|
2019-03-17 23:55:24 +00:00
|
|
|
type Starter struct {
|
2019-05-28 01:44:06 +00:00
|
|
|
player *asset.AudioPlayer
|
|
|
|
lout *layout.Layout
|
|
|
|
palette console.Palette
|
|
|
|
opt config.Options
|
|
|
|
cfg config.Config
|
|
|
|
}
|
|
|
|
|
2019-06-07 05:39:11 +00:00
|
|
|
func (s *Starter) startAll() []*data.Sampler {
|
|
|
|
samplers := make([]*data.Sampler, 0)
|
2019-05-28 01:44:06 +00:00
|
|
|
for _, c := range s.cfg.RunCharts {
|
|
|
|
cpt := runchart.NewRunChart(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
|
|
|
for _, c := range s.cfg.SparkLines {
|
|
|
|
cpt := sparkline.NewSparkLine(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
|
|
|
for _, c := range s.cfg.BarCharts {
|
|
|
|
cpt := barchart.NewBarChart(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
|
|
|
for _, c := range s.cfg.Gauges {
|
|
|
|
cpt := gauge.NewGauge(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
|
|
|
for _, c := range s.cfg.AsciiBoxes {
|
|
|
|
cpt := asciibox.NewAsciiBox(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
|
|
|
for _, c := range s.cfg.TextBoxes {
|
|
|
|
cpt := textbox.NewTextBox(c, s.palette)
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
|
2019-05-28 01:44:06 +00:00
|
|
|
}
|
2019-06-07 05:39:11 +00:00
|
|
|
return samplers
|
2019-03-17 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 05:39:11 +00:00
|
|
|
func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) *data.Sampler {
|
2019-04-07 15:09:24 +00:00
|
|
|
cpt := component.NewComponent(drawable, consumer, componentConfig)
|
2019-03-21 02:23:08 +00:00
|
|
|
triggers := data.NewTriggers(triggersConfig, consumer, s.opt, s.player)
|
2019-04-09 02:04:08 +00:00
|
|
|
items := data.NewItems(itemsConfig, *componentConfig.RateMs)
|
2019-03-17 23:55:24 +00:00
|
|
|
s.lout.AddComponent(cpt)
|
2019-05-20 04:12:40 +00:00
|
|
|
time.Sleep(10 * time.Millisecond) // desync coroutines
|
2019-06-07 05:39:11 +00:00
|
|
|
return data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs)
|
2019-03-17 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:09:52 +00:00
|
|
|
func main() {
|
|
|
|
|
2019-05-20 04:12:40 +00:00
|
|
|
cfg, opt := config.LoadConfig()
|
2019-05-28 01:44:06 +00:00
|
|
|
bc := client.NewBackendClient()
|
|
|
|
|
|
|
|
statistics := metadata.GetStatistics(cfg)
|
|
|
|
license := metadata.GetLicense()
|
|
|
|
|
2019-05-31 04:36:00 +00:00
|
|
|
defer handleCrash(statistics, opt, bc)
|
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
if opt.LicenseKey != nil {
|
|
|
|
registerLicense(statistics, opt, bc)
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-03-16 04:35:00 +00:00
|
|
|
console.Init()
|
|
|
|
defer console.Close()
|
2019-01-30 00:21:57 +00:00
|
|
|
|
2019-03-10 04:41:23 +00:00
|
|
|
player := asset.NewAudioPlayer()
|
|
|
|
defer player.Close()
|
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
palette := console.GetPalette(*cfg.Theme)
|
2019-06-09 04:16:56 +00:00
|
|
|
lout := layout.NewLayout(component.NewStatusLine(*opt.ConfigFile, palette, license),
|
|
|
|
component.NewMenu(palette), component.NewIntro(palette), component.NewNagWindow(palette))
|
2019-03-17 23:55:24 +00:00
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
if statistics.LaunchCount == 0 {
|
|
|
|
if !opt.DisableTelemetry {
|
|
|
|
go bc.ReportInstallation(statistics)
|
|
|
|
}
|
2019-06-09 04:16:56 +00:00
|
|
|
lout.StartWithIntro()
|
|
|
|
} else if statistics.LaunchCount%20 == 0 { // once in a while
|
|
|
|
if license == nil || !license.Valid {
|
|
|
|
lout.StartWithNagWindow()
|
|
|
|
} else {
|
|
|
|
go verifyLicense(license, bc)
|
|
|
|
}
|
|
|
|
if !opt.DisableTelemetry {
|
|
|
|
go bc.ReportStatistics(statistics)
|
|
|
|
}
|
2019-05-21 03:18:23 +00:00
|
|
|
}
|
2019-05-20 04:12:40 +00:00
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
metadata.PersistStatistics(cfg)
|
|
|
|
starter := &Starter{player, lout, palette, opt, *cfg}
|
2019-06-07 05:39:11 +00:00
|
|
|
samplers := starter.startAll()
|
2019-05-24 02:58:46 +00:00
|
|
|
|
2019-06-07 05:39:11 +00:00
|
|
|
handler := event.NewHandler(samplers, opt, lout)
|
2019-05-24 02:58:46 +00:00
|
|
|
handler.HandleEvents()
|
|
|
|
}
|
|
|
|
|
2019-05-31 04:36:00 +00:00
|
|
|
func handleCrash(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) {
|
|
|
|
if rec := recover(); rec != nil {
|
|
|
|
err := rec.(error)
|
|
|
|
if !opt.DisableTelemetry {
|
|
|
|
bc.ReportCrash(fmt.Sprintf("%s\n%s", err.Error(), string(debug.Stack())), statistics)
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:06 +00:00
|
|
|
func registerLicense(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) {
|
|
|
|
lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics)
|
|
|
|
if err != nil {
|
|
|
|
console.Exit("License registration failed: " + err.Error())
|
|
|
|
} else {
|
|
|
|
metadata.SaveLicense(*lc)
|
|
|
|
console.Exit("License successfully verified, Sampler can be restarted without --license flag now. Thank you.")
|
2019-04-10 02:30:21 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
2019-06-09 04:16:56 +00:00
|
|
|
|
|
|
|
func verifyLicense(license *metadata.License, bc *client.BackendClient) {
|
|
|
|
verifiedLicense, _ := bc.VerifyLicenseKey(*license.Key)
|
|
|
|
if verifiedLicense != nil {
|
|
|
|
metadata.SaveLicense(*verifiedLicense)
|
|
|
|
}
|
|
|
|
}
|