Created
November 23, 2013 08:17
-
-
Save DevJohnC/7612143 to your computer and use it in GitHub Desktop.
AdjutantOS addon for Android, this turns my office equipment (lights, computers) on and off at the press of a button.
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
| using System; | |
| using Android.App; | |
| using Android.Content; | |
| using Android.Runtime; | |
| using Android.Views; | |
| using Android.Widget; | |
| using Android.OS; | |
| using FragLabs.AdjutantOS.API; | |
| using FragLabs.AdjutantOS.API.Addons; | |
| using FragLabs.AdjutantOS.API.Services; | |
| namespace AdjutantPowerSwitch | |
| { | |
| [Activity (Label = "Office Power Switch", MainLauncher = true)] | |
| public class MainActivity : Activity | |
| { | |
| const string AdjutantServiceIntent = "com.adjutantos.AdjutantService"; | |
| Button _btnOn; | |
| Button _btnOff; | |
| AddonClient _addonClient; | |
| bool _attemptedServiceStart; | |
| protected override void OnCreate (Bundle bundle) | |
| { | |
| base.OnCreate (bundle); | |
| // Set our view from the "main" layout resource | |
| SetContentView (Resource.Layout.Main); | |
| AddonClient.Bootstrap(); | |
| Console.WriteLine(FragLabs.AdjutantOS.API.Environment.IsAddon); | |
| Console.WriteLine(FragLabs.AdjutantOS.API.Environment.NodeType); | |
| _addonClient = new AddonClient(); | |
| _addonClient.ConnectComplete += HandleConnectComplete; | |
| _addonClient.Connect(); | |
| _btnOn = FindViewById<Button>(Resource.Id.btnOn); | |
| _btnOff = FindViewById<Button>(Resource.Id.btnOff); | |
| _btnOn.Click += (sender, e) => { | |
| if (_addonClient.IsConnected && _addonClient.GetConnectionInformation().ConnectedToServer) | |
| { | |
| ServiceInvoker.CallOneWay(DeviceRepository.Server, new OfficeSwitch() { OnOff = "on" }); | |
| Toast.MakeText(this, "Turning on the office", ToastLength.Short).Show(); | |
| } | |
| else | |
| { | |
| Toast.MakeText(this, "Not connected to server", ToastLength.Short).Show(); | |
| if (!_addonClient.IsConnected) | |
| _addonClient.Connect(); | |
| } | |
| }; | |
| _btnOff.Click += (sender, e) => { | |
| if (_addonClient.IsConnected && _addonClient.GetConnectionInformation().ConnectedToServer) | |
| { | |
| ServiceInvoker.CallOneWay(DeviceRepository.Server, new OfficeSwitch() { OnOff = "off" }); | |
| Toast.MakeText(this, "Turning off the office", ToastLength.Short).Show(); | |
| } | |
| else | |
| { | |
| Toast.MakeText(this, "Not connected to server", ToastLength.Short).Show(); | |
| if (!_addonClient.IsConnected) | |
| _addonClient.Connect(); | |
| } | |
| }; | |
| } | |
| void HandleConnectComplete (object sender, AddonClientEventArgs e) | |
| { | |
| if (!e.Success && !_attemptedServiceStart) | |
| { | |
| // service isn't running, attempt to start it | |
| _attemptedServiceStart = true; | |
| StartService(new Intent(AdjutantServiceIntent)); | |
| _addonClient.Connect(); | |
| } | |
| else if (!e.Success && _attemptedServiceStart) | |
| { | |
| RunOnUiThread(()=>{ | |
| Toast.MakeText(this, "Failed to start AdjutantOS service", ToastLength.Short).Show(); | |
| }); | |
| } | |
| else if (e.Success) | |
| { | |
| RunOnUiThread(()=>{ | |
| Toast.MakeText(this, "Connected to server", ToastLength.Short).Show(); | |
| }); | |
| } | |
| } | |
| } | |
| public class OfficeSwitch : IRequestDto | |
| { | |
| public string GetUri() | |
| { | |
| return "/anonymous/officeswitch"; | |
| } | |
| public string OnOff { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment