2020-12-05 18:47:13 +01:00
using Hermes.Model ;
using IO.Swagger.Api ;
using IO.Swagger.Client ;
using IO.Swagger.Model ;
using Newtonsoft.Json ;
using System ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Windows ;
using System.Windows.Documents ;
2020-12-05 12:29:30 +01:00
namespace Hermes {
public partial class SmsWindow : Window {
2020-12-05 18:47:13 +01:00
private ObservableCollection < SmsSendingStatus > status = new ObservableCollection < SmsSendingStatus > ( ) ;
private bool sending = false ;
2020-12-06 21:54:59 +01:00
public SmsWindow ( Window parent , List < Citoyen > rcps ) {
2020-12-05 12:29:30 +01:00
InitializeComponent ( ) ;
2020-12-06 21:54:59 +01:00
Owner = parent ;
foreach ( Citoyen rcp in rcps ) {
SmsSendingStatus s = new SmsSendingStatus ( ) ;
s . Nom = rcp . Nom ;
s . Prenom = rcp . Prenom ;
s . Mobile = rcp . TelPort . Replace ( " " , "" ) ;
s . Status = "En attente" ;
status . Add ( s ) ;
}
2020-12-05 12:29:30 +01:00
2020-12-05 18:47:13 +01:00
lvLog . ItemsSource = status ;
}
2020-12-05 12:29:30 +01:00
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e ) {
2020-12-05 18:47:13 +01:00
if ( sending ) {
2020-12-06 21:54:59 +01:00
e . Cancel = true ;
2020-12-05 18:47:13 +01:00
return ;
}
}
2020-12-05 12:29:30 +01:00
public void Envoyer_Click ( object sender , RoutedEventArgs e ) {
2020-12-06 21:54:59 +01:00
ModelContext dbContext = ModelContext . Getinstance ( ) ;
2020-12-05 18:47:13 +01:00
bool error = false ;
string message = new TextRange ( messageTextBox . Document . ContentStart , messageTextBox . Document . ContentEnd ) . Text ;
string apiKey = dbContext . Preferences . Local [ 0 ] . SmsApiKey ;
if ( String . IsNullOrWhiteSpace ( apiKey ) ) {
MessageBox . Show ( "Erreur lors de la tentative d'envoi : Clé ISendPro invalide" , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
return ;
}
if ( String . IsNullOrWhiteSpace ( message ) ) {
MessageBox . Show ( "Votre message est vide." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Warning ) ;
return ;
}
envoyerButton . IsEnabled = false ;
annulerButton . IsEnabled = false ;
messageTextBox . IsEnabled = false ;
sending = true ;
2020-12-06 21:54:59 +01:00
ApiClient apiClient = new ApiClient ( ) ;
SmsApi smsApi = new SmsApi ( apiClient ) ;
2020-12-05 18:47:13 +01:00
foreach ( SmsSendingStatus stat in status ) {
if ( ! stat . Status . Equals ( "Envoyé" ) ) {
SmsUniqueRequest req = new SmsUniqueRequest ( ) ;
req . Keyid = apiKey ;
req . Sms = message ;
req . Smslong = "999" ;
req . Num = stat . Mobile ;
try {
SMSReponse resp = smsApi . SendSms ( req ) ;
if ( resp ! = null & & resp . Etat ! = null & & resp . Etat . Etat ! = null & & resp . Etat . Etat . Count > 0 ) {
SMSReponseEtatEtat eta = resp . Etat . Etat [ 0 ] ;
if ( eta . Code ! = null & & eta . Code = = 0 & & eta . Tel ! = null ) {
stat . Status = "Envoyé" ;
} else {
error = true ;
stat . Status = eta . Message ;
}
} else {
error = true ;
stat . Status = "Erreur technique" ;
}
} catch ( ApiException ex ) {
error = true ;
SMSReponse resp = JsonConvert . DeserializeObject < SMSReponse > ( ( String ) ex . ErrorContent ) ;
if ( resp ! = null & & resp . Etat ! = null & & resp . Etat . Etat ! = null & & resp . Etat . Etat . Count > 0 ) {
SMSReponseEtatEtat eta = resp . Etat . Etat [ 0 ] ;
if ( eta . Code ! = null & & eta . Code = = 0 & & eta . Tel ! = null ) {
stat . Status = "Envoyé" ;
} else {
error = true ;
stat . Status = eta . Message ;
}
} else {
error = true ;
stat . Status = "Erreur technique" ;
}
}
}
}
envoyerButton . IsEnabled = true ;
annulerButton . IsEnabled = true ;
messageTextBox . IsEnabled = true ;
sending = false ;
if ( error ) {
MessageBox . Show ( "Plusieurs envois se sont mal déroulés. Vérifiez la validité des numéros de téléphone." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
}
2020-12-05 12:29:30 +01:00
}
public void Annuler_Click ( object sender , RoutedEventArgs e ) {
Close ( ) ;
}
}
}