Skip to content

Commit

Permalink
Engine improvements
Browse files Browse the repository at this point in the history
Add back events tests
Fill out SMTP comms handler
Add getcommunicationrelayers gRPC command
  • Loading branch information
thrasher- committed Jun 14, 2019
1 parent 6b2cfe7 commit b901c4b
Show file tree
Hide file tree
Showing 14 changed files with 978 additions and 741 deletions.
26 changes: 26 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ func getRPCEndpoints(_ *cli.Context) error {
return nil
}

var getCommunicationRelayersCommand = cli.Command{
Name: "getcommsrelayers",
Usage: "gets GoCryptoTrader communication relayers",
Action: getCommunicationRelayers,
}

func getCommunicationRelayers(_ *cli.Context) error {
conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

client := gctrpc.NewGoCryptoTraderClient(conn)
result, err := client.GetCommunicationRelayers(context.Background(),
&gctrpc.GetCommunicationRelayersRequest{},
)

if err != nil {
return err
}

jsonOutput(result)
return nil
}

var getExchangesCommand = cli.Command{
Name: "getexchanges",
Usage: "gets a list of enabled or available exchanges",
Expand Down
1 change: 1 addition & 0 deletions cmd/gctcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func main() {
enableSubsystemCommand,
disableSubsystemCommand,
getRPCEndpointsCommand,
getCommunicationRelayersCommand,
getExchangesCommand,
enableExchangeCommand,
disableExchangeCommand,
Expand Down
6 changes: 6 additions & 0 deletions communications/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type Event struct {
Message string
}

// CommsStatus stores the status of a comms relayer
type CommsStatus struct {
Enabled bool `json:"enabled"`
Connected bool `json:"connected"`
}

// IsEnabled returns if the comms package has been enabled in the configuration
func (b *Base) IsEnabled() bool {
return b.Enabled
Expand Down
14 changes: 14 additions & 0 deletions communications/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func (c IComm) Setup() {
err := c[i].Connect()
if err != nil {
log.Errorf("Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
continue
}
log.Debugf("Communications: %v is enabled and online.", c[i].GetName())
}
}
}
Expand All @@ -48,6 +50,18 @@ func (c IComm) PushEvent(event Event) {
}
}

// GetStatus returns the status of the comms relayers
func (c IComm) GetStatus() map[string]CommsStatus {
result := make(map[string]CommsStatus)
for x := range c {
result[c[x].GetName()] = CommsStatus{
Enabled: c[x].IsEnabled(),
Connected: c[x].IsConnected(),
}
}
return result
}

// GetEnabledCommunicationMediums prints out enabled and connected communication
// packages
// (#debug output only)
Expand Down
47 changes: 24 additions & 23 deletions communications/smtpservice/smtpservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net/smtp"
"strings"

"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/communications/base"
"github.com/thrasher-/gocryptotrader/config"
log "github.com/thrasher-/gocryptotrader/logger"
)

