golang实现Viessmann Vitotrol网络服务客户端功能的插件库go-vitotrol的使用

Golang实现Viessmann Vitotrol网络服务客户端功能的插件库go-vitotrol的使用

概述

go-vitotrol是一个提供访问Viessmann™ Vitotrol™云API的Golang库,用于控制和监控锅炉设备。

Build Status Coverage Status Go Report Card GoDoc

安装

vitotrol命令行工具

要获取vitotrol可执行文件:

Go 1.18及以上版本

go install github.com/maxatome/go-vitotrol/cmd/vitotrol@master

Go 1.18之前版本

go get -u github.com/maxatome/go-vitotrol/cmd/vitotrol

库安装

go get -u github.com/maxatome/go-vitotrol

注意:从Go 1.18开始,-u参数变得无用。

功能

当前实现了以下API请求:

  • 登录(Login)
  • 获取设备列表(GetDevices)
  • 请求刷新状态(RequestRefreshStatus)
  • 请求写入状态(RequestWriteStatus)
  • 获取数据(GetData)
  • 写入数据(WriteData)
  • 刷新数据(RefreshData)
  • 获取错误历史(GetErrorHistory)
  • 获取时间表(GetTimesheet)
  • 写入时间表数据(WriteTimesheetData)
  • 获取类型信息(GetTypeInfo)

使用示例

命令行工具使用

usage: vitotrol [OPTIONS] ACTION [PARAMS]
  -config string
        login+password config file
  -debug
        print debug information
  -device string
        DeviceID, index, DeviceName, DeviceId@LocationID, DeviceName@LocationName (see `devices' action) (default "0")
  -json
        used by `timesheet' action to display timesheets using JSON format
  -login string
        login on vitotrol API
  -password string
        password on vitotrol API
  -verbose
        print verbose information

ACTION & PARAMS can be:
- devices              list all available devices
- list [attrs|timesheets]  list attribute (default) or timesheet names
- get ATTR_NAME ...    get the value of attributes ATTR_NAME, ... on vitodata
                         server
- get all              get all known attributes on vitodata server
- rget ATTR_NAME ...   refresh then get the value of attributes ATTR_NAME, ...
                         on vitodata server
- rget all             refresh than get all known attributes on vitodata server
- bget ATTR_IDX ...    get the value of attributes ATTR_IDX, ... on vitodata
                         server without checking their validity before (for
                         developing purpose)
- rbget ATTR_IDX ...   refresh then get the value of attributes ATTR_IDX, ...
                         on vitodata server without checking their validity
                         before (for developing purpose)
- set ATTR_NAME VALUE  set the value of attribute ATTR_NAME to VALUE
- timesheet TIMESHEET ...
                       get the timesheet TIMESHEET data
- set_timesheet TIMESHEET '{"wday":[{"from":630,"to":2200},...],...}'
                       replace the whole timesheet TIMESHEET
                       wday is either a day (eg. mon) or a range of days
                       (eg. mon-wed or sat-mon)
                       The JSON content can be in a file with the syntax @file
- errors               get the error history
- remote_attrs         list server available attributes
                         (for developing purpose)

配置文件

默认配置文件位于$HOME/.vitotrol-api,格式如下:

LOGIN
PASSWORD

代码示例

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/maxatome/go-vitotrol"
)

func main() {
	// 创建客户端
	client := vitotrol.NewClient(nil)

	// 从环境变量获取凭证
	login := os.Getenv("VITOTROL_LOGIN")
	password := os.Getenv("VITOTROL_PASSWORD")

	// 登录
	err := client.Auth(login, password)
	if err != nil {
		log.Fatalf("登录失败: %v", err)
	}

	// 获取设备列表
	devices, err := client.GetDevices()
	if err != nil {
		log.Fatalf("获取设备列表失败: %v", err)
	}

	// 打印设备信息
	for i, dev := range devices {
		fmt.Printf("设备 %d: %+v\n", i, dev)
	}

	// 选择第一个设备
	if len(devices) == 0 {
		log.Fatal("没有找到设备")
	}
	device := devices[0]

	// 获取设备属性
	attrs, err := client.GetData(device, nil)
	if err != nil {
		log.Fatalf("获取属性失败: %v", err)
	}

	// 打印属性
	for _, attr := range attrs {
		fmt.Printf("%s: %v (%s)\n", attr.Name, attr.Value, attr.Unit)
	}
}

