162 lines
5.2 KiB
C#
Raw Normal View History

2025-04-02 17:46:33 +08:00
using System;
using System.Collections.Generic;
2024-12-19 16:07:07 +08:00
using System.Threading.Tasks;
2025-04-07 10:56:40 +08:00
using Apache.IoTDB.DataStructure;
using Apache.IoTDB;
using Confluent.Kafka;
2025-04-02 17:46:33 +08:00
using JiShe.CollectBus.Ammeters;
2024-12-19 16:07:07 +08:00
using JiShe.CollectBus.FreeSql;
2025-04-02 17:46:33 +08:00
using JiShe.CollectBus.IoTDBProvider;
2025-03-14 14:28:04 +08:00
using JiShe.CollectBus.IotSystems.PrepayModel;
2024-12-19 16:07:07 +08:00
using Microsoft.AspNetCore.Authorization;
2025-04-03 16:46:26 +08:00
using Microsoft.AspNetCore.Mvc;
2025-04-07 10:56:40 +08:00
using Microsoft.Extensions.Options;
2024-12-19 16:07:07 +08:00
namespace JiShe.CollectBus.Samples;
public class SampleAppService : CollectBusAppService, ISampleAppService
{
2025-04-02 17:46:33 +08:00
private readonly IIoTDBProvider _iotDBProvider;
2025-04-07 10:56:40 +08:00
private readonly IoTDBOptions _options;
2025-04-02 17:46:33 +08:00
2025-04-07 10:56:40 +08:00
public SampleAppService(IIoTDBProvider iotDBProvider, IOptions<IoTDBOptions> options)
2025-04-02 17:46:33 +08:00
{
_iotDBProvider = iotDBProvider;
2025-04-07 10:56:40 +08:00
_options = options.Value;
2025-04-02 17:46:33 +08:00
}
2025-04-03 16:46:26 +08:00
[HttpGet]
public async Task AddReadingAsync(int buildTabletMode = 1)
2025-04-02 17:46:33 +08:00
{
ElectricityMeter meter = new ElectricityMeter()
{
2025-04-03 16:46:26 +08:00
SystemName = "energy",
2025-04-03 15:38:31 +08:00
DeviceId = "402440506",
2025-04-02 17:46:33 +08:00
DeviceType = "Ammeter",
Current = 10,
MeterModel = "DDZY-1980",
ProjectCode = "10059",
Voltage = 10
};
2025-04-03 16:46:26 +08:00
await _iotDBProvider.InsertAsync(meter, buildTabletMode);
2025-04-02 17:46:33 +08:00
}
2025-04-07 10:56:40 +08:00
[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<string> columnNames =
new List<string> {
"region_id",
"plant_id",
"device_id",
"model",
"temperature",
"humidity" };
List<TSDataType> dataTypes =
new List<TSDataType>{
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.STRING,
TSDataType.FLOAT,
TSDataType.DOUBLE};
List<ColumnCategory> columnCategories =
new List<ColumnCategory>{
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.TAG,
ColumnCategory.ATTRIBUTE,
ColumnCategory.FIELD,
ColumnCategory.FIELD};
var values = new List<List<object>> { };
var timestamps = new List<long> { };
for (long timestamp = 0; timestamp < 100; timestamp++)
{
timestamps.Add(timestamp);
values.Add(new List<object> { "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();
}
2024-12-19 16:07:07 +08:00
public Task<SampleDto> GetAsync()
{
return Task.FromResult(
new SampleDto
{
Value = 42
}
);
}
[Authorize]
public Task<SampleDto> GetAuthorizedAsync()
{
return Task.FromResult(
new SampleDto
{
Value = 42
}
);
}
[AllowAnonymous]
public async Task<List<Vi_BaseAmmeterInfo>> Test()
{
var ammeterList = await SqlProvider.Instance.Change(DbEnum.PrepayDB).Select<Vi_BaseAmmeterInfo>().Where(d => d.TB_CustomerID == 5).Take(10).ToListAsync();
return ammeterList;
}
}