Added IP-based location tracker that runs async (closes #6)

pls no steal api key kthx :)
This commit is contained in:
scoliono 2021-02-18 19:52:07 -08:00
parent a9556ba259
commit f847abdffc
11 changed files with 162 additions and 15 deletions

View File

@ -74,6 +74,10 @@
<HintPath>..\packages\AForge.Video.DirectShow.2.2.5\lib\AForge.Video.DirectShow.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net20\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Speech" />
@ -81,8 +85,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="GlobalHooks.cs" />
<Compile Include="LocationTracker.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Toolbar.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -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<IPLocationData>(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) { }
}
}
}

View File

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.

View File

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.

View File

@ -1,7 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Profiles />
<Settings />
</SettingsFile>
</SettingsFile>

View File

@ -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.
}
}
}

View File

@ -20,6 +20,7 @@ namespace FemboyWatchdog
components.Dispose();
}
CloseCurrentVideoSource();
GlobalHooks.UnhookWindowsHookEx(_hookID);
base.Dispose(disposing);

View File

@ -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)

View File

@ -1,3 +1,6 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
<configSections>
</configSections>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>

View File

@ -6,4 +6,5 @@
<package id="AForge.Math" version="2.2.5" targetFramework="net30" />
<package id="AForge.Video" version="2.2.5" targetFramework="net30" />
<package id="AForge.Video.DirectShow" version="2.2.5" targetFramework="net30" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net30" />
</packages>

View File

@ -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