{"action":"created","comment":{"url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/comments/33883700","id":33883700,"diff_hunk":"@@ -4,9 +4,83 @@ var es = require('event-stream');\n var Long = require('long');\n var decoders = require('sgvdata/lib/protobuf');\n var direction = require('sgvdata/lib/utils').direction;\n-var mqtt = require('mqtt');\n var moment = require('moment');\n \n+function init (env, ctx) {\n+\n+  function mqtt ( ) {\n+    return mqtt;\n+  }\n+\n+  mqtt.client = connect(env);\n+  var downloads = downloader();\n+\n+  if (mqtt.client) {\n+    listenForMessages(ctx);\n+    mqtt.entries = process();\n+  }\n+\n+  mqtt.every = every;\n+\n+  //expose for tests that don't need to connect\n+  mqtt.sgvSensorMerge = sgvSensorMerge;\n+\n+  function listenForMessages ( ) {\n+    mqtt.client.on('message', function (topic, msg) {\n+      console.log('topic', topic);\n+      console.log(topic, 'on message', 'msg', msg.length);\n+      switch (topic) {\n+        case '/uploader':\n+          console.log({type: topic, msg: msg.toString()});\n+          break;\n+        case '/downloads/protobuf':\n+          downloadProtobuf(msg, topic, downloads, ctx);\n+          break;\n+\n+        default:\n+          console.log(topic, 'on message', 'msg', msg);\n+          // ctx.entries.write(msg);\n+          break;\n+      }\n+    });\n+  }\n+\n+  mqtt.emitNotification = function emitNotification(notify) {\n+    console.info('Publishing notification to mqtt: ', notify);\n+    mqtt.client.publish('/notifications/json', JSON.stringify(notify), function mqttCallback (err) {\n+      if (err) {\n+        console.error('Unable to publish notification to MQTT', err);\n+      }\n+    });\n+  };\n+\n+  return mqtt();\n+}\n+\n+function connect (env) {\n+  var uri = env.MQTT_MONITOR;\n+  if (!uri) {\n+    return null;\n+  }\n+\n+  var opts = {\n+    encoding: 'binary',\n+    clean: false,\n+    clientId: env.mqtt_client_id\n+  };\n+  var client = require('mqtt').connect(uri, opts);\n+\n+  function granted () { console.log('granted', arguments); }\n+\n+  client.subscribe('sgvs');\n+  client.subscribe('published');\n+  client.subscribe('/downloads/protobuf', {qos: 2}, granted);\n+  client.subscribe('/uploader', granted);\n+  client.subscribe('/entries/sgv', granted);\n+\n+  return client;\n+}\n+\n function process ( ) {","path":"lib/mqtt.js","position":82,"original_position":82,"commit_id":"c9ecaa0bde51a9e0160931aaf4c4018f8707673f","original_commit_id":"c9ecaa0bde51a9e0160931aaf4c4018f8707673f","user":{"login":"jasoncalabrese","id":751143,"avatar_url":"https://avatars.githubusercontent.com/u/751143?v=3","gravatar_id":"","url":"https://api.github.com/users/jasoncalabrese","html_url":"https://github.com/jasoncalabrese","followers_url":"https://api.github.com/users/jasoncalabrese/followers","following_url":"https://api.github.com/users/jasoncalabrese/following{/other_user}","gists_url":"https://api.github.com/users/jasoncalabrese/gists{/gist_id}","starred_url":"https://api.github.com/users/jasoncalabrese/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasoncalabrese/subscriptions","organizations_url":"https://api.github.com/users/jasoncalabrese/orgs","repos_url":"https://api.github.com/users/jasoncalabrese/repos","events_url":"https://api.github.com/users/jasoncalabrese/events{/privacy}","received_events_url":"https://api.github.com/users/jasoncalabrese/received_events","type":"User","site_admin":false},"body":"![Codacy](https://www.codacy.com/versioned/images/favicon.png) Issue found: ['process' is defined but never used.](https://www.codacy.com/app/Nightscout/cgm-remote-monitor/file/768301283/issues/source?bid=2452379&fileBranchId=2457009#l84)","created_at":"2015-07-04T01:24:38Z","updated_at":"2015-07-04T01:24:38Z","html_url":"https://github.com/nightscout/cgm-remote-monitor/pull/674#discussion_r33883700","pull_request_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674","_links":{"self":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/comments/33883700"},"html":{"href":"https://github.com/nightscout/cgm-remote-monitor/pull/674#discussion_r33883700"},"pull_request":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674"}}},"pull_request":{"url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674","id":39191855,"html_url":"https://github.com/nightscout/cgm-remote-monitor/pull/674","diff_url":"https://github.com/nightscout/cgm-remote-monitor/pull/674.diff","patch_url":"https://github.com/nightscout/cgm-remote-monitor/pull/674.patch","issue_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/674","number":674,"state":"open","locked":false,"title":"send notifications over MQTT","user":{"login":"jasoncalabrese","id":751143,"avatar_url":"https://avatars.githubusercontent.com/u/751143?v=3","gravatar_id":"","url":"https://api.github.com/users/jasoncalabrese","html_url":"https://github.com/jasoncalabrese","followers_url":"https://api.github.com/users/jasoncalabrese/followers","following_url":"https://api.github.com/users/jasoncalabrese/following{/other_user}","gists_url":"https://api.github.com/users/jasoncalabrese/gists{/gist_id}","starred_url":"https://api.github.com/users/jasoncalabrese/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasoncalabrese/subscriptions","organizations_url":"https://api.github.com/users/jasoncalabrese/orgs","repos_url":"https://api.github.com/users/jasoncalabrese/repos","events_url":"https://api.github.com/users/jasoncalabrese/events{/privacy}","received_events_url":"https://api.github.com/users/jasoncalabrese/received_events","type":"User","site_admin":false},"body":"working with @ktind to integrate with Lasso","created_at":"2015-07-03T18:37:35Z","updated_at":"2015-07-04T01:24:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a451069a053421a1d8d3eb9e9addd718103b2e49","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674/commits","review_comments_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674/comments","review_comment_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/674/comments","statuses_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/statuses/c9ecaa0bde51a9e0160931aaf4c4018f8707673f","head":{"label":"nightscout:wip/mqtt-notifications","ref":"wip/mqtt-notifications","sha":"c9ecaa0bde51a9e0160931aaf4c4018f8707673f","user":{"login":"nightscout","id":7661012,"avatar_url":"https://avatars.githubusercontent.com/u/7661012?v=3","gravatar_id":"","url":"https://api.github.com/users/nightscout","html_url":"https://github.com/nightscout","followers_url":"https://api.github.com/users/nightscout/followers","following_url":"https://api.github.com/users/nightscout/following{/other_user}","gists_url":"https://api.github.com/users/nightscout/gists{/gist_id}","starred_url":"https://api.github.com/users/nightscout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nightscout/subscriptions","organizations_url":"https://api.github.com/users/nightscout/orgs","repos_url":"https://api.github.com/users/nightscout/repos","events_url":"https://api.github.com/users/nightscout/events{/privacy}","received_events_url":"https://api.github.com/users/nightscout/received_events","type":"Organization","site_admin":false},"repo":{"id":20042152,"name":"cgm-remote-monitor","full_name":"nightscout/cgm-remote-monitor","owner":{"login":"nightscout","id":7661012,"avatar_url":"https://avatars.githubusercontent.com/u/7661012?v=3","gravatar_id":"","url":"https://api.github.com/users/nightscout","html_url":"https://github.com/nightscout","followers_url":"https://api.github.com/users/nightscout/followers","following_url":"https://api.github.com/users/nightscout/following{/other_user}","gists_url":"https://api.github.com/users/nightscout/gists{/gist_id}","starred_url":"https://api.github.com/users/nightscout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nightscout/subscriptions","organizations_url":"https://api.github.com/users/nightscout/orgs","repos_url":"https://api.github.com/users/nightscout/repos","events_url":"https://api.github.com/users/nightscout/events{/privacy}","received_events_url":"https://api.github.com/users/nightscout/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nightscout/cgm-remote-monitor","description":"nightscout web monitor","fork":false,"url":"https://api.github.com/repos/nightscout/cgm-remote-monitor","forks_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/forks","keys_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/teams","hooks_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/hooks","issue_events_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/events{/number}","events_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/events","assignees_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/assignees{/user}","branches_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/branches{/branch}","tags_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/tags","blobs_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/refs{/sha}","trees_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/statuses/{sha}","languages_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/languages","stargazers_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/stargazers","contributors_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/contributors","subscribers_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/subscribers","subscription_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/subscription","commits_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/commits{/sha}","git_commits_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/commits{/sha}","comments_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/comments{/number}","issue_comment_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/comments{/number}","contents_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/contents/{+path}","compare_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/merges","archive_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/downloads","issues_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues{/number}","pulls_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls{/number}","milestones_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/milestones{/number}","notifications_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/labels{/name}","releases_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/releases{/id}","created_at":"2014-05-22T00:32:28Z","updated_at":"2015-06-29T08:01:27Z","pushed_at":"2015-07-04T01:01:00Z","git_url":"git://github.com/nightscout/cgm-remote-monitor.git","ssh_url":"git@github.com:nightscout/cgm-remote-monitor.git","clone_url":"https://github.com/nightscout/cgm-remote-monitor.git","svn_url":"https://github.com/nightscout/cgm-remote-monitor","homepage":"","size":13422,"stargazers_count":94,"watchers_count":94,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2981,"mirror_url":null,"open_issues_count":49,"forks":2981,"open_issues":49,"watchers":94,"default_branch":"master"}},"base":{"label":"nightscout:dev","ref":"dev","sha":"3179a0420f565959327336d69d27ba2e8b22ce4c","user":{"login":"nightscout","id":7661012,"avatar_url":"https://avatars.githubusercontent.com/u/7661012?v=3","gravatar_id":"","url":"https://api.github.com/users/nightscout","html_url":"https://github.com/nightscout","followers_url":"https://api.github.com/users/nightscout/followers","following_url":"https://api.github.com/users/nightscout/following{/other_user}","gists_url":"https://api.github.com/users/nightscout/gists{/gist_id}","starred_url":"https://api.github.com/users/nightscout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nightscout/subscriptions","organizations_url":"https://api.github.com/users/nightscout/orgs","repos_url":"https://api.github.com/users/nightscout/repos","events_url":"https://api.github.com/users/nightscout/events{/privacy}","received_events_url":"https://api.github.com/users/nightscout/received_events","type":"Organization","site_admin":false},"repo":{"id":20042152,"name":"cgm-remote-monitor","full_name":"nightscout/cgm-remote-monitor","owner":{"login":"nightscout","id":7661012,"avatar_url":"https://avatars.githubusercontent.com/u/7661012?v=3","gravatar_id":"","url":"https://api.github.com/users/nightscout","html_url":"https://github.com/nightscout","followers_url":"https://api.github.com/users/nightscout/followers","following_url":"https://api.github.com/users/nightscout/following{/other_user}","gists_url":"https://api.github.com/users/nightscout/gists{/gist_id}","starred_url":"https://api.github.com/users/nightscout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nightscout/subscriptions","organizations_url":"https://api.github.com/users/nightscout/orgs","repos_url":"https://api.github.com/users/nightscout/repos","events_url":"https://api.github.com/users/nightscout/events{/privacy}","received_events_url":"https://api.github.com/users/nightscout/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nightscout/cgm-remote-monitor","description":"nightscout web monitor","fork":false,"url":"https://api.github.com/repos/nightscout/cgm-remote-monitor","forks_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/forks","keys_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/teams","hooks_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/hooks","issue_events_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/events{/number}","events_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/events","assignees_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/assignees{/user}","branches_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/branches{/branch}","tags_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/tags","blobs_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/refs{/sha}","trees_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/statuses/{sha}","languages_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/languages","stargazers_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/stargazers","contributors_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/contributors","subscribers_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/subscribers","subscription_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/subscription","commits_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/commits{/sha}","git_commits_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/git/commits{/sha}","comments_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/comments{/number}","issue_comment_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/comments{/number}","contents_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/contents/{+path}","compare_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/merges","archive_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/downloads","issues_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues{/number}","pulls_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls{/number}","milestones_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/milestones{/number}","notifications_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/labels{/name}","releases_url":"https://api.github.com/repos/nightscout/cgm-remote-monitor/releases{/id}","created_at":"2014-05-22T00:32:28Z","updated_at":"2015-06-29T08:01:27Z","pushed_at":"2015-07-04T01:01:00Z","git_url":"git://github.com/nightscout/cgm-remote-monitor.git","ssh_url":"git@github.com:nightscout/cgm-remote-monitor.git","clone_url":"https://github.com/nightscout/cgm-remote-monitor.git","svn_url":"https://github.com/nightscout/cgm-remote-monitor","homepage":"","size":13422,"stargazers_count":94,"watchers_count":94,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2981,"mirror_url":null,"open_issues_count":49,"forks":2981,"open_issues":49,"watchers":94,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674"},"html":{"href":"https://github.com/nightscout/cgm-remote-monitor/pull/674"},"issue":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/674"},"comments":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/issues/674/comments"},"review_comments":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674/comments"},"review_comment":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/pulls/674/commits"},"statuses":{"href":"https://api.github.com/repos/nightscout/cgm-remote-monitor/statuses/c9ecaa0bde51a9e0160931aaf4c4018f8707673f"}}}}