Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 31 additions & 56 deletions TuyaKeyExtractor/KeyExtract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.IO;
using System.Linq;
using System.Xml;
using System.Text.RegularExpressions;
using System.Web;

namespace TuyaKeyExtractor
{
Expand All @@ -14,64 +16,37 @@ namespace TuyaKeyExtractor
public class KeyExtract
{
// public string search;
static readonly char[] delimiterChars = new[] { ',', '.', ':' };
static readonly char[] delimiterChars = new[] { ',', '.', ':', '?', '$', '[', '=', '*' };

/// <summary>
/// Parse the given data file and extract keys from it.
/// </summary>
/// <param name="path"> The path to the XML file</param>
/// <returns></returns>
private IList<ExtractedKey> ParseXmlFile ( string path )
public IList<ExtractedKey> ParseXmlFile(string path)
{
XmlDocument xmlDoc = new XmlDocument ();
try
{
xmlDoc.Load ( path );
var s = xmlDoc.InnerText;

string[] words = s.Split ( delimiterChars );

var keys = new List<ExtractedKey> ();
// var cleanList = new List<ExtractedKey> ();

int i = 0;
ExtractedKey key = new ExtractedKey ();
foreach ( var word in words )
{
i++;

if ( word == "\"localKey\"" )
{
var replace = words[i].Replace ( "\"", "" );
key.LocalKey = replace;
}

if ( word == "\"devId\"" )
{
var replace = words[i].Replace ( "\"", "" );
key.DeviceID = replace;
}

if ( word == "\"name\"" )
{
var replace = words[i].Replace ( "\"", "" );
key.DeviceName = replace;

if ( key.LocalKey != null && key.DeviceName != null )
{
keys.Add ( key );
key = new ExtractedKey ();
}
}
}

return keys;
}
catch ( System.IO.FileNotFoundException )
{
Console.WriteLine ( $"ERROR - It looks like your entered Path: \"{path}\" isn't valid. Try setting it again.\n\n" );
}
return null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
var xmlText = xmlDoc.InnerXml;

var localKeyMatches = Regex.Matches(xmlText, "\"localKey\"\\s*:\\s*\"([^\"]+)\"");
var devIdMatches = Regex.Matches(xmlText, "\"devId\"\\s*:\\s*\"([^\"]+)\"");
var nameMatches = Regex.Matches(xmlText, "\"name\"\\s*:\\s*\"([^\"]+)\"");

var keys = new List<ExtractedKey>();
for (int i = 0; i < localKeyMatches.Count; i++)
{
var localKey = HttpUtility.HtmlDecode(localKeyMatches[i].Groups[1].Value);
var devId = HttpUtility.HtmlDecode(devIdMatches[i].Groups[1].Value);
var name = HttpUtility.HtmlDecode(nameMatches[i].Groups[1].Value);

keys.Add(new ExtractedKey { LocalKey = localKey, DeviceID = devId, DeviceName = name });
}

return keys;
}
catch (FileNotFoundException)
{
Console.WriteLine($"ERROR - It looks like your entered Path: \"{path}\" isn't valid. Try setting it again.\n\n");
}
return null;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion TuyaKeyExtractor/TuyaKeyExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp8.0</TargetFramework>
<ApplicationIcon>6.ico</ApplicationIcon>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
Expand Down