Skip to content

Commit 7ba24d3

Browse files
committed
Add call to tm1637 from controller
1 parent afd6788 commit 7ba24d3

File tree

6 files changed

+33
-98
lines changed

6 files changed

+33
-98
lines changed

.github/workflows/publish-controller.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- main
99
paths:
1010
- 'controller/**'
11+
- 'rpc_client/**'
1112
- 'scripts/**'
1213
- '.github/**'
1314

controller/config/config.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func isDevEnv() bool {
1414
}
1515

1616
func initConfig() models.Config {
17-
1817
modules := utils.ParseYamlFile[[]models.Module]("/sensors/modules.yaml")
1918

2019
c := models.Config{

controller/go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
module github.com/torchiaf/Sensors/controller
22

3-
go 1.23.0
3+
go 1.23.1
44

55
toolchain go1.23.4
66

77
require (
88
github.com/fatih/structs v1.1.0
99
github.com/itchyny/gojq v0.12.17
10-
github.com/rabbitmq/amqp091-go v1.10.0
1110
gopkg.in/yaml.v2 v2.4.0
1211
k8s.io/client-go v0.32.0
1312
)
1413

14+
require github.com/rabbitmq/amqp091-go v1.10.0 // indirect
15+
1516
require (
1617
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
1718
github.com/go-logr/logr v1.4.2 // indirect
@@ -22,6 +23,7 @@ require (
2223
github.com/kr/text v0.2.0 // indirect
2324
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2425
github.com/modern-go/reflect2 v1.0.2 // indirect
26+
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b
2527
github.com/x448/float16 v0.8.4 // indirect
2628
golang.org/x/net v0.30.0 // indirect
2729
golang.org/x/text v0.19.0 // indirect

controller/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
4747
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4848
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4949
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
50+
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b h1:Gov23VN7l20GFoGg6CCmHbI/90mcUjiZKMizWjRbxTI=
51+
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b/go.mod h1:/wPjrnUN+OPaRpEyoe4YHUTSfXNBVsXGBTLowDX96AM=
5052
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
5153
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
5254
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

controller/models/models.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Config struct {
3333
}
3434

3535
type Message struct {
36-
Device string `json:"device"`
37-
// Args map[string]interface{} `json:"args"`
36+
Device string `json:"device"`
37+
Action string `json:"action"`
38+
Args []string `json:"args"`
3839
}

controller/rpc_client.go

+23-93
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ package main
22

33
import (
44
"context"
5-
"fmt"
65
"log"
7-
"math/rand"
86
"time"
97

10-
amqp "github.com/rabbitmq/amqp091-go"
118
"github.com/torchiaf/Sensors/controller/config"
12-
"github.com/torchiaf/Sensors/controller/models"
13-
"github.com/torchiaf/Sensors/controller/utils"
9+
"github.com/torchiaf/Sensors/rpc_client"
1410
)
1511

1612
func failOnError(err error, msg string) {
@@ -19,103 +15,37 @@ func failOnError(err error, msg string) {
1915
}
2016
}
2117

22-
func randomString(l int) string {
23-
bytes := make([]byte, l)
24-
for i := 0; i < l; i++ {
25-
bytes[i] = byte(randInt(65, 90))
26-
}
27-
return string(bytes)
28-
}
29-
30-
func randInt(min int, max int) int {
31-
return min + rand.Intn(max-min)
32-
}
33-
34-
func exec(routingKey string, message models.Message) (res string, err error) {
35-
36-
address := fmt.Sprintf("amqp://%s:%s@%s:%s/", config.Config.RabbitMQ.Username, config.Config.RabbitMQ.Password, config.Config.RabbitMQ.Host, config.Config.RabbitMQ.Port)
37-
38-
conn, err := amqp.Dial(address)
39-
failOnError(err, "Failed to connect to RabbitMQ")
40-
defer conn.Close()
41-
42-
ch, err := conn.Channel()
43-
failOnError(err, "Failed to open a channel")
44-
defer ch.Close()
45-
46-
q, err := ch.QueueDeclare(
47-
"", // name
48-
false, // durable
49-
false, // delete when unused
50-
true, // exclusive
51-
false, // noWait
52-
nil, // arguments
53-
)
54-
failOnError(err, "Failed to declare a queue")
55-
56-
msgs, err := ch.Consume(
57-
q.Name, // queue
58-
"", // consumer
59-
true, // auto-ack
60-
false, // exclusive
61-
false, // no-local
62-
false, // no-wait
63-
nil, // args
64-
)
65-
failOnError(err, "Failed to register a consumer")
66-
67-
corrId := randomString(32)
68-
69-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
70-
defer cancel()
71-
72-
log.Printf("Msg: %s:", utils.ToString(message))
73-
74-
err = ch.PublishWithContext(ctx,
75-
"", // exchange
76-
routingKey, // routing key
77-
false, // mandatory
78-
false, // immediate
79-
amqp.Publishing{
80-
ContentType: "text/plain",
81-
CorrelationId: corrId,
82-
ReplyTo: q.Name,
83-
Body: []byte(utils.ToString(message)),
84-
})
85-
failOnError(err, "Failed to publish a message")
86-
87-
for d := range msgs {
88-
if corrId == d.CorrelationId {
89-
res = string(d.Body)
90-
failOnError(err, "Error msgs")
91-
break
92-
}
93-
}
94-
95-
return
96-
}
97-
9818
func main() {
99-
rand.Seed(time.Now().UTC().UnixNano())
100-
10119
log.Printf("Config %+v", config.Config)
10220

21+
client := rpc_client.New(context.Background())
22+
10323
for {
10424
for _, module := range config.Config.Modules {
10525
log.Printf(" [x] Requesting on {%s, %s, %s}", module.Name, module.Type, module.RoutingKey)
10626

107-
res, err := exec(
27+
res, err := client.Read(
10828
module.RoutingKey,
109-
models.Message{
110-
Device: "dht11",
111-
// Args: map[string]interface{}{
112-
// "foo": "bar",
113-
// },
114-
},
29+
"dht11",
30+
[]string{},
11531
)
116-
failOnError(err, "Failed to handle RPC request")
117-
118-
log.Printf(" [%s] Got %+v", module.Name, res)
32+
failOnError(err, "Failed to handle RPC request: dht11")
33+
34+
log.Printf(" [%s] [%s] Got %+v", module.Name, "dht11", res)
35+
36+
if module.Name == "raspberrypi-1" {
37+
res1, err := client.Read(
38+
module.RoutingKey,
39+
"tm1637",
40+
[]string{
41+
"temperature",
42+
"12",
43+
},
44+
)
45+
failOnError(err, "Failed to handle RPC request: tm1637")
46+
47+
log.Printf(" [%s] [%s] Got %+v", module.Name, "tm1637", res1)
48+
}
11949
}
12050

12151
time.Sleep(time.Second)

0 commit comments

Comments
 (0)