Any way to write to a file on the gateway or to database from javascript?

I would like to write to the db and some files directly from out of my module in js.
Any way to do this (without webdev module?)

I usually use ajax or node.js to connect to a db. With node i can also write files…
But im guessing i dont have any access to those… or idk how they are set up to the ignition server

No, you can only do something like this by communicating with the backend somehow and then having the backend code write to the file or DB or whatever.

yeah i thought so, what kinda server is used to run the perspective?
jetty?

hmm any way to trigger scripts on the gateway from out js? with the right request? xD

Could always setup the web dev module, make Ignition API endpoints that do whatever you want when hit from the outside by whatever methodology you prefer

If you’re already writing a module, then you can implement mountRouteHandlers on your GatewayHook to set up whatever endpoints you want from Java:
https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.10/com/inductiveautomation/ignition/gateway/model/GatewayModuleHook.html#mountRouteHandlers(com.inductiveautomation.ignition.gateway.dataroutes.RouteGroup)

See the documentation for RouteGroup, it’s a reasonably elegant ‘fluent’ interface:
https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.10/com/inductiveautomation/ignition/gateway/dataroutes/RouteGroup.html

Or, more directly within Perspective, you can use the ComponentModelDelegateRegistry (com.inductiveautomation.perspective.gateway.api.ComponentModelDelegateRegistry#get) to register custom Java code that will be ‘attached’ to each instance of your component.

In com.inductiveautomation.perspective.gateway.api.ComponentModelDelegate you’ll see hooks to both send and receive messages from the frontend. On the frontend, you’d call an event via the componentEvents instance in your component’s ComponentProps, e.g.:
this.props.componentEvents.fireComponentEvent("onActionPerformed", {});

The payload can be whatever you want; it’ll be automatically marshalled to JSON and sent to the backend component.

1 Like

@PGriffith interessting!
I cant look tomuch into it today but fireComponentEvent seems promosing.
That is if i can predefine an event in ComponentModelDelegateRegistry with script attached to it from in the module? I have defined some events before but they were always empty, i cant recall if i could add in script there,
Can it also trigger custom methodes?
Maybe some default code i can add in the event schema?
ah or is the payload the script?

You can do pretty much anything from the Java side, including run user code. You could add an extension function to your component (with predefined default code) and call that method whenever the event is fired, for instance.

The payload can be whatever you want; it’s just “something relevant to this event” that the frontend knows but the backend might not; for instance, fireEvent is how key and mouse events get from the frontend to the backend, and the ‘payload’ there is basically the JSON structure of the raw React event object.

2 Likes

Hm extension functions are by default disabled? (atleast the ones on the alarmstatus table are). Will they still trigger with fireEvent even if they are? or how do i enable them by default? i dont seem to see anything about enable in ExtensionFunctionDescriptor

even enabled or not i dont seem to be able to trigger the ExtensionFunction through fireEvent :confused:
onClick and such work, but i cant put default code in there…

the alarmstatustable has ExtensionFunctions any idea where i can find how they get triggered?

ah its the fireEvent in java not js xd or both?

Alright i got a step further xd i have the ExtensionFunction, it shows up nicely in the designer. But it doesnt trigger from the java fireevent (enabled or not).

probably because i havent really figured out the delagate yet, but i think i was able to add in an empty one (just extending the ComponentModelDelegate) so far and it builded. now i need to figure out what to do to get the fireevent goingxD

In your delegate, you have access to the underlying component, which you would ‘request’ the extension function from:
Optional<ExtensionFunction> filterShelvedAlarm = component.findExtensionFunction("filterShelvedAlarm");

Then you call invoke on the function if it’s present…which it looks like it only will be if it’s enabled…

Thats unfortunate... is there a way to enable these through script in a gateway event or something?

the java code that is run in here


it runs on the gateway correct? i suppose i could write files on the gateway in here than instead of the extension function... tho that also limits flexibility sigh

I don’t really know exactly what you’re doing, but what about something like this:
Fire an event from the frontend.
On the backend, react to that event. Try to find the extension function. If it’s present, invoke it and use the result as an alternate file path to save to. If it’s not present, use the default location.
That allows user customization while still running by default. You could have a property on your component determine whether this save action is enabled at all, perhaps.

im making quite the complex table component xD… Where its possible to group/filter/rearrange column and rows in the clients browser. And to save these layout states and apply the one you want, for now i save it in a db using some bindings, but now im trying to make it so it can work without having to use bindings/external code.

(both chart and table are one and the same component, chart can be opened by a context menu on right click)
image

And yeah i guess that is possible, But i think ill be happy if it works in just the java bit lol, my java is a bit rusty. And its not so important where the states get saved anyway

Ive succesfully set up a delegate and im able to send an event and get a response in js and java thanks :smiley:
Now just the code to write to the files and rebuild some events so i dont need to get the response from the ignition properties anymore but from my delegate^^ Still a lot of work but it should all work now

Where would be a good location to save these files inside the ignition folder so that it gets copied in a gateway backup but not get overwritten in an update? ...\Inductive Automation\Ignition\data\local ?