Skip to content

Commit

Permalink
fix-devices-interface-and-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
torchiaf committed Feb 2, 2025
1 parent 7ba24d3 commit b4c0ddc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.4
require (
github.com/fatih/structs v1.1.0
github.com/itchyny/gojq v0.12.17
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202022913-2a42e8d8bd20
gopkg.in/yaml.v2 v2.4.0
k8s.io/client-go v0.32.0
)
Expand All @@ -23,7 +24,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions controller/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b h1:Gov23VN7l20GFoGg6CCmHbI/90mcUjiZKMizWjRbxTI=
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202005215-c296b8b6cb2b/go.mod h1:/wPjrnUN+OPaRpEyoe4YHUTSfXNBVsXGBTLowDX96AM=
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202022913-2a42e8d8bd20 h1:Ym/PM/TlNRyxyCyJp4u0AIB6zk+oPhrRtgm026niDIc=
github.com/torchiaf/Sensors/rpc_client v0.0.0-20250202022913-2a42e8d8bd20/go.mod h1:/wPjrnUN+OPaRpEyoe4YHUTSfXNBVsXGBTLowDX96AM=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
25 changes: 23 additions & 2 deletions controller/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package main

import (
"context"
"encoding/json"
"log"
"math"
"strconv"
"time"

"github.com/torchiaf/Sensors/controller/config"
"github.com/torchiaf/Sensors/rpc_client"
)

type Dht11 struct {
T float64 `json:"t"`
H float64 `json:"h"`
}

func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
Expand All @@ -21,6 +29,9 @@ func main() {
client := rpc_client.New(context.Background())

for {

temperature := ""

for _, module := range config.Config.Modules {
log.Printf(" [x] Requesting on {%s, %s, %s}", module.Name, module.Type, module.RoutingKey)

Expand All @@ -33,13 +44,23 @@ func main() {

log.Printf(" [%s] [%s] Got %+v", module.Name, "dht11", res)

if module.Name == "raspberrypi-0" {
var obj Dht11
err = json.Unmarshal([]byte(res), &obj)
if err != nil {
log.Fatalf("error: %v", err)
}

temperature = strconv.Itoa(int(math.Round(obj.T)))
}

if module.Name == "raspberrypi-1" {
res1, err := client.Read(
res1, err := client.Write(
module.RoutingKey,
"tm1637",
[]string{
"temperature",
"12",
temperature,
},
)
failOnError(err, "Failed to handle RPC request: tm1637")
Expand Down
3 changes: 2 additions & 1 deletion modules/raspberrypi3b/devices/dht11/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
import Adafruit_DHT
from config import device

Expand All @@ -14,7 +15,7 @@
d['t'] = temperature
d['h'] = humidity

print(d)
print(json.dumps(d))

except:
print("read() error")
4 changes: 3 additions & 1 deletion modules/raspberrypi5/devices/dht11/app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import json
import board
import adafruit_dht
from config import device, pin
Expand All @@ -15,7 +17,7 @@
d['t'] = dhtDevice.temperature
d['h'] = dhtDevice.humidity

print(d)
print(json.dumps(d))

except Exception as error:
# dhtDevice.exit()
Expand Down
10 changes: 6 additions & 4 deletions modules/raspberrypi5/devices/tm1637/app/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import sys
import tm1637
from time import sleep
from config import print, clk, dio

tm = tm1637.TM1637(clk=clk, dio=dio)

try:
arg = sys.argv[1]
print('Received: {}'.format(arg))
print('Received: {}'.format(sys.argv))

action = sys.argv[1]
fn = getattr(tm, sys.argv[2])
arg = sys.argv[3]

t = int(arg)
tm.temperature(t) # show temperature 't*C'
fn(t) # show temperature 't*C'
except Exception as error:
print(error)
pass
Expand Down

0 comments on commit b4c0ddc

Please sign in to comment.