|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package notify |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/blinklabs-io/snek/event" |
| 22 | + "github.com/blinklabs-io/snek/input/chainsync" |
| 23 | + "github.com/gen2brain/beeep" |
| 24 | +) |
| 25 | + |
| 26 | +type NotifyOutput struct { |
| 27 | + errorChan chan error |
| 28 | + eventChan chan event.Event |
| 29 | +} |
| 30 | + |
| 31 | +func New(options ...NotifyOptionFunc) *NotifyOutput { |
| 32 | + n := &NotifyOutput{ |
| 33 | + errorChan: make(chan error), |
| 34 | + eventChan: make(chan event.Event, 10), |
| 35 | + } |
| 36 | + for _, option := range options { |
| 37 | + option(n) |
| 38 | + } |
| 39 | + return n |
| 40 | +} |
| 41 | + |
| 42 | +// Start the notify output |
| 43 | +func (n *NotifyOutput) Start() error { |
| 44 | + go func() { |
| 45 | + for { |
| 46 | + evt, ok := <-n.eventChan |
| 47 | + // Channel has been closed, which means we're shutting down |
| 48 | + if !ok { |
| 49 | + return |
| 50 | + } |
| 51 | + switch evt.Type { |
| 52 | + case "chainsync.block": |
| 53 | + payload := evt.Payload |
| 54 | + if payload == nil { |
| 55 | + panic(fmt.Errorf("ERROR: %v", payload)) |
| 56 | + } |
| 57 | + |
| 58 | + data, err := json.Marshal(payload) |
| 59 | + if err != nil { |
| 60 | + panic(err) |
| 61 | + } |
| 62 | + |
| 63 | + var be chainsync.BlockEvent |
| 64 | + err = json.Unmarshal(data, &be) |
| 65 | + if err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + |
| 69 | + err = beeep.Notify( |
| 70 | + "Snek", |
| 71 | + fmt.Sprintf("New Block!\nBlockNumber: %d, SlotNumber: %d\nHash: %s", |
| 72 | + be.BlockNumber, |
| 73 | + be.SlotNumber, |
| 74 | + be.BlockHash, |
| 75 | + ), |
| 76 | + "assets/snek-icon.png", |
| 77 | + ) |
| 78 | + if err != nil { |
| 79 | + panic(err) |
| 80 | + } |
| 81 | + default: |
| 82 | + err := beeep.Notify( |
| 83 | + "Snek", |
| 84 | + fmt.Sprintf("New Event!\nEvent: %v", evt), |
| 85 | + "assets/snek-icon.png", |
| 86 | + ) |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }() |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// Stop the embedded output |
| 97 | +func (n *NotifyOutput) Stop() error { |
| 98 | + close(n.eventChan) |
| 99 | + close(n.errorChan) |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// ErrorChan returns the input error channel |
| 104 | +func (n *NotifyOutput) ErrorChan() chan error { |
| 105 | + return n.errorChan |
| 106 | +} |
| 107 | + |
| 108 | +// InputChan returns the input event channel |
| 109 | +func (n *NotifyOutput) InputChan() chan<- event.Event { |
| 110 | + return n.eventChan |
| 111 | +} |
| 112 | + |
| 113 | +// OutputChan always returns nil |
| 114 | +func (n *NotifyOutput) OutputChan() <-chan event.Event { |
| 115 | + return nil |
| 116 | +} |
0 commit comments