forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
521 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package db | ||
|
||
import ( | ||
"time" | ||
|
||
serverdb "github.com/evcc-io/evcc/server/db" | ||
"github.com/evcc-io/evcc/util" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// DB is a SQL database storage service | ||
type DB struct { | ||
log *util.Logger | ||
db *gorm.DB | ||
name string | ||
} | ||
|
||
type Database interface { | ||
Txn(startEnergy float64) *Transaction | ||
Persist(txn interface{}) | ||
} | ||
|
||
// New creates a database storage driver | ||
func New(name string) (*DB, error) { | ||
db := &DB{ | ||
log: util.NewLogger("db"), | ||
db: serverdb.Instance, | ||
name: name, | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
// Txn creates a charging transaction | ||
func (s *DB) Txn(meter float64) *Transaction { | ||
t := Transaction{ | ||
Loadpoint: s.name, | ||
Created: time.Now(), | ||
MeterStart: meter, | ||
} | ||
|
||
return &t | ||
} | ||
|
||
// Persist creates or updates a transaction in the database | ||
func (s *DB) Persist(txn interface{}) { | ||
s.log.TRACE.Printf("store: %+v", txn) | ||
|
||
if err := s.db.Save(txn).Error; err != nil { | ||
s.log.ERROR.Printf("store: %v", err) | ||
} | ||
} |
Oops, something went wrong.