Skip to content

Commit 8fcbb29

Browse files
committed
feat: messaging first step
1 parent 6787dc1 commit 8fcbb29

File tree

7 files changed

+81
-10
lines changed

7 files changed

+81
-10
lines changed

backend/config/properties.env

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
PORT=3000
22
DB_PARAMS="host=localhost user=sven1 password=sven1 dbname=angularandgo port=5432 sslmode=disable"
3+
MSG_PARAMS="tcp://localhost:1883"
4+
MSG_CLIENT_ID="reactandgo"
5+
MSG_SERVER_USER="artemis1"
6+
MSG_SERVER_PWD="artemis1"
37
APIKEY1="00000000-0000-0000-0000-000000000002"
48
APIKEY2="00000000-0000-0000-0000-000000000002"
59
APIKEY3="00000000-0000-0000-0000-000000000002"

backend/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module angular-and-go
33
go 1.19
44

55
require (
6+
github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
67
github.com/fatih/color v1.9.0 // indirect
78
github.com/fsnotify/fsnotify v1.4.9 // indirect
89
github.com/gin-contrib/sse v0.1.0 // indirect
@@ -13,6 +14,7 @@ require (
1314
github.com/go-playground/universal-translator v0.18.0 // indirect
1415
github.com/go-playground/validator/v10 v10.11.1 // indirect
1516
github.com/goccy/go-json v0.10.0 // indirect
17+
github.com/gorilla/websocket v1.4.2 // indirect
1618
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
1719
github.com/jackc/pgconn v1.13.0 // indirect
1820
github.com/jackc/pgio v1.0.0 // indirect

backend/go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
77
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
88
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
11+
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
1012
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
1113
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
1214
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
@@ -36,6 +38,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
3638
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3739
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
3840
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
41+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
42+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3943
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
4044
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
4145
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
@@ -199,6 +203,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
199203
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
200204
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
201205
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
206+
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
202207
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
203208
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
204209
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
@@ -218,6 +223,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193
218223
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
219224
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
220225
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
226+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
221227
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
222228
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
223229
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

backend/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"angular-and-go/pkd/contr"
66
"angular-and-go/pkd/cron"
77
"angular-and-go/pkd/database"
8+
"angular-and-go/pkd/messaging"
89
)
910

1011
func init() {
1112
config.LoadEnvVariables()
1213
database.ConnectToDB()
1314
database.MigrateDB()
15+
messaging.Start()
1416
cron.Start()
1517
}
1618

backend/pkd/cron/cron.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,27 @@ func updatePriceRegion(regionCircleCenters [16]CircleCenter, apikeys [3]string)
109109
for index, value := range HamburgAndSH {
110110
//time.Sleep(6 * time.Second)
111111
time.Sleep(15 * time.Second)
112-
err := gsclient.UpdateGsPrices(value.Latitude, value.Longitude, 25.0, apikeys[apikeyIndex])
113-
if err != nil {
114-
log.Printf("Region Canceled index: %v\n", index)
115-
break
116-
}
112+
log.Printf("index: %v value: %v", index, value)
113+
/*
114+
err := gsclient.UpdateGsPrices(value.Latitude, value.Longitude, 25.0, apikeys[apikeyIndex])
115+
if err != nil {
116+
log.Printf("Region Canceled index: %v\n", index)
117+
updateApiKeyIndex()
118+
break
119+
}
120+
*/
117121
requestCounter += 1
118122
if requestCounter%45 == 0 {
119-
if apikeyIndex < 2 {
120-
apikeyIndex += 1
121-
} else {
122-
apikeyIndex = 0
123-
}
123+
updateApiKeyIndex()
124124
}
125125
log.Printf("Request %v, ApikeyIndex: %v\n", requestCounter, apikeyIndex)
126126
}
127127
}
128+
129+
func updateApiKeyIndex() {
130+
if apikeyIndex < 2 {
131+
apikeyIndex += 1
132+
} else {
133+
apikeyIndex = 0
134+
}
135+
}

backend/pkd/messaging/msghandler.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package messaging
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
mqtt "github.com/eclipse/paho.mqtt.golang"
9+
)
10+
11+
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
12+
fmt.Printf("Message %s received on topic %s\n", msg.Payload(), msg.Topic())
13+
}
14+
15+
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
16+
fmt.Println("Connected")
17+
}
18+
19+
var connectionLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
20+
fmt.Printf("Connection Lost: %s\n", err.Error())
21+
}
22+
23+
func Start() {
24+
msgServerUrl := os.Getenv("MSG_PARAMS")
25+
msgClientId := os.Getenv("MSG_CLIENT_ID")
26+
msgServerUser := os.Getenv("MSG_SERVER_USER")
27+
msgServerPwd := os.Getenv("MSG_SERVER_PWD")
28+
options := mqtt.NewClientOptions()
29+
options.AddBroker(msgServerUrl)
30+
options.SetClientID(msgClientId)
31+
options.SetUsername(msgServerUser)
32+
options.SetPassword(msgServerPwd)
33+
options.SetDefaultPublishHandler(messagePubHandler)
34+
options.OnConnect = connectHandler
35+
options.OnConnectionLost = connectionLostHandler
36+
37+
client := mqtt.NewClient(options)
38+
token := client.Connect()
39+
if token.Wait() && token.Error() != nil {
40+
log.Printf("Connection failed: %v", token.Error())
41+
} else {
42+
log.Printf("Connected to: %v id: %v", msgServerUrl, msgClientId)
43+
}
44+
}

docker-artemis.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
docker run --name local-artemis-reactandgo -p 1883:1883 -p 8161:8161 -e ARTEMIS_USER=artemis1 -e ARTEMIS_PASSWORD=artemis1 angular2guy/artemis-ubuntu
2+
3+
docker start local-artemis-reactandgo
4+
5+
docker stop local-artemis-reactandgo

0 commit comments

Comments
 (0)