using System; using System.Collections.Generic; using System.Threading.Tasks; using Apache.IoTDB.DataStructure; using Apache.IoTDB; using Confluent.Kafka; using JiShe.CollectBus.Ammeters; using JiShe.CollectBus.FreeSql; using JiShe.CollectBus.IoTDBProvider; using JiShe.CollectBus.IotSystems.PrepayModel; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace JiShe.CollectBus.Samples; public class SampleAppService : CollectBusAppService, ISampleAppService { private readonly IIoTDBProvider _iotDBProvider; private readonly IoTDBOptions _options; public SampleAppService(IIoTDBProvider iotDBProvider, IOptions options) { _iotDBProvider = iotDBProvider; _options = options.Value; } [HttpGet] public async Task AddReadingAsync(int buildTabletMode = 1) { ElectricityMeter meter = new ElectricityMeter() { SystemName = "energy", DeviceId = "402440506", DeviceType = "Ammeter", Current = 10, MeterModel = "DDZY-1980", ProjectCode = "10059", Voltage = 10 }; await _iotDBProvider.InsertAsync(meter, buildTabletMode); } [HttpGet] public async Task AddReading2Async() { var tableSessionPool = new TableSessionPool.Builder() .SetNodeUrls(_options.ClusterList) .SetUsername(_options.UserName) .SetPassword(_options.Password) .SetFetchSize(1024) .Build(); await tableSessionPool.Open(false); await tableSessionPool.ExecuteNonQueryStatementAsync("CREATE DATABASE test1"); await tableSessionPool.ExecuteNonQueryStatementAsync("CREATE DATABASE test2"); //await tableSessionPool.ExecuteNonQueryStatementAsync("use test2"); //// or use full qualified table name //await tableSessionPool.ExecuteNonQueryStatementAsync( // "create table test1.table1(region_id STRING TAG, plant_id STRING TAG, device_id STRING TAG, model STRING ATTRIBUTE, temperature FLOAT FIELD, humidity DOUBLE FIELD) with (TTL=3600000)"); //await tableSessionPool.ExecuteNonQueryStatementAsync( // "create table table2(region_id STRING TAG, plant_id STRING TAG, color STRING ATTRIBUTE, temperature FLOAT FIELD, speed DOUBLE FIELD) with (TTL=6600000)"); //// show tables from current database //var res = await tableSessionPool.ExecuteQueryStatementAsync("SHOW TABLES"); //res.ShowTableNames(); //while (res.HasNext()) Console.WriteLine(res.Next()); //await res.Close(); //// show tables by specifying another database //// using SHOW tables FROM //res = await tableSessionPool.ExecuteQueryStatementAsync("SHOW TABLES FROM test1"); //res.ShowTableNames(); //while (res.HasNext()) Console.WriteLine(res.Next()); //await res.Close(); var tableName = "testTable1"; List columnNames = new List { "region_id", "plant_id", "device_id", "model", "temperature", "humidity" }; List dataTypes = new List{ TSDataType.STRING, TSDataType.STRING, TSDataType.STRING, TSDataType.STRING, TSDataType.FLOAT, TSDataType.DOUBLE}; List columnCategories = new List{ ColumnCategory.TAG, ColumnCategory.TAG, ColumnCategory.TAG, ColumnCategory.ATTRIBUTE, ColumnCategory.FIELD, ColumnCategory.FIELD}; var values = new List> { }; var timestamps = new List { }; for (long timestamp = 0; timestamp < 100; timestamp++) { timestamps.Add(timestamp); values.Add(new List { "1", "5", "3", "A", 1.23F + timestamp, 111.1 + timestamp }); } var tablet = new Tablet(tableName, columnNames, columnCategories, dataTypes, values, timestamps); await tableSessionPool.InsertAsync(tablet); //var res = await tableSessionPool.ExecuteQueryStatementAsync("select * from testTable1 " // + "where region_id = '1' and plant_id in ('3', '5') and device_id = '3'"); // res.ShowTableNames(); // while (res.HasNext()) Console.WriteLine(res.Next()); // await res.Close(); await tableSessionPool.Close(); } public Task GetAsync() { return Task.FromResult( new SampleDto { Value = 42 } ); } [Authorize] public Task GetAuthorizedAsync() { return Task.FromResult( new SampleDto { Value = 42 } ); } [AllowAnonymous] public async Task> Test() { var ammeterList = await SqlProvider.Instance.Change(DbEnum.PrepayDB).Select().Where(d => d.TB_CustomerID == 5).Take(10).ToListAsync(); return ammeterList; } }