Skip to content

Commit 2fa39c4

Browse files
author
amine-amaach
committed
Publish stable version v1.0.0
1 parent e8b7568 commit 2fa39c4

File tree

7 files changed

+123
-48
lines changed

7 files changed

+123
-48
lines changed

Diff for: .vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"go.inferGopath": false
3+
}

Diff for: docker-compose.yml

+46-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
1-
version: "3.2"
1+
version: "3.9"
22
services:
3+
mqtt:
4+
image: emqx/emqx:4.3.8
5+
ports:
6+
- 1883:1883
7+
- 8083:8083
8+
- 18083:18083
9+
restart: always
10+
environment:
11+
- EMQX_LOADED_PLUGINS=emqx_recon,emqx_retainer,emqx_management,emqx_dashboard,emqx_auth_mnesia
12+
- EMQX_ALLOW_ANONYMOUS=true
13+
- EMQX_ACL_NOMATCH=allow
14+
container_name: mqtt-emqx
15+
healthcheck:
16+
interval: 5s
17+
timeout: 3s
18+
retries: 10
19+
test: ["CMD", "curl", "-f", "http://mqtt:18083"]
20+
networks:
21+
- simulators
322
pgmqtt:
4-
build:
5-
context: .
6-
dockerfile: pgmqtt/Dockerfile
23+
image: simulators-pgmqtt:latest
724
restart: always
25+
container_name: simulators-pgmqtt
826
volumes:
9-
- .:/go/src/simulators
10-
container_name: pgmqtt
27+
- ./simulators-configs/pgmqtt/config.json:/configs/config.json
28+
environment:
29+
MQTT_SERVER_URL: mqtt://mqtt:1883
30+
MQTT_SERVER_USER: admin
31+
MQTT_SERVER_PWD: public
32+
MQTT_SERVER_RETAIN: 'false'
33+
MQTT_SERVER_QOS: 3
34+
MQTT_CLIENT_ID: 'pg_simulator'
35+
MQTT_KEEP_ALIVE: 300
36+
MQTT_RETRY_DELAY: 10
37+
SITE: 'Site'
38+
AREA: 'Area'
39+
RANDOM_DELAY_BETWEEN_MESSAGES: 'true'
40+
DELAY_BETWEEN_MESSAGES_MIN: 5
41+
DELAY_BETWEEN_MESSAGES_MAX: 10
42+
GENERATORS_NUMBER: 5
43+
GENERATORS_NUMBER_LIMIT: 50
44+
depends_on:
45+
- 'mqtt'
46+
networks:
47+
- simulators
48+
networks:
49+
simulators:
50+
name: simulators-net

Diff for: pgmqtt/Dockerfile

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
FROM golang
1+
# Build:
2+
FROM golang as build
23

3-
RUN mkdir -p /go/src/simulators
4+
# Set the Current Working Directory inside the container
5+
WORKDIR /simulators/pgmqtt
46

5-
ADD . /go/src/simulators
7+
# Populate the module cache based on the go.{mod,sum} files.
8+
COPY go.mod .
9+
COPY go.sum .
610

7-
RUN go get -t -v ./...
8-
RUN go get github.com/canthefason/go-watcher
9-
RUN go install github.com/canthefason/go-watcher/cmd/watcher
11+
# RUN go mod download
1012

11-
ENTRYPOINT watcher -run simulators/pgmqtt/cmd/ -watch simulators/pgmqtt
13+
COPY . .
14+
15+
# Build the Go Microservice
16+
RUN CGO_ENABLED=0 go build -o ./out/pgmqtt ./cmd/pgmqtt/
17+
18+
# Deployement:
19+
# Start fresh from a smaller image
20+
FROM alpine:latest
21+
22+
COPY --from=build /simulators/pgmqtt/out/pgmqtt /simulators/pgmqtt
23+
COPY --from=build /simulators/pgmqtt/configs/ /configs/
24+
25+
26+
ENTRYPOINT ["/simulators/pgmqtt"]

Diff for: pgmqtt/README.md

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<div align="center">
22

3-
<img src="docs/power-generator.png" alt="logo"/>
4-
<h1>MQTT Power-Generator</h1>
3+
<img src="docs/simulators.png" alt="logo"/>
4+
<h1>MQTT simulators</h1>
55

66
<p>
7-
An MQTT Power-Generator Simulator
7+
An MQTT simulators Simulator
88
</p>
99

