Created
November 3, 2013 05:20
-
-
Save DevJohnC/7287045 to your computer and use it in GitHub Desktop.
Simple AdjutantOS app meant for locating a device within a home.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Author: John Carruthers (johnc@frag-labs.com) | |
| // | |
| // Copyright (C) 2013 John Carruthers | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the | |
| // "Software"), to deal in the Software without restriction, including | |
| // without limitation the rights to use, copy, modify, merge, publish, | |
| // distribute, sublicense, and/or sell copies of the Software, and to | |
| // permit persons to whom the Software is furnished to do so, subject to | |
| // the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be | |
| // included in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| using System; | |
| using System.Threading.Tasks; | |
| using Android.App; | |
| using Android.Content; | |
| using Android.Runtime; | |
| using Android.Views; | |
| using Android.Widget; | |
| using Android.OS; | |
| using FragLabs.AdjutantOS.Client; | |
| using FragLabs.Adjutant.Network.Interfaces; | |
| using FragLabs.Adjutant.Network.Sockets; | |
| using FragLabs.Adjutant.Network.Presence; | |
| using FragLabs.AdjutantOS.API; | |
| using FragLabs.AdjutantOS.API.CoreModule.Dtos; | |
| using FragLabs.Adjutant.Network.Crypto; | |
| using FragLabs.AdjutantOS.API.Modules; | |
| using FragLabs.AdjutantOS.API.Modules.Domains; | |
| using FragLabs.AdjutantOS.API.Devices; | |
| using FragLabs.AdjutantOS.API.Location; | |
| using Logger = FragLabs.AdjutantOS.API.Log.Logger; | |
| using LogLevel = FragLabs.AdjutantOS.API.Log.LogLevel; | |
| namespace AudioLocator | |
| { | |
| [Activity (Label = "AudioLocator", MainLauncher = true)] | |
| public class MainActivity : Activity | |
| { | |
| Button _button; | |
| TextView _debugTxt; | |
| Network _network; | |
| protected override void OnCreate (Bundle bundle) | |
| { | |
| base.OnCreate (bundle); | |
| // Set our view from the "main" layout resource | |
| SetContentView (Resource.Layout.Main); | |
| // Get our button from the layout resource, | |
| // and attach an event to it | |
| _button = FindViewById<Button> (Resource.Id.myButton); | |
| _debugTxt = FindViewById<TextView> (Resource.Id.debugTxt); | |
| _button.Click += delegate { | |
| if (!_network.IsConnected) | |
| { | |
| _debugTxt.Text += System.Environment.NewLine + "Not connected to the server"; | |
| return; | |
| } | |
| _debugTxt.Text += System.Environment.NewLine + "Starting audio locator ping"; | |
| Task.Factory.StartNew(()=>{ | |
| var rooms = LocationRepository.All<Room>(); | |
| var audioDetector = new AudioDetector(); | |
| foreach(var room in rooms) | |
| { | |
| WriteDebug(String.Format("Audio ping {0} for 50ms", room.Name)); | |
| ServiceInvoker.CallOneWay(DeviceRepository.Server, new AudioPing | |
| { | |
| LocationId = room.Id, | |
| Duration = TimeSpan.FromMilliseconds(50) | |
| }); | |
| // runs the audio pattern detector with a grace period of time to account for communication delays | |
| audioDetector.RunDetector(room, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(50)); | |
| } | |
| if (audioDetector.LoudestLocation == null) | |
| { | |
| WriteDebug("Locating failed, you are lost"); | |
| } | |
| else | |
| { | |
| WriteDebug(String.Format("Found you! You're hiding in {0}", audioDetector.LoudestLocation.Name)); | |
| } | |
| }); | |
| }; | |
| FragLabs.AdjutantOS.API.Log.Logger.Init (System.Console.Out); | |
| DomainManager.Initialize(typeof(FragLabs.AdjutantOS.API.AccountRepository).Assembly); | |
| SetupEnvironment (); | |
| // static pre-generated rsa key for testing purposes | |
| var privateKey = "<RSAKeyValue><Modulus>rgjsx/sauzHW1gGh3v4AdcEClpJYxlFGh4crNjMJpm62cUT9xlcXiXBfesISvQ6Yji3xTTvsWaoHdOLnOs4+jrtJGX4bHXRV1FsMyVOaSM916w+uxTZAris5Yr67xhIBmMnLEkYhQuxHe+isPPzSe5YjI+mNUbbu4fHRUtqgLts=</Modulus><Exponent>EQ==</Exponent><P>sqn7cKlV/OKOUydbefMBBK/F2Jgwoy7CaTF/Cl5ZAe+hJHMmXGhGE6fp7z7er2KYUdGP5ktwvKNVrGoFLoIdiw==</P><Q>+V37apiu663iBzUBNSW9sCuhKjIqrRwV2I2zAI/pJhsURn67Uf2KFD47+VCjRKCtlgrKzxsbqF0Dw/DtrtD98Q==</Q><DP>NIxZAwSg0ehH+lbeq2WW40LBuCzDAtGEeUrLAwywxFWJv20pZnkFjU+BCiGMyiwOrqcMNKzGzhHsBYiYHLzbgw==</DP><DQ>WAMNcOqYFvITihK1IdEVxbULtIotLgnpl7mKeKtDOqAlRg6cd0ps+BX3DLMMcpMQFtah7r5F/y/jNhjMW9FKkQ==</DQ><InverseQ>Nuhne0ugc9CjkLQxLQ7NY7/gadEtK8qPFgjKx3kK5zgWYnTx7uhoR5N3WXebqu6vtT8dP5ZC7ksgdl/bvYozyA==</InverseQ><D>KPMKiWhChmYUbpb46Sy00GmmQYvYpyIutnooSPzzNjgq7XmlH5wFicAWdzy5HWzYmeyxP1lkyc2nZsv6K/RK9AOW2vkkBcaIzDxlvzXEIH668eTqc0UqyOukR7HWAOvBnukkC27+7dPaqtLZlWt8DKF6ETlyiMf74+I5+HfV5nE=</D></RSAKeyValue>"; | |
| var publicKey = "<RSAKeyValue><Modulus>rgjsx/sauzHW1gGh3v4AdcEClpJYxlFGh4crNjMJpm62cUT9xlcXiXBfesISvQ6Yji3xTTvsWaoHdOLnOs4+jrtJGX4bHXRV1FsMyVOaSM916w+uxTZAris5Yr67xhIBmMnLEkYhQuxHe+isPPzSe5YjI+mNUbbu4fHRUtqgLts=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>"; | |
| _network = new Network (44127, publicKey); | |
| _network.MessageSigner = new FragLabs.Adjutant.Network.Crypto.RsaSigner(privateKey); | |
| _network.Connected += HandleConnected; | |
| _network.VerifyServer += (sender, e) => { e.IsVerified = true; }; | |
| _network.Start (); | |
| } | |
| void HandleConnected (object sender, ConnectedEventArgs e) | |
| { | |
| DeviceRepository.Server = new Server(e.Client, _network.MessageSigner); | |
| Logger.Write(LogLevel.Debug, "Announcing presence to server"); | |
| ServiceInvoker.Call<AnnounceResponse> (DeviceRepository.Server, new Announce { | |
| Hostname = System.Environment.MachineName, | |
| NodeType = FragLabs.AdjutantOS.API.Environment.NodeType, | |
| Platform = FragLabs.AdjutantOS.API.Environment.Platform, | |
| PhysicalAddress = "", | |
| InstalledApps = new Guid[0], | |
| RunningApps = new Guid[0] | |
| }).ContinueWith (t => { | |
| try{ | |
| var response = t.Result; | |
| Logger.Write(LogLevel.Debug, "Device id: {0}", response.DeviceId); | |
| WriteDebug ("Connected to server"); | |
| } catch (Exception ex){ | |
| WriteDebug ("Failed to connect to the server"); | |
| Logger.Write(LogLevel.Debug, "Failed connecting to server: {0}", ex); | |
| } | |
| }); | |
| } | |
| void WriteDebug(string message) | |
| { | |
| RunOnUiThread (()=>{ | |
| _debugTxt.Text += System.Environment.NewLine + message; | |
| }); | |
| } | |
| static void SetupEnvironment() | |
| { | |
| FragLabs.AdjutantOS.API.Environment.NodeType = FragLabs.AdjutantOS.API.Network.NodeType.Client; | |
| Logger.Write(LogLevel.Debug, "Running on: {0}", FragLabs.AdjutantOS.API.Environment.Platform.OperatingSystem); | |
| Logger.Write(LogLevel.Debug, "System uptime: {0}", FragLabs.AdjutantOS.API.Environment.Platform.Uptime); | |
| Logger.Write(LogLevel.Debug, "Is raspberry pi: {0}", FragLabs.AdjutantOS.API.Environment.Platform.IsRaspberryPi); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment