private static void RunInDebugMode() { ConfigureDebugLogging(); Console.WriteLine("Running in debug"); var deviceMonitor = new DeviceMonitor(ConfigurationManager.ConnectionStrings["MetroMonitorData"].ConnectionString); deviceMonitor.Initialise(); deviceMonitor.Start(); Console.WriteLine("Press <enter> to stop or 'cls' and <enter> to clear the log"); while (true) { var command = Console.ReadLine() ?? ""; switch (command) { case "cls": Console.Clear(); break; default: deviceMonitor.Stop(); return; } } }
public void Stop_ForInvoked_CallsStop( DeviceMonitor sut, [Freeze] IWatcher watcher) { sut.Stop(); watcher.Received() .Stop(); }
/// <SUMMARY> /// Shutsdown the server /// </SUMMARY> public void Stop() { lock (this) { _listener.Close(); _listener = null; //Close all active connections foreach (object obj in _connections) { ConnectionState st = obj as ConnectionState; try { st._provider.OnDropConnection(st.RemoteEndPoint.ToString()); } catch { //some error in the provider } st._conn.Shutdown(SocketShutdown.Both); st._conn.Close(); } _connections.Clear(); //stop device monitor DeviceMonitor.Stop(); } }
protected override void OnStop() { Logger.Info(i => i("Stopping Device Monitor")); _deviceMonitor.Stop(); Logger.Info(i => i("Service Successfully Stopped")); }
static void Main() { var scanner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? (IScanner) new PingAndArp() : (IScanner) new NMapScanner(); Console.WriteLine($"Inital IP scan of {SubnetAddress} using {scanner.GetType().Name}"); var sw = Stopwatch.StartNew(); var online = scanner.Scan(SubnetAddress).Online; foreach (var entry in online) { Console.WriteLine($"\t{entry.Ip}\t{entry.Mac}"); } Console.WriteLine($"Scan tool {sw.Elapsed}"); Console.WriteLine(); var devices = DeviceList.CreateFromFile("devices.json"); if (devices.Devices.Count(d => d.Type == DeviceType.HueBridge) != 1) { throw new Exception("Exactly one Hue Bridge must be defined in devices.json"); } devices.ProcessDiscovery(online); var hue = (HueBridge)devices.Devices.First(d => d.Type == DeviceType.HueBridge); Console.WriteLine("Bridge:"); Console.WriteLine($"\t{hue.Name,-30}\t{hue.Mac}\t{hue.OnlineStatus.Ip}\t{hue.OnlineStatus.Status}"); if (hue.OnlineStatus.Status != OnlineStatusOnline.Online) { throw new Exception("Hue Bridge is not online"); } Console.WriteLine(); var lights = hue.GetLights(); Console.WriteLine("Lights:"); foreach (var light in lights) { Console.WriteLine($"\t{light.Name,-30}\t{light.TurnedOn}"); } Console.WriteLine(); var groups = hue.GetGroups(); Console.WriteLine("Groups:"); foreach (var grp in groups) { Console.WriteLine($"\t{grp.Name,-30}\t{grp.TurnedOn}"); } Console.WriteLine(); var switchable = groups.Concat <ISwitchable>(lights).ToList(); devices.UpdateDevices(switchable); Console.WriteLine("TVs:"); var tvs = new DeviceList(devices.Devices.Where(d => d.Type == DeviceType.Tv)); tvs.ProcessDiscovery(online); foreach (var tv in tvs.Devices.Cast <Tv>()) { Console.WriteLine($"\t{tv.Name,-30}\t{tv.Mac}\t{tv.OnlineStatus.Status}\t{String.Join(',', tv.ControlNames)}"); } Console.WriteLine(); var monitor = new DeviceMonitor(SubnetAddress, scanner, tvs); monitor.DeviceChanged += (sender, data) => { Console.WriteLine($"\t{DateTime.Now:T}\t{data.Device.Name}\t{data.FromStatus}\t->\t{data.ToStatus}"); if (!(data.Device is Tv tv)) { return; } switch (data.ToStatus) { case OnlineStatusOnline.Online: foreach (var sw in tv.ControlDevices) { sw.TurnOn(); Console.WriteLine($"\t{DateTime.Now:T}\t{sw.Name} turned on"); } break; case OnlineStatusOnline.Offline: foreach (var sw in tv.ControlDevices) { sw.TurnOff(); Console.WriteLine($"\t{DateTime.Now:T}\t{sw.Name} turned off"); } break; } }; monitor.Start(); var waitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (sender, args) => { waitEvent.Set(); args.Cancel = true; }; Console.WriteLine("DeviceMonitor started, Ctrl-C to stop"); waitEvent.WaitOne(); monitor.Stop(); Console.WriteLine("DeviceMonitor stopped"); }