许可证

go-vitotrol采用MIT许可证发布。

其他

想要自动将属性值导入Influx数据库?可以参考vitotrol2influx


更多关于golang实现Viessmann Vitotrol网络服务客户端功能的插件库go-vitotrol的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现Viessmann Vitotrol网络服务客户端功能的插件库go-vitotrol的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


go-vitotrol: Golang实现的Viessmann Vitotrol网络服务客户端

go-vitotrol是一个用于与Viessmann Vitotrol网络服务交互的Golang库,它允许开发者通过API访问和控制Viessmann供暖系统。

主要功能

  • 与Viessmann Vitotrol网络服务通信
  • 获取设备状态信息
  • 控制供暖系统参数
  • 支持认证和会话管理

安装

go get github.com/koestler/go-vitotrol

基本使用示例

1. 创建客户端并认证

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/koestler/go-vitotrol"
)

func main() {
	// 创建配置
	config := vitotrol.Config{
		Username: "your_username",
		Password: "your_password",
		Timeout:  30 * time.Second,
	}

	// 创建客户端
	client, err := vitotrol.NewClient(config)
	if err != nil {
		log.Fatalf("创建客户端失败: %v", err)
	}
	defer client.Close()

	// 创建上下文
	ctx := context.Background()

	// 认证
	if err := client.Authenticate(ctx); err != nil {
		log.Fatalf("认证失败: %v", err)
	}

	fmt.Println("成功连接到Vitotrol服务")
}

2. 获取设备列表

// 获取设备列表
devices, err := client.GetDevices(ctx)
if err != nil {
	log.Fatalf("获取设备列表失败: %v", err)
}

for _, device := range devices {
	fmt.Printf("设备ID: %s, 名称: %s, 类型: %s\n", 
		device.Id, device.Name, device.Type)
}

3. 读取设备数据

if len(devices) == 0 {
	log.Fatal("没有可用的设备")
}

// 使用第一个设备
device := devices[0]

// 获取设备属性
attrs, err := client.GetAttributes(ctx, device.Id)
if err != nil {
	log.Fatalf("获取属性失败: %v", err)
}

for _, attr := range attrs {
	fmt.Printf("属性: %s, 值: %v, 单位: %s\n", 
		attr.Name, attr.Value, attr.Unit)
}

4. 设置设备参数

// 设置目标温度
err = client.SetAttribute(ctx, device.Id, "HeatingTargetTemperature", 21.0)
if err != nil {
	log.Fatalf("设置温度失败: %v", err)
}

fmt.Println("目标温度设置成功")

高级功能

1. 使用WebSocket实时更新

// 创建WebSocket连接
ws, err := client.NewWebSocket(ctx, device.Id)
if err != nil {
	log.Fatalf("创建WebSocket连接失败: %v", err)
}
defer ws.Close()

// 处理实时更新
go func() {
	for {
		update, err := ws.ReadUpdate()
		if err != nil {
			log.Printf("读取更新错误: %v", err)
			return
		}
		fmt.Printf("实时更新 - 属性: %s, 新值: %v\n", 
			update.Name, update.Value)
	}
}()

// 保持连接一段时间
time.Sleep(5 * time.Minute)

2. 错误处理

// 带重试的认证
maxRetries := 3
var authErr error

for i := 0; i < maxRetries; i++ {
	authErr = client.Authenticate(ctx)
	if authErr == nil {
		break
	}
	log.Printf("认证尝试 %d 失败: %v", i+1, authErr)
	time.Sleep(2 * time.Second)
}

if authErr != nil {
	log.Fatalf("认证失败,尝试 %d 次后放弃", maxRetries)
}

最佳实践

  1. 重用客户端:避免频繁创建和销毁客户端,保持长连接
  2. 合理使用上下文:为长时间运行的操作设置超时
  3. 错误处理:实现适当的重试机制
  4. 资源清理:确保关闭客户端和WebSocket连接

注意事项

  • 确保遵守Viessmann API的使用条款
  • 避免过于频繁的请求,以免触发速率限制
  • 处理网络不稳定的情况
  • 保护认证信息的安全

这个库为Golang开发者提供了访问Viessmann供暖系统的便捷方式,可以集成到智能家居系统或监控应用中。

回到顶部