我有存储传感器测量值的 postgres 时间刻度表。该表具有复合主键,其中包含 id int 增量列和 dateTime 列(需要在 orderer 中复合才能使该表成为超表),我还有警告表,该表具有传感器测量表的外键,该表也是复合,但是当我尝试插入警告表时,它给了我传感器测量表中不存在的外键约束错误,但我检查了并且复合主键存在。
这是函数代码
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Utilities;
using Services.Interfaces;
using System.Text;
using Models;
using Models.Enum;
namespace AzureFunctionApplication
{
public class TelemetryHandlingFunction
{
private readonly ILogger<TelemetryHandlingFunction> _logger;
private readonly IDeviceService _deviceService;
private readonly ISensorService _sensorService;
private readonly IMeasureService _measureService;
private readonly IWarningService _warningService;
private readonly JsonDeserializer _jsonDeserializer;
public TelemetryHandlingFunction(ILogger<TelemetryHandlingFunction> logger, IDeviceService deviceService, ISensorService sensorService, IMeasureService measureService, IWarningService warningService, JsonDeserializer jsonDeserializer)
{
_logger = logger;
_deviceService = deviceService;
_sensorService = sensorService;
_measureService = measureService;
_warningService = warningService;
_jsonDeserializer = jsonDeserializer;
}
[Function("EventHubTrigger")]
public async Task Run([EventHubTrigger("eventhub", Connection = "EventHubConnectionString")] Azure.Messaging.EventHubs.EventData[] events)
{
foreach (Azure.Messaging.EventHubs.EventData @event in events)
{
if (@event.SystemProperties.TryGetValue("iothub-connection-device-id", out var deviceId))
{
_logger.LogInformation("Device ID: {deviceId}", deviceId);
var jsonString = Encoding.UTF8.GetString(@event.Body.ToArray());
var device = _jsonDeserializer.Deserialize<Device>(jsonString);
var sensorMeasureObject = _jsonDeserializer.Deserialize<SensorMeasure>(jsonString);
await _deviceService.InsertDeviceAsync(deviceId.ToString());
if (sensorMeasureObject != null)
{
_logger.LogInformation($"Device: {deviceId.ToString()}");
var sensorId = await _sensorService.GetSensorIdByAddressAndTypeAsync(sensorMeasureObject.SensorAddress, sensorMeasureObject.Type);
if(sensorId == 0)
{
await _sensorService.InsertSensorAsync(sensorMeasureObject.Name, sensorMeasureObject.SensorAddress, sensorMeasureObject.Type, 0.0, 100.0, deviceId.ToString());
sensorId = await _sensorService.GetSensorIdByAddressAndTypeAsync(sensorMeasureObject.SensorAddress, sensorMeasureObject.Type);
}
var measureId = await _measureService.InsertDataAsync(sensorMeasureObject.Value, sensorMeasureObject.Time, sensorMeasureObject.Buffered, sensorId);
var warningType = await _warningService.WarningCheck(sensorMeasureObject.Value, sensorId);
if (warningType != WarningType.NoWarning)
{
await _warningService.InsertWarningAsync(measureId, warningType, sensorMeasureObject.Time, false);
}
}
else if (device != null)
{
{
await _deviceService.UpdateDeviceStateAsync(deviceId.ToString(), device.CurrentState);
}
}
}
else
{
_logger.LogInformation("Device ID not found in system properties.");
}
}
}
}
}
您能否分享有关如何创建表格的更多详细信息?
您是否检查过插入后您的
sensorId
是否没有变为零?可能不存在?
您正在进行单笔交易吗?您已提交交易吗?我看到每个部分都有一个服务,我在想它们是否可能不在并发事务上。
您介意尝试隔离 timescaledb 部分吗?我认为它会像here
一样工作CREATE TABLE sensor_data (
time TIMESTAMPTZ NOT NULL,
sensor_id INTEGER NOT NULL REFERENCES sensors(id),
value DOUBLE PRECISION NOT NULL
);
SELECT create_hypertable('sensor_data', 'time');
INSERT INTO sensors (name) VALUES ('Sensor A'), ('Sensor B'), ('Sensor C');
INSERT INTO sensor_data (time, sensor_id, value) VALUES
('2022-01-01 00:00:00', 1, 42.5),
('2022-01-01 01:00:00', 2, 35.7),
('2022-01-01 02:00:00', 3, 22.3);