const (
Expand All @@ -23,6 +23,7 @@ type SMTPservice struct {
Port string
AccountName string
AccountPassword string
From string
RecipientList string
}

Expand All @@ -36,7 +37,9 @@ func (s *SMTPservice) Setup(cfg *config.CommunicationsConfig) {
s.Port = cfg.SMTPConfig.Port
s.AccountName = cfg.SMTPConfig.AccountName
s.AccountPassword = cfg.SMTPConfig.AccountPassword
s.From = cfg.SMTPConfig.From
s.RecipientList = cfg.SMTPConfig.RecipientList
log.Debugf("SMTP: Setup - From: %v. To: %s. Server: %s.", s.From, s.RecipientList, s.Host)
}

// IsConnected returns whether or not the connection is connected
Expand All @@ -51,36 +54,34 @@ func (s *SMTPservice) Connect() error {
}

// PushEvent sends an event to supplied recipient list via SMTP
func (s *SMTPservice) PushEvent(base.Event) error {
return common.ErrNotYetImplemented
func (s *SMTPservice) PushEvent(e base.Event) error {
return s.Send(e.Type, e.Message)
}

// Send sends an email template to the recipient list via your SMTP host when
// an internal event is triggered by GoCryptoTrader
func (s *SMTPservice) Send(subject, alert string) error {
if subject == "" || alert == "" {
func (s *SMTPservice) Send(subject, msg string) error {
if subject == "" || msg == "" {
return errors.New("STMPservice Send() please add subject and alert")
}

list := strings.Split(s.RecipientList, ",")
log.Debugf("SMTP: Sending email to %v. Subject: %s Message: %s [From: %s]", s.RecipientList,
subject, msg, s.From)
messageToSend := fmt.Sprintf(
msgSMTP,
s.RecipientList,
subject,
mime,
msg)

for i := range list {
messageToSend := fmt.Sprintf(
msgSMTP,
list[i],
subject,
mime,
alert)

err := smtp.SendMail(
s.Host+":"+s.Port,
smtp.PlainAuth("", s.AccountName, s.AccountPassword, s.Host),
s.AccountName,
[]string{list[i]},
[]byte(messageToSend))
if err != nil {
return err
}
err := smtp.SendMail(
s.Host+":"+s.Port,
smtp.PlainAuth("", s.AccountName, s.AccountPassword, s.Host),
s.From,
strings.Split(s.RecipientList, ","),
[]byte(messageToSend))
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type SMTPConfig struct {
Port string `json:"port"`
AccountName string `json:"accountName"`
AccountPassword string `json:"accountPassword"`
From string `json:"from"`
RecipientList string `json:"recipientList"`
}

Expand Down
7 changes: 7 additions & 0 deletions engine/comms_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func (c *commsManager) Start() (err error) {
return nil
}

func (c *commsManager) GetStatus() (map[string]base.CommsStatus, error) {
if !c.Started() {
return nil, errors.New("communications manager not started")
}
return c.comms.GetStatus(), nil
}

func (c *commsManager) Stop() error {
if atomic.LoadInt32(&c.started) == 0 {
return errors.New("communications manager not started")
Expand Down
31 changes: 12 additions & 19 deletions engine/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ func (e *Event) ExecuteAction() bool {
// String turns the structure event into a string
func (e *Event) String() string {
return fmt.Sprintf(
"If the %s%s [%s] %s on %s meets the following %v then %s.", e.Pair.Base.String(),
e.Pair.Quote.String(), e.Asset, e.Item, e.Exchange, e.Condition, e.Action,
"If the %s [%s] %s on %s meets the following %v then %s.", e.Pair.String(),
strings.ToUpper(e.Asset.String()), e.Item, e.Exchange, e.Condition, e.Action,
)
}

func (e *Event) processTicker() bool {
targetPrice := e.Condition.Price

t, err := ticker.GetTicker(e.Exchange, e.Pair, e.Asset)
if err != nil {
if Bot.Settings.Verbose {
Expand All @@ -159,16 +157,13 @@ func (e *Event) processTicker() bool {
return false
}

lastPrice := t.Last

if lastPrice == 0 {
if t.Last == 0 {
if Bot.Settings.Verbose {
log.Debugln("Events: ticker last price is 0")
}
return false
}

return e.processCondition(lastPrice, targetPrice)
return e.processCondition(t.Last, e.Condition.Price)
}

func (e *Event) processCondition(actual, threshold float64) bool {
Expand Down Expand Up @@ -259,14 +254,14 @@ func IsValidEvent(exchange, item string, condition EventConditionParams, action
}

if item == ItemPrice {
if condition.Price == 0 {
if condition.Price <= 0 {
return errInvalidCondition
}
}

if item == ItemOrderbook {
if condition.OrderbookAmount == 0 {
return errInvalidAction
if condition.OrderbookAmount <= 0 {
return errInvalidCondition
}
}

Expand All @@ -276,10 +271,6 @@ func IsValidEvent(exchange, item string, condition EventConditionParams, action
if a[0] != ActionSMSNotify {
return errInvalidAction
}

if a[1] != "ALL" {
Bot.CommsManager.PushEvent(base.Event{Type: a[1]})
}
} else if action != ActionConsolePrint && action != ActionTest {
return errInvalidAction
}
Expand All @@ -302,10 +293,12 @@ func EventManger() {
}
success := event.CheckEventCondition()
if success {
log.Debugf(
"Events: ID: %d triggered on %s successfully.\n", event.ID,
event.Exchange,
msg := fmt.Sprintf(
"Events: ID: %d triggered on %s successfully [%v]\n", event.ID,
event.Exchange, event.String(),
)
log.Info(msg)
Bot.CommsManager.PushEvent(base.Event{Type: "event", Message: msg})
event.Executed = true
}
}
Expand Down
Loading

0 comments on commit b901c4b

Please sign in to comment.