Skip to content

Commit

Permalink
wip: backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ngenovese11 committed Oct 17, 2022
1 parent 8acb0f2 commit 58a4f03
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 44 deletions.
12 changes: 6 additions & 6 deletions src/PepperDashRoomOs.Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace PepperDashRoomOs.Core
{
public static class Extensions
{
public static int ParseContactMethodIndex(this PhonebookSearchResult contact, string response)
public static int ParseContactMethodIndex(this PhonebookSearchContactResult contact, string response)
{
try
{
Expand All @@ -20,35 +20,35 @@ public static int ParseContactMethodIndex(this PhonebookSearchResult contact, st
}
}

public static PhonebookSearchResult ParseContactName(this PhonebookSearchResult contact, string response)
public static PhonebookSearchContactResult ParseContactName(this PhonebookSearchContactResult contact, string response)
{
var stringToRemove = string.Format("{0}{1} Name: ", RoomOsAddressBook.PhonebookContactResultStart, contact.Index);
contact.Name = response.Remove(0, stringToRemove.Length).TrimStart(new[] { '"' }).TrimEnd(new[] { '"' });
return contact;
}

public static PhonebookSearchResult ParseContactId(this PhonebookSearchResult contact, string response)
public static PhonebookSearchContactResult ParseContactId(this PhonebookSearchContactResult contact, string response)
{
var stringToRemove = string.Format("{0}{1} ContactId: ", RoomOsAddressBook.PhonebookContactResultStart, contact.Index);
contact.ContactId = response.Remove(0, stringToRemove.Length).TrimStart(new[] { '"' }).TrimEnd(new[] { '"' });
return contact;
}

public static PhonebookSearchResultContactMethod ParseContactMethodId(this PhonebookSearchResultContactMethod method, string response, PhonebookSearchResult contact)
public static PhonebookSearchResultContactMethod ParseContactMethodId(this PhonebookSearchResultContactMethod method, string response, PhonebookSearchContactResult contact)
{
var stringToRemove = string.Format("{0}{1} ContactMethod {2} ContactMethodId: ", RoomOsAddressBook.PhonebookContactResultStart, contact.Index, method.Index);
method.ContactMethodId = response.Remove(0, stringToRemove.Length).TrimStart(new[] { '"' }).TrimEnd(new[] { '"' });
return method;
}

public static PhonebookSearchResultContactMethod ParseContactMethodNumber(this PhonebookSearchResultContactMethod method, string response, PhonebookSearchResult contact)
public static PhonebookSearchResultContactMethod ParseContactMethodNumber(this PhonebookSearchResultContactMethod method, string response, PhonebookSearchContactResult contact)
{
var stringToRemove = string.Format("{0}{1} ContactMethod {2} Number: ", RoomOsAddressBook.PhonebookContactResultStart, contact.Index, method.Index);
method.Number = response.Remove(0, stringToRemove.Length).TrimStart(new[] { '"' }).TrimEnd(new[] { '"' });
return method;
}

public static PhonebookSearchResult ParseContactMethod(this PhonebookSearchResult contact, string response, uint debugLevel)
public static PhonebookSearchContactResult ParseContactMethod(this PhonebookSearchContactResult contact, string response, uint debugLevel)
{
var index = contact.ParseContactMethodIndex(response);
if (index == 0)
Expand Down
3 changes: 2 additions & 1 deletion src/PepperDashRoomOs.Core/PepperDashRoomOs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
<ItemGroup>
<Compile Include="Events\SearchProgressUpdatedArgs.cs" />
<Compile Include="Events\SearchResultReceivedArgs.cs" />
<Compile Include="PhonebookSearchResult.cs" />
<Compile Include="PhonebokSearchResult.cs" />
<Compile Include="PhonebookSearchContactResult.cs" />
<Compile Include="PhonebookSearchResultContactMethod.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="PhonebookType.cs" />
Expand Down
17 changes: 17 additions & 0 deletions src/PepperDashRoomOs.Core/PhonebokSearchResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;

namespace PepperDashRoomOs.Core
{
public class PhonebokSearchResult
{
public string Id { get; set; }

public List<PhonebookSearchContactResult> Contacts { get; set; }

public PhonebokSearchResult()
{
Contacts = new List<PhonebookSearchContactResult>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PepperDashRoomOs.Core
{
public class PhonebookSearchResult
public class PhonebookSearchContactResult
{
public int Index { get; set; }
public string ContactId { get; set; }
Expand All @@ -11,7 +11,7 @@ public class PhonebookSearchResult
public string LastName { get; set; }
public List<PhonebookSearchResultContactMethod> ContactMethods { get; set; }

public PhonebookSearchResult()
public PhonebookSearchContactResult()
{
ContactMethods = new List<PhonebookSearchResultContactMethod>();
}
Expand Down
96 changes: 61 additions & 35 deletions src/PepperDashRoomOs.Core/RoomOsAddressBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace PepperDashRoomOs.Core
{
public class RoomOsAddressBook
{
private readonly CTimer _requestDebounce;
private readonly List<PhonebookSearchResult> _searchResults = new List<PhonebookSearchResult>();
private PhonebokSearchResult _phonebokSearchResult = new PhonebokSearchResult();

private bool _searchResponseActive;
private bool _searchRequested;
Expand All @@ -29,21 +28,12 @@ public int TotalRows

public uint DebugLevel { get; set; }

public IEnumerable<PhonebookSearchResult> CurrentSearchResults { get { return _searchResults.ToList(); }}
public IEnumerable<PhonebookSearchContactResult> CurrentSearchResults { get { return _phonebokSearchResult.Contacts.ToList(); } }

public RoomOsAddressBook()
{
PhonebookType = PhonebookType.Corporate;
MaxNumberOfSearchResults = 20;
_requestDebounce = new CTimer(_ =>
{
var progressHandler = SearchStopped;
if (progressHandler != null)
progressHandler(this, EventArgs.Empty);
_searchResponseActive = false;
_searchRequested = false;
}, Timeout.Infinite);
}

public PhonebookType PhonebookType { get; set; }
Expand Down Expand Up @@ -81,23 +71,21 @@ public void ProcessCliResponse(string response)

if (ResponseIsSearchResultStart(response))
{
_searchResults.Clear();
_phonebokSearchResult = new PhonebokSearchResult();
_searchResponseActive = true;
return;
}

if (ResponseIsSearchResultError(response))
{
_searchResults.Clear();
_phonebokSearchResult = new PhonebokSearchResult();
_searchResponseActive = true;
return;
}

if (ResponseIsSearchResultComplete(response) && _searchResponseActive)
{
_searchResponseActive = false;
OnPhonebookSearchComplete();
_requestDebounce.Reset();
_waithHandle.Set();
return;
}

Expand All @@ -108,11 +96,11 @@ public void ProcessCliResponse(string response)
if (index == 0)
throw new ArgumentOutOfRangeException("index", "Index is 0 so something is incorrect");

var contact = _searchResults.FirstOrDefault(s => s.Index == index);
var contact = _phonebokSearchResult.Contacts.FirstOrDefault(s => s.Index == index);
if (contact == null)
{
contact = new PhonebookSearchResult { Index = index };
_searchResults.Add(contact);
contact = new PhonebookSearchContactResult { Index = index };
_phonebokSearchResult.Contacts.Add(contact);
}

if (response.Contains(" Name:"))
Expand All @@ -133,6 +121,10 @@ public void ProcessCliResponse(string response)
{
contact.ParseContactMethod(response, DebugLevel);
}
else if (response.Contains("** resultId:"))
{
_phonebokSearchResult.Id = ParseRequestId(response);
}
else
{
Debug.Console(DebugLevel, "Not sure how to parse this Contact String:{0}", response);
Expand All @@ -153,13 +145,11 @@ protected void OnPhonebookSearchComplete()
{
var index = i;
var result = CurrentSearchResults.FirstOrDefault(x => x.Index == index) ??
new PhonebookSearchResult {Index = index};
new PhonebookSearchContactResult {Index = index};

handler(this, new SearchResultReceivedArgs { Index = result.Index, Name = result.Name });
}
}

_requestDebounce.Reset();
}

public static bool ResponseIsSearchResultStart(string response)
Expand Down Expand Up @@ -195,16 +185,33 @@ public static int ParseContactIndex(string response)
}
}



public static string ParseRequestId(string response)
{
try
{
var result = response.Split(new[] {' '})[1].TrimStart(new[] {'"'}).TrimEnd(new[] {'"'});
return result;
}
catch (Exception)
{
return string.Empty;
}
}

private readonly CrestronQueue<string> _searchTags = new CrestronQueue<string>();
private readonly CEvent _waithHandle = new CEvent();

public void Search(string searchString)
{
const string command = "xCommand Phonebook Search PhonebookType: {0} SearchString: \"{1}\" Limit: {2} Tag: {3}\r\n";

if (searchString.Length < 3 || _searchRequested)
return;

const string command = "xCommand Phonebook Search PhonebookType: {0} SearchString: \"{1}\" Limit: {2}\r\n";
_searchRequested = true;
var searchTag = Guid.NewGuid().ToString();

_searchTags.Enqueue(searchTag);
_searchRequested = true;
var progressHandler = SearchInProgress;
if (progressHandler != null)
progressHandler(this, EventArgs.Empty);
Expand All @@ -216,19 +223,38 @@ public void Search(string searchString)
command,
PhonebookType.ToString(),
searchString,
MaxNumberOfSearchResults);
MaxNumberOfSearchResults,
searchTag);

handler(this, new StringTransmitRequestedArgs { TxString = txString });
}

_requestDebounce.Reset(30000);
}
CrestronInvoke.BeginInvoke(_ =>
{
var success = false;
_waithHandle.Wait(5000);
while (!_searchTags.IsEmpty)
{
var tag = _searchTags.Dequeue();
if (_phonebokSearchResult.Id != tag)
continue;
public void ClearSearch()
{
_requestDebounce.Reset();
_searchResults.Clear();
OnPhonebookSearchComplete();
OnPhonebookSearchComplete();
success = true;
}
var searchStopped = SearchStopped;
if (searchStopped != null)
searchStopped(this, EventArgs.Empty);
if (!success)
{
// TODO fire off a failed result
}
_searchResponseActive = false;
_searchRequested = false;
});
}
}
}

0 comments on commit 58a4f03

Please sign in to comment.