Skip to content

Commit

Permalink
Added Action for playing url on chromecast
Browse files Browse the repository at this point in the history
  • Loading branch information
dk307 committed Dec 24, 2018
1 parent c960018 commit ab58c04
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 14 deletions.
4 changes: 4 additions & 0 deletions HSPI_ChromecastSpeak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<RunCodeAnalysis>true</RunCodeAnalysis>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>Rules.ruleset</CodeAnalysisRuleSet>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
Expand Down Expand Up @@ -108,8 +109,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChromecastDevice.cs" />
<Compile Include="Pages\ActionPage.cs" />
<Compile Include="Pages\ChromecastCastAction.cs" />
<Compile Include="Pages\ConfigPage.cs" />
<Compile Include="Pages\PageHelper.cs" />
<Compile Include="Utils\EnumUtil.cs" />
<Compile Include="Utils\ExceptionHelper.cs" />
<Compile Include="Exceptions\VoiceGenerationException.cs" />
<Compile Include="GlobalSuppressions.cs" />
Expand Down
104 changes: 104 additions & 0 deletions Pages/ActionPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using HomeSeerAPI;
using Hspi.Utils;
using NullGuard;
using System;
using System.Collections.Specialized;
using System.Text;

namespace Hspi.Pages
{
internal class ActionPage : PageHelper
{
public ActionPage(IHSApplication HS, PluginConfig pluginConfig) : base(HS, pluginConfig, "Events")
{
}

public IPlugInAPI.strMultiReturn GetRefreshActionPostUI([AllowNull] NameValueCollection postData, IPlugInAPI.strTrigActInfo actionInfo)
{
IPlugInAPI.strMultiReturn result = default;
result.DataOut = actionInfo.DataIn;
result.TrigActInfo = actionInfo;
result.sResult = string.Empty;
if (postData != null && postData.Count > 0)
{
var action = (actionInfo.DataIn != null) ?
(ChromecastCastAction)ObjectSerialize.DeSerializeFromBytes(actionInfo.DataIn) :
new ChromecastCastAction();

foreach (var pair in postData)
{
string text = Convert.ToString(pair);
if (!string.IsNullOrWhiteSpace(text))
{
if (text.StartsWith(nameof(ChromecastCastAction.ChromecastDeviceId)))
{
action.ChromecastDeviceId = postData[text];
}
else if (text.StartsWith(nameof(ChromecastCastAction.Url)))
{
action.Url = postData[text];
}
else if (text.StartsWith(nameof(ChromecastCastAction.ContentType)))
{
action.ContentType = postData[text];
}
else if (text.StartsWith(nameof(ChromecastCastAction.Live)))
{
action.Live = postData[text] == "checked";
}
}
}

if (string.IsNullOrWhiteSpace(action.Url))
{
result.sResult += "<BR>Url is not valid";
}

if (string.IsNullOrWhiteSpace(action.ChromecastDeviceId))
{
result.sResult += "<BR>Chromecast device is not valid";
}
result.DataOut = ObjectSerialize.SerializeToBytes(action);
}

return result;
}

public string GetRefreshActionUI(string uniqueControlId, IPlugInAPI.strTrigActInfo actionInfo)
{
StringBuilder stb = new StringBuilder();
var action = ObjectSerialize.DeSerializeFromBytes(actionInfo.DataIn) as ChromecastCastAction;

var chromecastDevices = new NameValueCollection();
foreach (var device in pluginConfig.Devices)
{
chromecastDevices.Add(device.Key, device.Value.Name);
}

stb.Append(FormDropDown(nameof(ChromecastCastAction.ChromecastDeviceId) + uniqueControlId, chromecastDevices, action?.ChromecastDeviceId, 250, string.Empty, false));
stb.Append("<p> Url:");
stb.Append(HtmlTextBox(nameof(ChromecastCastAction.Url) + uniqueControlId, action?.Url, 100));
stb.Append("</p><p> Content mime type:");
stb.Append(HtmlTextBox(nameof(ChromecastCastAction.ContentType) + uniqueControlId, action?.ContentType, 50));
stb.Append("</p><p> Live:");
stb.Append(FormCheckBox(nameof(ChromecastCastAction.Live) + uniqueControlId, string.Empty, action?.Live ?? false));
stb.Append("</p>");
stb.Append(FormPageButton(SaveButtonName + uniqueControlId, "Save"));
return stb.ToString();
}

private NameValueCollection CreateNameValueCreation<T>() where T : Enum
{
var collection = new NameValueCollection();

foreach (var value in EnumUtil.GetValues<T>())
{
collection.Add(value.ToString(), value.ToString());
}

return collection;
}

private const string SaveButtonName = "SaveButton";
}
}
19 changes: 19 additions & 0 deletions Pages/ChromecastCastAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Hspi.Pages
{
[Serializable]
internal sealed class ChromecastCastAction
{
public string ChromecastDeviceId = null;
public string Url = null;
public string ContentType = null;
public bool Live = false;

public bool IsValid()
{
return !string.IsNullOrWhiteSpace(ChromecastDeviceId) &&
!string.IsNullOrWhiteSpace(Url);
}
}
}
34 changes: 34 additions & 0 deletions Pages/PageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Scheduler;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -75,6 +76,39 @@ protected string PageTypeButton(string name, string label, string pageType, stri
return b.Build();
}

protected string FormDropDown(string name, NameValueCollection options, string selected,
int width, string tooltip, bool autoPostBack = true)
{
return FormDropDown(name, options, selected,
width, tooltip, autoPostBack, PageName);
}

protected string FormDropDown(string name, NameValueCollection options, string selected,
int width, string tooltip, bool autoPostBack, string pageName)
{
var dropdown = new clsJQuery.jqDropList(name, pageName, false)
{
selectedItemIndex = -1,
id = NameToIdWithPrefix(name),
autoPostBack = autoPostBack,
toolTip = tooltip,
style = Invariant($"width: {width}px;"),
enabled = true,
submitForm = autoPostBack,
};

if (options != null)
{
for (var i = 0; i < options.Count; i++)
{
var sel = options.GetKey(i) == selected;
dropdown.AddItem(options.Get(i), options.GetKey(i), sel);
}
}

return dropdown.Build();
}

protected const string DeviceIdId = "DeviceIdId";
protected const string PageTypeId = "type";
protected readonly IHSApplication HS;
Expand Down
Loading

0 comments on commit ab58c04

Please sign in to comment.