Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created November 23, 2013 08:17
Show Gist options
  • Select an option

  • Save DevJohnC/7612143 to your computer and use it in GitHub Desktop.

Select an option

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