Tuya.Net
Public Member Functions | Properties
com.clusterrr.TuyaNet.TuyaDevice Class Reference

Connection with Tuya device. More...

Inheritance diagram for com.clusterrr.TuyaNet.TuyaDevice:
Inheritance graph
[legend]

Public Member Functions

 TuyaDevice (string ip, string localKey, string deviceId, TuyaProtocolVersion protocolVersion=TuyaProtocolVersion.V33, int port=6668, int receiveTimeout=250)
 Creates a new instance of the TuyaDevice class. More...
 
 TuyaDevice (string ip, TuyaApi.Region region, string accessId, string apiSecret, string deviceId, TuyaProtocolVersion protocolVersion=TuyaProtocolVersion.V33, int port=6668, int receiveTimeout=250)
 Creates a new instance of the TuyaDevice class. More...
 
string FillJson (string json, bool addGwId=true, bool addDevId=true, bool addUid=true, bool addTime=true)
 Fills JSON string with base fields required by most commands. More...
 
byte[] EncodeRequest (TuyaCommand command, string json)
 Creates encoded and encrypted payload data from JSON string. More...
 
TuyaLocalResponse DecodeResponse (byte[] data)
 Parses and decrypts payload data from received bytes. More...
 
async Task< TuyaLocalResponseSendAsync (TuyaCommand command, string json, int retries=2, int nullRetries=1, int? overrideRecvTimeout=null, CancellationToken cancellationToken=default)
 Sends JSON string to device and reads response. More...
 
async Task< byte[]> SendAsync (byte[] data, int retries=2, int nullRetries=1, int? overrideRecvTimeout=null, CancellationToken cancellationToken=default)
 Sends raw data over to device and read response. More...
 
async Task< Dictionary< int, object > > GetDpsAsync (int retries=5, int nullRetries=1, int? overrideRecvTimeout=null, CancellationToken cancellationToken=default)
 Requests current DPs status. More...
 
async Task< Dictionary< int, object > > SetDpAsync (int dp, object value, int retries=2, int nullRetries=1, int? overrideRecvTimeout=null, bool allowEmptyResponse=false, CancellationToken cancellationToken=default)
 Sets single DP to specified value. More...
 
async Task< Dictionary< int, object > > SetDpsAsync (Dictionary< int, object > dps, int retries=2, int nullRetries=1, int? overrideRecvTimeout=null, bool allowEmptyResponse=false, CancellationToken cancellationToken=default)
 Sets DPs to specified value. More...
 
async Task< Dictionary< int, object > > UpdateDpsAsync (IEnumerable< int > dpIds, int retries=5, int nullRetries=1, int? overrideRecvTimeout=null, CancellationToken cancellationToken=default)
 Update DP values. More...
 
async Task RefreshLocalKeyAsync (bool forceTokenRefresh=false, CancellationToken cancellationToken=default)
 Get current local key from Tuya Cloud API More...
 
void Dispose ()
 Disposes object. More...
 

Properties

string IP [get]
 IP address of device. More...
 
string LocalKey [get, set]
 Local key of device. More...
 
string DeviceId [get]
 Device ID. More...
 
int Port = 6668 [get]
 TCP port of device. More...
 
TuyaProtocolVersion ProtocolVersion [get, set]
 Protocol version. More...
 
int ConnectionTimeout = 500 [get, set]
 Connection timeout. More...
 
int ReceiveTimeout [get, set]
 Receive timeout. More...
 
int NetworkErrorRetriesInterval = 100 [get, set]
 Network error retry interval (msec) More...
 
int NullRetriesInterval = 0 [get, set]
 Empty responce retry interval (msec) More...
 
bool PermanentConnection = false [get, set]
 Permanent connection (connect and stay connected). More...
 

Detailed Description

Connection with Tuya device.

Constructor & Destructor Documentation

◆ TuyaDevice() [1/2]

com.clusterrr.TuyaNet.TuyaDevice.TuyaDevice ( string  ip,
string  localKey,
string  deviceId,
TuyaProtocolVersion  protocolVersion = TuyaProtocolVersion.V33,
int  port = 6668,
int  receiveTimeout = 250 
)
inline

Creates a new instance of the TuyaDevice class.

Parameters
ipIP address of device.
localKeyLocal key of device (obtained via API).
deviceIdDevice ID.
protocolVersionProtocol version.
portTCP port of device.
receiveTimeoutReceive timeout (msec).

◆ TuyaDevice() [2/2]

com.clusterrr.TuyaNet.TuyaDevice.TuyaDevice ( string  ip,
TuyaApi.Region  region,
string  accessId,
string  apiSecret,
string  deviceId,
TuyaProtocolVersion  protocolVersion = TuyaProtocolVersion.V33,
int  port = 6668,
int  receiveTimeout = 250 
)
inline

Creates a new instance of the TuyaDevice class.

Parameters
ipIP address of device.
regionRegion to access Cloud API.
accessIdAccess ID to access Cloud API.
apiSecretAPI secret to access Cloud API.
deviceIdDevice ID.
protocolVersionProtocol version.
portTCP port of device.
receiveTimeoutReceive timeout (msec).


Member Function Documentation

◆ FillJson()

string com.clusterrr.TuyaNet.TuyaDevice.FillJson ( string  json,
bool  addGwId = true,
bool  addDevId = true,
bool  addUid = true,
bool  addTime = true 
)
inline

Fills JSON string with base fields required by most commands.

Parameters
jsonJSON string
addGwIdAdd "gwId" field with device ID.
addDevIdAdd "devId" field with device ID.
addUidAdd "uid" field with device ID.
addTimeAdd "time" field with current timestamp.
Returns
JSON string with added fields.
Here is the caller graph for this function:

