126 lines
4.9 KiB
C#
126 lines
4.9 KiB
C#
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;
|
|
|
|
namespace Hermes {
|
|
public partial class SmsWindow : Window {
|
|
private ObservableCollection<SmsSendingStatus> status = new ObservableCollection<SmsSendingStatus>();
|
|
private ModelContext dbContext = null;
|
|
private ApiClient apiClient = new ApiClient();
|
|
private SmsApi smsApi = null;
|
|
private bool sending = false;
|
|
|
|
public SmsWindow() {
|
|
dbContext = ModelContext.Getinstance();
|
|
smsApi = new SmsApi(apiClient);
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
|
lvLog.ItemsSource = status;
|
|
}
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
|
|
e.Cancel = true;
|
|
if(sending) {
|
|
return;
|
|
}
|
|
messageTextBox.Document.Blocks.Clear();
|
|
status.Clear();
|
|
this.Hide();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
public void Envoyer_Click(object sender, RoutedEventArgs e) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
public void Annuler_Click(object sender, RoutedEventArgs e) {
|
|
Close();
|
|
}
|
|
}
|
|
}
|