Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Update Status of Lock in View
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBiene committed Dec 21, 2016
1 parent 0f278ca commit 3a64e7b
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 40 deletions.
12 changes: 6 additions & 6 deletions Nuki UWP/Services/SettingsServices/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Nuki.Services.SettingsServices
public class SettingsService
{
private static ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger(nameof(SettingsService));
private static XmlSerializer s_XmlSerializer = new XmlSerializer(typeof(NukiConnectionBinding[]));
private static XmlSerializer s_XmlSerializer = new XmlSerializer(typeof(NukiConnectionConfig[]));
public static SettingsService Instance { get; } = new SettingsService();
Template10.Services.SettingsService.ISettingsHelper _helper;

Expand All @@ -29,9 +29,9 @@ private SettingsService()
PairdLocks.CollectionChanged += PairdLocks_CollectionChanged;
}

private NukiConnectionBinding[] LoadBluetoothConnectionInfo()
private NukiConnectionConfig[] LoadBluetoothConnectionInfo()
{
NukiConnectionBinding[] retValues = new NukiConnectionBinding[0];
NukiConnectionConfig[] retValues = new NukiConnectionConfig[0];
try
{
string strLockSettings = _helper.Read<string>(nameof(PairdLocks), string.Empty);
Expand All @@ -43,7 +43,7 @@ private NukiConnectionBinding[] LoadBluetoothConnectionInfo()
w.Write(strLockSettings);

mem.Position = 0;
retValues = s_XmlSerializer.Deserialize(mem) as NukiConnectionBinding[];
retValues = s_XmlSerializer.Deserialize(mem) as NukiConnectionConfig[];
}
}
else { }
Expand Down Expand Up @@ -79,8 +79,8 @@ private void PairdLocks_CollectionChanged(object sender, NotifyCollectionChanged
}


public ObservableCollection<NukiConnectionBinding> PairdLocks { get; private set; }
= new ObservableCollection<NukiConnectionBinding>();
public ObservableCollection<NukiConnectionConfig> PairdLocks { get; private set; }
= new ObservableCollection<NukiConnectionConfig>();

public bool UseShellBackButton
{
Expand Down
50 changes: 40 additions & 10 deletions Nuki UWP/ViewModels/NukiLockHomePartViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,25 @@ public override async Task OnNavigatedToAsync(object parameter, NavigationMode m
{
LockRingState = "Connecting...";
Log.Info("OnNavigatedToAsync");
if(BaseModel?.NukiConncetion != null)
{
BaseModel.NukiConncetion.PropertyChanged -= NukiConncetion_PropertyChanged;
BaseModel.NukiConncetion.PropertyChanged += NukiConncetion_PropertyChanged;
}
else { }
await base.OnNavigatedToAsync(parameter, mode, state);
await RefreshNukiState();
}

private void NukiConncetion_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(BaseModel.NukiConncetion.LastKnownDeviceState))
{
UpdateNukiDeviceState(BaseModel.NukiConncetion.LastKnownDeviceState).GetAwaiter();
}
else { }
}

private async Task RefreshNukiState()
{
LockRingState = "Requsting state...";
Expand All @@ -122,22 +137,37 @@ private async Task RefreshNukiState()
{
Log.Error("Failed to request Nuki Stat: {0}", ex);
}
await UpdateNukiDeviceState(nukiStateCmd);
SendCalibrateCommand.RaiseCanExecuteChanged();
SendLockCommand.RaiseCanExecuteChanged();
SendUnlockCommand.RaiseCanExecuteChanged();
BaseModel.ShowProgressbar(false);
}

