-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- *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
Showing
66 changed files
with
375 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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>() | ||
|
@@ -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) | ||
|
@@ -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} | ||
////////////////////////////////////////////////////////////////// | ||
} | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
Oops, something went wrong.