Skip to content

Commit

Permalink
v1.4.0-beta.23
Browse files Browse the repository at this point in the history
- *Python* automation programs are back (v3.7) - fixes issue #453
- Added directive `#region program-context` to CSharp programs to allow declaring fields, classes, enums, structs
- ZigBee: optimized discovery process
- UI: upgraded to latest YoT release
  • Loading branch information
genemars committed Feb 23, 2023
1 parent 0268787 commit 70601f0
Show file tree
Hide file tree
Showing 66 changed files with 375 additions and 93 deletions.
2 changes: 1 addition & 1 deletion HomeGenie.sln
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Global
$19.inheritsSet = null
$19.scope = application/x-sharedlib
$0.StandardHeader = $20
$20.Text = @/*\n This file is part of HomeGenie Project source code.\n\n HomeGenie is free software: you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n HomeGenie is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with HomeGenie. If not, see <http://www.gnu.org/licenses/>. \n*/\n\n/*\n * Author: Generoso Martello <gene@homegenie.it>\n * Project Homepage: http://homegenie.it\n */\n\n
$20.Text = @/*\n This file is part of HomeGenie Project source code.\n\n HomeGenie is free software: you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n HomeGenie is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with HomeGenie. If not, see <http://www.gnu.org/licenses/>. \n*/\n\n/*\n * Author: Generoso Martello <gene@homegenie.it>\n * Project Homepage: https://homegenie.it\n */\n\n
$20.IncludeInNewFiles = False
$0.NameConventionPolicy = $21
$21.Rules = $22
Expand Down
2 changes: 1 addition & 1 deletion assets/build/all/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@

</div>

<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.710b144f4805f4b0.js" type="module"></script><script src="main.7864eecef597058b.js" type="module"></script>
<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.710b144f4805f4b0.js" type="module"></script><script src="main.9dd8fa9a411e536e.js" type="module"></script>

</body></html>
1 change: 0 additions & 1 deletion assets/build/all/app/main.7864eecef597058b.js

This file was deleted.