if (nukiStateCmd != null)
private async Task UpdateNukiDeviceState(INukiDeviceStateMessage nukiStateCmd)
{
var action = new Action(() =>
{
if (nukiStateCmd != null)
{
LockState = nukiStateCmd.LockState;
NukiState = nukiStateCmd.NukiState;
CriticalBattery = nukiStateCmd.CriticalBattery;
LockRingState = LockState.ToString();
}
else
{
LockState = NukiLockState.Undefined;
}
});
if (Dispatcher != null)
{
LockState = nukiStateCmd.LockState;
NukiState = nukiStateCmd.NukiState;
CriticalBattery = nukiStateCmd.CriticalBattery;
LockRingState = LockState.ToString();
await Dispatcher.DispatchAsync(action);
}
else
{
LockState = NukiLockState.Undefined;
action();
}
SendCalibrateCommand.RaiseCanExecuteChanged();
SendLockCommand.RaiseCanExecuteChanged();
SendUnlockCommand.RaiseCanExecuteChanged();
BaseModel.ShowProgressbar(false);
}
}
}
10 changes: 5 additions & 5 deletions Nuki UWP/ViewModels/NukiLockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Nuki.ViewModels
{
public partial class NukiLockViewModel : PivotBaseViewModel<NukiLockViewModel>
{
private NukiConnectionBinding m_NukiConnectionBinding = null;
private NukiConnectionConfig m_NukiConnectionBinding = null;
private Visibility m_ProgressbarVisibility = Visibility.Collapsed;


public string SelectedLock
{
get { return NukiConncection?.ConnectionName ?? string.Empty; }
get { return NukiConnectionConfig?.ConnectionName ?? string.Empty; }

}

Expand All @@ -43,7 +43,7 @@ public void ShowProgressbar(bool blnVisibility)

public INukiConnection NukiConncetion { get; private set; }

public NukiConnectionBinding NukiConncection
public NukiConnectionConfig NukiConnectionConfig
{
get { return m_NukiConnectionBinding; }
set
Expand All @@ -57,9 +57,9 @@ public NukiConnectionBinding NukiConncection

public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
{
NukiConncection = SettingsService.Instance.PairdLocks.Where((l) => l.UniqueClientID.Value == parameter as uint?).FirstOrDefault();
NukiConnectionConfig = SettingsService.Instance.PairdLocks.Where((l) => l.UniqueClientID.Value == parameter as uint?).FirstOrDefault();

var connectResult = await NukiConnectionFactory.TryConnect(NukiConncection, (action) => Dispatcher.DispatchAsync(action,priority: CoreDispatcherPriority.Low).AsAsyncAction());
var connectResult = await NukiConnectionFactory.TryConnect(NukiConnectionConfig, (action) => Dispatcher.DispatchAsync(action,priority: CoreDispatcherPriority.Low).AsAsyncAction());

NukiConncetion = connectResult.Connection;
RaisePropertyChanged(nameof(NukiConncetion));
Expand Down
32 changes: 27 additions & 5 deletions Nuki.Communication/Connection/Bluetooth/BluetoothConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Nuki.Communication.Connection.Bluetooth.Commands.Request;
using Nuki.Communication.Connection.Bluetooth.Commands.Response;
using Nuki.Communication.SemanticTypes;
using Nuki.Communication.Util;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -21,7 +22,7 @@

namespace Nuki.Communication.Connection.Bluetooth
{
public class BluetoothConnection : IConnectionContext, INukiConnection
public class BluetoothConnection : NotifyPropertyChanged, IConnectionContext, INukiConnection
{
public static readonly BluetoothServiceUUID KeyTurnerPairingService = new BluetoothServiceUUID("a92ee100550111e4916c0800200c9a66");
public static readonly BluetoothServiceUUID KeyTurnerInitializingService = new BluetoothServiceUUID("a92ee000550111e4916c0800200c9a66");
Expand All @@ -40,12 +41,13 @@ public class BluetoothConnection : IConnectionContext, INukiConnection
private BluetoothGattCharacteristicConnection m_UGDIO = null;
private object Syncroot = new object();
private BluetoothLEDevice m_bleDevice = null;
public INukiDeviceStateMessage m_LastDeviceState = null;
public bool Connected => m_bleDevice != null;

public INukiDeviceStateMessage LastKnownDeviceState { get { return m_LastDeviceState; } set { Set(ref m_LastDeviceState, value); } }

public string DeviceName => m_connectionInfo.DeviceName;
public static Collection Connections => Collection.Instance;
private NukiConnectionBinding m_connectionInfo = new NukiConnectionBinding();
private NukiConnectionConfig m_connectionInfo = new NukiConnectionConfig();
public ClientPublicKey ClientPublicKey => m_connectionInfo.ClientPublicKey;
public SmartLockPublicKey SmartLockPublicKey => m_connectionInfo.SmartLockPublicKey;
public SharedKey SharedKey => m_connectionInfo.SharedKey;
Expand All @@ -58,6 +60,8 @@ public SmartLockNonce SmartLockNonce
internal set;
}



public class Collection
{
public static Regex regex = new Regex(
Expand Down Expand Up @@ -93,6 +97,23 @@ public BluetoothConnection this[string strDeviceName]
}
}

internal void Update(RecieveErrorReportCommand recieveErrorReportCommand)
{
Log.Error(recieveErrorReportCommand.ToString());
}

internal void Update(RecieveNukiStatesCommand recieveNukiStatesCommand)
{
Log.Debug("Recieved new SmartLock state...");
LastKnownDeviceState = recieveNukiStatesCommand;
}

internal void Update(RecieveChallengeCommand recieveChallengeCommand)
{
Log.Debug("Recieved new SmartLock Nonce...");
this.SmartLockNonce = recieveChallengeCommand.Nonce;
}

private BluetoothConnection(string strDeviceName)
{
m_connectionInfo.DeviceName = strDeviceName;
Expand All @@ -101,7 +122,7 @@ private BluetoothConnection(string strDeviceName)
//m_GDIO = new BluetoothGattCharacteristicConnectionEncrypted(this);
m_UGDIO = new BluetoothGattCharacteristicConnectionEncrypted(this);
}
public Task<bool> Connect(NukiConnectionBinding connectionInfo)
public Task<bool> Connect(NukiConnectionConfig connectionInfo)
{
if (connectionInfo == null)
throw new ArgumentNullException(nameof(connectionInfo));
Expand Down Expand Up @@ -182,7 +203,7 @@ public async Task<bool> Connect(string strDeviceID)
{
return await Connect(strDeviceID, null) >= ConnectResult.Successfull;
}
public async Task<ConnectResult> Connect(string strDeviceID, NukiConnectionBinding connectionInfo)
public async Task<ConnectResult> Connect(string strDeviceID, NukiConnectionConfig connectionInfo)
{
ConnectResult result = ConnectResult.Failed;

Expand Down Expand Up @@ -219,6 +240,7 @@ public async Task<ConnectResult> Connect(string strDeviceID, NukiConnectionBindi
m_bleDevice.ConnectionStatusChanged -= M_bleDevice_ConnectionStatusChanged;
m_bleDevice = deviceService.Device;
m_bleDevice.ConnectionStatusChanged += M_bleDevice_ConnectionStatusChanged;
RaisePropertyChanged(nameof(Connected));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BluetoothConnectionFactory : INukiConnectionFactory
{
private static ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger(nameof(BluetoothConnectionFactory));

public async Task<NukiConnectResult> TryConnect(NukiConnectionBinding connectionInfo, Func<Action, IAsyncAction> dispatch, int nTimeout = 3000)
public async Task<NukiConnectResult> TryConnect(NukiConnectionConfig connectionInfo, Func<Action, IAsyncAction> dispatch, int nTimeout = 3000)
{

bool blnRet = false;
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task<NukiConnectResult> TryConnect(NukiConnectionBinding connection
}


private Task<INukiConnection> TryConnect(NukiConnectionBinding connectionInfo, Func<Action, IAsyncAction> dispatch)
private Task<INukiConnection> TryConnect(NukiConnectionConfig connectionInfo, Func<Action, IAsyncAction> dispatch)
{
return Task.Run(async () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ private void M_GattCharacteristic_ValueChanged(GattCharacteristic sender, GattVa
var cmd = m_cmdInProgress;
m_cmdInProgress = null;
Log.Info($"Recieved Command {cmd}...");
if (m_responseWaitHandle?.TrySetResult(cmd) != true)



if (m_responseWaitHandle?.TrySetResult(cmd) == true)
{
Log.Warn($"Recieved Command {cmd} is not handlet...");
Log.Debug("m_responseWaitHandle set");
}
else if (cmd is RecieveNukiStatesCommand)
Connection.Update(cmd as RecieveNukiStatesCommand);
else if (cmd is RecieveChallengeCommand)
Connection.Update(cmd as RecieveChallengeCommand);
else if(cmd is RecieveErrorReportCommand)
Connection.Update(cmd as RecieveErrorReportCommand);
else
{
Log.Debug("m_responseWaitHandle set");
Log.Warn($"Recieved Command {cmd} is not handlet...");
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Nuki.Communication.Connection.Bluetooth
public class BluetoothPairResult
{
public BlutoothPairStatus Status { get; private set; }
public NukiConnectionBinding ConnectionInfo { get; private set; }
public BluetoothPairResult(BlutoothPairStatus status, NukiConnectionBinding connectionInfo)
public NukiConnectionConfig ConnectionInfo { get; private set; }
public BluetoothPairResult(BlutoothPairStatus status, NukiConnectionConfig connectionInfo)
{
Status = status;
ConnectionInfo = connectionInfo;
Expand Down
4 changes: 3 additions & 1 deletion Nuki.Communication/Connection/INukiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
using Nuki.Communication.Connection.Bluetooth.Commands.Response;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nuki.Communication.Connection
{
public interface INukiConnection
public interface INukiConnection: INotifyPropertyChanged
{

bool Connected { get; }
string DeviceName { get; }
INukiDeviceStateMessage LastKnownDeviceState { get; }

Task<INukiDeviceStateMessage> RequestNukiState();
Task<INukiReturnMessage> SendCalibrateRequest(ushort securityPin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Nuki.Communication.Connection
{
public class NukiConnectionBinding : NotifyPropertyChanged
public class NukiConnectionConfig : NotifyPropertyChanged
{
private ClientPublicKey m_ClientPublicKey = null;
private SharedKey m_SharedKey = null;
Expand Down
4 changes: 2 additions & 2 deletions Nuki.Communication/Connection/NukiConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static NukiConnectionFactory()
s_Factories.AddFirst(new BluetoothConnectionFactory());
}

public static async Task<NukiConnectResult> TryConnect(NukiConnectionBinding connectionInfo, Func<Action, IAsyncAction> dispatch,int nTimeout = 3000)
public static async Task<NukiConnectResult> TryConnect(NukiConnectionConfig connectionInfo, Func<Action, IAsyncAction> dispatch,int nTimeout = 3000)
{
NukiConnectResult returnValue = null;
var current = s_Factories.First;
Expand All @@ -30,7 +30,7 @@ public static async Task<NukiConnectResult> TryConnect(NukiConnectionBinding con
}
internal interface INukiConnectionFactory
{
Task<NukiConnectResult> TryConnect(NukiConnectionBinding connectionInfo, Func<Action, IAsyncAction> dispatch, int nTimeout = 3000);
Task<NukiConnectResult> TryConnect(NukiConnectionConfig connectionInfo, Func<Action, IAsyncAction> dispatch, int nTimeout = 3000);
}

public class NukiConnectResult
Expand Down
2 changes: 1 addition & 1 deletion Nuki.Communication/Nuki.Communication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<Compile Include="Connection\Bluetooth\Commands\Response\RecievePublicKeyCommand.cs" />
<Compile Include="Connection\Bluetooth\Commands\Response\RecieveStatusCommand.cs" />
<Compile Include="Connection\Bluetooth\BluetoothConnection.cs" />
<Compile Include="Connection\NukiConnectionBinding.cs" />
<Compile Include="Connection\NukiConnectionConfig.cs" />
<Compile Include="Connection\Bluetooth\BluetoothConnectionFactory.cs" />
<Compile Include="Connection\Bluetooth\BluetoothGattCharacteristicConnection.cs" />
<Compile Include="Connection\Bluetooth\BluetoothGattCharacteristicConnectionEncrypted.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Nuki.Communication/Util/NotifyPropertyChanged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand All @@ -21,7 +21,7 @@ public bool Set<T>(ref T storage, T value, [CallerMemberName()]string propertyNa
{
if (!object.Equals(storage, value)) {
storage = value;
OnPropertyChanged(propertyName);
RaisePropertyChanged(propertyName);
return true;
}
return false;
Expand Down

0 comments on commit 3a64e7b

Please sign in to comment.