1010
<!-- Badges -->
1111
<p>
12-
<a href="https://github.com/amine-amaach/power-generator/graphs/contributors">
13-
<img src="https://img.shields.io/github/contributors/amine-amaach/power-generator" alt="contributors" />
12+
<a href="https://github.com/amine-amaach/simulators/graphs/contributors">
13+
<img src="https://img.shields.io/github/contributors/amine-amaach/simulators" alt="contributors" />
1414
</a>
15-
<a href="https://github.com/amine-amaach/power-generator/network/members">
16-
<img src="https://img.shields.io/github/forks/amine-amaach/power-generator" alt="forks" />
15+
<a href="https://github.com/amine-amaach/simulators/network/members">
16+
<img src="https://img.shields.io/github/forks/amine-amaach/simulators" alt="forks" />
1717
</a>
18-
<a href="https://github.com/amine-amaach/power-generator/stargazers">
19-
<img src="https://img.shields.io/github/stars/amine-amaach/power-generator" alt="stars" />
18+
<a href="https://github.com/amine-amaach/simulators/stargazers">
19+
<img src="https://img.shields.io/github/stars/amine-amaach/simulators" alt="stars" />
2020
</a>
21-
<a href="https://github.com/amine-amaach/power-generator/issues/">
22-
<img src="https://img.shields.io/github/issues/amine-amaach/power-generator" alt="open issues" />
21+
<a href="https://github.com/amine-amaach/simulators/issues/">
22+
<img src="https://img.shields.io/github/issues/amine-amaach/simulators" alt="open issues" />
2323
</a>
2424
</p>
2525

2626
<h4>
27-
<a href="https://github.com/amine-amaach/power-generator/">View Demo</a>
27+
<a href="https://github.com/amine-amaach/simulators/">View Demo</a>
2828
<span> · </span>
29-
<a href="https://github.com/amine-amaach/power-generator">Documentation</a>
29+
<a href="https://github.com/amine-amaach/simulators">Documentation</a>
3030
<span> · </span>
31-
<a href="https://github.com/amine-amaach/power-generator/issues/">Report Bug</a>
31+
<a href="https://github.com/amine-amaach/simulators/issues/">Report Bug</a>
3232
<span> · </span>
33-
<a href="https://github.com/amine-amaach/power-generator/issues/">Request Feature</a>
33+
<a href="https://github.com/amine-amaach/simulators/issues/">Request Feature</a>
3434
</h4>
3535
</div>
3636

@@ -107,13 +107,13 @@ To run tests, run the following command
107107
Clone the project
108108

109109
```bash
110-
git clone [email protected]:amine-amaach/power-generator.git
110+
git clone [email protected]:amine-amaach/simulators.git
111111
```
112112

113113
Go to the project directory
114114

115115
```bash
116-
cd power-generator
116+
cd simulators/pgmqtt
117117
```
118118

119119
<!-- Install dependencies
@@ -125,7 +125,7 @@ Go to the project directory
125125
Start the simulator
126126

127127
```bash
128-
make run
128+
go run cmd/pgmqtt/main.go
129129
```
130130

131131

