feat: publish dbussmsforward refactor
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using DbusSmsForward.Services;
|
||||
using DbusSmsForward.SettingModel;
|
||||
using DbusSmsForward.SMSModel;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace DbusSmsForward.Notifications;
|
||||
|
||||
public sealed class ShellNotificationSender(DbusSmsForwardOptions options) : NotificationSenderBase("shell")
|
||||
{
|
||||
private readonly ShellConfig _config = options.Notifications.Shell;
|
||||
|
||||
public override bool IsEnabled => _config.Enabled && !string.IsNullOrWhiteSpace(_config.ShellPath);
|
||||
|
||||
public override async Task SendAsync(SmsContentModel sms, string body, string deviceName, CancellationToken cancellationToken)
|
||||
{
|
||||
ProcessStartInfo startInfo = new()
|
||||
{
|
||||
FileName = _config.ShellPath,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
|
||||
foreach (string arg in new[]
|
||||
{
|
||||
sms.TelNumber,
|
||||
sms.SmsDate,
|
||||
sms.SmsContent,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
deviceName
|
||||
})
|
||||
{
|
||||
startInfo.ArgumentList.Add(arg ?? string.Empty);
|
||||
}
|
||||
|
||||
using Process process = Process.Start(startInfo) ?? throw new InvalidOperationException("Unable to start shell process.");
|
||||
using CancellationTokenRegistration registration = cancellationToken.Register(() =>
|
||||
{
|
||||
try { if (!process.HasExited) process.Kill(true); } catch { }
|
||||
});
|
||||
|
||||
await process.WaitForExitAsync(cancellationToken);
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
string error = await process.StandardError.ReadToEndAsync(cancellationToken);
|
||||
throw new InvalidOperationException($"Shell sender exited with code {process.ExitCode}: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user