I have a customer who wants to trigger Modbus events on a shop floor from within ERPNext.
The requirement is to make it so that when an order is placed on the eCommerce site, the corresponding item in the warehouse will trigger a robot attached to a programmable logic controller.
Writing the code to do this is relatively easy with the pymodbus library.
My question is more about how you would implement such a thing. My current thinking is a small module that:
Has a Singleton “Modbus Settings” Doctype that contains the host and port connection info
Create a “Modbus Address” Doctype that knows how to send or receive a bit signal to or from a specific address on the Modbus
Now for the tricky part. What’s the best way to connect the Modbus Address document to a Stock transaction so that when an item is ordered, the Modbus Address associated with the Item’s Warehouse location triggers the appropriate Modbus command?
It may be a tricky question, or it may not. I don’t think we yet have enough information to provide meaningful advice. Leaving aside erpnext and pymodbus for a moment, do you have an explicit set of rules for translating orders into modbus commands? If so, it should be pretty straightforward to code those rules up and have them triggered whenever a new order is submitted.
You will notice that I have a DocType called “Modbus Action” that connects to a specific pin on a Modbus network. I can get it to connect to the network, verify the connection and read the current value of “Modbus Location” children of “Modbus Connection”.
I’ve customized my “Warehouse” DocType so that it has a link to a “Modbus Action” document. Let’s say that the Warehouse location “Bin 1” is connected to a Modbus Action called “Trigger Bin 1” that lights up an LED on a shelf.
What is the best way to configure ERPNext so that when a Sales Order for a stock Item that is stored in Bin 1 triggers the “Trigger Bin 1” action?
I would prefer that a Modbus Action could be configured into any DocType and get triggered on any arbitrary event - but imagination fails as to how to do that.
I think I follow what you’re trying to do. I’m not familiar enough with Modbus to know exactly what triggering an action means, but it looks like your app does a nice job encapsulating all that anyway.
This should be pretty straightforward. Create a method that runs when the Sales Order is submitted, and then attach it to the on_submit hook using either a Server Script or your hooks.py file in a custom app. The method should check each warehouse on the Sales Order when it gets submitted, see if there’s a Modbus Action document linked, then run that trigger if so.
The doc_events hook in hooks.py allows you to run arbitrary methods on any on_change (or other) event. You’d need to define conditions for an action’s triggering in the Modbus Action doc, but it would not be overly complex to do that. (You could look at something like the Webhooks doctype for inspiration on similar functionality.) Every time your hooked method gets called, iterate through your Modbus Actions and trigger the ones that need triggering.
Note that this method would probably end up getting called a lot, so performance is worth thinking about, especially if you anticipate having numerous ModBus Action docs. You might, for example, link each ModBus Action to a specific doctype, such that it’s pretty easy to skip the complex logic for any event on doctypes not relevant to your ModBus interface.