{"action":"created","comment":{"url":"https://api.github.com/repos/openT2T/translators/pulls/comments/70482513","id":70482513,"diff_hunk":"@@ -0,0 +1,174 @@\n+'use strict';\n+\n+var https = require('https');\n+var q = require('q');\n+\n+// logs device state\n+function logDeviceState(device) {\n+    if (typeof (device) !== 'undefined') {\n+        console.log('  device.name          : ' + device.name);\n+        console.log('  device.props         : ' + device.props);\n+    } else {\n+        console.log('device is undefined');\n+    }\n+};\n+\n+// Helper method to send power state commands to Vera\n+function sendPowerStateCommandToDevice(loadTarget, deviceId, relayServer, relaySessionToken, pkDevice) {\n+    var deferred = q.defer();\n+\n+    // set up the url which needs:\n+    //   the serial number of the hub\n+    //   deviceid of the light bulb\n+    //   target brightness\n+    var path = '/relay/relay/relay/device/' + pkDevice + '/port_3480/data_request?id=action&output_format=json&DeviceNum=' + deviceId + '&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget=' + loadTarget;\n+\n+    var options = {\n+        protocol: 'https:',\n+        host: relayServer,\n+        path: path,\n+        headers: {\n+            'MMSSession': relaySessionToken\n+        },\n+        method: 'GET'\n+    };\n+\n+    var req = https.request(options);\n+\n+    req.on('error', (e) => {\n+        console.log('problem with request:' + e.message);\n+        deferred.reject(e);\n+    });\n+\n+    req.on('response', (res) => {\n+        res.setEncoding('utf8');\n+\n+        res.on('data', (chunk) => {\n+        });\n+\n+        res.on('end', () => {\n+            if (res.statusCode != 200)\n+            {\n+                console.log(res);\n+                deferred.reject(res);\n+            }\n+            else\n+            {\n+                deferred.resolve(res);\n+            }\n+        });\n+\n+        res.on('error', (e) => {\n+            console.log('problem with response:' + e.message);\n+            deferred.reject(e);\n+        });\n+    });\n+\n+    req.end();\n+\n+    return deferred.promise;\n+}\n+\n+// module exports, implementing the schema\n+module.exports = {\n+\n+    // numeric int device id\n+    deviceId : null,\n+\n+    // server used to connect to the vera hub\n+    relayServer : null,\n+\n+    // session token for the relay server\n+    relaySessionToken : null,\n+\n+    // serial number of the vera hub\n+    pkDevice : null,\n+\n+    // data structure which represents the device targetted by this translator\n+    device: null,\n+\n+    initDevice: function(dev) {\n+        var deferred = q.defer();\n+        this.device = dev;\n+\n+        if (typeof this.device != 'undefined') {\n+            if (typeof (this.device.props) !== 'undefined') {\n+                var props = JSON.parse(this.device.props);\n+\n+                if (typeof (props.relay_session_token) !== 'undefined') {\n+                    this.relaySessionToken = props.relay_session_token;\n+                } else {\n+                    console.log('props.relay_session_token is undefined.');\n+                    setImmediate(deferred.reject);\n+                }\n+\n+                if (typeof (props.relay_server) !== 'undefined') {\n+                    this.relayServer = props.relay_server;\n+                } else {\n+                    console.log('props.relay_server is undefined.');\n+                    setImmediate(deferred.reject);\n+                }\n+\n+                if (typeof (props.pk_device) !== 'undefined') {\n+                    this.pkDevice = props.pk_device;\n+                } else {\n+                    console.log('props.pk_device is undefined.');\n+                    setImmediate(deferred.reject);\n+                }\n+\n+                if (typeof (props.id) !== 'undefined') {\n+                    this.deviceId = props.id;\n+                } else {\n+                    console.log('props.id is undefined.');\n+                    setImmediate(deferred.reject);\n+                }\n+\n+                deferred.resolve();\n+            } else {\n+                console.log('props is undefined.');\n+                setImmediate(deferred.reject);\n+            }\n+        } else {\n+            console.log('device is undefined.');\n+            setImmediate(deferred.reject);\n+        }\n+\n+        console.log('Javascript initialized.');\n+        logDeviceState(this.device);\n+\n+        return deferred.promise;\n+    },\n+\n+    turnOn: function() {\n+        console.log('[' + this.deviceId + '] turnOn called.');\n+        return sendPowerStateCommandToDevice(100, this.deviceId, this.relayServer, this.relaySessionToken, this.pkDevice);\n+    },\n+\n+    turnOff: function() {\n+        console.log('[' + this.deviceId + '] turnOff called.');\n+        return sendPowerStateCommandToDevice(0, this.deviceId, this.relayServer, this.relaySessionToken, this.pkDevice);\n+    },\n+\n+    // sets the dimmable bulb to the desired brightness, valid values: integer 0-100\n+    setBrightness: function(brightness) {\n+        console.log('[' + this.deviceId + '] setBrightness called with value: ' + brightness);\n+        return sendPowerStateCommandToDevice(brightness, this.deviceId, this.relayServer, this.relaySessionToken, this.pkDevice);\n+    },\n+\n+    disconnect: function() {\n+        console.log('disconnect called.');\n+        var deferred = q.defer();","path":"org.OpenT2T.Sample.SuperPopular.Lamp/Vera Dimmable Light Bulb/js/thingTranslator.js","position":160,"original_position":160,"commit_id":"64d994b7e33f03d81c38908295cb81586af3c9e5","original_commit_id":"64d994b7e33f03d81c38908295cb81586af3c9e5","user":{"login":"jasongin","id":13093042,"avatar_url":"https://avatars.githubusercontent.com/u/13093042?v=3","gravatar_id":"","url":"https://api.github.com/users/jasongin","html_url":"https://github.com/jasongin","followers_url":"https://api.github.com/users/jasongin/followers","following_url":"https://api.github.com/users/jasongin/following{/other_user}","gists_url":"https://api.github.com/users/jasongin/gists{/gist_id}","starred_url":"https://api.github.com/users/jasongin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasongin/subscriptions","organizations_url":"https://api.github.com/users/jasongin/orgs","repos_url":"https://api.github.com/users/jasongin/repos","events_url":"https://api.github.com/users/jasongin/events{/privacy}","received_events_url":"https://api.github.com/users/jasongin/received_events","type":"User","site_admin":false},"body":"Again, no need for a promise here. (For all the methods, the caller will detect whether or not a promise was returned.)","created_at":"2016-07-12T17:27:37Z","updated_at":"2016-07-12T17:27:37Z","html_url":"https://github.com/openT2T/translators/pull/29#discussion_r70482513","pull_request_url":"https://api.github.com/repos/openT2T/translators/pulls/29","_links":{"self":{"href":"https://api.github.com/repos/openT2T/translators/pulls/comments/70482513"},"html":{"href":"https://github.com/openT2T/translators/pull/29#discussion_r70482513"},"pull_request":{"href":"https://api.github.com/repos/openT2T/translators/pulls/29"}}},"pull_request":{"url":"https://api.github.com/repos/openT2T/translators/pulls/29","id":76816526,"html_url":"https://github.com/openT2T/translators/pull/29","diff_url":"https://github.com/openT2T/translators/pull/29.diff","patch_url":"https://github.com/openT2T/translators/pull/29.patch","issue_url":"https://api.github.com/repos/openT2T/translators/issues/29","number":29,"state":"open","locked":false,"title":"Initial checkin for the vera dimmable light translator.","user":{"login":"arjun-msft","id":20304470,"avatar_url":"https://avatars.githubusercontent.com/u/20304470?v=3","gravatar_id":"","url":"https://api.github.com/users/arjun-msft","html_url":"https://github.com/arjun-msft","followers_url":"https://api.github.com/users/arjun-msft/followers","following_url":"https://api.github.com/users/arjun-msft/following{/other_user}","gists_url":"https://api.github.com/users/arjun-msft/gists{/gist_id}","starred_url":"https://api.github.com/users/arjun-msft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjun-msft/subscriptions","organizations_url":"https://api.github.com/users/arjun-msft/orgs","repos_url":"https://api.github.com/users/arjun-msft/repos","events_url":"https://api.github.com/users/arjun-msft/events{/privacy}","received_events_url":"https://api.github.com/users/arjun-msft/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-07-08T21:24:45Z","updated_at":"2016-07-12T17:27:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"354fc705f6d4a8330effd85ba563d0f9108b07b8","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/openT2T/translators/pulls/29/commits","review_comments_url":"https://api.github.com/repos/openT2T/translators/pulls/29/comments","review_comment_url":"https://api.github.com/repos/openT2T/translators/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openT2T/translators/issues/29/comments","statuses_url":"https://api.github.com/repos/openT2T/translators/statuses/64d994b7e33f03d81c38908295cb81586af3c9e5","head":{"label":"arjun-msft:master","ref":"master","sha":"64d994b7e33f03d81c38908295cb81586af3c9e5","user":{"login":"arjun-msft","id":20304470,"avatar_url":"https://avatars.githubusercontent.com/u/20304470?v=3","gravatar_id":"","url":"https://api.github.com/users/arjun-msft","html_url":"https://github.com/arjun-msft","followers_url":"https://api.github.com/users/arjun-msft/followers","following_url":"https://api.github.com/users/arjun-msft/following{/other_user}","gists_url":"https://api.github.com/users/arjun-msft/gists{/gist_id}","starred_url":"https://api.github.com/users/arjun-msft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjun-msft/subscriptions","organizations_url":"https://api.github.com/users/arjun-msft/orgs","repos_url":"https://api.github.com/users/arjun-msft/repos","events_url":"https://api.github.com/users/arjun-msft/events{/privacy}","received_events_url":"https://api.github.com/users/arjun-msft/received_events","type":"User","site_admin":false},"repo":{"id":62918099,"name":"translators","full_name":"arjun-msft/translators","owner":{"login":"arjun-msft","id":20304470,"avatar_url":"https://avatars.githubusercontent.com/u/20304470?v=3","gravatar_id":"","url":"https://api.github.com/users/arjun-msft","html_url":"https://github.com/arjun-msft","followers_url":"https://api.github.com/users/arjun-msft/followers","following_url":"https://api.github.com/users/arjun-msft/following{/other_user}","gists_url":"https://api.github.com/users/arjun-msft/gists{/gist_id}","starred_url":"https://api.github.com/users/arjun-msft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjun-msft/subscriptions","organizations_url":"https://api.github.com/users/arjun-msft/orgs","repos_url":"https://api.github.com/users/arjun-msft/repos","events_url":"https://api.github.com/users/arjun-msft/events{/privacy}","received_events_url":"https://api.github.com/users/arjun-msft/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/arjun-msft/translators","description":"Repository for Open Translators to Things. ","fork":true,"url":"https://api.github.com/repos/arjun-msft/translators","forks_url":"https://api.github.com/repos/arjun-msft/translators/forks","keys_url":"https://api.github.com/repos/arjun-msft/translators/keys{/key_id}","collaborators_url":"https://api.github.com/repos/arjun-msft/translators/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/arjun-msft/translators/teams","hooks_url":"https://api.github.com/repos/arjun-msft/translators/hooks","issue_events_url":"https://api.github.com/repos/arjun-msft/translators/issues/events{/number}","events_url":"https://api.github.com/repos/arjun-msft/translators/events","assignees_url":"https://api.github.com/repos/arjun-msft/translators/assignees{/user}","branches_url":"https://api.github.com/repos/arjun-msft/translators/branches{/branch}","tags_url":"https://api.github.com/repos/arjun-msft/translators/tags","blobs_url":"https://api.github.com/repos/arjun-msft/translators/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/arjun-msft/translators/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/arjun-msft/translators/git/refs{/sha}","trees_url":"https://api.github.com/repos/arjun-msft/translators/git/trees{/sha}","statuses_url":"https://api.github.com/repos/arjun-msft/translators/statuses/{sha}","languages_url":"https://api.github.com/repos/arjun-msft/translators/languages","stargazers_url":"https://api.github.com/repos/arjun-msft/translators/stargazers","contributors_url":"https://api.github.com/repos/arjun-msft/translators/contributors","subscribers_url":"https://api.github.com/repos/arjun-msft/translators/subscribers","subscription_url":"https://api.github.com/repos/arjun-msft/translators/subscription","commits_url":"https://api.github.com/repos/arjun-msft/translators/commits{/sha}","git_commits_url":"https://api.github.com/repos/arjun-msft/translators/git/commits{/sha}","comments_url":"https://api.github.com/repos/arjun-msft/translators/comments{/number}","issue_comment_url":"https://api.github.com/repos/arjun-msft/translators/issues/comments{/number}","contents_url":"https://api.github.com/repos/arjun-msft/translators/contents/{+path}","compare_url":"https://api.github.com/repos/arjun-msft/translators/compare/{base}...{head}","merges_url":"https://api.github.com/repos/arjun-msft/translators/merges","archive_url":"https://api.github.com/repos/arjun-msft/translators/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/arjun-msft/translators/downloads","issues_url":"https://api.github.com/repos/arjun-msft/translators/issues{/number}","pulls_url":"https://api.github.com/repos/arjun-msft/translators/pulls{/number}","milestones_url":"https://api.github.com/repos/arjun-msft/translators/milestones{/number}","notifications_url":"https://api.github.com/repos/arjun-msft/translators/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/arjun-msft/translators/labels{/name}","releases_url":"https://api.github.com/repos/arjun-msft/translators/releases{/id}","deployments_url":"https://api.github.com/repos/arjun-msft/translators/deployments","created_at":"2016-07-08T21:20:55Z","updated_at":"2016-07-08T21:20:56Z","pushed_at":"2016-07-08T21:24:16Z","git_url":"git://github.com/arjun-msft/translators.git","ssh_url":"git@github.com:arjun-msft/translators.git","clone_url":"https://github.com/arjun-msft/translators.git","svn_url":"https://github.com/arjun-msft/translators","homepage":"http://www.opentranslatorstothings.org","size":101,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"openT2T:master","ref":"master","sha":"6c08f416314fb7d7ac14f87ee0a2d18c7c8d1b3f","user":{"login":"openT2T","id":17659874,"avatar_url":"https://avatars.githubusercontent.com/u/17659874?v=3","gravatar_id":"","url":"https://api.github.com/users/openT2T","html_url":"https://github.com/openT2T","followers_url":"https://api.github.com/users/openT2T/followers","following_url":"https://api.github.com/users/openT2T/following{/other_user}","gists_url":"https://api.github.com/users/openT2T/gists{/gist_id}","starred_url":"https://api.github.com/users/openT2T/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openT2T/subscriptions","organizations_url":"https://api.github.com/users/openT2T/orgs","repos_url":"https://api.github.com/users/openT2T/repos","events_url":"https://api.github.com/users/openT2T/events{/privacy}","received_events_url":"https://api.github.com/users/openT2T/received_events","type":"Organization","site_admin":false},"repo":{"id":55200869,"name":"translators","full_name":"openT2T/translators","owner":{"login":"openT2T","id":17659874,"avatar_url":"https://avatars.githubusercontent.com/u/17659874?v=3","gravatar_id":"","url":"https://api.github.com/users/openT2T","html_url":"https://github.com/openT2T","followers_url":"https://api.github.com/users/openT2T/followers","following_url":"https://api.github.com/users/openT2T/following{/other_user}","gists_url":"https://api.github.com/users/openT2T/gists{/gist_id}","starred_url":"https://api.github.com/users/openT2T/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openT2T/subscriptions","organizations_url":"https://api.github.com/users/openT2T/orgs","repos_url":"https://api.github.com/users/openT2T/repos","events_url":"https://api.github.com/users/openT2T/events{/privacy}","received_events_url":"https://api.github.com/users/openT2T/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openT2T/translators","description":"Repository for Open Translators to Things. ","fork":false,"url":"https://api.github.com/repos/openT2T/translators","forks_url":"https://api.github.com/repos/openT2T/translators/forks","keys_url":"https://api.github.com/repos/openT2T/translators/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openT2T/translators/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openT2T/translators/teams","hooks_url":"https://api.github.com/repos/openT2T/translators/hooks","issue_events_url":"https://api.github.com/repos/openT2T/translators/issues/events{/number}","events_url":"https://api.github.com/repos/openT2T/translators/events","assignees_url":"https://api.github.com/repos/openT2T/translators/assignees{/user}","branches_url":"https://api.github.com/repos/openT2T/translators/branches{/branch}","tags_url":"https://api.github.com/repos/openT2T/translators/tags","blobs_url":"https://api.github.com/repos/openT2T/translators/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openT2T/translators/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openT2T/translators/git/refs{/sha}","trees_url":"https://api.github.com/repos/openT2T/translators/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openT2T/translators/statuses/{sha}","languages_url":"https://api.github.com/repos/openT2T/translators/languages","stargazers_url":"https://api.github.com/repos/openT2T/translators/stargazers","contributors_url":"https://api.github.com/repos/openT2T/translators/contributors","subscribers_url":"https://api.github.com/repos/openT2T/translators/subscribers","subscription_url":"https://api.github.com/repos/openT2T/translators/subscription","commits_url":"https://api.github.com/repos/openT2T/translators/commits{/sha}","git_commits_url":"https://api.github.com/repos/openT2T/translators/git/commits{/sha}","comments_url":"https://api.github.com/repos/openT2T/translators/comments{/number}","issue_comment_url":"https://api.github.com/repos/openT2T/translators/issues/comments{/number}","contents_url":"https://api.github.com/repos/openT2T/translators/contents/{+path}","compare_url":"https://api.github.com/repos/openT2T/translators/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openT2T/translators/merges","archive_url":"https://api.github.com/repos/openT2T/translators/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openT2T/translators/downloads","issues_url":"https://api.github.com/repos/openT2T/translators/issues{/number}","pulls_url":"https://api.github.com/repos/openT2T/translators/pulls{/number}","milestones_url":"https://api.github.com/repos/openT2T/translators/milestones{/number}","notifications_url":"https://api.github.com/repos/openT2T/translators/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openT2T/translators/labels{/name}","releases_url":"https://api.github.com/repos/openT2T/translators/releases{/id}","deployments_url":"https://api.github.com/repos/openT2T/translators/deployments","created_at":"2016-04-01T03:25:20Z","updated_at":"2016-06-10T18:14:17Z","pushed_at":"2016-07-08T21:24:45Z","git_url":"git://github.com/openT2T/translators.git","ssh_url":"git@github.com:openT2T/translators.git","clone_url":"https://github.com/openT2T/translators.git","svn_url":"https://github.com/openT2T/translators","homepage":"http://www.opentranslatorstothings.org","size":107,"stargazers_count":17,"watchers_count":17,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":13,"forks":16,"open_issues":13,"watchers":17,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openT2T/translators/pulls/29"},"html":{"href":"https://github.com/openT2T/translators/pull/29"},"issue":{"href":"https://api.github.com/repos/openT2T/translators/issues/29"},"comments":{"href":"https://api.github.com/repos/openT2T/translators/issues/29/comments"},"review_comments":{"href":"https://api.github.com/repos/openT2T/translators/pulls/29/comments"},"review_comment":{"href":"https://api.github.com/repos/openT2T/translators/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openT2T/translators/pulls/29/commits"},"statuses":{"href":"https://api.github.com/repos/openT2T/translators/statuses/64d994b7e33f03d81c38908295cb81586af3c9e5"}}}}
{"actor":{"display_login":"jasongin"}}