2016-11-17 13:06:17 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-11-17 13:37:57 -05:00
|
|
|
|
using System.Diagnostics;
|
2016-11-17 13:06:17 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2016-11-18 01:23:45 -05:00
|
|
|
|
using System.Windows.Input;
|
2016-11-18 23:29:27 -05:00
|
|
|
|
using System.ServiceProcess;
|
2016-11-19 01:32:23 -05:00
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
|
2016-11-30 03:18:17 -05:00
|
|
|
|
// internal usings
|
2016-11-17 13:37:57 -05:00
|
|
|
|
using WifiSitter;
|
|
|
|
|
using WifiSitter.Model;
|
2016-11-17 13:06:17 -05:00
|
|
|
|
using WifiSitterGui.Helpers;
|
2016-11-19 01:32:23 -05:00
|
|
|
|
|
2016-11-30 03:18:17 -05:00
|
|
|
|
// 3rd party usings
|
|
|
|
|
using NetMQ;
|
|
|
|
|
using NetMQ.Sockets;
|
2016-11-17 13:06:17 -05:00
|
|
|
|
|
|
|
|
|
namespace WifiSitterGui.ViewModel
|
|
|
|
|
{
|
|
|
|
|
public class WifiSitterAgentViewModel : MvvmObservable
|
|
|
|
|
{
|
|
|
|
|
#region fields
|
|
|
|
|
|
|
|
|
|
private static MainWindowViewModel _windowVM;
|
|
|
|
|
private RelayCommand _launchWindowCommand;
|
2016-11-18 01:23:45 -05:00
|
|
|
|
private RelayCommand _takeFiveCommand; // Asks service to pause for 5 minutes
|
2016-11-17 13:06:17 -05:00
|
|
|
|
private static MainWindow _statusGui;
|
2016-11-17 13:37:57 -05:00
|
|
|
|
private static string _serviceChannel;
|
2016-11-18 01:23:45 -05:00
|
|
|
|
private System.Timers.Timer _netstateCheckTimer;
|
2016-11-30 03:18:17 -05:00
|
|
|
|
private static string _myChannel = String.Format("{0}-{1}", Process.GetCurrentProcess().Id, Process.GetCurrentProcess().ProcessName);
|
|
|
|
|
private static DealerSocket _mqClient;
|
2016-11-30 03:43:34 -05:00
|
|
|
|
private static NetMQPoller _poller;
|
2016-11-18 01:23:45 -05:00
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // fields
|
|
|
|
|
|
|
|
|
|
#region constructor
|
|
|
|
|
|
|
|
|
|
public WifiSitterAgentViewModel() {
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-19 01:34:12 -05:00
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
public WifiSitterAgentViewModel(MainWindowViewModel WindowVM) {
|
|
|
|
|
_windowVM = WindowVM;
|
2016-11-17 13:37:57 -05:00
|
|
|
|
Intitialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Intitialize() {
|
2016-11-30 03:18:17 -05:00
|
|
|
|
int port = 37247;
|
|
|
|
|
string connString = String.Format("tcp://127.0.0.1:{0}", port);
|
|
|
|
|
|
|
|
|
|
_mqClient = new DealerSocket();
|
|
|
|
|
_mqClient.Options.Identity = Encoding.UTF8.GetBytes(_myChannel);
|
|
|
|
|
_mqClient.Connect(connString);
|
2016-11-30 03:43:34 -05:00
|
|
|
|
_poller = new NetMQPoller();
|
|
|
|
|
_poller.Add(_mqClient);
|
|
|
|
|
_mqClient.ReceiveReady += _mqClient_ReceiveReady;
|
|
|
|
|
_poller.RunAsync();
|
2016-11-30 03:18:17 -05:00
|
|
|
|
|
2016-11-19 01:32:23 -05:00
|
|
|
|
// Get NetState
|
|
|
|
|
RequestNetworkState();
|
|
|
|
|
|
2016-11-18 01:23:45 -05:00
|
|
|
|
// Intermittent network state polling
|
|
|
|
|
_netstateCheckTimer = new System.Timers.Timer();
|
|
|
|
|
_netstateCheckTimer.AutoReset = true;
|
|
|
|
|
_netstateCheckTimer.Interval = 30 * 1000; // 30 seconds
|
|
|
|
|
_netstateCheckTimer.Elapsed += (o, e) => { RequestNetworkState(); };
|
|
|
|
|
_netstateCheckTimer.Start();
|
2016-11-17 13:37:57 -05:00
|
|
|
|
|
2016-11-19 01:32:23 -05:00
|
|
|
|
// Connection state changed event handler setup
|
|
|
|
|
NetworkChange.NetworkAvailabilityChanged += (o, e) => { RequestNetworkState(3 * 1000); };
|
|
|
|
|
NetworkChange.NetworkAddressChanged += (o, e) => { RequestNetworkState(3 * 1000); };
|
|
|
|
|
|
2016-11-18 01:23:45 -05:00
|
|
|
|
Trace.WriteLine(String.Format("WifiSitter service msg channel: {0}", ServiceChannelName));
|
2016-11-17 13:06:17 -05:00
|
|
|
|
}
|
2016-11-30 03:43:34 -05:00
|
|
|
|
|
|
|
|
|
~WifiSitterAgentViewModel() {
|
|
|
|
|
_poller?.Dispose();
|
|
|
|
|
}
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // constructor
|
|
|
|
|
|
|
|
|
|
#region properties
|
2016-11-30 03:43:34 -05:00
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
public MainWindowViewModel WindowVM {
|
|
|
|
|
get { if (_windowVM == null) {
|
|
|
|
|
_windowVM = new MainWindowViewModel();
|
|
|
|
|
}
|
|
|
|
|
return _windowVM;
|
|
|
|
|
}
|
|
|
|
|
private set { _windowVM = value; OnPropertyChanged("WindowVM"); }
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-19 01:34:12 -05:00
|
|
|
|
|
2016-11-17 13:37:57 -05:00
|
|
|
|
internal string ServiceChannelName {
|
|
|
|
|
get {
|
2016-11-21 17:28:17 -05:00
|
|
|
|
var serviceProc = Process.GetProcesses().Where(x => x.ProcessName.ToLower().StartsWith("wifisitter"))
|
|
|
|
|
.Where(x => !x.ProcessName.ToLower().Contains("gui")).ToArray();
|
|
|
|
|
if (serviceProc != null &&
|
|
|
|
|
serviceProc.Length > 0) {
|
|
|
|
|
_serviceChannel = String.Format("{0}-{1}", serviceProc[0].Id, serviceProc[0].ProcessName);
|
2016-11-17 13:37:57 -05:00
|
|
|
|
}
|
2016-11-21 17:28:17 -05:00
|
|
|
|
|
2016-11-17 13:37:57 -05:00
|
|
|
|
return _serviceChannel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-18 23:29:27 -05:00
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // properties
|
|
|
|
|
|
|
|
|
|
#region methods
|
2016-11-18 01:23:45 -05:00
|
|
|
|
|
2016-11-19 01:32:23 -05:00
|
|
|
|
public void RequestNetworkState(int Delay) {
|
|
|
|
|
Task.Delay(Delay).ContinueWith((task) => { RequestNetworkState(); }, TaskScheduler.FromCurrentSynchronizationContext());
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 19:16:01 -05:00
|
|
|
|
|
2016-11-19 01:32:23 -05:00
|
|
|
|
public void RequestNetworkState () {
|
2016-11-18 01:23:45 -05:00
|
|
|
|
if (!String.IsNullOrEmpty(ServiceChannelName)) {
|
|
|
|
|
try {
|
|
|
|
|
Trace.WriteLine("Checking for network state.");
|
2016-11-30 03:18:17 -05:00
|
|
|
|
string request = new WifiSitterIpcMessage("get_netstate", _myChannel).ToJsonString();
|
|
|
|
|
var reqMessage = new NetMQMessage();
|
|
|
|
|
reqMessage.Append(_mqClient.Options.Identity);
|
|
|
|
|
reqMessage.AppendEmptyFrame();
|
|
|
|
|
reqMessage.Append(request);
|
|
|
|
|
bool success = _mqClient.TrySendMultipartMessage(reqMessage);
|
|
|
|
|
if (!success) Trace.WriteLine("Failed to send get_networkstate");
|
2016-11-18 01:23:45 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Trace.WriteLine(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // methods
|
|
|
|
|
|
|
|
|
|
#region commands
|
|
|
|
|
|
2016-11-18 01:23:45 -05:00
|
|
|
|
public ICommand LaunchSettingsWindow {
|
2016-11-17 13:06:17 -05:00
|
|
|
|
get {
|
|
|
|
|
if (_launchWindowCommand == null) {
|
|
|
|
|
_launchWindowCommand = new RelayCommand(() => {
|
|
|
|
|
if (_statusGui == null) {
|
|
|
|
|
_statusGui = new MainWindow();
|
|
|
|
|
_statusGui.DataContext = WindowVM;
|
|
|
|
|
_statusGui.Closed += (s, e) => { _statusGui = null; };
|
|
|
|
|
_statusGui.Show();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
_statusGui.WindowState = WindowState.Normal;
|
|
|
|
|
_statusGui.Activate();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return _launchWindowCommand;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 01:23:45 -05:00
|
|
|
|
|
|
|
|
|
public ICommand SendTakeFiveRequest {
|
|
|
|
|
get {
|
|
|
|
|
if (_takeFiveCommand == null) {
|
|
|
|
|
_takeFiveCommand = new RelayCommand(() => {
|
2016-11-30 03:18:17 -05:00
|
|
|
|
var request = new WifiSitterIpcMessage("take_five", _myChannel).ToJsonString();
|
|
|
|
|
var reqMessage = new NetMQMessage();
|
|
|
|
|
reqMessage.Append(_mqClient.Options.Identity);
|
|
|
|
|
reqMessage.AppendEmptyFrame();
|
|
|
|
|
reqMessage.Append(request);
|
|
|
|
|
bool success = _mqClient.TrySendMultipartMessage(reqMessage);
|
|
|
|
|
if (!success) Trace.WriteLine("Failed to send take_five");
|
2016-11-18 01:23:45 -05:00
|
|
|
|
// TODO need response validation mechanism
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return _takeFiveCommand;
|
|
|
|
|
}
|
2016-11-30 03:43:34 -05:00
|
|
|
|
}
|
2016-11-18 01:23:45 -05:00
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // commands
|
|
|
|
|
|
|
|
|
|
#region events
|
2016-11-17 13:37:57 -05:00
|
|
|
|
|
2016-11-30 03:43:34 -05:00
|
|
|
|
private void _mqClient_ReceiveReady(object sender, NetMQSocketEventArgs e) {
|
|
|
|
|
bool hasmore = false;
|
|
|
|
|
e.Socket.Receive(out hasmore);
|
|
|
|
|
if (hasmore) {
|
|
|
|
|
// TODO handle responses
|
|
|
|
|
Trace.WriteLine("Response received.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 13:06:17 -05:00
|
|
|
|
#endregion // events
|
|
|
|
|
}
|
|
|
|
|
}
|