1 change: 1 addition & 0 deletions assets/build/all/app/main.9dd8fa9a411e536e.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions assets/build/all/app/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1676411488541,
"timestamp": 1677171154619,
"index": "/app/index.html",
"assetGroups": [
{
Expand All @@ -15,7 +15,7 @@
"/app/613.16b9c52c0ae74833.js",
"/app/859.0f150be9c99a6b5a.js",
"/app/index.html",
"/app/main.7864eecef597058b.js",
"/app/main.9dd8fa9a411e536e.js",
"/app/manifest.webmanifest",
"/app/polyfills.710b144f4805f4b0.js",
"/app/runtime.b92e2ca178ebbf4a.js",
Expand Down Expand Up @@ -724,8 +724,8 @@
"/app/assets/widgets/weather/images_alt/weather.svg": "c719c09048977fd40688272316b35ddf1bdf89e8",
"/app/assets/widgets/weather/style.css": "1b70730e73885cc0ee83566eaf584008d34e2255",
"/app/assets/workers/typings-worker.js": "8df10d867c12d36cdebfb7e16caacd0020db301f",
"/app/index.html": "2726bf68ef7bec4ea446f502d79af01d9e9b8396",
"/app/main.7864eecef597058b.js": "6d5f3a70231c0d97027a4ebd64b96b078b9d1722",
"/app/index.html": "2c8d20b401b5d1e49ecfde8c28d31e73b5099ad9",
"/app/main.9dd8fa9a411e536e.js": "b4cfe0a889d93f1b8678a12e6fd694d48159e1c8",
"/app/manifest.webmanifest": "d47bb07a495e27ddf072f8702df373f79b4eb183",
"/app/polyfills.710b144f4805f4b0.js": "594a5ea1b9d3f9cbc4f363eca84eda2c8bb8a901",
"/app/runtime.b92e2ca178ebbf4a.js": "b50d841a4c50d466c34aab3267d3fb4afe87dede",
Expand Down
2 changes: 1 addition & 1 deletion src/HomeGenie/Automation/Engines/ArduinoAppFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down
2 changes: 1 addition & 1 deletion src/HomeGenie/Automation/Engines/ArduinoEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down
107 changes: 88 additions & 19 deletions src/HomeGenie/Automation/Engines/CSharpAppFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand All @@ -44,7 +44,7 @@ namespace HomeGenie.Automation.Engines
{
public static class CSharpAppFactory
{
public const int ConditionCodeOffset = 8;
public const int ConditionCodeOffset = 7;

// TODO: move this to a config file
private static readonly List<string> Includes = new List<string>()
Expand Down Expand Up @@ -116,7 +116,7 @@ public static class CSharpAppFactory
"Utility = HomeGenie.Service.Utility"
};

public static int ProgramCodeOffset => Includes.Count() + 15;
public static int ProgramCodeOffset => Includes.Count() + 11;

#if NETCOREAPP
public static EmitResult CompileScript(string scriptSetup, string scriptSource, string outputDllFile)
Expand All @@ -135,10 +135,13 @@ namespace HomeGenie.Automation.Scripting
[Serializable]
public class ScriptingInstance : ScriptingHost
{
//////////////////////////////////////////////////////////////////
{context}
//////////////////////////////////////////////////////////////////
private void RunCode(string PROGRAM_OPTIONS_STRING)
{
//////////////////////////////////////////////////////////////////
// NOTE: user code start line is 16 *** please add new code after this method, do not alter start line! ***
{source}
//////////////////////////////////////////////////////////////////
}
Expand All @@ -147,7 +150,6 @@ private void RunCode(string PROGRAM_OPTIONS_STRING)
private bool SetupCode()
{
//////////////////////////////////////////////////////////////////
// NOTE: user code start line is ??? *** please add new code after this method, do not alter start line! ***
{setup}
//////////////////////////////////////////////////////////////////
return false;
Expand Down Expand Up @@ -186,15 +188,12 @@ private HomeGenie.Automation.MethodRunResult Setup()
public ScriptingHost hg { get { return (ScriptingHost)this; } }
}
}";
var userIncludes = new List<string>();
scriptSetup = GetUserIncludes(scriptSetup, ref userIncludes);
scriptSource = GetUserIncludes(scriptSource, ref userIncludes);
var usingNs = String.Join(" ", Includes.Concat(userIncludes)
.Select(x => String.Format("using {0};" + Environment.NewLine, x)));
var parsedCode = ParseCode(scriptSetup, scriptSource);
source = source
.Replace("{using}", usingNs)
.Replace("{source}", scriptSource)
.Replace("{setup}", scriptSetup);
.Replace("{using}", parsedCode.UsingNamespaces)
.Replace("{source}", parsedCode.MainCode)
.Replace("{setup}", parsedCode.SetupCode)
.Replace("{context}", parsedCode.ContextCode);
#if NETCOREAPP
var dotNetCoreDir = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);
var homeGenieDir = Path.GetDirectoryName(typeof(HomeGenieService).GetTypeInfo().Assembly.Location);
Expand Down Expand Up @@ -371,26 +370,96 @@ private HomeGenie.Automation.MethodRunResult Setup()
#endif
}

public static ParseCodeResult ParseCode(string scriptSetup, string scriptSource)
{
var userIncludes = new List<string>();
var scriptContext = "";
scriptSetup = GetIncludes(scriptSetup, ref userIncludes);
scriptSetup = GetContext(scriptSetup, ref scriptContext);
scriptSource = GetIncludes(scriptSource, ref userIncludes);
var usingNs = String.Join(" ", Includes.Concat(userIncludes)
.Select(x => String.Format("using {0};" + Environment.NewLine, x)));
return new ParseCodeResult()
{
UsingNamespaces = usingNs,
UserIncludes = userIncludes,
ContextCode = scriptContext,
MainCode = scriptSource,
SetupCode = scriptSetup
};
}

/**
* Parse custom "using" pre-processor directive to allow including namespaces in HG programs
*/
private static string GetUserIncludes(string codeBlock, ref List<string> userIncludes)
private static string GetIncludes(string codeBlock, ref List<string> userIncludes)
{
if (userIncludes == null) userIncludes = new List<string>();
var setupLines = codeBlock.Split('\n');
var codeBlockLines = codeBlock.Split('\n');
codeBlock = "";
foreach (var codeLine in codeBlockLines)
{
if (codeLine.StartsWith("#using "))
{
userIncludes.Add(codeLine.Substring(7));
codeBlock += "//" + codeLine + "\n";
}
else
{
codeBlock += codeLine + "\n";
}
}
return codeBlock;
}

private static string GetContext(string codeBlock, ref string contextCode)
{
var codeBlockLines = codeBlock.Split('\n');
codeBlock = "";
foreach (var setupLine in setupLines)
bool contextOpen = false;
int currentLine = 0;
foreach (var codeLine in codeBlockLines)
{
if (setupLine.StartsWith("#using "))
if (contextOpen)
{
userIncludes.Add(setupLine.Substring(7));
if ((codeLine.Trim() + " ").StartsWith("#endregion "))
{
contextOpen = false;
}
contextCode += codeLine + "\n";
}
else if ((codeLine.Trim() + " ").StartsWith("#region program-context "))
{
if (currentLine == 0)
{
contextOpen = true;
}
else
{
throw new Exception("Directive '#region program-context' must be on first line");
}
contextCode += codeLine + "\n";
}
else
{
codeBlock += setupLine + "\n";
codeBlock += codeLine + "\n";
}
currentLine++;
}
if (contextOpen)
{
throw new Exception("Missing #endregion preprocessor directive");
}
return codeBlock;
}

public class ParseCodeResult
{
public string UsingNamespaces { get; set; }
public string ContextCode { get; set; }
public string MainCode { get; set; }
public string SetupCode { get; set; }
public List<string> UserIncludes { get; set; }
}
}
}
43 changes: 37 additions & 6 deletions src/HomeGenie/Automation/Engines/CSharpEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down Expand Up @@ -132,27 +132,58 @@ public override List<ProgramError> Compile()
EndColumn = 0,
ErrorMessage = ex.Message,
ErrorNumber = ex.Source,
CodeBlock = CodeBlockEnum.CR
CodeBlock = CodeBlockEnum.PC
});
}

