From f847abdffcc27f944fae903cc77a03ea98be52a1 Mon Sep 17 00:00:00 2001 From: scoliono <2191476+scoliono@users.noreply.github.com> Date: Thu, 18 Feb 2021 19:52:07 -0800 Subject: [PATCH] Added IP-based location tracker that runs async (closes #6) pls no steal api key kthx :) --- FemboyWatchdog/FemboyWatchdog.csproj | 6 ++ FemboyWatchdog/LocationTracker.cs | 92 +++++++++++++++++++ .../Properties/Resources.Designer.cs | 2 +- .../Properties/Settings.Designer.cs | 2 +- FemboyWatchdog/Properties/Settings.settings | 6 +- FemboyWatchdog/Settings.cs | 28 ++++++ FemboyWatchdog/Toolbar.Designer.cs | 1 + FemboyWatchdog/Toolbar.cs | 30 ++++-- FemboyWatchdog/app.config | 5 +- FemboyWatchdog/packages.config | 1 + README.md | 4 +- 11 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 FemboyWatchdog/LocationTracker.cs create mode 100644 FemboyWatchdog/Settings.cs diff --git a/FemboyWatchdog/FemboyWatchdog.csproj b/FemboyWatchdog/FemboyWatchdog.csproj index 7089776..dbf86c5 100644 --- a/FemboyWatchdog/FemboyWatchdog.csproj +++ b/FemboyWatchdog/FemboyWatchdog.csproj @@ -74,6 +74,10 @@ ..\packages\AForge.Video.DirectShow.2.2.5\lib\AForge.Video.DirectShow.dll True + + ..\packages\Newtonsoft.Json.9.0.1\lib\net20\Newtonsoft.Json.dll + True + @@ -81,8 +85,10 @@ + + Form diff --git a/FemboyWatchdog/LocationTracker.cs b/FemboyWatchdog/LocationTracker.cs new file mode 100644 index 0000000..bdeaa65 --- /dev/null +++ b/FemboyWatchdog/LocationTracker.cs @@ -0,0 +1,92 @@ +using System; +using System.Net; +using System.Windows.Forms; +using Newtonsoft.Json; + +namespace FemboyWatchdog +{ + class LocationTracker + { + private WebClient _wcIp; + private WebClient _wcLocation; + private Timer _timer; + + public IPLocationData LocationData { get; private set; } + + internal class IPLocationData + { + public double latitude; + public double longitude; + public string region_name; + public string city; + public string country_name; + } + + + public LocationTracker(double interval = 15.0) + { + _wcIp = new WebClient(); + _wcLocation = new WebClient(); + _wcIp.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ip_DownloadStringCompleted); + _wcLocation.DownloadStringCompleted += new DownloadStringCompletedEventHandler(loc_DownloadStringCompleted); + + // get location for the first time + GetLocationFromIp(); + + _timer = new Timer(); + _timer.Interval = Convert.ToInt32(interval * 1000); + _timer.Tick += new EventHandler(_timer_Tick); + _timer.Start(); + } + + private void _timer_Tick(object sender, EventArgs e) + { + // i would use GeoCoordinateWatcher, but it is broken + GetLocationFromIp(); + } + + private void GetLocationFromIp() + { + try + { + if (!_wcIp.IsBusy) + _wcIp.DownloadStringAsync(new Uri("http://ipv4.icanhazip.com")); + } + catch (WebException err) + { + // fail silently + } + } + + private void loc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) + { + if (e.Cancelled || e.Error != null) + return; + + try + { + LocationData = JsonConvert.DeserializeObject(e.Result); + } + catch (JsonSerializationException err) { } + } + + void ip_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) + { + if (e.Cancelled || e.Error != null) + return; + + try + { + if (!_wcLocation.IsBusy) + _wcLocation.DownloadStringAsync(new Uri( + string.Format( + "http://api.ipstack.com/{0}?access_key={1}&fields=latitude,longitude,region_name,country_name,city", + e.Result, + "31d59064bb7ef00a9daff4794b73118c" + ) + )); + } + catch (WebException err) { } + } + } +} diff --git a/FemboyWatchdog/Properties/Resources.Designer.cs b/FemboyWatchdog/Properties/Resources.Designer.cs index 6ed1919..836395a 100644 --- a/FemboyWatchdog/Properties/Resources.Designer.cs +++ b/FemboyWatchdog/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.1008 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. diff --git a/FemboyWatchdog/Properties/Settings.Designer.cs b/FemboyWatchdog/Properties/Settings.Designer.cs index f356135..786af43 100644 --- a/FemboyWatchdog/Properties/Settings.Designer.cs +++ b/FemboyWatchdog/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.1008 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. diff --git a/FemboyWatchdog/Properties/Settings.settings b/FemboyWatchdog/Properties/Settings.settings index 3964565..8e615f2 100644 --- a/FemboyWatchdog/Properties/Settings.settings +++ b/FemboyWatchdog/Properties/Settings.settings @@ -1,7 +1,5 @@  - - - + - + \ No newline at end of file diff --git a/FemboyWatchdog/Settings.cs b/FemboyWatchdog/Settings.cs new file mode 100644 index 0000000..c7ce2d3 --- /dev/null +++ b/FemboyWatchdog/Settings.cs @@ -0,0 +1,28 @@ +namespace FemboyWatchdog.Properties { + + + // This class allows you to handle specific events on the settings class: + // The SettingChanging event is raised before a setting's value is changed. + // The PropertyChanged event is raised after a setting's value is changed. + // The SettingsLoaded event is raised after the setting values are loaded. + // The SettingsSaving event is raised before the setting values are saved. + internal sealed partial class Settings { + + public Settings() { + // // To add event handlers for saving and changing settings, uncomment the lines below: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // Add code to handle the SettingChangingEvent event here. + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // Add code to handle the SettingsSaving event here. + } + } +} diff --git a/FemboyWatchdog/Toolbar.Designer.cs b/FemboyWatchdog/Toolbar.Designer.cs index bcbd857..bb6b361 100644 --- a/FemboyWatchdog/Toolbar.Designer.cs +++ b/FemboyWatchdog/Toolbar.Designer.cs @@ -20,6 +20,7 @@ namespace FemboyWatchdog components.Dispose(); } + CloseCurrentVideoSource(); GlobalHooks.UnhookWindowsHookEx(_hookID); base.Dispose(disposing); diff --git a/FemboyWatchdog/Toolbar.cs b/FemboyWatchdog/Toolbar.cs index 2d10291..3225fa4 100644 --- a/FemboyWatchdog/Toolbar.cs +++ b/FemboyWatchdog/Toolbar.cs @@ -2,21 +2,23 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; +using System.Drawing.Imaging; +using System.IO; using System.Media; +using System.Net; using System.Runtime.InteropServices; using System.Speech.Synthesis; using System.Text; using System.Windows.Forms; using AForge.Video; using AForge.Video.DirectShow; -using System.Drawing.Imaging; -using System.IO; namespace FemboyWatchdog { public partial class Toolbar : Form { private DateTime clockIn = DateTime.Now; + private LocationTracker tracker; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoDevice; @@ -47,6 +49,8 @@ namespace FemboyWatchdog ss = new SpeechSynthesizer(); + tracker = new LocationTracker(); + ttsTimer = new Timer(); ttsTimer.Interval = 15000; labelBlinkTimer = new Timer(); @@ -67,13 +71,13 @@ namespace FemboyWatchdog OpenCamera(); } - void lastmousemvmtTimer_Tick(object sender, EventArgs e) + private void lastmousemvmtTimer_Tick(object sender, EventArgs e) { ++mousemvmtCount; lastmousemvmtTimer.Stop(); } - void mousemoveTimer_Tick(object sender, EventArgs e) + private void mousemoveTimer_Tick(object sender, EventArgs e) { if (mousemvmtCount < 5) { @@ -121,15 +125,21 @@ namespace FemboyWatchdog } else { + /* MessageBox.Show(this, "Camera device not found! Get a webcam!", "Error initializing camera", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-2); + */ + vsp.Dispose(); return; } videoCapabilities = videoDevice.VideoCapabilities; if (videoCapabilities.Length == 0) { + /* MessageBox.Show(this, "Camera does not support video capture! Get a better webcam!", "Error initializing camera", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-3); + */ + vsp.Dispose(); return; } @@ -141,19 +151,25 @@ namespace FemboyWatchdog } catch (Exception err) { + /* MessageBox.Show(this, err.ToString(), "Error initializing camera", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); + */ + vsp.Dispose(); } } - void labelBlinkTimer_Tick(object sender, EventArgs e) + private void labelBlinkTimer_Tick(object sender, EventArgs e) { this.label2.Visible = !this.label2.Visible; } - void ttsTimer_Tick(object sender, EventArgs e) + private void ttsTimer_Tick(object sender, EventArgs e) { - ss.SpeakAsync("You are being monitored"); + string msg = "You are being monitored."; + if (tracker.LocationData != null) + msg += string.Format(" Your location is: {0}, {1}", tracker.LocationData.latitude, tracker.LocationData.longitude); + ss.SpeakAsync(msg); } public void OpenVideoSource(IVideoSource source) diff --git a/FemboyWatchdog/app.config b/FemboyWatchdog/app.config index e59af44..7f5ee61 100644 --- a/FemboyWatchdog/app.config +++ b/FemboyWatchdog/app.config @@ -1,3 +1,6 @@ - + + + + diff --git a/FemboyWatchdog/packages.config b/FemboyWatchdog/packages.config index 31125fb..04c0b19 100644 --- a/FemboyWatchdog/packages.config +++ b/FemboyWatchdog/packages.config @@ -6,4 +6,5 @@ + \ No newline at end of file diff --git a/README.md b/README.md index 6f9c138..2aa9e46 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The ultimate corporate surveillance mechanism! ## User Requirements -- .NET Framework 3.0 +- .NET Framework 3.5 - Windows XP and up @@ -14,4 +14,6 @@ The ultimate corporate surveillance mechanism! - AForge.NET +- NuGet + - Visual C# 2010