Jump to content
C4 Forums | Control4

How to create Events from an Action?


emaxedon

Recommended Posts

My driver needs to be able to dynamically create Events. I want to create these Events from a LUA_ACTION.

How would I complete the XML configuration to create an Action that would pass 3 parameters, eventId, eventName, and eventDescription?

<actions>
  <action>
    ???
  </action>
</actions>

When pressed, it would trigger the "ExecuteCommand", and subsequently create the Event using the C4:AddEvent API.

function ExecuteCommand(strCommand, tParams)
	if (strCommand == 'LUA_ACTION') then
		if tParams ~= nil then
			for pName, pValue in pairs(tParams) do
				if pName == 'ACTION' then
					-- TODO: C4:AddEvent(eventId, eventName, eventDescription)
				end
			end
		end
	end
end

In Leyman's terms, I need to create a button under the Actions tab that would create an event by passing 3 parameters - the event ID, event name, and event description.

Any help getting me moving in the right direction would be greatly appreciated. Thank you.

Link to comment
Share on other sites


your action XML would be something like: 

 <action>
                <name>Update Properties</name>
                <command>update</command>
   </action>

Then you could add something like

function LUA_ACTION.update()
    Dbg:Trace("Properties update")

   DO something
 end

 

Link to comment
Share on other sites

Can I pass arguments to the Update  Properties command in the action tab?

<action>
  <name>Update Properties</name>
  <command>update</command>
  ??? Any way to pass arguments that would show up as text fields in the action tab?
</action>

That way, when the "update" function is called, I can do stuff with user generated input... say:

function LUA_ACTION.update(arg1, arg2)
  Dbg:Trace("Update properties")
  Dbg:Trace("Argument 1:" .. arg1)
  Dbg:Trace("Argument 2:" .. arg2)
end

Any ideas @msgreenf?

Link to comment
Share on other sites

Hey @msgreenf,

So I created an Action that pulls in Property values - and this works. I'm wondering if you've ever used the C4:AddEvent API.

I'm calling C4:AddEvent, and creating events dynamically (not in the XML configuration), but I'm not seeing the events under the Programming/Events tab in Composer after they are allegedly being created.

Does the AddEvent function create events that Composer programmers can then use in the Programming tab?

Link to comment
Share on other sites

10 hours ago, emaxedon said:

Hey @msgreenf,

So I created an Action that pulls in Property values - and this works. I'm wondering if you've ever used the C4:AddEvent API.

I'm calling C4:AddEvent, and creating events dynamically (not in the XML configuration), but I'm not seeing the events under the Programming/Events tab in Composer after they are allegedly being created.

Does the AddEvent function create events that Composer programmers can then use in the Programming tab?

I am not familiar with that API. Maybe @seth_j or @alanchow can chime in?

Link to comment
Share on other sites

2 things:

* In versions prior to 2.10, you had to have at least one event in the XML portion of the driver for AddEvent to show new events (bug, fixed in 2.10).

* When events are added, if you're *on* the driver, you have to go off the driver and back on for ComposerPro to show the new events.

RyanE

 

Link to comment
Share on other sites

As far as I can tell you just want to provide parameters to your action that are user inputted. I've created an example for you.

<action>
  <name>Create Event</name>
  <command>CREATE_EVENT</command>
  <params>
    <param>
      <name>EventID</name>
      <type>STRING</type>
      <readonly>false</readonly>
      <default>1</default>
    </param>
    <param>
      <name>EventName</name>
      <type>STRING</type>
      <readonly>false</readonly>
      <default></default>
    </param>
    <param>
      <name>EventDescription</name>
      <type>STRING</type>
      <readonly>false</readonly>
      <default></default>
    </param>
  </params>
</action>

This would give you the attached image. The action will only be triggered once 'OK' is clicked.

I should mention I don't recall which version they added support for this off the top of my head, maybe @RyanE knows. I know it's at least 2.9.1.

ActionParametersEvent.png

Link to comment
Share on other sites

On another note, you don't need to loop through the tParams argument in order to get all the keys. If you know the key already index it directly.

In the case above it would be:

tParams.ACTION = "ENABLE_EVENT"

-- Action parameters
tParams.EventID
tParams.EventName
tParams.EventDescription

-- Example
if (strCommand == "LUA_ACTION" and tParams) then  
  local action = tParams.ACTION

  if (action == "ENABLE_EVENT") then
    -- TODO: Check for empty parameter values ("")

    C4:AddEvent(tParams.EventID, tParams.EventName, tParams.EventDescription)
  end
end

 

Link to comment
Share on other sites

Hey @TheWizard,

1. I've seen Events in the programming tab with a drop down select. Is it possible to use C4:AddEvent to create events that have drop downs?

2. I have to trigger Events using C4:FireEvent, or C4:FireEventByID. Is it possible to fire an Event that has a drop down and only trigger an element of the dropdown?

For instance,

Events:

(event with a dropdown)

- Command ['1', '2', '3', ... ]

(normal events)

- Pressed On Button

- Pressed Off Button

Let's say I want to trigger the Command event but only where it's number '2'?

What's the XML configuration look like for an event that has more fields than just ID, name, and description?

Link to comment
Share on other sites

As far as I'm aware you're not able to create anything but an event with no parameters. If you wanted to 'simulate' a parameter with an event you would have to use variables and set them before you fire off the event. You could also use the variable itself as the event and listen for it to change and retrieve the value it changed to.

What are you trying to achieve?

Link to comment
Share on other sites

@TheWizard

I'm writing a driver that receives HTTP requests from a web service. Depending on the request, an event is fired. There are 2 types of requests, for simplification, let's assume the requests are Turn On, and Turn Off. Each request also passes an ID, let's assume this ID is some device.

I want to allow the programmer to dynamically create Events that will handle the two types of requests (Turn On, Turn Off) with an associated ID.

So, the programmer knows there will be 3 IDs, 100, 101, and 102. He wants to create a Turn On, and Turn Off event for each of the IDs. Then, when a web request comes in, it will fire the appropriate event.

I could create Events individually, say, C4:AddEvent(id, 'Turn On 100', description), C4:AddEvent(id, 'Turn Off 101', description), etc. etc., but I it would be more elegant to have only two Events, say "Turn On", and "Turn Off", where each event has parameters (100, 101, 102). Then when a request comes in that is say Turn On, it fires the Turn On event with parameter ID.

Link to comment
Share on other sites

You're likely better off creating events individually, "Turn on 100", "Turn on 101" type events rather than creating a variable and running an if statement against generic "Turn On" and "Turn Off" events. My thoughts are if you did it the variable way you'd have to run an if statement against the variable for any 'Turn On' event which isn't what you really want.

Stick with the individual events.

Link to comment
Share on other sites

Events are just a 'hook' to hang some code on.

There are no parameters to events, just an ID and a Name (in DriverWorks).  In the C++ API, they're just a number.

You can create new events in DriverWorks, but code attached to an event doesn't follow the name, and isn't necessarily persisted if the event is removed and the system is restarted.

i.e. you must recreate your events in the driver on driver late init.

RyanE

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.