relaysdk

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

relaySDK

继电器客户端SDK

Example

package main

import (
	"github.com/Atian-OE/RelaySDK_Golang"
    "log"
    "sync"
    "testing"
    "time"
)
func main()  {
	   c := relaysdk.NewSDKClient("192.168.0.112").
       		SetReconnectTime(5).
       		SetReconnectTimes(5).
       		OnTimeout(func(c *relaysdk.Client) {
       			log.Println("连接超时........")
       			c.SetAddress("192.168.0.113").OnConnected(func(c *relaysdk.Client) {
       				c.RelayCloseAll()
       				c.RelayOpenAll()
       			}).OnTimeout(func(c *relaysdk.Client) {
       				c.SetAddress("192.168.0.111").OnConnected(func(c *relaysdk.Client) {
       					log.Println("连接成功......开始逻辑处理......")
       					c.RelayCloseAll()
       					time.Sleep(time.Second*3)
       					c.RelayOpenAll()
       					time.Sleep(time.Second*3)
       					c.RelayCloseAll()
       					time.Sleep(time.Second*5)
       					c.Close()
       				})
       			})
       		}).OnConnected(func(c *relaysdk.Client) {
       	})
       	log.Println(c.Id())
       	time.Sleep(time.Minute/2)
       	log.Println("单条测试关闭.....")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SendErr = func(err error) error {
		return errors.New(fmt.Sprintf("客户端发送消息失败:%s", err.Error()))
	}
)

Functions

func ByteToInt

func ByteToInt(b []byte) int

func Encode

func Encode(msgObj interface{}) ([]byte, error)

func IntToBytes

func IntToBytes(n int64, b byte) ([]byte, error)

整形转换成字节

Types

type Client

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

客户端

func NewSDKClient

func NewSDKClient(addr string) *Client

实例化客户端

func (*Client) Address

func (c *Client) Address() string

func (*Client) Close

func (c *Client) Close()

关闭操作

func (*Client) Handle

func (c *Client) Handle(msgId MsgID, data []byte, conn net.Conn)

func (*Client) HeartBeatTime

func (c *Client) HeartBeatTime() time.Duration

func (*Client) Id

func (c *Client) Id() string

func (*Client) IsConnected

func (c *Client) IsConnected() bool

func (*Client) IsReconnecting

func (c *Client) IsReconnecting() bool

func (*Client) OnClose added in v0.0.3

func (c *Client) OnClose(f func(c *Client)) *Client

func (*Client) OnConnected

func (c *Client) OnConnected(f func(c *Client)) *Client

func (*Client) OnConnecting

func (c *Client) OnConnecting(f func(c *Client)) *Client

func (*Client) OnDisconnect

func (c *Client) OnDisconnect(f func(c *Client)) *Client

func (*Client) OnRelayClosed

func (c *Client) OnRelayClosed(f func(data []byte)) *Client

func (*Client) OnRelayOpen

func (c *Client) OnRelayOpen(f func(data []byte)) *Client

func (*Client) OnRelayReset

func (c *Client) OnRelayReset(f func(data []byte)) *Client

func (*Client) OnTimeout

func (c *Client) OnTimeout(f func(c *Client)) *Client

func (*Client) Port

func (c *Client) Port() int

func (*Client) ReconnectTime

func (c *Client) ReconnectTime() time.Duration

func (*Client) ReconnectTimes

func (c *Client) ReconnectTimes() int

func (*Client) RelayCloseAll

func (c *Client) RelayCloseAll()

断开所有继电器

func (*Client) RelayClosed

func (c *Client) RelayClosed(relay []bool)

关闭继电器

func (*Client) RelayOpen

func (c *Client) RelayOpen(relay []bool)

打开继电器

func (*Client) RelayOpenAll

func (c *Client) RelayOpenAll()

闭合所有继电器

func (*Client) RelayReset

func (c *Client) RelayReset()

重置继电器

func (*Client) Send

func (c *Client) Send(msg interface{}) error

发送数据

func (*Client) SetAddress

func (c *Client) SetAddress(address string) *Client

func (*Client) SetId

func (c *Client) SetId(id string) *Client

func (*Client) SetPort

func (c *Client) SetPort(port int) *Client

func (*Client) SetReconnectTime

func (c *Client) SetReconnectTime(reconnectTime time.Duration) *Client

func (*Client) SetReconnectTimes

func (c *Client) SetReconnectTimes(reconnectTimes int) *Client

type CloseMessage

type CloseMessage struct {
	Success bool
	Err     string
}

关闭继电器响应参数

type CloseMessageRequest

type CloseMessageRequest struct {
	Relay []bool
}

关闭继电器请求参数

type DisconnectedMessage

type DisconnectedMessage struct {
	Success bool
	Err     string
}

type HeartBeat

type HeartBeat struct {
}

心跳

type IllegalMessage

type IllegalMessage struct {
	Success bool
	Err     string
}

type MsgID

type MsgID int32
const (
	ConnectID    MsgID = 0   //连接
	DisconnectID MsgID = 1   //关闭连接
	Open         MsgID = 2   //打开继电器
	Close        MsgID = 3   //关闭继电器
	Reset        MsgID = 4   //重置继电器
	HeartBeatID  MsgID = 250 //心跳
	Illegal      MsgID = 255 //非法请求
)

type OpenMessage

type OpenMessage struct {
	Success bool
	Err     string
}

打开继电器响应参数

type OpenMessageRequest

type OpenMessageRequest struct {
	Relay []bool
}

打开继电器请求参数

type ResetMessage

type ResetMessage struct {
	Success bool
	Err     string
}

重置继电器响应参数

type ResetMessageRequest

type ResetMessageRequest struct {
}

重置继电器请求参数

Jump to

Keyboard shortcuts

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