This is somewhat of an antipattern for openHAB. openHAB’s rules are event driven. An event occurs and a rule (or rules) will run to do something in response to that event.
You could code it the way you describe but I suspect you’ll be frustrated with it and over all the code will be way longer and more complex than it needs to be.
Yes but…
openHAB mostly deals with it’s own internal state so it doesn’t really provide mechanisms to easily read from and write to files. In JavaScript you’d probably need to resort to using the the java.io classes from Java (all the scripting is embedded in Java so all of standard Java is available to you). If you used Python, there is more stuff built into the language for reading and writing files.
Java, no. You can import stuff from openHAB and core Java. Theoretically you can load JavaScript libraries but the JavaScript is version 5.1 and Nashorn (the embedded JavaScript in Java) really only supports loading them which can cause all sorts of problems.
Work is going on to let us use GraalVM JavaScript as a replacement which will support libraries in a much better way but that’s some time off.
With Python the situation is a little bit better but even there it doesn’t handle making sure all the dependencies are installed so we can to install the manually. Again, this should become better when we get Graal Python support. Note that Python is provided using Jython which is version 2.7, not Python 3, which can cause some problems with library availability.
It’s worth noting the existing of HABApp which is a separately running rules engine built in pure Python 3. But because it is separate it can only do what is exposed through the openHAB REST API.
You could but the event driven nature of the rules will make it somewhat awkward as you’d have to open the socket each time the rule runs. Also you won’t be able to listen for and accept connections. Again you’d probably use the core Java libraries to implement it. Finally, this isn’t what rules are for, that’s something that bindings do. I believe a new TCP/UDP binding is in work for OH 3.
openHAB supports database connectivity via it’s Persistence service. But it does so in a highly abstracted way. It does not directly expose the interactions with the database and instead it works only on Item states and it provides a small set of query methods (averageSince, lastUpdate, previousState, etc.).
If you want to access non-openHAB data from a database you are likely out of luck. You’ll have to write something separate to push the data into openHAB. If you want to perform more complex queries of the database here too you may have to do that outside of openHAB.
However, note that openHAB does have bindings and actions to make HTTP requests and execute command line commands. So it may not be that far outside of openHAB.
Anything can can be done through any of openHAB’s UIs can be done through it’s REST API. OH 3 comes with interactive documentation for the REST API which you will find under Developer Tools. In OH 2.5 you’ll have to install the docs as a separate addon.
The list of stuff that can be done it too long to list here but in general the creation/removal/configuration of pretty much anything can be done through HTTP queries. For events openHAB provides an SSE stream.
Over all it kind of sounds like you already have an overall architecture for how you want to build your home automation in mind, and that approach is in some ways antithetical to how openHAB is designed to work. I recommend downloading and starting to play with it. Go through the Getting Started tutorial (starts here and continues here) and I think you will find that everything you want to do is possible, but it might be better/easier to do it in a different way than you have envisioned.