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

Use custom type for Input model #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions src/dcreadout/readout.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/nDenerserve/SmartPi/models"
"github.com/nDenerserve/SmartPi/repository/config"
"github.com/nDenerserve/SmartPi/smartpidc"
"github.com/nDenerserve/SmartPi/utils"
log "github.com/sirupsen/logrus"
)

Expand All @@ -31,7 +30,7 @@ func pollSmartPiDC(config *config.DCconfig) {
var accumulatorEnergyBalanced []float64
var accumulatorInput []float64
var accumulatorPower []float64
inputConfiguration := [4]int{models.NotUsed, models.NotUsed, models.NotUsed, models.NotUsed}
inputConfiguration := []models.InputType{models.NotUsed, models.NotUsed, models.NotUsed, models.NotUsed}

if config.MQTTenabled {
mqttclient = smartpidc.NewMQTTClient(config)
Expand Down Expand Up @@ -68,7 +67,7 @@ func pollSmartPiDC(config *config.DCconfig) {
accumulatorEnergyProduced = make([]float64, powerCalculationCounter)
accumulatorEnergyBalanced = make([]float64, powerCalculationCounter)

log.Debug("InputConfiguration: " + strings.Join(utils.Int2StringSlice(inputConfiguration[:]), "|"))
log.Debug("InputConfiguration: " + strings.Join(models.InputTypesAsStrings(inputConfiguration), "|"))

tick := time.Tick(time.Duration(1000/config.Samplerate) * time.Millisecond)

Expand Down Expand Up @@ -138,19 +137,19 @@ func pollSmartPiDC(config *config.DCconfig) {

// Publish readouts to MQTT.
if config.MQTTenabled && (config.MQTTpublishintervall == "sample") {
smartpidc.PublishMQTTReadouts(config, mqttclient, inputConfiguration[:], values[:], power, energyConsumed, energyProduced)
smartpidc.PublishMQTTReadouts(config, mqttclient, inputConfiguration, values[:], power, energyConsumed, energyProduced)
}

log.Debug("SharedFileEnable: " + strconv.FormatBool(config.SharedFileEnabled))

if config.SharedFileEnabled {
smartpidc.WriteSharedFile(config, inputConfiguration[:], values[:], power, energyConsumed, energyProduced)
smartpidc.WriteSharedFile(config, inputConfiguration, values[:], power, energyConsumed, energyProduced)
}

// write Database for FastData
if config.DatabaseEnabled && (config.DatabaseStoreIntervall == "sample") {
log.Debug("Write sample data to database")
smartpidc.InsertInfluxData(config, inputConfiguration[:], values[:], power, energyConsumed, energyProduced)
smartpidc.InsertInfluxData(config, inputConfiguration, values[:], power, energyConsumed, energyProduced)
}

}
Expand All @@ -163,13 +162,13 @@ func pollSmartPiDC(config *config.DCconfig) {

// Publish readouts to MQTT.
if config.MQTTenabled && (config.MQTTpublishintervall == "minute") {
smartpidc.PublishMQTTReadouts(config, mqttclient, inputConfiguration[:], values[:], power, accumulatorEnergyConsumed, accumulatorEnergyProduced)
smartpidc.PublishMQTTReadouts(config, mqttclient, inputConfiguration, values[:], power, accumulatorEnergyConsumed, accumulatorEnergyProduced)
smartpidc.PublishMQTTCalculations(config, mqttclient, accumulatorEnergyBalanced)
}

if config.DatabaseEnabled && (config.DatabaseStoreIntervall == "minute") {
log.Debug("Write minute data to database")
smartpidc.InsertInfluxData(config, inputConfiguration[:], accumulatorInput[:], accumulatorPower, accumulatorEnergyConsumed, accumulatorEnergyProduced)
smartpidc.InsertInfluxData(config, inputConfiguration, accumulatorInput[:], accumulatorPower, accumulatorEnergyConsumed, accumulatorEnergyProduced)
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module github.com/nDenerserve/SmartPi

go 1.21.4
toolchain go1.22.8
go 1.22.6

toolchain go1.23.1

require (
github.com/eclipse/paho.mqtt.golang v1.5.0
Expand All @@ -10,7 +11,6 @@ require (
github.com/gorilla/context v1.1.2
github.com/gorilla/mux v1.8.1
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/julien040/go-ternary v1.0.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/msteinert/pam v1.2.0
github.com/nDenerserve/RN2483 v0.0.0-20231123165121-2ab5d8f2030a
Expand Down
2 changes: 0 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uT
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/julien040/go-ternary v1.0.0 h1:ZqoUWG9HZvcVu0426B7/jKS5eilgHeqp5X15+RvTP7g=
github.com/julien040/go-ternary v1.0.0/go.mod h1:XXIcjDHL7vyuHA7V0UwaTKMscsqKzFkE9FTGbBeqJHM=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
18 changes: 18 additions & 0 deletions src/models/inputtype.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package models

type InputType int

const (
NotUsed = iota
Voltage
Current
)

func (i InputType) String() string {
return [...]string{"NotUsed", "Voltage", "Current"}[i]
}

func (i InputType) UnitSymbol() string {
return [...]string{"N/A", "V", "A"}[i]
}

func InputTypesAsStrings(input []InputType) []string {
s := make([]string, len(input))
for i, v := range input {
s[i] = v.String()
}
return s
}
7 changes: 3 additions & 4 deletions src/smartpidc/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/julien040/go-ternary"
"github.com/nDenerserve/SmartPi/models"
"github.com/nDenerserve/SmartPi/repository/config"
log "github.com/sirupsen/logrus"
Expand All @@ -27,7 +26,7 @@ type tMeasurement struct {
Values []tValue `json:"values" xml:"values"`
}

func WriteSharedFile(c *config.DCconfig, inputconfig []int, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {
func WriteSharedFile(c *config.DCconfig, inputconfig []models.InputType, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {
var f *os.File
var err error
var measurement tMeasurement
Expand All @@ -41,9 +40,9 @@ func WriteSharedFile(c *config.DCconfig, inputconfig []int, values []float64, po
s = append(s, timeStamp)

for j := range values {
if inputconfig[j] != 0 {
if inputconfig[j] != models.NotUsed {
if !math.IsNaN(values[j]) {
measurement.Values = append(measurement.Values, tValue{Input: "input" + strconv.Itoa(j), Value: values[j], Unit: ternary.If(inputconfig[j] == models.Voltage, "V", "A")})
measurement.Values = append(measurement.Values, tValue{Input: "input" + strconv.Itoa(j), Value: values[j], Unit: inputconfig[j].UnitSymbol()})
}

s = append(s, fmt.Sprint(values[j]))
Expand Down
3 changes: 2 additions & 1 deletion src/smartpidc/influxdatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (

influxdb2 "github.com/influxdata/influxdb-client-go/v2"
client "github.com/influxdata/influxdb1-client/v2"
"github.com/nDenerserve/SmartPi/models"
"github.com/nDenerserve/SmartPi/repository/config"
"github.com/nDenerserve/SmartPi/smartpi/network"

log "github.com/sirupsen/logrus"
)

func InsertInfluxData(c *config.DCconfig, inputconfig []int, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {
func InsertInfluxData(c *config.DCconfig, inputconfig []models.InputType, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {

client := influxdb2.NewClient(c.Influxdatabase, c.InfluxAPIToken)
defer client.Close()
Expand Down
3 changes: 2 additions & 1 deletion src/smartpidc/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/nDenerserve/SmartPi/models"
"github.com/nDenerserve/SmartPi/repository/config"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func publishMQTT(m mqtt.Client, status *bool, t string, v float64) bool {
return false
}

func PublishMQTTReadouts(c *config.DCconfig, mqttclient mqtt.Client, inputconfig []int, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {
func PublishMQTTReadouts(c *config.DCconfig, mqttclient mqtt.Client, inputconfig []models.InputType, values []float64, power []float64, energyConsumed []float64, energyProduced []float64) {
//[basetopic]/[node]/[keyname]
// Let's try to (re-)connect if MQTT connection was lost.
if !mqttclient.IsConnected() {
Expand Down