/// <summary> /// The constructor for the element. /// Called at runtime when the element is first referenced. /// </summary> /// <param name="data"></param> public MqttSubscriberElement(IElementData data) { _data = data; _props = _data.Properties; // The IElementData contains a reference to the execution context var context = _data.ExecutionContext; prServerElement = (IElementProperty)_props.GetProperty("MqttServer"); MqttConnector = (MqttSubscribeConnector)prServerElement.GetElement(context); // Get the Property Readers IPropertyReader prTopic = _data.Properties.GetProperty("Topic"); // Place simio events into a lookup array that will be used at runtime. TopicEventList = new List <IEvent>(); foreach (var eventDef in _data.Events) { TopicEventList.Add(eventDef); } LogIt($"SubscriberElement Start: Url={MqttConnector.ServerUrl} Port={MqttConnector.ServerPort} "); SubscribeTopic = prTopic.GetStringValue(_data.ExecutionContext); }
/// <summary> /// Method called when a process token executes the step. /// </summary> public ExitType Execute(IStepExecutionContext context) { // Get an array of double values from the repeat group's list of expressions object[] paramsArray = new object[_items.GetCount(context)]; for (int i = 0; i < _items.GetCount(context); i++) { // The thing returned from GetRow is IDisposable, so we use the using() pattern here using (IPropertyReaders row = _items.GetRow(i, context)) { // Get the expression property IExpressionPropertyReader expressionProp = row.GetProperty("Expression") as IExpressionPropertyReader; // Resolve the expression to get the value paramsArray[i] = expressionProp.GetExpressionValue(context); } } // set Excel data ExcelConnectElementEPPlus Excelconnect = (ExcelConnectElementEPPlus)_ExcelconnectElementProp.GetElement(context); if (Excelconnect == null) { context.ExecutionInformation.ReportError("ExcelConnectEPPlus element is null. Makes sure ExcelWorkbook is defined correctly."); } String worksheetString = _worksheetProp.GetStringValue(context); Int32 rowInt = (Int32)_rowProp.GetDoubleValue(context); Int32 startingColumnInt = Convert.ToInt32(_startingColumnProp.GetDoubleValue(context)); try { // for each parameter for (int ii = 0; ii < paramsArray.Length; ii++) { double doubleValue = TryAsDouble((Convert.ToString(paramsArray[ii], CultureInfo.InvariantCulture))); if (!System.Double.IsNaN(doubleValue)) { Excelconnect.WriteResults(worksheetString, rowInt, startingColumnInt + ii, doubleValue, DateTime.MinValue, String.Empty, context); } else { DateTime datetimeValue = TryAsDateTime((Convert.ToString(paramsArray[ii], CultureInfo.InvariantCulture))); if (datetimeValue > System.DateTime.MinValue) { Excelconnect.WriteResults(worksheetString, rowInt, startingColumnInt + ii, System.Double.MinValue, datetimeValue, String.Empty, context); } else { Excelconnect.WriteResults(worksheetString, rowInt, startingColumnInt + ii, System.Double.MinValue, System.DateTime.MinValue, (Convert.ToString(paramsArray[ii], CultureInfo.InvariantCulture)), context); } } } } catch (FormatException) { context.ExecutionInformation.ReportError("Bad format provided in Excel Write step."); } // We are done writing, have the token proceed out of the primary exit return(ExitType.FirstExit); }
/// <summary> /// Method called when a process token executes the step. /// </summary> public ExitType Execute(IStepExecutionContext context) { // Prepare Message //================= AgentConnection agentConnection = (AgentConnection)_agentProperty.GetElement(context); var socket = agentConnection.GetSocket(); var states = ReadRepeatingProperty(context, _statesProperty, "State"); var action = _actionProperty.GetState(context).StateValue; var reward = _rewardProperty.GetState(context).StateValue; var episode = _episodeProperty.GetState(context).StateValue; var status = _statusProperty.GetState(context).StateValue; var requestMessage = new AgentRequestMessage() { Status = status, EpisodeNumber = episode, Reward = reward, States = states }; // Send Request Message to Agent //=============================== var message = JsonConvert.SerializeObject(requestMessage); bool result = socket.TrySendFrame(TimeSpan.FromSeconds(3), message); if (!result) { context.ExecutionInformation.ReportError("Failed to communicate with agent"); } // Wait For Response Message From Agent //====================================== string responseMessage; result = socket.TryReceiveFrameString(TimeSpan.FromSeconds(3), out responseMessage); if (!result) { context.ExecutionInformation.ReportError("Failed to communicate with agent"); } var jsonresponse = JsonConvert.DeserializeObject <AgentResponseMessage>(responseMessage); // Return Action Value //====================================== context.ExecutionInformation.TraceInformation(responseMessage); (_actionProperty.GetState(context) as IRealState).Value = jsonresponse.Action; context.ReturnValue = jsonresponse.Action; return(ExitType.FirstExit); }
/// <summary> /// Method called when a process token executes the step. /// </summary> public ExitType Execute(IStepExecutionContext context) { // Get Excel data ExcelConnectElementEPPlus Excelconnect = (ExcelConnectElementEPPlus)prExcelconnectElement.GetElement(context); if (Excelconnect == null) { context.ExecutionInformation.ReportError("ExcelConnectEPPlus element is null. Makes sure ExcelWorkbook is defined correctly."); } String worksheetString = prWorksheet.GetStringValue(context); Int32 rowInt = (Int32)prRow.GetDoubleValue(context); Int32 startingColumnInt = Convert.ToInt32(prStartingColumn.GetDoubleValue(context)); int numReadIn = 0; int numReadFailures = 0; for (int ii = 0; ii < rprStates.GetCount(context); ii++) { // Tokenize the input string resultsString = Excelconnect.ReadResults(worksheetString, rowInt, startingColumnInt + ii, context); // The thing returned from GetRow is IDisposable, so we use the using() pattern here using (IPropertyReaders row = rprStates.GetRow(ii, context)) { // Get the state property out of the i-th tuple of the repeat group IStateProperty stateprop = (IStateProperty)row.GetProperty("State"); // Resolve the property value to get the runtime state IState state = stateprop.GetState(context); if (TryAsNumericState(state, resultsString) || TryAsDateTimeState(state, resultsString) || TryAsStringState(state, resultsString)) { numReadIn++; } else { numReadFailures++; } } } string worksheetName = prWorksheet.GetStringValue(context); context.ExecutionInformation.TraceInformation($"Read from row={rowInt} worksheet={worksheetName} into {numReadIn} state columns. {numReadFailures} read failures"); // We are done reading, have the token proceed out of the primary exit return(ExitType.FirstExit); }
/// <summary> /// Method called when a process token executes the step. /// Does an MQTT publish to the MqttServer with MqttTopic. /// The data that is published is MqttPayload /// </summary> public ExitType Execute(IStepExecutionContext context) { string topic = ""; try { // Example of how to get the value of a step property. var eprPayload = (IExpressionPropertyReader)prPayload; var payload = eprPayload.GetExpressionValue(context).ToString(); topic = prTopic.GetStringValue(context); // Get the MQTT connector which holds the MQTT client information MqttPublishConnector mqttConnector = (MqttPublishConnector)prServerElement.GetElement(context); // The referenced element has a MQTT client IMqttClient client = (IMqttClient)mqttConnector.PublishClient; if (client.IsConnected) { MqttHelpers.MqttPublish(client, topic, payload); // payload); Logit(context, $"Info: Topic={topic} Payload={payload}"); return(ExitType.FirstExit); } else { Logit(context, $"Execute. Step Topic={topic} .Client not connected. Topic={topic} Payload={payload}."); return(ExitType.AlternateExit); } } catch (Exception ex) { Logit(context, $"Execute Topic={topic} Error={ex.Message}"); return(ExitType.AlternateExit); } }
/// <summary> /// Method called when a process token executes the step. /// </summary> public ExitType Execute(IStepExecutionContext context) { bool intersects = false; // Example of how to get the value of a step property. IPropertyReader myExpressionProp = _properties.GetProperty("MyExpression") as IPropertyReader; string myExpressionPropStringValue = myExpressionProp.GetStringValue(context); double myExpressionPropDoubleValue = myExpressionProp.GetDoubleValue(context); // Example of how to get an element reference specified in an element property of the step. IElementProperty myElementProp = (IElementProperty)_properties.GetProperty("UserElementName"); UserElement myElement = (UserElement)myElementProp.GetElement(context); // Example of how to display a trace line for the step. context.ExecutionInformation.TraceInformation(String.Format("The value of expression '{0}' is '{1}'.", myExpressionPropStringValue, myExpressionPropDoubleValue)); origin[0] = _properties.GetProperty("Ox").GetDoubleValue(context); origin[1] = _properties.GetProperty("Oy").GetDoubleValue(context); origin[2] = _properties.GetProperty("Oz").GetDoubleValue(context); ray[0] = _properties.GetProperty("Rx").GetDoubleValue(context); ray[1] = _properties.GetProperty("Ry").GetDoubleValue(context); ray[2] = _properties.GetProperty("Rz").GetDoubleValue(context); boxMin[0] = _properties.GetProperty("B0x").GetDoubleValue(context); boxMin[1] = _properties.GetProperty("B0y").GetDoubleValue(context); boxMin[2] = _properties.GetProperty("B0z").GetDoubleValue(context); boxMax[0] = _properties.GetProperty("B1x").GetDoubleValue(context); boxMax[1] = _properties.GetProperty("B1y").GetDoubleValue(context); boxMax[2] = _properties.GetProperty("B1z").GetDoubleValue(context); double tmin = (boxMin[0] - origin[0]) / ray[0]; double tmax = (boxMax[0] - origin[0]) / ray[0]; if (tmin > tmax) { Swap <double>(ref tmin, ref tmax); } double tymin = (boxMin[1] - origin[1]) / ray[1]; double tymax = (boxMax[1] - origin[1]) / ray[1]; if (tymin > tymax) { Swap <double>(ref tymin, ref tymax); } if ((tmin > tymax) || (tymin > tmax)) { return(ExitType.AlternateExit); } if (tymin > tmin) { tmin = tymin; } if (tymax < tmax) { tmax = tymax; } double tzmin = (boxMin[2] - origin[2]) / ray[2]; double tzmax = (boxMax[2] - origin[2]) / ray[2]; if (tzmin > tzmax) { Swap <double>(ref tzmin, ref tzmax); } if ((tmin > tzmax) || (tzmin > tmax)) { return(ExitType.AlternateExit); } if (tzmin > tmin) { tmin = tzmin; } if (tzmax < tmax) { tmax = tzmax; } if (intersects) { return(ExitType.FirstExit); } else { return(ExitType.AlternateExit); } }