/
intrusions.go
42 lines (33 loc) · 975 Bytes
/
intrusions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package redis
import (
"context"
"encoding/base64"
"github.com/go-json-experiment/json"
)
const IntrusionChannel = iotPrefix + "camera"
type Intrusion struct {
IDIoT string `json:"id_iot"`
Image string `json:"image"`
}
type Base64String string
func (b Base64String) MarshalJSON() ([]byte, error) {
out := make([]byte, base64.RawStdEncoding.EncodedLen(len(b)))
base64.RawStdEncoding.Encode(out, []byte(b))
return out, nil
}
func (b *Base64String) UnmarshalJSON(data []byte) error {
decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(data)))
n, err := base64.RawStdEncoding.Decode(decoded, data)
if err != nil {
return err
}
*b = Base64String(decoded[:n])
return nil
}
func (s *Store) IntrusionPub(ctx context.Context, intrusion Intrusion) error {
jsonData, err := json.Marshal(intrusion)
if err != nil {
return err
}
return s.client.Do(ctx, s.client.B().Publish().Channel(IntrusionChannel).Message(string(jsonData)).Build()).Error()
}