void SendRuleTSM(bool serviceOnly) { string engine = TheCommonUtils.CStr(GetProperty("TSMEngine", false)); string text = TheCommonUtils.CStr(GetProperty("TSMText", false)); string payload = TheCommonUtils.CStr(GetProperty("TSMPayload", false)); //payload = payload.Replace("%DTO%", TheCommonUtils.CStr(DateTimeOffset.Now)); //text = text.Replace("%DTO%", TheCommonUtils.CStr(DateTimeOffset.Now)); ICDEThing triggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(TriggerObject)) as ICDEThing; string escPayload = TheCommonUtils.GenerateFinalStr(payload, triggerThing); escPayload = TheCommonUtils.GenerateFinalStr(escPayload, MyBaseThing); string escText = TheCommonUtils.GenerateFinalStr(text, triggerThing); escText = TheCommonUtils.GenerateFinalStr(escText, MyBaseThing); if (!string.IsNullOrEmpty(engine) && !string.IsNullOrEmpty(text)) { TSM customTSM = new TSM(engine, escText, escPayload); if (serviceOnly) { customTSM.SetToServiceOnly(true); } TheCommCore.PublishCentral(customTSM, true); } if (IsRuleLogged) { LogEvent(escPayload); } if (TheThing.GetSafePropertyBool(MyBaseThing, "IsEVTLogged")) { TheLoggerFactory.LogEvent(eLoggerCategory.RuleEvent, TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing), eMsgLevel.l4_Message, TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), escText, escPayload); } }
private static TheScript LoadScript(string scriptFile) { var scriptText = File.ReadAllText(scriptFile); //var scriptText = File.ReadAllText(@"test.json"); scriptText = Regex.Replace(scriptText, @"(?:jsonFragment\()(.*)(?:\))", (m) => { return(TheCommonUtils.GenerateFinalStr(m.Groups[1].Value)); }); var script = TheCommonUtils.DeserializeJSONStringToObject <TheScript>(scriptText); if (string.IsNullOrEmpty(script.Name)) { script.Name = Path.GetFileNameWithoutExtension(scriptFile); } script.FileName = scriptFile; script.ScriptRaw = scriptText; return(script); }
public bool LogEvent(string ActionText) { TheEventLogData tSec = new TheEventLogData { EventTime = DateTimeOffset.Now, EventLevel = eMsgLevel.l4_Message, StationName = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), EventName = TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing) }; if (!string.IsNullOrEmpty(tSec.EventName)) { tSec.EventName = tSec.EventName.Replace("%OldValue%", TriggerOldValue); } tSec.EventTrigger = TheThingRegistry.GetThingByMID(TheCommonUtils.CGuid(TriggerObject))?.FriendlyName; tSec.ActionObject = ActionText; var tEng = MyBaseEngine.GetBaseThing().GetObject() as TheRulesEngine; tEng?.LogEvent(tSec); return(true); }
internal void FireAction(bool FireNow) { if (TheCDEngines.MyThingEngine == null || !TheBaseAssets.MasterSwitch) { return; } if (!FireNow) { int tDelay = ActionDelay; if (tDelay > 0) { Timer tTimer = GetDelayTimer(); if (tTimer != null) { tTimer.Dispose(); tTimer = null; } tTimer = new Timer(sinkFireOnTimer, this, tDelay * 1000, Timeout.Infinite); SetDelayTimer(tTimer); return; } } switch (ActionObjectType) { case "CDE_PUBLISHCENTRAL": SendRuleTSM(false); break; case "CDE_PUBLISH2SERVICE": SendRuleTSM(true); break; default: //case "CDE_THING": TheThing tActionThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(ActionObject)); if (tActionThing != null) { string tActionValue = ActionValue; if (!string.IsNullOrEmpty(tActionValue)) { ICDEThing triggerThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(TriggerObject)) as ICDEThing; tActionValue = TheCommonUtils.GenerateFinalStr(tActionValue, triggerThing); tActionValue = tActionValue.Replace("%OldValue%", TriggerOldValue); tActionValue = TheCommonUtils.GenerateFinalStr(tActionValue, MyBaseThing); } ICDEThing tObject = tActionThing.GetObject() as ICDEThing; if (tObject != null) { tObject.SetProperty(ActionProperty, tActionValue); } else { tActionThing.SetProperty(ActionProperty, tActionValue); } if (IsRuleLogged) { LogEvent(tActionValue); } if (TheThing.GetSafePropertyBool(MyBaseThing, "IsEVTLogged")) { TheLoggerFactory.LogEvent(eLoggerCategory.RuleEvent, TheCommonUtils.GenerateFinalStr(MyBaseThing.FriendlyName, MyBaseThing), eMsgLevel.l4_Message, TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), TriggerObject, tActionValue); } } break; } TheThing.SetSafePropertyDate(MyBaseThing, "LastAction", DateTimeOffset.Now); FireEvent("RuleFired", this, this, true); }
private async Task RunScriptAsync(TheScript script, TheThing variables, int stepNumber = 1, bool replay = false) { TheThing variablesSnapshot; try { for (; stepNumber <= script.Steps.Length; stepNumber++) { //Clone thing before step occurs variablesSnapshot = new TheThing(); variables.CloneThingAndPropertyMetaData(variablesSnapshot, true); var step = script.Steps[stepNumber - 1]; var existingSnapshot = MyScriptTableStorage.MyMirrorCache.GetEntryByFunc(snapshot => snapshot.ScriptName == script.Name && snapshot.ScriptStep == stepNumber); if (existingSnapshot?.Disabled == true) { TheBaseAssets.MySYSLOG.WriteToLog(175002, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Finished script step: skipped step because it was disabled", eMsgLevel.l3_ImportantMessage, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", step.Message.MessageName }, { "Target", step.Message.Target }, }))); UpdateStorageList(script.Name, "Disabled", stepNumber, script, variablesSnapshot, replay); continue; } if (step.Condition != null) { var condition = TheCommonUtils.GenerateFinalStr(step.Condition, variables); if ( (condition == "" || condition.ToLowerInvariant() == "false" || condition.Trim() == "0") || (condition.StartsWith("!") && condition.Length >= 1 && (condition.Substring(1).ToLowerInvariant() == "true") || condition.Substring(1).Trim() == "1")) { TheBaseAssets.MySYSLOG.WriteToLog(175002, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Finished script step: skipped step due to condition not met", eMsgLevel.l3_ImportantMessage, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", step.Message.MessageName }, { "Target", step.Message.Target }, { "Condition", step.Condition }, { "ConditionEvaluated", condition }, }))); UpdateStorageList(script.Name, "Condition Not Met", stepNumber, script, variablesSnapshot, replay); continue; } } var messageType = TheCommonUtils.GenerateFinalStr(step.Message.MessageName, variables); var txtPayload = TheCommonUtils.GenerateFinalStr(step.Message.Parameters?.ToString(), variables); { var txtPayload2 = txtPayload?.Replace("\"\\\"", ""); var txtPayload3 = txtPayload2?.Replace("\\\"\"", ""); txtPayload = txtPayload3; } // TODO Need a simpler and more flexible way to specify thing address in the script JSON var target = step.Message.Target; if (target == null) { if (txtPayload.Contains("EngineName")) { var payloadDict = TheCommonUtils.DeserializeJSONStringToObject <Dictionary <string, object> >(txtPayload); object engineNameInferred = null; if (payloadDict?.TryGetValue("EngineName", out engineNameInferred) == true && !string.IsNullOrEmpty(engineNameInferred?.ToString())) { target = new TheMessageAddress { EngineName = engineNameInferred.ToString() }; } } } if (target.EngineName.StartsWith("%") || target.EngineName.StartsWith("{")) { target.EngineName = TheCommonUtils.GenerateFinalStr(target.EngineName, variables); // TODO Clean this up: support a serialized TheMessageAddress in the engine name, so that an output variable can be fed into a method invocation try { var newTarget = TheCommonUtils.DeserializeJSONStringToObject <TheMessageAddress>(target.EngineName); if (newTarget != null) { target = newTarget; } } catch { // parsing error: ignore, will result in other errors downstream } } await TheThingRegistry.WaitForInitializeAsync(target); bool bDoRetry; int remainingRetryCount = step.RetryCount ?? 0; do { existingSnapshot = MyScriptTableStorage.MyMirrorCache.GetEntryByFunc(snapshot => snapshot.ScriptName == script.Name && snapshot.ScriptStep == stepNumber); if (existingSnapshot?.Disabled == true) { TheBaseAssets.MySYSLOG.WriteToLog(175002, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Finished script step: skipped step because it was disabled", eMsgLevel.l3_ImportantMessage, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", step.Message.MessageName }, { "Target", step.Message.Target }, }))); UpdateStorageList(script.Name, "Disabled", stepNumber, script, variablesSnapshot, replay); break; } bDoRetry = false; var response = await TheCommRequestResponse.PublishRequestAsync(MyBaseThing, target, messageType, new TimeSpan(0, 0, 0, 0, step.Message.timeout), null, txtPayload, null); if (!string.IsNullOrEmpty(response?.PLS)) { var outputs = TheCommonUtils.DeserializeJSONStringToObject <Dictionary <string, object> >(response.PLS); if (outputs != null) { if (step.Message.outputs != null) { foreach (var output in step.Message.outputs) { if (output.Key == "*") { variables.SetProperty(output.Value, response.PLS); } else if (outputs.TryGetValue(output.Key, out var outputValue)) { variables.SetProperty(output.Value, outputValue); if (output.Value.Contains("Error") && !string.IsNullOrEmpty(TheCommonUtils.CStr(outputValue))) { TheBaseAssets.MySYSLOG.WriteToLog(175004, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error in script step: output reported error", eMsgLevel.l1_Error, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", messageType }, { "Target", target }, { "PLS", txtPayload }, { "Response", response }, { "ResponsePLS", response?.PLS }, }))); UpdateStorageList(script.Name, $"Error {outputValue} in output", stepNumber, script, variablesSnapshot, replay); if (remainingRetryCount < 0 || remainingRetryCount > 0) { remainingRetryCount--; bDoRetry = true; } string retriesRemaining = bDoRetry ? (remainingRetryCount >= 0 ? $"{remainingRetryCount + 1}" : "infinite") : "none"; MyBaseThing.SetStatus(3, $"Error in script '{script?.Name}', step {stepNumber}: output '{output.Value}' reported error {outputValue}. Retries remaining: {retriesRemaining}"); } } else { // TODO provide access to sub-elements in the JSON //var outputParts = output.Key.Split('/'); //dynamic currentNode = outputs; //foreach (var outputPart in outputParts) //{ // if (currentNode.TryGetValue(outputPart, out var nextNode)) // { // currentNode = nextNode; // } //} } } } TheBaseAssets.MySYSLOG.WriteToLog(175003, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Finished script step", eMsgLevel.l3_ImportantMessage, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", messageType }, { "Target", target }, { "PLS", txtPayload }, { "ResponsePLS", response.PLS }, }))); UpdateStorageList(script.Name, "Finished", stepNumber, script, variablesSnapshot, replay); } else { TheBaseAssets.MySYSLOG.WriteToLog(175004, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error in script step: no outputs found in response", eMsgLevel.l1_Error, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", messageType }, { "Target", target }, { "PLS", txtPayload }, { "Response", response }, { "ResponsePLS", response?.PLS }, }))); UpdateStorageList(script.Name, "Error: No Output", stepNumber, script, variablesSnapshot, replay); if (step.DontRetryOnEmptyResponse != true && (remainingRetryCount < 0 || remainingRetryCount > 0)) { remainingRetryCount--; bDoRetry = true; } string retriesRemaining = bDoRetry ? (remainingRetryCount >= 0 ? $"{remainingRetryCount + 1}" : "infinite") : "none"; MyBaseThing.SetStatus(3, $"Error in script '{script?.Name}', step {stepNumber}: no outputs found in response. Retries remaining: {retriesRemaining}"); } } else { TheBaseAssets.MySYSLOG.WriteToLog(175005, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error Script step: timeout", eMsgLevel.l1_Error, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Step", stepNumber }, { "Message", messageType }, { "Target", target }, { "PLS", txtPayload }, { "Response", response }, }))); UpdateStorageList(script.Name, "Error: Timeout", stepNumber, script, variablesSnapshot, replay); //Retries infinitely unless count is specified if (remainingRetryCount < 0 || remainingRetryCount > 0) { remainingRetryCount--; bDoRetry = true; } string retriesRemaining = bDoRetry ? (remainingRetryCount >= 0 ? $"{remainingRetryCount + 1}" : "infinite") : "none"; MyBaseThing.SetStatus(3, $"Error in script '{script?.Name}', step {stepNumber}: timeout. Retries remaining: {retriesRemaining}"); } if (bDoRetry) { await TheCommonUtils.TaskDelayOneEye(30000, 100).ConfigureAwait(false); } } while (bDoRetry && TheBaseAssets.MasterSwitch); } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(175006, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, "Error in script step", eMsgLevel.l1_Error, TheCommonUtils.SerializeObjectToJSONString(new Dictionary <String, object> { { "Script", script.Name }, { "Exception", e.Message }, }))); MyBaseThing.SetStatus(3, $"Error in script '{script?.Name}': {e.Message}"); //Save variables instead of snapshot in case of error UpdateStorageList(script.Name, $"Error: {e.Message}", stepNumber, script, variables, replay); } }
internal static Task <ApplyConfigFilesResult> ApplyConfigurationFilesAsync() { bool bSuccess = true; int fileCount = 0; int failedFileCount = 0; var pipeLineTasks = new List <Task <List <TheThing> > >(); try { var configDir = TheCommonUtils.cdeFixupFileName(Path.Combine("ClientBin", "config")); if (!Directory.Exists(configDir)) { return(TheCommonUtils.TaskFromResult(new ApplyConfigFilesResult { Success = true, })); } var configDirs = Directory.GetParent(configDir).GetDirectories("config"); if (configDirs.Length != 1 || configDirs[0].FullName != configDir) { TheBaseAssets.MySYSLOG.WriteToLog(7721, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ThingService, "ClientBin/config directory has invalid casing. Must be lower case.", eMsgLevel.l1_Error)); return(TheCommonUtils.TaskFromResult(new ApplyConfigFilesResult { Success = false, })); } var configFiles = Directory.GetFiles(configDir, "*.cdeconfig"); fileCount = configFiles.Length; foreach (var configFile in configFiles) { var pipelineJson = File.ReadAllText(configFile); pipelineJson = TheCommonUtils.GenerateFinalStr(pipelineJson); var pipelineConfig = TheCommonUtils.DeserializeJSONStringToObject <ThePipelineConfiguration>(pipelineJson); var answerFileNames = Directory.GetFiles(configDir, $"{Path.GetFileNameWithoutExtension(configFile)}*.cdeanswer"); var answerConfigs = answerFileNames.Select(af => { var answerJson = File.ReadAllText(af); answerJson = TheCommonUtils.GenerateFinalStr(answerJson); var answerConfig = TheCommonUtils.DeserializeJSONStringToObject <ThePipelineConfiguration>(answerJson); return(answerConfig); }); var tasks = ApplyPipelineConfigJsonInternal(pipelineConfig, answerConfigs, configFile, answerFileNames); if (tasks?.Count == 0) { failedFileCount++; } pipeLineTasks.AddRange(tasks); } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(7722, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ThingService, "Error processing config files", eMsgLevel.l1_Error, $"{e.Message}")); bSuccess = false; } return(TheCommonUtils.TaskWhenAll(pipeLineTasks.Select(t => (Task)t)) .ContinueWith(t => { foreach (var task in pipeLineTasks) { if (task.IsFaulted) { failedFileCount++; bSuccess = false; } } return new ApplyConfigFilesResult { Success = bSuccess, NumberOfFiles = fileCount, NumberOfFailedFiles = failedFileCount }; })); }