Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for KnowGo Adaptation Layer events #279

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/bees/knowgobee.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 166 additions & 0 deletions bees/knowgobee/knowgobee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (C) 2019 Adaptant Solutions AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Paul Mundt <[email protected]>
*/

package knowgobee

import (
"context"
"encoding/json"
"github.com/knowgoio/knowgo-pubsub/api"
"github.com/muesli/beehive/bees"
"net"
"net/url"
"strconv"
"time"
)

type KnowGoBee struct {
bees.Bee

apiKey string
server string
client *api.ClientConfig
sub *api.Subscription
interval int

eventChan chan bees.Event
}

type CountryChangeNotification struct {
DriverID int `json:"driverId,omitempty"`
Entering string `json:"entering"`
Leaving string `json:"leaving"`
Timestamp string `json:"timestamp"`
}

func (mod *KnowGoBee) handleEvent(eventData []byte) {
var notification CountryChangeNotification

err := json.Unmarshal(eventData, &notification)
if err != nil {
mod.LogErrorf("Failed to unmarshal event")
return
}

ev := bees.Event{
Bee: mod.Name(),
Name: "country-change",
Options: []bees.Placeholder{
{
Name: "driverId",
Type: "int",
Value: notification.DriverID,
},
{
Name: "entering",
Type: "string",
Value: notification.Entering,
},
{
Name: "leaving",
Type: "string",
Value: notification.Leaving,
},
{
Name: "timestamp",
Type: "timestamp",
Value: notification.Timestamp,
},
},
}

mod.eventChan <- ev
}

func (mod *KnowGoBee) newAPIClient() *api.ClientConfig {
client := api.DefaultClientConfig()

if mod.apiKey != "" {
client.APIKey = mod.apiKey
}

if mod.interval > 0 {
client.PollInterval = time.Duration(mod.interval) * time.Second
}

if mod.server != "" {
u, err := url.Parse(mod.server)
if err == nil {
client.Host = u.Hostname()
client.Port, _ = strconv.Atoi(u.Port())
} else {
host, port, err := net.SplitHostPort(mod.server)
if err != nil {
client.Host = mod.server
} else {
client.Host = host
client.Port, _ = strconv.Atoi(port)
}
}
}

return client
}

// Run executes the Bee's event loop.
func (mod *KnowGoBee) Run(eventChan chan bees.Event) {
client := mod.newAPIClient()

sub, err := client.Subscribe(&api.SubscriptionRequest{
Event: "country-change",
})
if err != nil {
mod.LogErrorf("Failed to subscribe:", err)
return
}

mod.Logf("Successfully subscribed")
pmundt marked this conversation as resolved.
Show resolved Hide resolved

mod.client = client
mod.sub = sub
mod.eventChan = eventChan

ticker := time.NewTicker(client.PollInterval).C
for {
select {
case <-mod.SigChan:
return
case <-ticker:
b := client.Receive(context.Background(), mod.sub)
if b != nil {
mod.handleEvent(b)
}
}
}
}

// Action triggers the action passed to it.
func (mod *KnowGoBee) Action(action bees.Action) []bees.Placeholder {
return []bees.Placeholder{}
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *KnowGoBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)

options.Bind("polling_interval", &mod.interval)
options.Bind("server", &mod.server)
options.Bind("api_key", &mod.apiKey)
}
131 changes: 131 additions & 0 deletions bees/knowgobee/knowgobeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2019 Adaptant Solutions AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Paul Mundt <[email protected]>
*/

package knowgobee

import (
"github.com/muesli/beehive/bees"
)

// KnowGoBeeFactory is a factory for KnowGoBees.
type KnowGoBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *KnowGoBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := KnowGoBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)

return &bee
}

// ID returns the ID of this Bee.
func (factory *KnowGoBeeFactory) ID() string {
return "knowgobee"
}

// Name returns the name of this Bee.
func (factory *KnowGoBeeFactory) Name() string {
return "KnowGo"
}

// Description returns the description of this Bee.
func (factory *KnowGoBeeFactory) Description() string {
return "React to various KnowGo platform events"
}

// Image returns the filename of an image for this Bee.
func (factory *KnowGoBeeFactory) Image() string {
return factory.ID() + ".jpg"
}

// LogoColor returns the preferred logo background color (used by the admin interface).
func (factory *KnowGoBeeFactory) LogoColor() string {
return "#7ace56"
}

// Options returns the options available to configure this Bee.
func (factory *KnowGoBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "server",
Description: "URL for the desired KnowGo server",
Type: "string",
},
{
Name: "polling_interval",
Description: "Number of seconds to wait between polling intervals",
Type: "int",
Default: 10,
},
{
Name: "api_key",
Description: "API Key for the KnowGo server",
Type: "string",
Mandatory: true,
},
}
return opts
}

// Events describes the available events provided by this Bee.
func (factory *KnowGoBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Name: "country-change",
Description: "A country change event has taken place",
Options: []bees.PlaceholderDescriptor{
{
Name: "driverId",
Description: "ID of Driver",
Type: "int",
},
{
Name: "entering",
Description: "ISO 3166-2 country code of country entered",
Type: "string",
Mandatory: true,
},
{
Name: "leaving",
Description: "ISO 3166-2 country code of country left",
Type: "string",
Mandatory: true,
},
{
Name: "timestamp",
Description: "Time when event took place",
Type: "timestamp",
Mandatory: true,
},
},
},
}
return events
}

func init() {
f := KnowGoBeeFactory{}
bees.RegisterFactory(&f)
}
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ require (
github.com/glaxx/go_pastebin v0.0.0-20170619211819-7e72d56770d0
github.com/go-ini/ini v1.42.0 // indirect
github.com/go-mail/mail v2.3.1+incompatible
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/golang/mock v1.2.0 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
Expand All @@ -44,20 +44,16 @@ require (
github.com/jayeshsolanki93/devgorant v0.0.0-20160810172004-69fb03e5c3b1
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.6 // indirect
github.com/knowgoio/knowgo-pubsub v0.0.0-20191213102205-0c509a43fd2b
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0
github.com/kurrik/oauth1a v0.0.0-20151019171716-cb1b80e32dd4 // indirect
github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5 // indirect
github.com/lusis/slack-test v0.0.0-20190426140909-c40012f20018 // indirect
github.com/mattn/go-colorable v0.1.1
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mattn/go-mastodon v0.0.3
github.com/mattn/go-xmpp v0.0.0-20190124093244-6093f50721ed
github.com/minio/minio-go v6.0.14+incompatible
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 // indirect
github.com/mrexodia/wray v0.0.0-20160318003008-78a2c1f284ff // indirect
github.com/muesli/go-pkg-rss v0.0.0-20180307042412-3bef0f3126ec
Expand All @@ -68,13 +64,13 @@ require (
github.com/nlopes/slack v0.6.0
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/odwrtw/transmission v0.0.0-20170515140915-08885b3058e7
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/client_golang v1.2.1
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4 // indirect
github.com/shuheiktgw/go-travis v0.1.10-0.20190502100712-2d0b3e9898f0
github.com/simplepush/simplepush-go v0.0.0-20170307205831-8980e96b7b02
github.com/simplereach/timeutils v1.2.0 // indirect
github.com/sirupsen/logrus v1.4.1
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
github.com/sromku/go-gitter v0.0.0-20170828210750-70f7030a94a6
Expand All @@ -84,7 +80,6 @@ require (
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 // indirect
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/appengine v1.5.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
Expand Down
Loading