{"id":638,"date":"2013-07-22T00:05:21","date_gmt":"2013-07-21T22:05:21","guid":{"rendered":"http:\/\/www.jensd.de\/wordpress\/?p=638"},"modified":"2013-12-19T12:43:50","modified_gmt":"2013-12-19T11:43:50","slug":"raspi-does-the-home-automation-part-iv-cut-the-rope","status":"publish","type":"post","link":"https:\/\/www.jensd.de\/wordpress\/?p=638","title":{"rendered":"RasPi does the Home Automation (Part IV): Cut the &#8216;rope&#8217;"},"content":{"rendered":"<p>Hi all, <\/p>\n<p>until now all UI-Actions to control my wireless gears had to run directly on the Pi which controls the remote as e.g. <code><strong>deviceControl.turnOn(device);<\/strong><\/code> is called.<\/p>\n<p>But now I&#8217;d rather make a <strong>web service<\/strong> available for this purpose to be more independently.<br \/>\nQuestion is: Which server should I use?<\/p>\n<ul>\n<li><strong>Apache\/PHP<\/strong>: too less Java \ud83d\ude09<\/li>\n<li><strong>GlassFish\/Java EE<\/strong>: too fat for this purpose and hard to take for a RasPi<\/li>\n<\/ul>\n<p><strong>But wait a minute<\/strong>: wasn&#8217;t there a <code><strong>HttpServer<\/strong><\/code> class that comes with Java6?<br \/>\nUsing <code>HttpServer<\/code> I wrote a very small Server to provide the desired service. <\/p>\n<p>Now I can switch my devices by an URL like this:<\/p>\n<p><code><strong>http:\/\/RASPI-IP:PORT\/CONTEXT?houseCode=HOUSE-CODE&group=GROUP&device=DEVICE&command=COMMAND<\/strong><\/code><\/p>\n<p>e.g. <code><strong>http:\/\/raspi.home:22222\/homeserver?houseCode=a&group=1&device=2&command=1<\/strong><\/code><\/p>\n<p><strong>SEND URL:<\/strong><br \/>\n<a href=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"647\" data-permalink=\"https:\/\/www.jensd.de\/wordpress\/?attachment_id=647\" data-orig-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1.png\" data-orig-size=\"831,135\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"RasPi_HomeServer1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1-300x48.png\" data-large-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1.png\" tabindex=\"0\" role=\"button\" src=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1.png\" alt=\"RasPi_HomeServer1\" width=\"831\" height=\"135\" class=\"aligncenter size-full wp-image-647\" srcset=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1.png 831w, https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer1-300x48.png 300w\" sizes=\"(max-width: 831px) 100vw, 831px\" \/><\/a><\/p>\n<p><strong>SERVER (RE-)ACTION:<\/strong><br \/>\n<a href=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"645\" data-permalink=\"https:\/\/www.jensd.de\/wordpress\/?attachment_id=645\" data-orig-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2.png\" data-orig-size=\"711,429\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"RasPi_HomeServer2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2-300x181.png\" data-large-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2.png\" tabindex=\"0\" role=\"button\" src=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2.png\" alt=\"RasPi_HomeServer2\" width=\"711\" height=\"429\" class=\"aligncenter size-full wp-image-645\" srcset=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2.png 711w, https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/RasPi_HomeServer2-300x181.png 300w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/a><\/p>\n<p>Recently my Pi received a command from Madrid (thanks Jos&eacute; for supporting me ;-)):<\/p>\n<p><a href=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"658\" data-permalink=\"https:\/\/www.jensd.de\/wordpress\/?attachment_id=658\" data-orig-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions.png\" data-orig-size=\"523,327\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"2013-07-18 08_53_13-Twitter _ Interactions\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions-300x187.png\" data-large-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions.png\" tabindex=\"0\" role=\"button\" src=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions.png\" alt=\"2013-07-18 08_53_13-Twitter _ Interactions\" width=\"523\" height=\"327\" class=\"aligncenter size-full wp-image-658\" srcset=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions.png 523w, https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2013\/07\/2013-07-18-08_53_13-Twitter-_-Interactions-300x187.png 300w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/a><\/p>\n<p>For a more convenient usage with e.g. an JavaFX-EventHandler I use a <code><strong>HttpCommand<\/strong><\/code> to act like a <code>DeviceContol<\/code>:<\/p>\n<p>[java]<br \/>\npublic class SendHttpCommand {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Settings settings = new Settings();<br \/>\n        HttpCommand httpCommand = new HttpCommand(settings.getProperty(&#8220;sweethome.server.ip.local&#8221;));<br \/>\n        Device terrace = new Device(&#8220;Terrace&#8221;, &#8220;a&#8221;, &#8220;1&#8221;, &#8220;2&#8221;);<br \/>\n        String result = httpCommand.turnOn(terrace);<br \/>\n        System.out.println(result);<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now the <code>DevicePane<\/code> looks like this:<\/p>\n<p>[java]<br \/>\npublic class DevicePane extends AnchorPane {<\/p>\n<p>    @FXML<br \/>\n    private Button offButton;<br \/>\n    @FXML<br \/>\n    private Button onButton;<br \/>\n    @FXML<br \/>\n    private Text deviceNameText;<\/p>\n<p>    private Device device;<\/p>\n<p>    private DeviceControl deviceControl;<br \/>\n    private HttpCommand httpCommand;<\/p>\n<p>    public DevicePane(final Device device) {<br \/>\n        this.device = device;<br \/>\n        init();<br \/>\n    }<\/p>\n<p>    private void init() {<br \/>\n        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(&#8220;\/fxml\/DevicePane.fxml&#8221;));<br \/>\n        fxmlLoader.setRoot(this);<br \/>\n        fxmlLoader.setController(this);<br \/>\n        try {<br \/>\n            fxmlLoader.load();<br \/>\n        } catch (IOException ex) {<br \/>\n            Logger.getLogger(DevicePane.class.getName()).log(Level.SEVERE, null, ex);<br \/>\n        }<\/p>\n<p>        Settings settings = new Settings();<br \/>\n        httpCommand = new HttpCommand(settings.getProperty(&#8220;sweethome.server.ip.local&#8221;));<\/p>\n<p>        deviceControl = new DeviceControl();<\/p>\n<p>        deviceNameText.setText(device.getName());<\/p>\n<p>        offButton.setOnAction(new EventHandler<ActionEvent>() {<br \/>\n            @Override<br \/>\n            public void handle(ActionEvent t) {<br \/>\n                turnOff();<br \/>\n            }<br \/>\n        });<\/p>\n<p>        onButton.setOnAction(new EventHandler<ActionEvent>() {<br \/>\n            @Override<br \/>\n            public void handle(ActionEvent t) {<br \/>\n                turnOn();<br \/>\n            }<br \/>\n        });<br \/>\n    }<\/p>\n<p>    private void turnOn() {<br \/>\n        Platform.runLater(new Runnable() {<br \/>\n            @Override<br \/>\n            public void run() {<br \/>\n                deviceControl.turnOn(device);<br \/>\n                httpCommand.turnOn(device);<br \/>\n            }<br \/>\n        });<\/p>\n<p>    }<\/p>\n<p>    private void turnOff() {<br \/>\n        Platform.runLater(new Runnable() {<br \/>\n            @Override<br \/>\n            public void run() {<br \/>\n                deviceControl.turnOff(device);<br \/>\n                httpCommand.turnOff(device);<br \/>\n            }<br \/>\n        });<\/p>\n<p>    }<br \/>\n}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi all, until now all UI-Actions to control my wireless gears had to run directly on the Pi which controls the remote as e.g. deviceControl.turnOn(device); is called. But now I&#8217;d rather make a web service available for this purpose to be more independently. Question is: Which server should I use? Apache\/PHP: too less Java \ud83d\ude09&hellip; <span class=\"clear\"><\/span><a href=\"https:\/\/www.jensd.de\/wordpress\/?p=638\" class=\"more-link read-more\" rel=\"bookmark\">Continue Reading <span class=\"screen-reader-text\">RasPi does the Home Automation (Part IV): Cut the &#8216;rope&#8217;<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"#RaspberryPi does the #HomeAutomation with #JavaFX (Part IV): Cut the 'rope' http:\/\/wp.me\/p38FCL-ai","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21,22,30,4,18,1],"tags":[87,24,91,76,86],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p38FCL-ai","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/638"}],"collection":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=638"}],"version-history":[{"count":46,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}