@@ -148,15 +148,17 @@ function App() {
148148
<!-- Roadmap -->
149149
## 🛣️ Roadmap
150150

151+
- [x] Randomize the delay between messages separately for each generator.
152+
- [x] Simulate multiple generators in a single microservice.
151153
- [x] Concurrent Simulator.
152154
- [ ] Set up TLS connection.
153155

154156

155157
<!-- Contributing -->
156158
## 👋 Contributing
157159

158-
<a href="https://github.com/amine-amaach/power-generator/graphs/contributors">
159-
<img src="https://contrib.rocks/image?repo=amine-amaach/power-generator" />
160+
<a href="https://github.com/amine-amaach/simulators/graphs/contributors">
161+
<img src="https://contrib.rocks/image?repo=amine-amaach/simulators" />
160162
</a>
161163

162164

@@ -174,15 +176,15 @@ Distributed under the no License. See LICENSE.txt for more information.
174176

175177
Amine Amaach - [LinkedIn](https://www.linkedin.com/in/amine-amaach/) - [Email]([email protected])
176178

177-
Project Link: [https://github.com/amine-amaach/power-generator.git](https://github.com/amine-amaach/power-generator.git)
179+
Project Link: [https://github.com/amine-amaach/simulators.git](https://github.com/amine-amaach/simulators.git)
178180

179181

180182
<!-- Acknowledgments -->
181183
## 💎 Acknowledgements
182184

183185
<!-- Use this section to mention useful resources and libraries that you have used in your projects. -->
184186

185-
- [Libre Technologies](https://github.com/Spruik/libre-common)
186-
- [InfluxDB-Roadshow-Training](https://github.com/InfluxCommunity/InfluxDB-Roadshow-Training)
187+
- [InfluxDB-Roadshow-Training](https://github.com/InfluxCommunity/InfluxDB-Roadshow-Training)
187188
- [Zap](https://github.com/uber-go/zap)
188-
- [Viper](https://github.com/spf13/viper)
189+
- [Viper](https://github.com/spf13/viper)
190+
- [Libre Technologies](https://github.com/Spruik/libre-common)

Diff for: pgmqtt/cmd/pgmqtt/main.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import (
1616
"go.uber.org/zap"
1717
)
1818

19+
const (
20+
version = "v1.0.0"
21+
website = "https://www.linkedin.com/in/amine-amaach/"
22+
banner = `
23+
____ ____ _ __ __ ___ _____ _____
24+
| _ \ ___ __ __ ___ _ __ / ___| ___ _ __ ___ _ __ __ _ | |_ ___ _ __ | \/ | / _ \|_ _||_ _|
25+
| |_) |/ _ \\ \ /\ / // _ \| '__|_____ | | _ / _ \| '_ \ / _ \| '__|/ _' || __|/ _ \ | '__| | |\/| || | | | | | | |
26+
| __/| (_) |\ V V /| __/| | |_____|| |_| || __/| | | || __/| | | (_| || |_| (_) || | | | | || |_| | | | | |
27+
|_| \___/ \_/\_/ \___||_| \____| \___||_| |_| \___||_| \__,_| \__|\___/ |_| |_| |_| \__\_\ |_| |_| %s
28+
Power-Generator MQTT Simulator
29+
___________________________________________________________________________________________________________________O/___________
30+
%s O\
31+
`
32+
)
33+
1934
var (
2035
// Viper Config used to handle config files and env variables.
2136
cfg *utils.Config
@@ -42,7 +57,9 @@ func init() {
4257
}
4358

4459
func main() {
45-
fmt.Println(cfg.RandomDelayBetweenMessages, cfg.DelayBetweenMessagesMin)
60+
61+
// Print Banner
62+
fmt.Println(utils.Colorize(fmt.Sprintf(banner, version, website), utils.Cyan))
4663

4764
ctx, cancel := context.WithCancel(context.Background())
4865

@@ -55,9 +72,7 @@ func main() {
5572

5673
simService := services.NewSimService()
5774

58-
//
59-
// Handle Random delay between messages :
60-
//
75+
// ####### Handle Random delay between messages :
6176
wg := sync.WaitGroup{}
6277
wg.Add(cfg.GeneratorsNumber)
6378

@@ -74,7 +89,7 @@ func main() {
7489
case <-time.After(time.Duration(r) * time.Second): // update the delay
7590
// If RANDOM_DELAY_BETWEEN_MESSAGES == true set the delayBetweenMessages between the messages randomly.
7691
if cfg.RandomDelayBetweenMessages {
77-
r = rand.Float64() * float64(cfg.DelayBetweenMessagesMax)
92+
r = float64(cfg.DelayBetweenMessagesMin) + rand.Float64()*float64(cfg.DelayBetweenMessagesMax)
7893
randChannel <- r
7994
} else {
8095
r = float64(cfg.DelayBetweenMessagesMin)

Diff for: pgmqtt/configs/config.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"MQTT_SERVER_URL":"mqtt://localhost:1883",
33
"MQTT_SERVER_USER": "admin",
4-
"MQTT_SERVER_PWD": "eminEMQXe",
5-
"MQTT_SERVER_RETAIN": false,
4+
"MQTT_SERVER_PWD": "public",
5+
"MQTT_SERVER_RETAIN": "false",
66
"MQTT_SERVER_QOS": 3,
77
"MQTT_CLIENT_ID": "pg_simulator",
88
"MQTT_KEEP_ALIVE": 300,
99
"MQTT_RETRY_DELAY": 10,
1010
"SITE": "Site",
1111
"AREA": "Area",
12-
"RANDOM_DELAY_BETWEEN_MESSAGES": true,
12+
"RANDOM_DELAY_BETWEEN_MESSAGES": "true",
1313
"DELAY_BETWEEN_MESSAGES_MIN": 5,
1414
"DELAY_BETWEEN_MESSAGES_MAX": 10,
1515
"GENERATORS_NUMBER": 10,

Diff for: pgmqtt/utils/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func (config *Config) setDefaults(logger *zap.SugaredLogger) {
6464

6565
viper.SetDefault("MQTT_SERVER_URL", "mqtt://localhost:1883")
6666
viper.SetDefault("MQTT_SERVER_USER", "admin")
67-
viper.SetDefault("MQTT_SERVER_PWD", "eminEMQXe")
67+
viper.SetDefault("MQTT_SERVER_PWD", "public")
6868
viper.SetDefault("MQTT_SERVER_QOS", 0)
69-
viper.SetDefault("MQTT_SERVER_RETAIN", false)
69+
viper.SetDefault("MQTT_SERVER_RETAIN", "false")
7070
viper.SetDefault("MQTT_CLIENT_ID", "someID")
7171
viper.SetDefault("MQTT_KEEP_ALIVE", 300)
7272
viper.SetDefault("MQTT_RETRY_DELAY", 10)
7373
viper.SetDefault("SITE", "Site")
7474
viper.SetDefault("AREA", "Area")
75-
viper.SetDefault("RANDOM_DELAY_BETWEEN_MESSAGES", true)
75+
viper.SetDefault("RANDOM_DELAY_BETWEEN_MESSAGES", "true")
7676
viper.SetDefault("DELAY_BETWEEN_MESSAGES_MIN", 5)
7777
viper.SetDefault("DELAY_BETWEEN_MESSAGES_MAX", 10)
7878
viper.SetDefault("GENERATORS_NUMBER", 3)

0 commit comments

Comments
 (0)