tiko/room.go
2024-11-28 22:44:31 +01:00

173 lines
5.2 KiB
Go

package tiko
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type RoomMode string
const (
RoomModeBoost = RoomMode("boost")
RoomModeFalse = RoomMode("false")
RoomModeAway = RoomMode("absence")
RoomModeFrost = RoomMode("frost")
RoomModeDisable = RoomMode("disableHeating")
)
type Room struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type int `json:"type"`
Color string `json:"color"`
Heaters int `json:"heaters"`
CurrentTemperature float64 `json:"currentTemperatureDegrees"`
TargetTemperature float64 `json:"targetTemperatureDegrees"`
Humidity *int `json:"humidity"`
Sensors int `json:"sensors"`
Mode struct {
Boost bool `json:"boost"`
Absence bool `json:"absence"`
Frost bool `json:"frost"`
DisableHeating bool `json:"disableHeating"`
TypeName string `json:"__typename"`
} `json:"mode"`
Status struct {
Disconnected bool `json:"disconnected"`
// HeaterDisconnected bool `json:"heaterDisconnected"`
HeatingOperating bool `json:"heatingOperating"`
SensorBatteryLow bool `json:"sensorBatteryLow"`
SensorDisconnected bool `json:"sensorDisconnected"`
// TemporaryAdjustment bool `json:"temporaryAdjustment"`
HeatersRegulated bool `json:"heatersRegulated"`
HeaterCalibrationState [][2]interface{} `json:"heaterCalibrationState"`
TypeName string `json:"__typename"`
} `json:"status"`
TypeName string `json:"__typename"`
}
type PropertyRoom struct {
Room Room `json:"room"`
}
type PropertyRoomResp struct {
Property PropertyRoom `json:"property"`
}
func (c *Client) GetRoom(propertyID, roomID int64) (*Room, error) {
if err := c.Init(); err != nil {
return nil, err
}
// Créer les données de la requête
requestData := map[string]interface{}{
"operationName": "GET_PROPERTY_MODE_AND_ROOM",
"variables": map[string]int64{
"propertyId": propertyID,
"roomId": roomID,
},
"query": "query GET_PROPERTY_MODE_AND_ROOM($propertyId: Int!, $roomId: Int!) { property(id: $propertyId) { id mode room(id: $roomId) { id name type color heaters currentTemperatureDegrees targetTemperatureDegrees humidity sensors mode { boost absence frost disableHeating __typename } status { disconnected heaterDisconnected heatingOperating sensorBatteryLow sensorDisconnected temporaryAdjustment heatersRegulated heaterCalibrationState __typename } __typename } __typename } }",
}
// Convertir les données en JSON
requestDataJSON, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
// Créer la requête HTTP
req, err := http.NewRequest(http.MethodPost, graphqlAPIURL, bytes.NewBuffer(requestDataJSON))
if err != nil {
return nil, err
}
resp, err := Do[ApiResp[PropertyRoomResp]](c, req)
if err != nil {
return nil, err
}
return &resp.Data.Property.Room, nil
}
func (c *Client) SetRoomMode(propertyID, roomID int64, mode RoomMode) (*Room, error) {
if err := c.Init(); err != nil {
return nil, err
}
// Créer les données de la requête
requestData := map[string]interface{}{
"operationName": "SET_ROOM_MODE",
"variables": map[string]interface{}{
"propertyId": propertyID,
"roomId": roomID,
"mode": mode,
},
"query": "mutation SET_ROOM_MODE($propertyId: Int!, $roomId: Int!, $mode: String!) { setRoomMode(input: {propertyId: $propertyId, roomId: $roomId, mode: $mode}) { id mode { boost absence frost disableHeating __typename } __typename } }",
}
// Convertir les données en JSON
requestDataJSON, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
// Créer la requête HTTP
req, err := http.NewRequest(http.MethodPost, graphqlAPIURL, bytes.NewBuffer(requestDataJSON))
if err != nil {
return nil, err
}
resp, err := Do[map[string]interface{}](c, req)
if err != nil {
return nil, err
}
fmt.Println("SetRoomMode Resp", resp)
return c.GetRoom(propertyID, roomID)
}
func (c *Client) SetRoomTemperature(propertyID, roomID int64, temp float32) (*Room, error) {
if err := c.Init(); err != nil {
return nil, err
}
if temp <= 7 || temp >= 25 {
return nil, errors.New("temp is out of bounds")
}
// Créer les données de la requête
requestData := map[string]interface{}{
"operationName": "SET_PROPERTY_ROOM_ADJUST_TEMPERATURE",
"variables": map[string]interface{}{
"propertyId": propertyID,
"roomId": roomID,
"temperature": temp,
},
"query": "mutation SET_PROPERTY_ROOM_ADJUST_TEMPERATURE($propertyId: Int!, $roomId: Int!, $temperature: Float!) { setRoomAdjustTemperature(input: {propertyId: $propertyId, roomId: $roomId, temperature: $temperature}) { id adjustTemperature { active endDateTime temperature __typename } __typename } }",
}
// Convertir les données en JSON
requestDataJSON, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
// Créer la requête HTTP
req, err := http.NewRequest(http.MethodPost, graphqlAPIURL, bytes.NewBuffer(requestDataJSON))
if err != nil {
return nil, err
}
resp, err := Do[map[string]interface{}](c, req)
if err != nil {
return nil, err
}
fmt.Println("SetRoomTemperature Resp", resp)
return c.GetRoom(propertyID, roomID)
}