Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Updates #107

Merged
merged 12 commits into from
Jul 11, 2024
5 changes: 3 additions & 2 deletions 3-series/Messengers/MessengerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ private void HandleMessage(string path, string id, JToken content)
// replace base path with empty string. Should leave something like /fullStatus
var route = path.Replace(MessagePath, string.Empty);

if(!_actions.TryGetValue(route, out var action)) {
Debug.Console(1, this, $"No action found for path {path}");
if(!_actions.TryGetValue(route, out var action)) {
return;
}

Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Executing action for path {path}", this, path);

action(id, content);
}

Expand Down
14 changes: 12 additions & 2 deletions 3-series/MobileControlSystemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto.Prng;
using PepperDash.Core;
using PepperDash.Essentials.AppServer;
using PepperDash.Essentials.AppServer.Messengers;
Expand Down Expand Up @@ -1585,15 +1586,24 @@ private void HandleHeartBeat(JToken content)
private void HandleClientJoined(JToken content)
{
var clientId = content["clientId"].Value<string>();
var roomKey = content["roomKey"].Value<string>();

var roomKey = content["roomKey"].Value<string>();
var touchpanelKey = content.SelectToken("touchpanelKey"); //content["touchpanelKey"].Value<string>();

SendMessageObject(new MobileControlMessage
{
Type = "/system/roomKey",
ClientId = clientId,
Content = roomKey
});

if(touchpanelKey == null) { return; }

SendMessageObject(new MobileControlMessage
{
Type = "/system/touchpanelKey",
ClientId = clientId,
Content = touchpanelKey.Value<string>()
});
}

private void HandleUserCode(JToken content, Action<string, string> action = null)
Expand Down
2 changes: 2 additions & 0 deletions 3-series/RoomBridges/MobileControlEssentialsRoomBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using PepperDash.Essentials.Core.Lighting;
using PepperDash.Essentials.Core.Shades;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core.Routing;



#if SERIES4
Expand Down
17 changes: 17 additions & 0 deletions 4-series/epi-essentials-mobile-control/Touchpanel/ITheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using PepperDash.Core;
using PepperDash.Essentials.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PepperDash.Essentials.Touchpanel
{
public interface ITheme:IKeyed
{
string Theme { get; }

void UpdateTheme(string theme);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public class MobileControlTouchpanelProperties : CrestronTouchpanelPropertiesCon

[JsonProperty("buttonToolbarTimeoutInS")]
public ushort ButtonToolbarTimoutInS { get; set; } = 0;

[JsonProperty("theme")]
public string Theme { get; set; } = "light";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Essentials.AppServer;
using PepperDash.Essentials.AppServer.Messengers;

namespace PepperDash.Essentials.Touchpanel
{
public class ThemeMessenger : MessengerBase
{
private readonly ITheme _tpDevice;

public ThemeMessenger(string key, string path, ITheme device) : base(key, path, device as Device)
{
_tpDevice = device;
}

protected override void RegisterActions()
{
AddAction("/fullStatus", (id, content) =>
{
PostStatusMessage(new ThemeUpdateMessage { Theme = _tpDevice.Theme });
});

AddAction("/saveTheme", (id, content) =>
{
var theme = content.ToObject<MobileControlSimpleContent<string>>();

Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Setting theme to {theme}", this, theme.Value);
_tpDevice.UpdateTheme(theme.Value);

PostStatusMessage(JToken.FromObject(new {theme = theme.Value}));
});
}
}

public class ThemeUpdateMessage:DeviceStateMessageBase
{
[JsonProperty("theme")]
public string Theme { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class UiClient : WebSocketBehavior

public string RoomKey { get; set; }

public string TouchpanelKey { get; set; }

private DateTime _connectionTime;

public TimeSpan ConnectedDuration
Expand Down Expand Up @@ -64,34 +66,47 @@ protected override void OnOpen()

var match = Regex.Match(url.AbsoluteUri, "(?:ws|wss):\\/\\/.*(?:\\/mc\\/api\\/ui\\/join\\/)(.*)");

if (match.Success)
if (!match.Success)
{
var clientId = match.Groups[1].Value;
return;
}

var clientId = match.Groups[1].Value;

// Inform controller of client joining
if (Controller != null)
{
var clientJoined = new MobileControlMessage
{
Type = "/system/roomKey",
ClientId = clientId,
Content = RoomKey,
};
// Inform controller of client joining
if (Controller == null)
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Verbose, "WebSocket UiClient Controller is null");
}
/*
var clientJoined = new MobileControlMessage
{
Type = "/system/roomKey",
ClientId = clientId,
Content = RoomKey,
};

Controller.SendMessageObjectToDirectClient(clientJoined);
Controller.SendMessageObjectToDirectClient(clientJoined);*/

var bridge = Controller.GetRoomBridge(RoomKey);
var clientJoined = new MobileControlMessage
{
Type = "/system/clientJoined",
Content = JToken.FromObject(new
{
clientId,
roomKey = RoomKey,
touchpanelKey = string.IsNullOrEmpty(TouchpanelKey) ? TouchpanelKey : string.Empty,
})
};

SendUserCodeToClient(bridge, clientId);
Controller.HandleClientMessage(JsonConvert.SerializeObject(clientJoined));

bridge.UserCodeChanged += (sender, args) => SendUserCodeToClient((MobileControlEssentialsRoomBridge)sender, clientId);
}
else
{
Debug.Console(2, "WebSocket UiClient Controller is null");
}
}
var bridge = Controller.GetRoomBridge(RoomKey);

SendUserCodeToClient(bridge, clientId);

bridge.UserCodeChanged += (sender, args) => SendUserCodeToClient((MobileControlEssentialsRoomBridge)sender, clientId);

_connectionTime = DateTime.Now;

// TODO: Future: Check token to see if there's already an open session using that token and reject/close the session
Expand Down Expand Up @@ -509,13 +524,15 @@ private void RetrieveSecret()
var key = client.Key;
var path = _wsPath + key;
var roomKey = client.Value.Token.RoomKey;
var touchpanelKey = client.Value.Token.TouchpanelKey;

_server.AddWebSocketService(path, () =>
{
var c = new UiClient();
Debug.Console(2, this, "Constructing UiClient with id: {0}", key);
c.Controller = _parent;
c.RoomKey = roomKey;
c.TouchpanelKey = touchpanelKey;
UiClients[key].SetClient(c);
return c;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Copyright>Copyright 2020</Copyright>
<Version>4.0.0-local</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<AssemblyInformationalVersion>$(Version)</AssemblyInformationalVersion>
<InformationalVersion>$(Version)</InformationalVersion>
<OutputPath>bin\$(Configuration)\</OutputPath>
<Authors>PepperDash Technologies</Authors>
<PackageId>PepperDash.Essentials.4Series.Plugin.MobileControl</PackageId>
Expand All @@ -28,7 +28,7 @@
<DefineConstants>TRACE;SERIES4</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PepperDashEssentials" Version="2.0.0-beta-2490" />
<PackageReference Include="PepperDashEssentials" Version="2.0.0-alpha-2529" />
<PackageReference Include="WebSocketSharp-netstandard" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PepperDash.Essentials.Core.Routing;

namespace PepperDash.Essentials.AppServer.Messengers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
<Folder Include="SIMPLJoinMaps\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PepperDashEssentials" Version="2.0.0-beta-2490" />
<PackageReference Include="PepperDashEssentials" Version="2.0.0-alpha-2529" />
</ItemGroup>
</Project>
Loading