Skip to content

Commit

Permalink
Merge pull request #255 from taosdata/ExecuteSubscribe
Browse files Browse the repository at this point in the history
加入订阅的示例 和孔方法, 后续开始实现。
  • Loading branch information
sangshuduo committed May 7, 2023
2 parents 3b6630a + 73a8174 commit 323570a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Data.Common;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -82,6 +83,53 @@ private static void Main(string[] args)
ExecSqlByWebSocket(builder.UseWebSocket());
UseTaosEFCore(builder.UseWebSocket());
#endif
using (var connection = new TaosConnection(builder.ConnectionString))
{
try
{
connection.Open();
connection.CreateCommand($"select * from power.meters where current > 10").ExecuteSubscribe("topic-meter-current-bg-10", reader =>
{
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine($"ts:{reader.GetDateTime("ts")} current:{reader.GetDouble("current")} voltage:{reader.GetDouble("voltage")} phase:{reader.GetDouble("phase")} location:{reader.GetDouble("location")} "); ;
}
}
});
}
catch (Exception ex)
{
Console.WriteLine("执行TDengine异常" + ex.Message);
}
finally
{
connection.Close();
}
}


using (var connection = new TaosConnection(builder.ConnectionString))
{
try
{
connection.Open();

connection.CreateCommand($"select * from power.meters where current > 10")
.ExecuteSubscribe<(DateTime ts, double current, double voltage, double phase, string location)>
("topic-meter-current-bg-10", data => Console.WriteLine($"ts:{data.ts} current:{data.current} voltage:{data.voltage} phase:{data.phase} location:{data.location} "));

}
catch (Exception ex)
{
Console.WriteLine("执行TDengine异常" + ex.Message);
}
finally
{
connection.Close();
}
}
}

private static void ExecSqlByNative(TaosConnectionStringBuilder builder)
Expand Down
26 changes: 24 additions & 2 deletions src/IoTSharp.Data.Taos/TaosCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,37 @@ public override object ExecuteScalar()
}
return result;
}

/// <summary>
/// 订阅数据, 使用 TaosDataReader自行处理数据。
/// </summary>
/// <param name="subscribe"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool ExecuteSubscribe(string topic, Action<TaosDataReader> subscribe)
{
throw new NotImplementedException();
}
/// <summary>
/// 订阅数据, 以对象方式返回。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="subscribe"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool ExecuteSubscribe<T>(string topic, Action<T> subscribe)
{
throw new NotImplementedException();
}

/// <summary>
/// Attempts to cancel the execution of the command. Does nothing.
/// </summary>
public override void Cancel()
{
//unsubscribe
}


}

}

0 comments on commit 323570a

Please sign in to comment.