Xedge is a Lua development platform built upon the Barracuda App Server. Xedge provides a rapid development environment for edge devices. With Xedge, developers rapidly test and prototype code, bypassing the need for a full compile and deploy cycle. This accelerates the iteration and development process, enhancing productivity.
Figure 1: Xedge UI
When utilizing the Mako Server, Xedge is typically loaded as follows:
mako -l::xedge.zip
When running standalone on an embedded device, the Xedge Lua application and the web UI are integrated into the firmware, along with an Xedge C application. For firmware build instructions, refer to the Xedge C application documentation. This document outlines the usage of Xedge. Additionally, we offer an advanced Xedge reference version for the ESP32 microcontroller, known as Xedge32.
Xedge Video
Upon starting Xedge, the left pane displays the File Systems set up by the C code, which may vary depending on the platform Xedge is running on.
To use Xedge, you must create an application by navigating into one of the file systems and selecting a directory as the root for the application. You can also create new directories using the file browser. Alternatively, you can select a deployed Lua application packaged as a ZIP file, but note that the app will be read-only.
Begin by expanding a file system. Navigate to the destination directory or create a new one using the Context Menu. You can open the Context Menu by right-clicking on a resource or long-pressing using a tablet or phone. Select "New App" in the Context Menu to open the Application Configuration dialog. After creating an application, the left pane tree view refreshes, and your new app appears in the tree view. In Figure 1 above, you can see three applications: two running and one disabled. See the Xedge Introductory video for additional details.
.Preload script:
Xedge provides the same .preload script as the Mako Server. See the tutorial Lua Environments for more information on how both Xedge and Mako Server sets up the application environments.
An application in Xedge is a directory or a zip file and comprises one or more Lua programs managed by Xedge. An application can include a .preload file containing Lua code and any number of individual Lua files with the .xlua extension. The .preload file and .xlua files automatically execute when an application starts and automatically stops when the application is turned off.
An application in Xedge can also include web applications. This is possible if the application is 'LSP enabled'. To make an application 'LSP enabled', you need to adjust its settings through the 'Application Configuration' menu. In simpler terms, the 'LSP enabled' setting allows your application to include and manage web applications. This is done through the 'Application Configuration' dialog. We'll delve into the specifics of web applications and 'LSP enabled' applications in detail later.
Every .preload file and .xlua file in an Xedge application runs in its own unique Lua environment. This means they operate separately and independently from each other. However, it's important to note that .xlua files do inherit elements from the .preload file. This means that any functions, variables, or libraries defined in the .preload file can be used by the .xlua files. Further, each .preload file inherits from the global environment. This means that any elements defined in the global environment are also accessible to the .preload file and, by extension, the .xlua files. Think of these environments like a stack of individual sheets, where each sheet represents an environment. The global environment is at the top, the .preload file environment is in the middle, and the .xlua file environments are at the bottom. This relationship is illustrated in Figure 2.
Figure 2: Applications and individual files managed by Xedge
If you are new to Lua or Lua environments, we recommend checking out our online Lua scope tutorial. This tutorial provides a comprehensive explanation of Lua environments, catering to beginners who are just starting with Lua.
The .xlua files are the cornerstones of the Lua REPL. Each running .xlua file should be considered as an independent program. Saving this file terminates any previous version, and the new version is loaded and executed. By separating Lua into individual programs, developers can work on one program while having separate programs run independently, streamlining the development process.
An '.xlua' file is typically used to write non visual programs that interact with hardware resources, such as GPIO, or to operate as an MQTT client. However, it's not designed for creating web applications (visual applications).
Web Applications:
If you want to design web applications in Xedge, you'll need to use the 'LSP' (Lua Server Pages) feature. This requires enabling 'LSP' for your application, which you can do via the application configuration dialog.
An 'LSP-enabled' application contains files with the '.lsp' extension. Usually, there will be a file called 'index.lsp' in the root directory of the application, which serves as the entry point for your web application.
While '.lsp' pages can access the environment established by the '.preload' file, they do not inherit any elements from it, unlike '.xlua' files. To better understand how '.lsp' pages operate and interact with the rest of the application, please refer to the Command Environment section."
Xedge has been specifically designed to enable rapid teardown and recreation of Lua programs, facilitating swift testing and development. The Lua language features a garbage collector that, over time, collects (destroys) any non-referenced Lua code. When restarting a program, the old version is dereferenced and replaced with a new one. However, when dealing with hardware resources, relying on the garbage collector may not be practical, as there might only be one or a very limited set of hardware resources. Generally, hardware resources must be released immediately in a REPL. For this reason, Xedge includes logic that can automatically shut down resources when a program is renewed.
For optimal results, hardware resources should not be declared using the Lua keyword "local" when using the REPL. If you do, ensure that you use the unload handler to release the resource. The unload handler is executed when the program is shut down (or replaced with a new version).
ESP32 example 1, not using the "local" keyword:
pin18 = esp32.gpio(18,"OUT")
ESP32 example 2, using the "local" keyword:
local pin18 = esp32.gpio(18,"OUT") function onunload() pin18:close() end
The above considerations apply to any resource, not just hardware resources, when using the REPL.
The Xedge UI is a Single Page Application (SPA) that primarily interacts with the Xedge server-side application through a virtualized file system. You can directly access this file system by navigating to http://device-address/rtl/apps/. Figure 3 below shows the Web File Manager and the Windows File Explorer (mapped as a WebDAV drive) connected to the same device as shown in Figure 1. The directories Ajax, MyLspApp, and MyLuaApp represent the same applications as shown in Figure 1. When using the Web File Manager or WebDAV, it is not possible to visually distinguish applications from file systems. You can directly access the files using any editor when the device is mapped as a WebDAV drive. For instance, saving an .xlua file using an external editor will trigger the logic for running the file on the server side.
Figure 3: Accessing the Xedge virtual file system.
Check the WebDAV product page if you are new to WebDAV
Xedge extends the API provided by the Barracuda App Server with a few additional APIs. Additionally, the Xedge ESP32 reference version Xedge32 extends Xedge with a wealth of HW APIs.
The global xedge object includes many functions and objects. All of them should be considered private, except for the ones listed below.
A variable extracted from C code and presented as seconds since January 1, 1970.
Functions elog and eflush are enabled if the SMTP settings have been configured. Function elog either appends to the internal message queue or flushes the queue after 30 seconds if the option table includes the attribute flush set to true. Function eflush sets flush to true and immediately flushes the message queue by sending an email.
The option table op may include:
Send a message to the trace buffer, using priority 5, with the following prepended: 'Xedge:'.
The virtual file system used by Xedge provides the same API as the Barracuda App Server's IO Interface.
Xedge.event() serves as a messaging distribution handler. To subscribe to an events, install a callback function. To remove the callback, call the function with the same parameters and set the third argument to true.
To trigger an event, invoke the global function _XedgeEvent(event, ...). The first parameter must specify the event name. The _XedgeEvent() function is primarily designed for being called from C code, offering an efficient and straightforward approach to transmitting events to Lua code. For more details, consult the Xedge C application documentation. Additionally, see Xedge32 for information on how the C code generates events for the ESP32.
Internally, Xedge creates an event when Lua code crashes, allowing you to implement logic that reboots the system if any of your scripts fail during runtime. The following example demonstrates how to design an event function that restarts the system in case your scripts experience runtime failures.
local function errEvent(emsg) -- arg1 is the error message, but it is already in the log buffer if SMTP is enabled xedge.eflush{subject="The code crashed"} -- Flush email with error message immediately ba.thread.run(function() ba.sleep(5000) -- Give SMTP time to send error message -- Call a Lua binding that reboots the system; the following works for ESP32 esp32.execute"restart" end) end xedge.event("error",errEvent) -- Subscribe
Identical to file() in the Mako Server module rwfile.
Identical to json() in the Mako Server module rwfile.
You can access the configuration menu by clicking the 3 dots located in the top-right corner of the interface.
By enabling the SMTP settings, Xedge gains the ability to send crucial information via email. One notable feature includes reporting Lua exceptions. In case one of your Lua programs crashes, a detailed message will be sent to your email address. SMTP settings also enable the xedge.elog() function, allowing you to send messages through email. Keep in mind that, unless it's an emergency with the flush attribute set, messages are accumulated and sent in bulk either when the maximum size or time limit is reached. Note that email messaging is automatically disabled when the Xedge browser UI is connected to an Xedge device.
Xedge is equipped with the Mako Server's Let's Encrypt plugin, providing you with automated certificate management capabilities. Configurable via the menu, this feature is set up to work with Real Time Logic's SharkTrustX demo portal, accessible at x.realtimelogic.com (or local.makoserver.net when Xedge is powered by Mako Server). For a more comprehensive understanding of this functionality, consult the Mako Server's Let's Encrypt plugin documentation, which also includes links to relevant tutorials.
To enable or fine-tune the Automatic Certificate Management Settings, navigate to the menu and click on the three dots. From there, select "TLS Certificate." It's crucial to understand that this automated certificate management, as configured through this menu, is designed specifically for Xedge deployments within private networks and is not recommended for public internet deployments.
Be advised that activating the automatic certificate management feature may take up to 4 minutes to become fully operational.
The Reverse Connection feature, included in the Let's Encrypt plugin, allows remote access to your Xedge via the SharkTrustX demo portal. This feature requires activation of the Automatic Certificate Management. You can easily enable or disable the remote connection at any time without the need to click the save button after toggling the Reverse Connection switch. See SharkTrustX for details.
Xedge supports both local user accounts and Single Sign-On (SSO). To open the Authentication Settings, click the 3 dots, followed by clicking on Authentication.
Note that installing an authenticator complicates the use of the Web File Manager and the WebDAV server. When an authenticator is installed, you must first log in to Xedge at the URL http://device-address/rtl/. After that, you can navigate to http://device-address/rtl/apps/. To use WebDAV, log in using your browser, navigate to the Web File Manager, and click the Session URL button, which generates a unique URL with an included token. Use this URL when mapping the WebDAV drive.
You can create multiple local users. To add a new user, input the desired username and password. To remove an existing user, enter the username and leave the password field empty.
SSO is a user authentication process that enables users to access multiple applications with just one set of login credentials, streamlining the sign-in experience, reducing password fatigue, and enhancing security across an organization's digital ecosystem. Xedge supports Azure AD SSO, a feature in Azure Active Directory that simplifies user access to multiple applications using a single set of credentials. To set up users, they must be part of the organization's Azure AD tenant, with appropriate roles and permissions assigned to access the desired applications. Configuring SSO involves registering the applications, defining user access, and managing authentication settings in the Azure portal.
Azure AD offers a free tier called "Azure Active Directory Free" with basic features that can support smaller-scale use, such as for personal or family purposes. To get started, you need to sign up for a free Microsoft Azure account and create an Azure AD tenant. Then, you can invite your friends as users in your tenant and configure access to applications using Azure AD and SSO.
After signing up for Azure AD, create the required SSO settings as follows:
http(s)://name/rtl/login/
. For example, if you are setting this up for testing purposes, enter the URLs http://localhost/rtl/login/
and https://localhost/rtl/login/
.After completing the above instructions, you can grant individual users within the organization access to the application by following these steps: