api

package
v0.1.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package api provides functionality to interact with smart home devices connected to F&Home system.

Index

Constants

View Source
const (
	ActionOpenClientSession          = "open_client_session"
	ActionGetMyData                  = "get_my_data"
	ActionGetMyResources             = "get_my_resources"
	ActionOpenClienToResourceSession = "open_client_to_resource_session"
	ActionGetSystemConfig            = "touches"
	ActionGetUserConfig              = "get_user_config"
	ActionEvent                      = "xevent"
	ActionStatusTouchesChanged       = "statustoucheschanged"
)
View Source
const APIURL = "wss://fhome.cloud/webapp-interface/"

APIURL is a URL at which F&Home API lives.

It has to end with a trailing slash, otherwise handshake fails.

Variables

View Source
var Dialer = websocket.Dialer{
	EnableCompression: true,
	HandshakeTimeout:  5 * time.Second,
}

Dialer is used to create a websocket connection.

View Source
var ValueToggle = "0x4001"

Functions

func DecodeTemperatureValue

func DecodeTemperatureValue(value string) (float64, error)

DecodeTemperatureValue converts hex string to float64 representing temperature in °C.

Examples:

* "0xa005" -> 0.5

* "0xa078" -> 12.0

* "0xa118" -> 28.0

func DecodeTemperatureValueStr

func DecodeTemperatureValueStr(value string) (float64, error)

DecodeTemperatureValueStr converts °C in string to float64.

Example:

"24,0°C" -> 24

func EncodeTemperature

func EncodeTemperature(value float64) string

EncodeTemperature encodes value to represent temperature that is ready to be passed to Client.SendEvent.

Examples of the process:

* 12 -> 41080 + 12 * 10 -> 41080 -> "0xa078"

* 25 -> 41080 + 25 * 10 -> 41210 -> "0xa0fa"

* 28 -> 41080 + 28 * 10 -> 41240 -> "0xa118"

func MapLighting

func MapLighting(value int) string

MapLighting maps value to a string that is ready to be passed to Client.SendEvent.

Clamps if the value is too small or too big.

func Pprint

func Pprint(i interface{}) string

Pprint pretty prints i.

func RemapLighting

func RemapLighting(value string) (int, error)

Types

type Action

type Action struct {
	ActionName   string `json:"action_name"`
	Login        string `json:"login"`
	PasswordHash string `json:"password"`
	RequestToken string `json:"request_token"`
}

type Cell

type Cell struct {
	ID   int
	Icon Icon
	Name string // Set in client apps
	Desc string // Set in the configurator app

	Value       string
	TypeNumber  string
	DisplayType string
	Preset      string
	Style       string
	MinValue    string
	MaxValue    string
}

type CellValue

type CellValue struct {
	ID          string      `json:"VOI"`
	Ii          string      `json:"II"`
	DisplayType DisplayType `json:"DT"`
	Value       string      `json:"DV"`  // Probably "data value"
	ValueStr    string      `json:"DVS"` // Probably "data value string"
}

func (*CellValue) IntID

func (cv *CellValue) IntID() int

func (CellValue) String

func (cv CellValue) String() string

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient() (*Client, error)

NewClient creates a new client and automatically starts connecting to websockets.

func (*Client) Close

func (c *Client) Close() error

func (*Client) GetMyResources

func (c *Client) GetMyResources() (*GetMyResourcesResponse, error)

GetMyResources gets resources assigned to the user.

Most of the time, there will be just one resource. Currently we handle only this case and assign its unique ID on the client.

func (*Client) GetSystemConfig

func (c *Client) GetSystemConfig() (*TouchesResponse, error)

GetSystemConfig returns additional information about particular cells, e.g their style (icon) and configurator-set name.

Configuration returned by this method is set in the desktop configurator app.

This action is named "Touches" in F&Home's terminology.

func (*Client) GetUserConfig

func (c *Client) GetUserConfig() (*UserConfig, error)

GetUserConfig returns configuration of cells and panels.

Configuration returned by this method is set in the web or mobile app.

func (*Client) OpenCloudSession

func (c *Client) OpenCloudSession(email, password string) error

OpenCloudSession opens a websocket connection to F&Home Cloud.

