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 ModelContext dbContext = null ;
private ApiClient apiClient = new ApiClient ( ) ;
private SmsApi smsApi = null ;
private bool sending = false ;
2020-12-05 12:29:30 +01:00
public SmsWindow ( ) {
2020-12-05 18:47:13 +01:00
dbContext = ModelContext . Getinstance ( ) ;
smsApi = new SmsApi ( apiClient ) ;
2020-12-05 12:29:30 +01:00
InitializeComponent ( ) ;
}
2020-12-05 18:47:13 +01:00
private void Window_Loaded ( object sender , RoutedEventArgs e ) {
lvLog . ItemsSource = status ;
}
2020-12-05 12:29:30 +01:00
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e ) {
e . Cancel = true ;
2020-12-05 18:47:13 +01:00
if ( sending ) {
return ;
}
messageTextBox . Document . Blocks . Clear ( ) ;
status . Clear ( ) ;
2020-12-05 12:29:30 +01:00
this . Hide ( ) ;
}
2020-12-05 18:47:13 +01:00
public void LoadCitoyens ( List < Citoyen > citoyens ) {
foreach ( Citoyen citoyen in citoyens ) {
SmsSendingStatus s = new SmsSendingStatus ( ) ;
s . Nom = citoyen . Nom ;
s . Prenom = citoyen . Prenom ;
s . Mobile = citoyen . TelPort . Replace ( " " , "" ) ;
s . Status = "En attente" ;
status . Add ( s ) ;
}
}
2020-12-05 12:29:30 +01:00
public void Envoyer_Click ( object sender , RoutedEventArgs e ) {
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 ;
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 ( ) ;
}
}
}