Open kjhazelwood opened 3 years ago
Seems like most of what you want is available now.
When you create a DPMContext
, it's doing a service discovery and opening a list to the found DPM. At this point, there's a connection but nothing is being sent in either direction. Your script's initialization could do the set-up:
async def main(con):
with DPMContext(con) as dpm_periodic:
await dpm_periodic.add_entry(...)
await dpm_periodic.add_entry(...)
with DPMContext(con) as dpm_button:
await dpm_button.add_entry(...)
# Collection doesn't start on either DPM connection
# until .start() is called.
await main_loop(dpm_periodic, dpm_button)
You can build your button's handler to use the pre-made context so you're not doing service discoveries each time the button is pressed:
def mk_button_handler(dpm):
async def button_pushed():
await dpm.start()
async for ii in dpm.replies():
# Do something with button-related data. Exit
# this loop when done with data.
await dpm.stop()
return button_pushed
If your script raises an exception up through the with-statements
, you'll lose your connection with DPM. But if you handle everything in main_loop()
, then you DPM contexts stay valid and you can simply start and stop acquisition as you see fit. main_loop()
might look like this:
async def main_loop(dpm_p, dpm_b):
# register button handler with GUI.
register_button_handler(mk_button_handler(dpm_b))
# Start-up DPM data collection for the main loop. Presumably
# this is data you want to collect constantly during execution.
await dpm_p.start()
async for ii in dpm_p.replies():
# handle periodic data
The current API closely follows the underlying DPM protocol. I was hoping for an API that was a little more exotic. For instance:
# Get devices. If a device doesn't exist, an exception will be raised.
d_temp = Device('M:OUTTMP')
d_humid = Device('G:HUMID')
# Create a collection rate context
a = Acquisition('e,8f')
# Now enjoy correlated data
async for (ts, v_temp, v_humid) in a.start(d_temp, d_humid):
print(f'timestamp: {ts}, temperature: {v_temp}, humidity: {v_humid}')
Quick thought on potential API
Inside dpm start() the code could group like handler_methods and attach it to a Thread. Maintain threads in a pool so that the number of threads is manageable and that dead time between thread executions allow threads to pop.