How to handle external events in CQRS? - domain-driven-design

Supposed I have a CQRS-based system with a web UI that I want to integrate with some external device, let's say an Arduino board.
Basically, I can think of three scenarios:
When the user clicks on a button in the web UI, the Arduino shall do something.
When the user pushes a hardware button, the domain shall react and the UI shall update.
When the user pushes a hardware button, the UI shall update.
How do I model these scenarios?
This is quite easy IMHO: UI emits a command, business logic runs, emits an event, and Arduino is listening for events. Basically, the Arduino is nothing but an event denormalizer. Is this the correct approach?
This is quite easy IMHO as well: Arduino emits a command, sends it to the command bus, and the same procedure runs as with scenario 1. Basically, there's no difference for the CQRS-system whether the command comes from the web UI of from anything else.
This is where I'm really unsure: Supposed the Arduino handles the button press itself, and flashes an LED in response. I just want to make sure that my application takes notice of "the LED has been flashing". This is no command (as the flashing already happened), instead it's an event. What do I do with this event? Do I simply store it in the event store and bypass my domain? This seems horribly wrong to me. Do I emit a pseudo-command which is turned 1:1 into a matching event? This will work and does not bypass the domain, but it feels wrong as well, as it actually is no command. What should I do?
Any advice on these things?

1.Just use an event handler( updating ui maybe using websocket ) listening on Arduino event if no state should change in your domain.
2. Use a saga listening on Arduino event and fire a command if state should change in your domain.
UI is not a domain concern. So I prefer option 1.

Related

MediaRoute Dialog: Disconnect vs Stop

I'm working on an App that sends audio and video content to a chromecast. But I don't want to stop the playback when we disconnect our Sender App.
In the mediaroute-v7 package we find reference to an option to either disconnect or stop the connection. In fact the button to disconnect is hidden on https://github.com/android/platform_frameworks_support/blob/master/v7/mediarouter/res/layout/mr_media_route_controller_material_dialog_b.xml#L103 and to make it visible mediaroute-v7 gets the SelectedRoute and checks the boolean canDisconnect() on https://github.com/android/platform_frameworks_support/blob/39b32a9feea6b821d6a5c92202c400aa1f252200/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java#L259.
Is there any way to change the boolean value of canDisconnect(), so we can show the button, with VideoCastManager? Or how is supposed to get that button visible?
I presume you are using CCL (since you'd mentioned VideoCastManager). The current dialog that you see there is put out by CCL and is not the one provided by the mediarouter framework (unless you have overridden getMediaRouteDialogFactory()). In CCL, you have an option to decide if you want to simply disconnect from the cast device or stop the running app on the cast device when you click on that "Stop Casting" in the dialog; by default it just disconnects you and to change that, you have to call setStopOnDisconnect(true) to force it to stop the app. That said, the most common behavior on the receiver side (the default and styled receivers follow that as well) is to stop the app if the last connected device intentionally disconnects (i.e. if the last connected device is disconnected due to, say, network disruption, it doesn't stop the app). This behavior can be controlled on the receiver side by overriding onSenderDisconnected(reason) callback there.

Possible to control garage door with Garmin IQ?

I'd like my Fenix 3 to do the following:
Trigger = hold down start button (i.e. shortcut)
Send message via BT or WiFi to a server (Linux or Windows or Arduino or whatever)
I'll take care of the message and open/close my garage door.
After a bike tour I'd like to easily and safely open my garage door. I have a VmWare server running at home. I could use one of the machines on this server to listen to the messages or I could set up an Arduino or similar.
The main question is: Can I write an IQ app that utilizes the shortcut concept on the clock, i.e. triggered by long click on start or lap button?
Clarification: There seems to be some kind of global actions for long press. I can for example assign "Save position" to long press on start/stop. This works even from inside of other apps.
Can the clock communicate with sensors (i.e. Arduino or other BT client) even if not in training mode?
Clarification: I need to communicate directly with my Arduino via Bluetooth, i.e. not via my iPhone.
Thanks in advance.
Short answer: Yes
Long answer: If you record the time a keydown event comes in, and then check for a "long" press when the key is let up based on the time difference, you can fake it. There is not an event for a long press of a physical key though. I am also pretty sure your app needs to be the current one for this to work.
Link to the InputDelegate event options: http://developer.garmin.com/downloads/connect-iq/monkey-c/doc/Toybox/WatchUi/InputDelegate.html
As for the sensors question, I am not sure exactly what you are asking. Your app can do whatever you want, and it is my understanding that only one app will be running at a time.
Disclaimer: Thus far I have only been working with the emulator, I'm still waiting for my watch to get here.
You cannot write anything that hijacks user input events from another active application (including the watch face). You could make your own watch face, but it wouldn't have the ability to send network messages and it has only one way to accept user input (the look-at-watch gesture).
This is something that you can do pretty easily from a watch-app or a widget. Assuming that your fenix3 is connected to your phone via bluetooth, you can send http get requests as you see fit.
I've written a simple app that I call GIFTTT that uses the IFTTT Maker channel to open/close my garage door (and all sorts of other things).

