{"action":"created","comment":{"url":"https://api.github.com/repos/openhab/openhab/pulls/comments/54232924","id":54232924,"diff_hunk":"@@ -0,0 +1,258 @@\n+/**\n+ * Copyright (c) 2010-2015, openHAB.org and others.\n+ *\n+ * All rights reserved. This program and the accompanying materials\n+ * are made available under the terms of the Eclipse Public License v1.0\n+ * which accompanies this distribution, and is available at\n+ * http://www.eclipse.org/legal/epl-v10.html\n+ */\n+package org.openhab.binding.onkyo.internal.eiscp;\n+\n+import java.io.IOException;\n+import java.io.InputStream;\n+import java.io.OutputStream;\n+import java.util.Date;\n+\n+import org.apache.commons.io.IOUtils;\n+import org.openhab.binding.onkyo.internal.OnkyoEventListener;\n+import org.openhab.binding.onkyo.internal.OnkyoStatusUpdateEvent;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import gnu.io.CommPort;\n+import gnu.io.CommPortIdentifier;\n+import gnu.io.SerialPort;\n+import gnu.io.SerialPortEvent;\n+import gnu.io.SerialPortEventListener;\n+\n+/**\n+ * Onkyo Connector for serial port communication.\n+ *\n+ * @author Sriram Balakrishnan\n+ * @since 1.9.0\n+ */\n+public class EiscpSerial implements EiscpInterface, SerialPortEventListener {\n+\n+    private static final Logger logger = LoggerFactory.getLogger(EiscpSerial.class);\n+\n+    String serialPortName = null;\n+    InputStream in = null;\n+    OutputStream out = null;\n+    SerialPort serialPort = null;\n+    EispEventNotifier notifier = null;\n+\n+    private int retryCount = 1;\n+    private int timeout = 10000; // Onkyo Doc says 50ms timeout\n+\n+    public EiscpSerial(String serialPort) {\n+        serialPortName = serialPort;\n+        notifier = new EispEventNotifier(serialPort);\n+    }\n+\n+    @Override\n+    public synchronized void addEventListener(OnkyoEventListener listener) {\n+        notifier.addEventListener(listener);\n+    }\n+\n+    @Override\n+    public synchronized void removeEventListener(OnkyoEventListener listener) {\n+        notifier.removeEventListener(listener);\n+    }\n+\n+    @Override\n+    public int getRetryCount() {\n+        return retryCount;\n+    }\n+\n+    @Override\n+    public void setRetryCount(int retryCount) {\n+        this.retryCount = retryCount;\n+    }\n+\n+    @Override\n+    public boolean connectSocket() {\n+        return connect(serialPortName);\n+    }\n+\n+    /**\n+     * {@inheritDoc}\n+     */\n+    public boolean connect(String serialPortName) {\n+\n+        try {\n+            logger.debug(\"Open connection to serial port '{}'\", serialPortName);\n+            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);\n+\n+            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);\n+\n+            serialPort = (SerialPort) commPort;\n+            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);\n+            serialPort.enableReceiveThreshold(1);\n+            serialPort.disableReceiveTimeout();\n+\n+            in = serialPort.getInputStream();\n+            out = serialPort.getOutputStream();\n+\n+            out.flush();\n+            if (in.markSupported()) {\n+                in.reset();\n+            }\n+\n+            // RXTX serial port library causes high CPU load\n+            // Start event listener, which will just sleep and slow down event loop\n+            serialPort.addEventListener(this);\n+            serialPort.notifyOnDataAvailable(true);\n+            return true;\n+        } catch (Exception e) {\n+            logger.error(\"serial port connect error\", e);\n+            e.printStackTrace();\n+            return false;\n+        }\n+\n+    }\n+\n+    /**\n+     * {@inheritDoc}\n+     */\n+    @Override\n+    public boolean closeSocket() {\n+        try {\n+            if (out != null) {\n+                logger.debug(\"Close serial out stream\");\n+                IOUtils.closeQuietly(out);\n+            }\n+            if (in != null) {\n+                logger.debug(\"Close serial in stream\");\n+                IOUtils.closeQuietly(in);\n+            }\n+            if (serialPort != null) {\n+                logger.debug(\"Close serial port\");\n+                serialPort.close();\n+            }\n+\n+            serialPort.removeEventListener();\n+\n+            serialPort = null;\n+            out = null;\n+            in = null;\n+\n+            logger.debug(\"Closed\");\n+            return true;\n+        } catch (Exception e) {\n+            logger.error(\"Unable to close \", e);\n+            return false;\n+        }\n+    }\n+\n+    private StringBuilder getEiscpMessage(String eiscpCmd) {\n+        logger.debug(\"Requested Command is \" + eiscpCmd);\n+        StringBuilder sb = new StringBuilder();\n+        sb.append(\"!\");\n+        sb.append(\"1\");\n+        sb.append(eiscpCmd);\n+        sb.append((char) 0x0D);\n+        logger.debug(\"Serial command is \" + sb);","path":"bundles/binding/org.openhab.binding.onkyo/src/main/java/org/openhab/binding/onkyo/internal/eiscp/EiscpSerial.java","position":154,"original_position":154,"commit_id":"fd96d6344f223fb680ea7b1a0752f9a6774230ed","original_commit_id":"fd96d6344f223fb680ea7b1a0752f9a6774230ed","user":{"login":"watou","id":3008217,"avatar_url":"https://avatars.githubusercontent.com/u/3008217?v=3","gravatar_id":"","url":"https://api.github.com/users/watou","html_url":"https://github.com/watou","followers_url":"https://api.github.com/users/watou/followers","following_url":"https://api.github.com/users/watou/following{/other_user}","gists_url":"https://api.github.com/users/watou/gists{/gist_id}","starred_url":"https://api.github.com/users/watou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/watou/subscriptions","organizations_url":"https://api.github.com/users/watou/orgs","repos_url":"https://api.github.com/users/watou/repos","events_url":"https://api.github.com/users/watou/events{/privacy}","received_events_url":"https://api.github.com/users/watou/received_events","type":"User","site_admin":false},"body":"Definitely use parameterized logging here instead of string concatenation, because otherwise this method will be generating garbage objects on every call!","created_at":"2016-02-26T11:32:34Z","updated_at":"2016-02-26T11:32:34Z","html_url":"https://github.com/openhab/openhab/pull/4100#discussion_r54232924","pull_request_url":"https://api.github.com/repos/openhab/openhab/pulls/4100","_links":{"self":{"href":"https://api.github.com/repos/openhab/openhab/pulls/comments/54232924"},"html":{"href":"https://github.com/openhab/openhab/pull/4100#discussion_r54232924"},"pull_request":{"href":"https://api.github.com/repos/openhab/openhab/pulls/4100"}}},"pull_request":{"url":"https://api.github.com/repos/openhab/openhab/pulls/4100","id":60589321,"html_url":"https://github.com/openhab/openhab/pull/4100","diff_url":"https://github.com/openhab/openhab/pull/4100.diff","patch_url":"https://github.com/openhab/openhab/pull/4100.patch","issue_url":"https://api.github.com/repos/openhab/openhab/issues/4100","number":4100,"state":"open","locked":false,"title":"modified to support both network based and serial based Onkyo receivers","user":{"login":"basriram","id":5576115,"avatar_url":"https://avatars.githubusercontent.com/u/5576115?v=3","gravatar_id":"","url":"https://api.github.com/users/basriram","html_url":"https://github.com/basriram","followers_url":"https://api.github.com/users/basriram/followers","following_url":"https://api.github.com/users/basriram/following{/other_user}","gists_url":"https://api.github.com/users/basriram/gists{/gist_id}","starred_url":"https://api.github.com/users/basriram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basriram/subscriptions","organizations_url":"https://api.github.com/users/basriram/orgs","repos_url":"https://api.github.com/users/basriram/repos","events_url":"https://api.github.com/users/basriram/events{/privacy}","received_events_url":"https://api.github.com/users/basriram/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-24T22:21:37Z","updated_at":"2016-02-26T11:32:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7418c9600bcfaab2bd47695b662a9305d98b661b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/openhab/openhab/pulls/4100/commits","review_comments_url":"https://api.github.com/repos/openhab/openhab/pulls/4100/comments","review_comment_url":"https://api.github.com/repos/openhab/openhab/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openhab/openhab/issues/4100/comments","statuses_url":"https://api.github.com/repos/openhab/openhab/statuses/fd96d6344f223fb680ea7b1a0752f9a6774230ed","head":{"label":"basriram:master","ref":"master","sha":"fd96d6344f223fb680ea7b1a0752f9a6774230ed","user":{"login":"basriram","id":5576115,"avatar_url":"https://avatars.githubusercontent.com/u/5576115?v=3","gravatar_id":"","url":"https://api.github.com/users/basriram","html_url":"https://github.com/basriram","followers_url":"https://api.github.com/users/basriram/followers","following_url":"https://api.github.com/users/basriram/following{/other_user}","gists_url":"https://api.github.com/users/basriram/gists{/gist_id}","starred_url":"https://api.github.com/users/basriram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basriram/subscriptions","organizations_url":"https://api.github.com/users/basriram/orgs","repos_url":"https://api.github.com/users/basriram/repos","events_url":"https://api.github.com/users/basriram/events{/privacy}","received_events_url":"https://api.github.com/users/basriram/received_events","type":"User","site_admin":false},"repo":{"id":52476359,"name":"openhab","full_name":"basriram/openhab","owner":{"login":"basriram","id":5576115,"avatar_url":"https://avatars.githubusercontent.com/u/5576115?v=3","gravatar_id":"","url":"https://api.github.com/users/basriram","html_url":"https://github.com/basriram","followers_url":"https://api.github.com/users/basriram/followers","following_url":"https://api.github.com/users/basriram/following{/other_user}","gists_url":"https://api.github.com/users/basriram/gists{/gist_id}","starred_url":"https://api.github.com/users/basriram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basriram/subscriptions","organizations_url":"https://api.github.com/users/basriram/orgs","repos_url":"https://api.github.com/users/basriram/repos","events_url":"https://api.github.com/users/basriram/events{/privacy}","received_events_url":"https://api.github.com/users/basriram/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/basriram/openhab","description":"Source code of the open Home Automation Bus (openHAB)","fork":true,"url":"https://api.github.com/repos/basriram/openhab","forks_url":"https://api.github.com/repos/basriram/openhab/forks","keys_url":"https://api.github.com/repos/basriram/openhab/keys{/key_id}","collaborators_url":"https://api.github.com/repos/basriram/openhab/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/basriram/openhab/teams","hooks_url":"https://api.github.com/repos/basriram/openhab/hooks","issue_events_url":"https://api.github.com/repos/basriram/openhab/issues/events{/number}","events_url":"https://api.github.com/repos/basriram/openhab/events","assignees_url":"https://api.github.com/repos/basriram/openhab/assignees{/user}","branches_url":"https://api.github.com/repos/basriram/openhab/branches{/branch}","tags_url":"https://api.github.com/repos/basriram/openhab/tags","blobs_url":"https://api.github.com/repos/basriram/openhab/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/basriram/openhab/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/basriram/openhab/git/refs{/sha}","trees_url":"https://api.github.com/repos/basriram/openhab/git/trees{/sha}","statuses_url":"https://api.github.com/repos/basriram/openhab/statuses/{sha}","languages_url":"https://api.github.com/repos/basriram/openhab/languages","stargazers_url":"https://api.github.com/repos/basriram/openhab/stargazers","contributors_url":"https://api.github.com/repos/basriram/openhab/contributors","subscribers_url":"https://api.github.com/repos/basriram/openhab/subscribers","subscription_url":"https://api.github.com/repos/basriram/openhab/subscription","commits_url":"https://api.github.com/repos/basriram/openhab/commits{/sha}","git_commits_url":"https://api.github.com/repos/basriram/openhab/git/commits{/sha}","comments_url":"https://api.github.com/repos/basriram/openhab/comments{/number}","issue_comment_url":"https://api.github.com/repos/basriram/openhab/issues/comments{/number}","contents_url":"https://api.github.com/repos/basriram/openhab/contents/{+path}","compare_url":"https://api.github.com/repos/basriram/openhab/compare/{base}...{head}","merges_url":"https://api.github.com/repos/basriram/openhab/merges","archive_url":"https://api.github.com/repos/basriram/openhab/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/basriram/openhab/downloads","issues_url":"https://api.github.com/repos/basriram/openhab/issues{/number}","pulls_url":"https://api.github.com/repos/basriram/openhab/pulls{/number}","milestones_url":"https://api.github.com/repos/basriram/openhab/milestones{/number}","notifications_url":"https://api.github.com/repos/basriram/openhab/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/basriram/openhab/labels{/name}","releases_url":"https://api.github.com/repos/basriram/openhab/releases{/id}","deployments_url":"https://api.github.com/repos/basriram/openhab/deployments","created_at":"2016-02-24T21:40:29Z","updated_at":"2016-02-24T21:40:44Z","pushed_at":"2016-02-25T16:40:52Z","git_url":"git://github.com/basriram/openhab.git","ssh_url":"git@github.com:basriram/openhab.git","clone_url":"https://github.com/basriram/openhab.git","svn_url":"https://github.com/basriram/openhab","homepage":null,"size":190652,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openhab:master","ref":"master","sha":"e068a13158e31540713d459271cac10ad6cfec01","user":{"login":"openhab","id":1007353,"avatar_url":"https://avatars.githubusercontent.com/u/1007353?v=3","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"repo":{"id":12983151,"name":"openhab","full_name":"openhab/openhab","owner":{"login":"openhab","id":1007353,"avatar_url":"https://avatars.githubusercontent.com/u/1007353?v=3","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openhab/openhab","description":"Source code of the open Home Automation Bus (openHAB)","fork":false,"url":"https://api.github.com/repos/openhab/openhab","forks_url":"https://api.github.com/repos/openhab/openhab/forks","keys_url":"https://api.github.com/repos/openhab/openhab/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openhab/openhab/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openhab/openhab/teams","hooks_url":"https://api.github.com/repos/openhab/openhab/hooks","issue_events_url":"https://api.github.com/repos/openhab/openhab/issues/events{/number}","events_url":"https://api.github.com/repos/openhab/openhab/events","assignees_url":"https://api.github.com/repos/openhab/openhab/assignees{/user}","branches_url":"https://api.github.com/repos/openhab/openhab/branches{/branch}","tags_url":"https://api.github.com/repos/openhab/openhab/tags","blobs_url":"https://api.github.com/repos/openhab/openhab/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openhab/openhab/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openhab/openhab/git/refs{/sha}","trees_url":"https://api.github.com/repos/openhab/openhab/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openhab/openhab/statuses/{sha}","languages_url":"https://api.github.com/repos/openhab/openhab/languages","stargazers_url":"https://api.github.com/repos/openhab/openhab/stargazers","contributors_url":"https://api.github.com/repos/openhab/openhab/contributors","subscribers_url":"https://api.github.com/repos/openhab/openhab/subscribers","subscription_url":"https://api.github.com/repos/openhab/openhab/subscription","commits_url":"https://api.github.com/repos/openhab/openhab/commits{/sha}","git_commits_url":"https://api.github.com/repos/openhab/openhab/git/commits{/sha}","comments_url":"https://api.github.com/repos/openhab/openhab/comments{/number}","issue_comment_url":"https://api.github.com/repos/openhab/openhab/issues/comments{/number}","contents_url":"https://api.github.com/repos/openhab/openhab/contents/{+path}","compare_url":"https://api.github.com/repos/openhab/openhab/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openhab/openhab/merges","archive_url":"https://api.github.com/repos/openhab/openhab/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openhab/openhab/downloads","issues_url":"https://api.github.com/repos/openhab/openhab/issues{/number}","pulls_url":"https://api.github.com/repos/openhab/openhab/pulls{/number}","milestones_url":"https://api.github.com/repos/openhab/openhab/milestones{/number}","notifications_url":"https://api.github.com/repos/openhab/openhab/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openhab/openhab/labels{/name}","releases_url":"https://api.github.com/repos/openhab/openhab/releases{/id}","deployments_url":"https://api.github.com/repos/openhab/openhab/deployments","created_at":"2013-09-20T20:28:43Z","updated_at":"2016-02-26T07:03:51Z","pushed_at":"2016-02-26T10:40:32Z","git_url":"git://github.com/openhab/openhab.git","ssh_url":"git@github.com:openhab/openhab.git","clone_url":"https://github.com/openhab/openhab.git","svn_url":"https://github.com/openhab/openhab","homepage":null,"size":271141,"stargazers_count":2306,"watchers_count":2306,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1221,"mirror_url":null,"open_issues_count":511,"forks":1221,"open_issues":511,"watchers":2306,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openhab/openhab/pulls/4100"},"html":{"href":"https://github.com/openhab/openhab/pull/4100"},"issue":{"href":"https://api.github.com/repos/openhab/openhab/issues/4100"},"comments":{"href":"https://api.github.com/repos/openhab/openhab/issues/4100/comments"},"review_comments":{"href":"https://api.github.com/repos/openhab/openhab/pulls/4100/comments"},"review_comment":{"href":"https://api.github.com/repos/openhab/openhab/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openhab/openhab/pulls/4100/commits"},"statuses":{"href":"https://api.github.com/repos/openhab/openhab/statuses/fd96d6344f223fb680ea7b1a0752f9a6774230ed"}}}}