◆ EncodeRequest()

byte [] com.clusterrr.TuyaNet.TuyaDevice.EncodeRequest ( TuyaCommand  command,
string  json 
)
inline

Creates encoded and encrypted payload data from JSON string.

Parameters
commandTuya command ID.
jsonString with JSON to send.
Returns
Raw data.

◆ DecodeResponse()

TuyaLocalResponse com.clusterrr.TuyaNet.TuyaDevice.DecodeResponse ( byte[]  data)
inline

Parses and decrypts payload data from received bytes.

Parameters
dataRaw data to parse and decrypt.
Returns
Instance of TuyaLocalResponse.

◆ SendAsync() [1/2]

async Task<TuyaLocalResponse> com.clusterrr.TuyaNet.TuyaDevice.SendAsync ( TuyaCommand  command,
string  json,
int  retries = 2,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
CancellationToken  cancellationToken = default 
)

Sends JSON string to device and reads response.

Parameters
commandTuya command ID.
jsonJSON string.
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
cancellationTokenCancellation token.
Returns
Parsed and decrypred received data as instance of TuyaLocalResponse.
Here is the caller graph for this function:

◆ SendAsync() [2/2]

async Task<byte[]> com.clusterrr.TuyaNet.TuyaDevice.SendAsync ( byte[]  data,
int  retries = 2,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
CancellationToken  cancellationToken = default 
)
inline

Sends raw data over to device and read response.

Parameters
dataRaw data to send.
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
cancellationTokenCancellation token.
Returns
Received data (raw).

◆ GetDpsAsync()

async Task<Dictionary<int, object> > com.clusterrr.TuyaNet.TuyaDevice.GetDpsAsync ( int  retries = 5,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
CancellationToken  cancellationToken = default 
)
inline

Requests current DPs status.

Parameters
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
cancellationTokenCancellation token.
Returns
Dictionary of DP numbers and values.
Here is the call graph for this function:

◆ SetDpAsync()

async Task<Dictionary<int, object> > com.clusterrr.TuyaNet.TuyaDevice.SetDpAsync ( int  dp,
object  value,
int  retries = 2,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
bool  allowEmptyResponse = false,
CancellationToken  cancellationToken = default 
)

Sets single DP to specified value.

Parameters
dpDP number.
valueValue.
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
allowEmptyResponseDo not throw exception on empty Response
cancellationTokenCancellation token.
Returns
Dictionary of DP numbers and values.

◆ SetDpsAsync()

async Task<Dictionary<int, object> > com.clusterrr.TuyaNet.TuyaDevice.SetDpsAsync ( Dictionary< int, object >  dps,
int  retries = 2,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
bool  allowEmptyResponse = false,
CancellationToken  cancellationToken = default 
)
inline

Sets DPs to specified value.

Parameters
dpsDictionary of DP numbers and values to set.
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
allowEmptyResponseDo not throw exception on empty Response
cancellationTokenCancellation token.
Returns
Dictionary of DP numbers and values.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateDpsAsync()

async Task<Dictionary<int, object> > com.clusterrr.TuyaNet.TuyaDevice.UpdateDpsAsync ( IEnumerable< int >  dpIds,
int  retries = 5,
int  nullRetries = 1,
int?  overrideRecvTimeout = null,
CancellationToken  cancellationToken = default 
)
inline

Update DP values.

Parameters
dpIdsDP identificators to update (can be empty for some devices).
retriesNumber of retries in case of network error (default - 2).
nullRetriesNumber of retries in case of empty answer (default - 1).
overrideRecvTimeoutOverride receive timeout (default - ReceiveTimeout property).
cancellationTokenCancellation token.
Returns
Dictionary of DP numbers and values.
Here is the call graph for this function:

◆ RefreshLocalKeyAsync()

async Task com.clusterrr.TuyaNet.TuyaDevice.RefreshLocalKeyAsync ( bool  forceTokenRefresh = false,
CancellationToken  cancellationToken = default 
)
inline

Get current local key from Tuya Cloud API

Parameters
forceTokenRefreshRefresh access token even it's not expired.
cancellationTokenCancellation token.

◆ Dispose()

void com.clusterrr.TuyaNet.TuyaDevice.Dispose ( )
inline

Disposes object.

Property Documentation

◆ IP

string com.clusterrr.TuyaNet.TuyaDevice.IP
get

IP address of device.

◆ LocalKey

string com.clusterrr.TuyaNet.TuyaDevice.LocalKey
getset

Local key of device.

◆ DeviceId

string com.clusterrr.TuyaNet.TuyaDevice.DeviceId
get

Device ID.

◆ Port

int com.clusterrr.TuyaNet.TuyaDevice.Port = 6668
get

TCP port of device.

◆ ProtocolVersion

TuyaProtocolVersion com.clusterrr.TuyaNet.TuyaDevice.ProtocolVersion
getset

Protocol version.

◆ ConnectionTimeout

int com.clusterrr.TuyaNet.TuyaDevice.ConnectionTimeout = 500
getset

Connection timeout.

◆ ReceiveTimeout

int com.clusterrr.TuyaNet.TuyaDevice.ReceiveTimeout
getset

Receive timeout.

◆ NetworkErrorRetriesInterval

int com.clusterrr.TuyaNet.TuyaDevice.NetworkErrorRetriesInterval = 100
getset

Network error retry interval (msec)

◆ NullRetriesInterval

int com.clusterrr.TuyaNet.TuyaDevice.NullRetriesInterval = 0
getset

Empty responce retry interval (msec)

◆ PermanentConnection

bool com.clusterrr.TuyaNet.TuyaDevice.PermanentConnection = false
getset

Permanent connection (connect and stay connected).