2024-11-28 13:52:58 +01:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProcesarMensaje(update tgbotapi.Update, db *gorm.DB) {
|
|
|
|
if update.Message == nil || update.Message.Text == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Fields(update.Message.Text)
|
|
|
|
command := strings.ToUpper(parts[0])
|
|
|
|
var timestamp time.Time
|
|
|
|
|
2024-11-28 14:32:23 +01:00
|
|
|
if command != "ON" && command != "OFF" {
|
|
|
|
log.Println("Comando desconocido:", command)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-04 20:44:16 +01:00
|
|
|
loc, _ := time.LoadLocation("CET")
|
|
|
|
now := time.Now().In(loc)
|
2024-11-28 14:32:23 +01:00
|
|
|
if len(parts) == 1 {
|
|
|
|
// Sin fecha ni hora -> usar timestamp actual
|
|
|
|
timestamp = now
|
|
|
|
} else {
|
|
|
|
// Intentar parsear fecha y hora
|
|
|
|
var parsedDate time.Time
|
|
|
|
var parsedTime time.Time
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for i := 1; i < len(parts); i++ {
|
|
|
|
if parsedDate.IsZero() {
|
|
|
|
parsedDate, err = time.Parse("2006-01-02", parts[i])
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if parsedTime.IsZero() {
|
|
|
|
parsedTime, err = time.Parse("15:04", parts[i])
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parsedDate.IsZero() && !parsedTime.IsZero() {
|
|
|
|
// Ambos fecha y hora proporcionados
|
|
|
|
timestamp = time.Date(
|
|
|
|
parsedDate.Year(), parsedDate.Month(), parsedDate.Day(),
|
2024-12-04 20:44:16 +01:00
|
|
|
parsedTime.Hour(), parsedTime.Minute(), 0, 0, loc,
|
2024-11-28 14:32:23 +01:00
|
|
|
)
|
|
|
|
} else if !parsedDate.IsZero() {
|
|
|
|
// Solo fecha proporcionada -> usar las 00:00 como hora
|
|
|
|
timestamp = time.Date(
|
|
|
|
parsedDate.Year(), parsedDate.Month(), parsedDate.Day(),
|
2024-12-04 20:44:16 +01:00
|
|
|
0, 0, 0, 0, loc,
|
2024-11-28 14:32:23 +01:00
|
|
|
)
|
|
|
|
} else if !parsedTime.IsZero() {
|
|
|
|
// Solo hora proporcionada -> usar fecha actual
|
|
|
|
timestamp = time.Date(
|
|
|
|
now.Year(), now.Month(), now.Day(),
|
2024-12-04 20:44:16 +01:00
|
|
|
parsedTime.Hour(), parsedTime.Minute(), 0, 0, loc,
|
2024-11-28 14:32:23 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
log.Println("Formato de entrada inválido:", parts[1:])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event := HeatingEvent{
|
|
|
|
EventType: command,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
if err := db.Create(&event).Error; err != nil {
|
|
|
|
log.Printf("Error al guardar el evento %s: %v", command, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Evento %s registrado con éxito para el %s", command, timestamp.Format("2006-01-02 15:04"))
|
|
|
|
}
|