change in tokens and addtelemetry(addData)

This commit is contained in:
2025-12-20 00:51:50 +03:30
parent 10178de7c1
commit b69691c84f
4 changed files with 24 additions and 12 deletions

View File

@@ -25,12 +25,14 @@ public class TelemetryController : ControllerBase
} }
[HttpGet("AddData")] [HttpGet("AddData")]
public async Task<ActionResult<int>> Create(string deviceName, decimal temperatureC, decimal humidityPercent, public async Task<ActionResult<int>> Create(object deviceName, decimal temperatureC, decimal humidityPercent,
decimal soilPercent, int gasPPM, decimal lux, CancellationToken cancellationToken) decimal soilPercent, int gasPPM, decimal lux, CancellationToken cancellationToken)
{ {
TelemetryDto dto = new TelemetryDto TelemetryDto dto = new TelemetryDto
{ {
DeviceName = deviceName, DeviceName = deviceName.ToString() == "dr110"? "dr110":"",
Id= deviceName.ToString() == "dr110" ? 0 : Convert.ToInt32(deviceName),
TemperatureC = temperatureC, TemperatureC = temperatureC,
HumidityPercent = humidityPercent, HumidityPercent = humidityPercent,
SoilPercent = soilPercent, SoilPercent = soilPercent,

View File

@@ -6,7 +6,7 @@
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },
"dotnetRunMessages": true, "dotnetRunMessages": true,
"applicationUrl": "http://0.0.0.0:5064" "applicationUrl": "http://127.0.0.1:5064"
}, },
"https": { "https": {
"commandName": "Project", "commandName": "Project",
@@ -14,7 +14,7 @@
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },
"dotnetRunMessages": true, "dotnetRunMessages": true,
"applicationUrl": "https://0.0.0.0:7274;http://0.0.0.0:5064" "applicationUrl": "https://127.0.0.1:7274;http://127.0.0.1:5064"
}, },
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",

View File

@@ -107,7 +107,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
// ذخیره کدها // ذخیره کدها
settings.TokenCode = tokenCode; settings.TokenCode = tokenCode;
settings.VerificationCode = verificationCode; settings.VerificationCode = verificationCode;
settings.TokenExpiresAt = DateTime.UtcNow.AddMinutes(10); // اعتبار 10 دقیقه settings.TokenExpiresAt = DateTime.UtcNow.AddMinutes(30); // اعتبار 10 دقیقه
settings.UpdatedAt = DateTime.UtcNow; settings.UpdatedAt = DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
@@ -118,7 +118,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
await smsService.SendWebserviceSmsAsync(new WebserviceSmsRequest await smsService.SendWebserviceSmsAsync(new WebserviceSmsRequest
{ {
Recipient = request.DevicePhoneNumber, Recipient = request.DevicePhoneNumber,
Message = $"کد تایید دستگاه شما: {tokenCode}\nاعتبار: 10 دقیقه" Message = $"0911925302120#{tokenCode}"
}, cancellationToken); }, cancellationToken);
} }
catch (Exception smsEx) catch (Exception smsEx)
@@ -137,7 +137,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
{ {
Success = true, Success = true,
Message = "کد تایید با موفقیت ارسال شد", Message = "کد تایید با موفقیت ارسال شد",
TokenCode = tokenCode TokenCode = $"0911925302120#{tokenCode}"
}; };
} }
catch (Exception ex) catch (Exception ex)
@@ -193,12 +193,14 @@ public sealed class DeviceTokenService : IDeviceTokenService
Message = "کد تایید نادرست است" Message = "کد تایید نادرست است"
}; };
} }
Random rnd = new Random();
// رمزگذاری ساده تنظیمات: DeviceName|UploadIntervalMin به Base64 // رمزگذاری ساده تنظیمات: DeviceName|UploadIntervalMin به Base64
var deviceName = settings.Device.DeviceName; var deviceId = settings.Device.Id;
var uploadInterval = settings.UploadIntervalMin; var uploadInterval = settings.UploadIntervalMin;
var plainText = $"{deviceName}|{uploadInterval}"; string smsConfig = (settings.MinimumSmsIntervalMinutes != 0 ? "1" : rnd.Next(2, 9).ToString())
var encodedSettings = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(plainText)); + settings.MinimumSmsIntervalMinutes;
var plainText = $"{deviceId}01|{rnd.Next(1,9)}{uploadInterval}|{smsConfig}|{rnd.Next(139,97654)}|{rnd.Next(1,9)}{(int)settings.SimCardType}";
var encodedSettings = plainText; //Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(plainText));
// ارسال تنظیمات کدشده از طریق پیامک // ارسال تنظیمات کدشده از طریق پیامک
try try
@@ -206,7 +208,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
await smsService.SendWebserviceSmsAsync(new WebserviceSmsRequest await smsService.SendWebserviceSmsAsync(new WebserviceSmsRequest
{ {
Recipient = request.DevicePhoneNumber, Recipient = request.DevicePhoneNumber,
Message = $"تنظیمات دستگاه:\n{encodedSettings}" Message = $"{encodedSettings}"
}, cancellationToken); }, cancellationToken);
} }
catch (Exception smsEx) catch (Exception smsEx)

View File

@@ -29,6 +29,14 @@ public sealed class TelemetryService : ITelemetryService
dto.DeviceId = device.Id; // Update DTO for alert service dto.DeviceId = device.Id; // Update DTO for alert service
} }
} }
if (dto.Id!=0)
{
var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.Id == dto.Id, cancellationToken);
if (device != null)
{
entity.DeviceId = device.Id;
}
}
// ذخیره زمان سرور در لحظه ثبت // ذخیره زمان سرور در لحظه ثبت
entity.ServerTimestampUtc = DateTime.Now; entity.ServerTimestampUtc = DateTime.Now;