func (*Client) OpenResourceSession

func (c *Client) OpenResourceSession(resourcePassword string) error

OpenResourceSession open a websocket connection to the only resource that the user has.

Currently, it assumes that a user has only one resource.

func (*Client) ReadAnyMessage

func (c *Client) ReadAnyMessage() (*Message, error)

ReadAnyMessage returns any message received from the server.

If the message has status and it is not ok, it returns an error.

func (*Client) ReadMessage

func (c *Client) ReadMessage(actionName string, requestToken string) (*Message, error)

ReadMessage waits until the client receives message with matching actionName and requestToken.

If requestToken is empty, then it is ignored. In such case, the first message with matching actionName is returned.

If its status is not "ok", it returns an error.

func (*Client) SendEvent

func (c *Client) SendEvent(cellID int, value string) error

SendEvent sends an event containing value to the cell.

Events are named "Xevents" in F&Home's terminology.

type Config

type Config struct {
	Panels []Panel
}

Config merges UserConfig and TouchesResponse.

func MergeConfigs

func MergeConfigs(userConfig *UserConfig, touchesResp *TouchesResponse) (*Config, error)

MergeConfigs creates Config config from "get_user_config" action and "get_system_config" action.

func (*Config) Cells

func (c *Config) Cells() []Cell

func (*Config) GetCellByID

func (c *Config) GetCellByID(cellID int) (*Cell, error)

func (*Config) GetPanelByID

func (c *Config) GetPanelByID(id string) (*Panel, error)

func (*Config) GetPanelByName

func (c *Config) GetPanelByName(name string) (*Panel, error)

type DisplayType

type DisplayType string
const (
	Bit         DisplayType = "BIT"
	Byte        DisplayType = "BYTE"
	Temperature DisplayType = "TEMP"
	Percentage  DisplayType = "PROC"
	RGB         DisplayType = "RGB"
)

type Event added in v0.0.2

type Event struct {
	ActionName   string `json:"action_name"`
	Login        string `json:"login"`
	PasswordHash string `json:"password"`
	RequestToken string `json:"request_token"`
	CellID       string `json:"cell_id"`
	Value        string `json:"value"`
	Type         string `json:"type"`
}

type GetMyResources

type GetMyResources struct {
	ActionName   string `json:"action_name"`
	Email        string `json:"email"`
	RequestToken string `json:"request_token"`
}

type GetMyResourcesResponse

type GetMyResourcesResponse struct {
	ActionName    string `json:"action_name"`
	RequestToken  string `json:"request_token"`
	Status        string `json:"status"`
	Source        string `json:"source"`
	AvatarID0     string `json:"avatar_id_0"`
	FriendlyName0 string `json:"friendly_name_0"`
	ResourceType0 string `json:"resource_type_0"`
	UniqueID0     string `json:"unique_id_0"`
}

type GetUserConfigResponse

type GetUserConfigResponse struct {
	ActionName   string `json:"action_name"`
	RequestToken string `json:"request_token"`
	Status       string `json:"status"`
	Source       string `json:"source"`
	File         string `json:"file"`
}

type Icon

type Icon string
const (
	IconHeating     Icon = "icon_cell_717_white"
	IconLighting    Icon = "icon_cell_710_white"
	IconTemperature Icon = "icon_cell_706_white"
	IconNumber      Icon = "icon_cell_707_white"
	IconGate        Icon = "icon_cell_724_white"
	IconUnknown     Icon = "unknown"
)

func CreateIcon

func CreateIcon(icon string) Icon

type Message

type Message struct {
	ActionName   string  `json:"action_name"`
	RequestToken *string `json:"request_token"`
	Status       *string `json:"status"`
	Raw          []byte
}

Message is a websocket message sent from the client to the server.

Messages are most commonly used to make server do perform somehing, e.g change the status of some resource/device.

type MobileDisplayCell