How do I track and handle an event in node

So let's say I want to make a twitter bot. I want to send a certain message to whoever has sent it a reply, so I need to make an event for it. Obviously one way is to get all the replies (or last n replies) in a certain time interval, find out which ones are new, etc; but first of all it's not live, and it requires an extra query to find new tweets.
Say we want to track some changes in a website. For instance, we want to handle an event when that change happens, instantly.
I used socket.io to handle some other kind of events, like when some changes happen in a particular port, but I couldn't figure out how I can handle these types of events.
The word "event" does not mean what you think it means!
In a DOM environment, an Event is a very specific (and core) concept which allows you to write code based on user interactions with elements on the screen.
In NodeJS, an Event is something that can be generated and announced by an instance of events.EventEmitter
In your question, an Event seems to refer to anything that happens on the internet, potentially anywhere.
Under that last definition, there is simply no single answer for how to "track an event."
If you want to write code that can respond to change (which is just a more specific version of "react to an input") you need to create a mechanism to identify that a change has occurred, followed by a mechanism to trigger whatever code you want to be run in response (this last part is you would normally call "emitting" an "event").
SocketIO accomplishes both of these things for certain situations, using a graceful degredataion of protocols in order to explicitly emit local events that you can listen for and handle. It starts trying to use WebSockets, and eventually falls back to more expensive techniques such as polling.
SocketIO only works if the source of the information or change has decided to support the protocol. In those cases, the source is actually emitting the event (over websockets) and socketIO listens for it.
In cases where the source of the information you are looking for does not support websockets (and hasn't been coded to explicitly notify your servers of changes), you are going to have to come up with your own solutions. However: You shouldn't think of this as a case of tracking "events". Rather, you are watching for changes.
How you watch for changes will depend on the nature of the change. Generally you'll probably have to poll for it.

Using PubNub, is Unsubscribe a dual use command for Publish and Subscribe?

Yes, I know it seems like a simple question but I just recently started using PubNub and I am confused on how to disconnect from a channel. I think the command to use is "Unsubscribe" and my misunderstanding relates to the dual use of the word.
Logically, I understand that once you initialize PubNub and publish a message a separate process can subscribe to the establish channel. When it's done it unsubscribes. Got it!
Now we want to completely disconnect from PubNub. That is end the channel.
Do I use the command "Unsubscribe" to do this? I guess I am logically looking for an "End" or "Disconnect" command and not an "Unsubscribe" command because it did not subscribe to the channel, it established the channel. I know it seems petty but until I understand this it's difficult to move forward. So is this a dual use command?
Thanks
You are on the right track here. Depending on the client platform in question, an unsubscribe resulting in an empty channel list will completely disconnect you.
On the more sophisticated clients, advanced/smart frameworks, there are the API calls of un/subscribe (which as you described subs /unsubs you to a specific channel), and separately, the public and/or private method calls defining/detecting being "connected" or "online".
For example, iOS has specific connect and disconnect calls, separate from subscribe/unsubscribe calls. On JS, there is no explicit connect/disconnect, but regardless if you are subbed or not to an active channel list, there may be background "pings/heartbeats" being made to the PN cloud to detect connectivity/online/offline state.
If you give more info on the client platform and version you are on, we can give you more info on how to completely sever all connects to the PN cloud and achieve a "complete disconnect".
geremy

How to handle external and internal event in j2me

i am developing game in j2me. In that i have to handle external and internal event.. I studied in some of websites that we can handle the event with the help of hidenotify() and shownotify().
But it dont know where to use these two methods? whether in hidenotify() in pauseApp() and shownotify() in startApp() or somewhere else..
Please anyone give me a clear idea about handling the event in mobile.
In J2ME world, external events resemble the following:
In coming SMS message. This is used trigger application specific action caused by a message sent from a pre-defined and well known sender. This feature is known as "push".
Media card inserted and the application needs to recognize it and act upon it.
If the phone is NFC enabled, launching an application when the phone is taken to a card reader.
When a server is attempting to connect to the phone, launch the application and perform some specific action. This requires that phone is addressable on network; very few of them support it.
Launching the application at a specific time.
I hope you get the idea. Most of the above are achieved by making use Push Registry.
The events that you are talking about are the callbacks to application that AMS (Application Management Software) notifies before the component is being shown and before the component is being hidden.
And pauseApp will be called by AMS when the application is about to be paused; this typically happens when there is an incoming phone call, or the flip is closed (on a flip phone) etc.
Hope it answers your question.

Resources