Skip to content

Commit

Permalink
feat: added redis
Browse files Browse the repository at this point in the history
  • Loading branch information
OldTyT committed Nov 15, 2023
1 parent e3ec382 commit 31360e3
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ docker compose up -d
| `SLEEP_TIME`| `5` | Sleep time after cycle, in second. |
| `FRIGATE_EXTERNAL_URL` | `http://localhost:5000` | External link in frigate(need for generate link in message). |
| `TZ` | `""` | Timezone |
| `REDIS_ADDR` | `localhost:6379` | IP and port redis |
| `REDIS_PASSWORD` | `""` | Redis password |
| `REDIS_DB` | `0` | Redis DB |
| `REDIS_PROTOCOL` | `3` | Redis protocol |
| `REDIS_TTL` | `604800` | Redis TTL for key event(in seconds.) |
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
---
version: '3'
networks:
net:

services:
redis:
image: redis:7.2.3-alpine3.18
networks: [net]
restart: always
volumes:
- /data:/mnt/docker/redis/data

frigate-telegram:
image: ghcr.io/oldtyt/frigate-telegram
restart: always
networks: [net]
environment:
TELEGRAM_BOT_TOKEN: 1234567890:ABCDEFGHEFGREHBTYJBTRGBER34T54Y54
FRIGATE_URL: http://192.168.0.254:5000
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/oldtyt/frigate-telegram

go 1.21.3

require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/redis/go-redis/v9 v9.3.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
12 changes: 11 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@ type Config struct {
TelegramChatID int64
SleepTime int
FrigateExternalURL string
RedisAddr string
RedisPassword string
RedisDB int
RedisProtocol int
RedisTTL int
}

// New returns a new Config struct
func New() *Config {
return &Config{
TelegramBotToken: getEnv("TELEGRAM_BOT_TOKEN", ""),
FrigateURL: getEnv("FRIGATE_URL", "http://localhost:5000"),
FrigateEventLimit: getEnvAsInt("FRIGATE_EVENT_LIMIT", 3),
FrigateEventLimit: getEnvAsInt("FRIGATE_EVENT_LIMIT", 50),
Debug: getEnvAsBool("DEBUG", false),
TelegramChatID: getEnvAsInt64("TELEGRAM_CHAT_ID", 0),
SleepTime: getEnvAsInt("SLEEP_TIME", 5),
FrigateExternalURL: getEnv("FRIGATE_EXTERNAL_URL", "http://localhost:5000"),
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
RedisPassword: getEnv("REDIS_PASSWORD", ""),
RedisDB: getEnvAsInt("REDIS_DB", 0),
RedisProtocol: getEnvAsInt("REDIS_PROTOCOL", 3),
RedisTTL: getEnvAsInt("REDIS_TTL", 604800), // 7 days
}
}

Expand Down
21 changes: 10 additions & 11 deletions internal/frigate/frigate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/oldtyt/frigate-telegram/internal/config"
"github.com/oldtyt/frigate-telegram/internal/log"
"github.com/oldtyt/frigate-telegram/internal/redis"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
Expand Down Expand Up @@ -62,7 +63,6 @@ type EventStruct struct {

var Events EventsStruct
var Event EventStruct
var LastStartTime float64 = 1

func ErrorSend(TextError string, bot *tgbotapi.BotAPI) {
conf := config.New()
Expand Down Expand Up @@ -100,9 +100,6 @@ func GetEvents(FrigateURL string, bot *tgbotapi.BotAPI) EventsStruct {
conf := config.New()

FrigateURL = FrigateURL + "?limit=" + strconv.Itoa(conf.FrigateEventLimit)
if LastStartTime > 0 {
FrigateURL = FrigateURL + fmt.Sprintf("&after=%f", LastStartTime)
}

log.Debug.Println("Geting events from Frigate via URL: " + FrigateURL)

Expand Down Expand Up @@ -228,18 +225,20 @@ func SendMessageEvent(FrigateEvent EventStruct, bot *tgbotapi.BotAPI) {
if messages == nil {
ErrorSend("No received messages", bot)
}
var State string
State = "InProgress"
if FrigateEvent.EndTime != 0 {
State = "Finished"
}
redis.AddNewEvent(FrigateEvent.ID, State)
}

func ParseEvents(FrigateEvents EventsStruct, bot *tgbotapi.BotAPI) {
// Parse events
for Event := range FrigateEvents {
log.Info.Println("Found new event. ID - ", FrigateEvents[Event].ID)
if LastStartTime == 0 {
LastStartTime = FrigateEvents[Event].StartTime
}
if FrigateEvents[Event].StartTime > LastStartTime && FrigateEvents[Event].EndTime != 0 {
LastStartTime = FrigateEvents[Event].StartTime
if redis.CheckEvent(FrigateEvents[Event].ID) {
log.Info.Println("Found new event. ID - ", FrigateEvents[Event].ID)
go SendMessageEvent(FrigateEvents[Event], bot)
}
SendMessageEvent(FrigateEvents[Event], bot)
}
}
45 changes: 45 additions & 0 deletions internal/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package redis

import (
"context"
"time"

"github.com/oldtyt/frigate-telegram/internal/config"
"github.com/oldtyt/frigate-telegram/internal/log"
redis "github.com/redis/go-redis/v9"
)

var ctx = context.Background()
var conf = config.New()
var rdb = redis.NewClient(&redis.Options{
Addr: conf.RedisAddr,
Password: conf.RedisPassword, // no password set
DB: conf.RedisDB, // use default DB
Protocol: conf.RedisProtocol, // specify 2 for RESP 2 or 3 for RESP 3
})

func AddNewEvent(EventID string, State string) {
RedisTTL := time.Duration(conf.RedisTTL) * time.Second
err := rdb.Set(ctx, EventID, State, RedisTTL).Err()
if err != nil {
log.Error.Fatalln(err)
}
}

func CheckEvent(EventID string) bool {
event, err := rdb.Exists(ctx, EventID).Result()
if err != nil {
log.Error.Fatalln(err)
}
if event == 0 {
return true
}
val, err := rdb.Get(ctx, EventID).Result()
if err != nil {
log.Error.Fatalln(err)
}
if val == "InProgress" {
return true
}
return false
}

0 comments on commit 31360e3

Please sign in to comment.