if (result != null && !result.Success)
{
var sourceLines = ProgramBlock.ScriptSource.Split('\n').Length;
var parsedCode = CSharpAppFactory.ParseCode(ProgramBlock.ScriptSetup, ProgramBlock.ScriptSource);
var contextLines = parsedCode.ContextCode.Split('\n').Length;
var sourceLines = parsedCode.MainCode.Split('\n').Length;
foreach (var diagnostic in result.Diagnostics)
{
var errorRow = (diagnostic.Location.GetLineSpan().StartLinePosition.Line - CSharpAppFactory.ProgramCodeOffset) + 1;
var errorEndRow = (diagnostic.Location.GetLineSpan().EndLinePosition.Line - CSharpAppFactory.ProgramCodeOffset) + 1;
var errorRow = (diagnostic.Location.GetLineSpan().StartLinePosition.Line - CSharpAppFactory.ProgramCodeOffset) - parsedCode.UserIncludes.Count;
var errorEndRow = (diagnostic.Location.GetLineSpan().EndLinePosition.Line - CSharpAppFactory.ProgramCodeOffset) - parsedCode.UserIncludes.Count;
var errorCol = diagnostic.Location.GetLineSpan().StartLinePosition.Character + 1;
var errorEndCol = diagnostic.Location.GetLineSpan().EndLinePosition.Character + 1;
if (errorRow <= 0)
{
errors.Add(new ProgramError
{
Line = 0,
Column = 0,
EndLine = 0,
EndColumn = 0,
ErrorMessage = diagnostic.GetMessage(),
ErrorNumber = diagnostic.Descriptor.Id,
CodeBlock = CodeBlockEnum.PC
});
continue;
}
var blockType = CodeBlockEnum.CR;
if (diagnostic.Severity == DiagnosticSeverity.Error)
if (errorRow <= contextLines)
{
blockType = CodeBlockEnum.TC;
}
else
{
errorRow -= 6;
errorEndRow -= 6;
if (errorRow >= sourceLines + CSharpAppFactory.ConditionCodeOffset)
{
errorRow -= (sourceLines + CSharpAppFactory.ConditionCodeOffset);
errorEndRow -= (sourceLines + CSharpAppFactory.ConditionCodeOffset);
blockType = CodeBlockEnum.TC;
}
else
{
errorRow -= contextLines - 1;
errorEndRow -= contextLines - 1;
}
}
if (diagnostic.Severity == DiagnosticSeverity.Error)
{
errors.Add(new ProgramError
{
Line = errorRow,
Expand Down
2 changes: 1 addition & 1 deletion src/HomeGenie/Automation/Engines/IProgramEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down
2 changes: 1 addition & 1 deletion src/HomeGenie/Automation/Engines/JavascriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down
2 changes: 1 addition & 1 deletion src/HomeGenie/Automation/Engines/ProgramEngineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License

/*
* Author: Generoso Martello <[email protected]>
* Project Homepage: http://homegenie.it
* Project Homepage: https://homegenie.it
*/

using System;
Expand Down
Loading

0 comments on commit 70601f0

Please sign in to comment.