type MobileDisplayCell struct {
	// Cell description. Note that this is by the configurator app, not by the
	// user in the mobile or web app.
	Desc string `json:"CD"`
	// Object ID
	ID string `json:"OI"`
	// Type number. Known values: 706, 707, 708, 709, 710, 711, 717, 718, 719,
	// 722, 724, 760
	TypeNumber string `json:"TN"`
	// Preset. Known values: 0, 1, 4
	Preset string `json:"P"`
	// Style. Display Type TEMP always has this set to 2.
	Style string `json:"Se"`
	// Minimum value
	MinValue string `json:"Min"`
	// Maximum value
	MaxValue string `json:"Max"`
	// Step (aka current value). Display Type TEMP always has this set to
	// 0xa005.
	Step string `json:"Sp"`
	// Display Type.
	DisplayType DisplayType `json:"DT"`
	// Cell permission. Known values: FC (Full Control), RO (Read Only)
	Permission string `json:"CP"`
}

MobileDisplayCell is a Cell, but returned from "touches" action.

func (MobileDisplayCell) String

func (cell MobileDisplayCell) String() string

type OpenClientSession

type OpenClientSession struct {
	ActionName   string `json:"action_name"`
	Email        string `json:"email"`
	Password     string `json:"password"`
	RequestToken string `json:"request_token"`
}

type OpenClientToResourceSession

type OpenClientToResourceSession struct {
	ActionName   string `json:"action_name"`
	Email        string `json:"email"`
	UniqueID     string `json:"unique_id"`
	RequestToken string `json:"request_token"`
}

type Panel

type Panel struct {
	ID    string
	Name  string
	Cells []Cell
}

func (*Panel) GetCellByID

func (p *Panel) GetCellByID(cellID int) (*Cell, error)

type PositionInPanel

type PositionInPanel struct {
	Orientation string `json:"orientation"`
	PanelID     string `json:"panelId"`
	X           int    `json:"x"`
	Y           int    `json:"y"`
}

func (PositionInPanel) String

func (p PositionInPanel) String() string

type Response

type Response struct {
	ActionName   string `json:"action_name"`
	RequestToken string `json:"request_token"`
	Status       string `json:"status"`
	Source       string `json:"source"`

	Details string `json:"details"` // Non-empty for "disconnecting" action
	Reason  string `json:"reason"`  // Non-empty for "disconnecting" action
}

Response is a websocket message sent from the server to the client in response to the client's previous websocket message to the server.

type StatusTouchesChangedResponse

type StatusTouchesChangedResponse struct {
	ActionName string `json:"action_name"`
	Response   struct {
		ProjectVersion string      `json:"ProjectVersion"`
		Status         bool        `json:"Status"`
		StatusText     string      `json:"StatusText"`
		CellValues     []CellValue `json:"CV"`
		ServerTime     int         `json:"ServerTime"`
	} `json:"response"`
	Status string `json:"status"`
	Source string `json:"source"`
}

type TouchesResponse

type TouchesResponse struct {
	ActionName string `json:"action_name"`
	Response   struct {
		ProjectVersion          string `json:"ProjectVersion"`
		Status                  bool   `json:"Status"`
		StatusText              string `json:"StatusText"`
		MobileDisplayProperties struct {
			Cells []MobileDisplayCell `json:"Cells"`
		} `json:"MobileDisplayProperties"`
	} `json:"response"`
	Status       string `json:"status"`
	Source       string `json:"source"`
	RequestToken string `json:"request_token"`
}

type UserCell

type UserCell struct {
	ObjectID        int               `json:"objectId"`
	Icon            string            `json:"icon"`
	Name            string            `json:"name"`
	PositionInPanel []PositionInPanel `json:"positionInPanel"`
}

func (*UserCell) IconName

func (cell *UserCell) IconName() string

type UserConfig

type UserConfig struct {
	Cells  []UserCell  `json:"cells"`
	Panels []UserPanel `json:"panels"`
	Server struct {
		ProjectVersion string `json:"projectVersion"`
	} `json:"server"`
}

func (*UserConfig) GetCellsByPanelID

func (f *UserConfig) GetCellsByPanelID(id string) []UserCell

type UserPanel

type UserPanel struct {
	ID                   string      `json:"id"`
	Name                 string      `json:"name"`
	X                    int         `json:"x"`
	Y                    int         `json:"y"`
	Icon                 interface{} `json:"icon"`
	ColumnCountPortrait  int         `json:"columnCountPortrait"`
	ColumnCountLandscape int         `json:"columnCountLandscape"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL