[英]How can I implement actions based on my rest API
I have a plan of automating a process via REST api and multiple smart devices. 我有一个通过REST api和多个智能设备自动化流程的计划。 What I am trying to do is the following.
我想要做的是以下内容。 I have 4 devices that each do a series of actions to complete their task.
我有4个设备,每个设备都执行一系列操作来完成他们的任务。 (practicaly Im gonna be lighting 4 leds in a particular series based on the device).
(实际上,我将根据设备点亮特定系列中的4个LED)。 Im currently emulating this in Java (since I dont have the devices at hand)
我目前正在用Java模拟这个(因为我手头没有设备)
So for example lets say device no.1 lights up the red led first for 10 seconds, after that device no.2 needs to light up for 10 seconds etc. That's the task. 因此,例如,假设设备1号首先点亮红色LED指示灯10秒钟,之后设备2号点亮需要点亮10秒等等。这就是任务。 This is done automatically and forever till I shutdown the program.
这是自动完成的,直到我关闭程序。
I have developed a REST API using JAVA and the JAX-RS library where I state the devices and their info (so device name, current state ex. Yellow Led ON/OFF etc.). 我使用JAVA和JAX-RS库开发了一个REST API,其中我说明了设备及其信息(因此设备名称,当前状态,例如Yellow Led ON / OFF等)。
What I dont understand is how I am gonna make the program I have now respond to the info from the API. 我不明白的是我将如何制作我现在回复API信息的程序。 How can I make my program get notified when a value is changed on the API info?
如何在API信息上更改值时,如何通知程序?
How can I make device 2 poll the API till it gets the response to start its action? 如何让设备2轮询API直到获得响应以启动其操作? Or make it so it starts the action if i manually change the value on the server data?
或者,如果我手动更改服务器数据上的值,它会启动操作? Example if the yellow led is on, and i go in the server database and put it on off, how will my application get notified and change the state of the simulated led?
例如,如果黄色LED指示灯亮起,我进入服务器数据库并将其关闭,我的应用程序将如何得到通知并更改模拟指示灯的状态?
Since you are using HTTP, most suitable solution is to use web sockets . 由于您使用的是HTTP,因此最合适的解决方案是使用Web套接字 。 You can use them for full duplex communication.
您可以将它们用于全双工通信。 When you receive input from one device, you can process it and inform other devices.
当您从一台设备接收输入时,您可以处理它并通知其他设备。 Note that this is not suitable for a large client base.
请注意,这不适合大型客户群。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.