Skip to content

Instantly share code, notes, and snippets.

@caruizdiaz
Last active December 22, 2015 03:09
Show Gist options
  • Save caruizdiaz/6408402 to your computer and use it in GitHub Desktop.
Save caruizdiaz/6408402 to your computer and use it in GitHub Desktop.
Main form of the sample application for Windows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using dwgsms.API;
using dwgsms.DWG;
namespace dwg2000_demo
{
public partial class FormMain : Form
{
static FormMain formInstance;
object locker = new object();
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
formInstance = this;
Gateway.StatusChanged += HandleGatewayStatusChanged;
Gateway.SentSMSStatus += HandleGatewaySentSMSStatus;
Gateway.SMSReceived += HandleGatewaySMSReceived;
Gateway.USSDReceived += HandleGatewayUSSDReceived;
}
void WriteToConsole(string text)
{
this.BeginInvoke((MethodInvoker)delegate
{
lock (this.locker)
{
txtConsole.Text = txtConsole.Text + text + Environment.NewLine;
txtConsole.SelectionStart = txtConsole.Text.Length;
txtConsole.ScrollToCaret();
}
});
}
private void btnBind_Click(object sender, EventArgs e)
{
if (Gateway.IsGwConnected)
Gateway.StopListener();
BindPort((int) numPort.Value);
btnBind.Enabled = false;
}
void BindPort(int port)
{
WriteToConsole("Listening at port " + port.ToString());
Gateway.StartListener((int)numPort.Value);
}
static void HandleGatewayUSSDReceived(string gatewayIP, int port, dwg_ussd_result_code status, string message)
{
if (message == null)
message = string.Empty;
formInstance.WriteToConsole(string.Format("USSD response {0} at port {1}, status {2}", gatewayIP,
port,
Enum.GetName(typeof(dwg_ussd_result_code), status)));
formInstance.BeginInvoke((MethodInvoker)delegate
{
formInstance.txtUSSDResponse.Text = message;
formInstance.btnSendUSSD.Enabled = !formInstance.btnSendUSSD.Enabled;
formInstance.txtUSSDCode.Enabled = !(status == dwg_ussd_result_code.FutherActionRequired);
formInstance.txtUSSDResponseCode.Enabled = (status == dwg_ussd_result_code.FutherActionRequired);
}
);
}
static void HandleGatewaySMSReceived(string gatewayIP, string number, string text, int port, string timestamp)
{
if (text == null)
text = string.Empty;
formInstance.WriteToConsole(string.Format("New SMS received. From={0} Port={1} Content={2}", number, port.ToString(), text));
formInstance.BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show(text, "SMS from " + number, MessageBoxButtons.OK, MessageBoxIcon.Information);
});
}
static void HandleGatewaySentSMSStatus(string gatewayIP, string number, int port, dwg_sms_result_code status)
{
string sts = Enum.GetName(typeof(dwg_sms_result_code), status);
formInstance.WriteToConsole(string.Format("ACK received from sent SMS to {0}: {1}", number, sts));
formInstance.BeginInvoke((MethodInvoker) delegate
{
MessageBox.Show("SMS was accepted by Network: " + sts, "SMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
formInstance.btnSendSMS.Enabled = !formInstance.btnSendSMS.Enabled;
});
}
static void HandleGatewayStatusChanged(string gatewayIP, dwg_port_status_value[] portsStatus)
{
formInstance.WriteToConsole(string.Format("Status changed for GW {0}", gatewayIP));
formInstance.BeginInvoke((MethodInvoker)delegate
{
lock (formInstance.locker)
{
formInstance.numSmsPort.Maximum = portsStatus.Length;
formInstance.numUSSDPort.Maximum = portsStatus.Length;
formInstance.gbSms.Enabled = true;
formInstance.gbUSSD.Enabled = true;
}
});
int i = 0;
foreach (dwg_port_status_value port in portsStatus)
formInstance.WriteToConsole(string.Format("Port #{0}: {1}", i++, Enum.GetName(typeof(dwg_port_status_value), port)));
}
bool IsValid(int port)
{
if (!Gateway.IsGwConnected)
{
MessageBox.Show("Gateway isn't connected. Please check your API configuration in 'Mobile Configuration > Basic Configuration' in your gateway's web admin page.", "Error sending message", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (!Gateway.IsPortReady(port) && MessageBox.Show("Port #" + port.ToString() + " is not connected. Send anyway?", "Error sending message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
{
return false;
}
return true;
}
private void btnSendSMS_Click(object sender, EventArgs e)
{
if (txtSmsFrom.Text.Trim() == string.Empty)
{
MessageBox.Show("From number cannot be empty", "Error sending SMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (!IsValid((int)numSmsPort.Value))
return;
Gateway.SendMessage(txtSmsFrom.Text.Trim(), txtSmsContent.Text, (int)numSmsPort.Value);
MessageBox.Show("SMS sent to gateway", "SMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSendSMS.Enabled = !btnSendSMS.Enabled;
}
private void btnSendUSSD_Click(object sender, EventArgs e)
{
if (txtUSSDCode.Text.Trim() == string.Empty)
{
MessageBox.Show("Request code cannot be empty", "Error sending USSD", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (!IsValid((int) numUSSDPort.Value))
return;
if (txtUSSDCode.Enabled)
Gateway.SendUSSDMessage(txtUSSDCode.Text.Trim(), (int) numUSSDPort.Value);
else
Gateway.SendUSSDMessage(txtUSSDResponseCode.Text.Trim(), (int) numUSSDPort.Value);
MessageBox.Show("USSD sent to gateway", "USSD", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSendUSSD.Enabled = !btnSendUSSD.Enabled;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment