From 02a72f68532e7ba706da2503a572bce663c8d1fa Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Thu, 27 Nov 2025 22:35:22 +0100 Subject: [PATCH] feat: add hash and toJson methods --- internal/pkg/model/typeMethods.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 internal/pkg/model/typeMethods.go diff --git a/internal/pkg/model/typeMethods.go b/internal/pkg/model/typeMethods.go new file mode 100644 index 0000000..e4589bb --- /dev/null +++ b/internal/pkg/model/typeMethods.go @@ -0,0 +1,25 @@ +package model + +import ( + "crypto/sha256" + "encoding/json" + "fmt" +) + +func (b *BeaconEvent) Hash() []byte { + rBatt := (b.Battery / 10) * 10 + c := fmt.Sprintf("%d%d%s%s%s", rBatt, b.Event, b.ID, b.Name, b.Type) + h := sha256.New() + h.Write([]byte(c)) + bs := h.Sum(nil) + + return bs +} + +func (b BeaconEvent) ToJSON() ([]byte, error) { + eData, err := json.Marshal(b) + if err != nil { + return nil, err + } + return eData, nil +}