{"action":"created","comment":{"url":"https://api.github.com/repos/anthill/pheromon/pulls/comments/40088245","id":40088245,"diff_hunk":"@@ -34,323 +36,157 @@ var debug = function() {\n }\n \n // ############### Sensors communication ######################\n-\n-var tcpServerForSensors = net.createServer(function(tcpSocketSensor) {\n-\n-    console.log(\"New socket to sensor.\");\n-    var sim;\n-\n-    var tcpSocketSensorReceiver = makeTcpReceiver(tcpSocketSensor, \"\\n\");\n-    \n-    tcpSocketSensorReceiver.on('message', function(message) {\n-\n-        console.log(\"received tcp data: \", message);\n-\n-        // register the sim id corresponding to the socket\n-        var match = message.match(/init\\s(\\d+)\\s(.+)/)\n-        if (match) {\n-            var token = match[2];\n-            if (token === PRIVATE.token){\n-                sim = match[1];\n-                sim2socket[sim] = tcpSocketSensor;\n-                console.log(tcpSocketSensor.remoteAddress + \" is now known as \" + sim);\n-                \n-                // check sensor is registered\n-                database.Sensors.findBySIMid(sim)\n-                .then(function(sensors) {\n-                    // if sensor was never registered, create it\n-                    if(sensors.length === 0) {\n-                        console.log(\"Creating the sensor\")\n-                        return database.Sensors.create({\n-                            'name': pokemon.random(),\n-                            'sim': sim,\n-                            'data_period': CONF.data_period,\n-                            'start_time': CONF.start_time,\n-                            'stop_time': CONF.stop_time\n-                        })\n-                        .then(function(createdSensor){\n-                            return createdSensor;\n-                        })\n-                        .catch(function(err) {\n-                            console.log(\"[ERROR] Couldn't create sensor :\", err);\n-                        })\n-                    }\n-                    else { \n-                        return sensors[0];\n-                    }\n-                    \n+var authenticate = function(client, username, token, callback) {\n+    var authorized = (token.toString() === PRIVATE.token);\n+    if (authorized) {\n+\n+        // check if sensor exists in db\n+        database.Sensors.get(client.id)\n+        .then(function(sensor) {\n+            // if sensor was never registered, create it\n+            if(!sensor) {\n+                console.log(\"Creating the sensor\")\n+                return database.Sensors.create({\n+                    'name': pokemon.random(),\n+                    'sim': client.id,\n+                    'period': CONF.period,\n+                    'start_hour': CONF.start_hour,\n+                    'stop_hour': CONF.stop_hour\n                 })\n-                .then(function(sensor){\n-                    console.log('sending config');\n-                    var date = new Date();\n-                    // Send config to the sensor\n-                    sendCommand(tcpSocketSensor, 'init '+ [sensor.data_period, sensor.start_time, sensor.stop_time, date.toISOString()].join(\" \"));\n+                .then(function(createdSensor){\n+                    return createdSensor;\n                 })\n                 .catch(function(err) {\n-                    console.log(\"[ERROR] Couldn't get sensor's config in DB :\", err);\n+                    console.log(\"[ERROR] Couldn't create sensor :\", err);\n                 })\n-            } else {\n-                console.log(\"Wrong token for authenticating the sensor.\");\n             }\n-        }\n-\n-        // handle data\n-        else if (sim) {\n-            handleData(message, sim2socket[sim], sim);\n-        }\n-\n-    });\n-\n-    tcpSocketSensor.on('close', function() {\n-        console.log(\"connection closed\");\n-        if (sim) {\n-            debug(\"Removing from sim2socket\");\n-            delete sim2socket[sim];\n-        }\n-    });\n-\n-    tcpSocketSensor.on('error', function(err) {\n-        console.log(\"[ERROR] \" + (err ? err.code : \"???\") + \" : \" + (err ? err : \"unknown\"));\n-        if (sim) {\n-            debug(\"Removing from sim2socket\");\n-            delete sim2socket[sim];\n-        }\n-    });\n-\n-});\n-\n-// tcpServerForSensors.on(\"listening\", function(){\n-//     // if dev mode simulate data\n-//     if (process.env.NODE_ENV === \"development\") simulateSensorStatusArrivalTCP();\n-// })\n-\n-tcpServerForSensors.on('error', function(err) {\n-    console.log(\"[ERROR] : \", err.message);\n-    if (err.code.toString() === 'EADDRINUSE') {\n-        console.log(\"address in use, please retry later ...\");\n-        process.exit(1);\n+            else { \n+                return sensor;\n+            }\n+            \n+        })\n+        .then(function(sensor){\n+            id2sensor[client.id] = sensor;\n+        })\n+        .catch(function(err) {\n+            console.log(\"[ERROR] Couldn't get sensor's config in DB :\", err);\n+        })\n     }\n-});\n \n-tcpServerForSensors.listen(monitorPort);\n-\n-\n-\n-\n-\n-\n-\n-\n-\n-// ############### API communication ######################\n-\n-var tcpServerToAdminApp = net.createServer(function(tcpSocketAdminApp) {\n+    callback(null, authorized);","path":"broker/index.js","position":166,"original_position":166,"commit_id":"b7ebe7218ef0fab3100663bd2f80e5e45a6fce33","original_commit_id":"b7ebe7218ef0fab3100663bd2f80e5e45a6fce33","user":{"login":"iOiurson","id":8417814,"avatar_url":"https://avatars.githubusercontent.com/u/8417814?v=3","gravatar_id":"","url":"https://api.github.com/users/iOiurson","html_url":"https://github.com/iOiurson","followers_url":"https://api.github.com/users/iOiurson/followers","following_url":"https://api.github.com/users/iOiurson/following{/other_user}","gists_url":"https://api.github.com/users/iOiurson/gists{/gist_id}","starred_url":"https://api.github.com/users/iOiurson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iOiurson/subscriptions","organizations_url":"https://api.github.com/users/iOiurson/orgs","repos_url":"https://api.github.com/users/iOiurson/repos","events_url":"https://api.github.com/users/iOiurson/events{/privacy}","received_events_url":"https://api.github.com/users/iOiurson/received_events","type":"User","site_admin":false},"body":"Why is this not in a `.then()` ?","created_at":"2015-09-22T13:53:03Z","updated_at":"2015-09-22T13:53:03Z","html_url":"https://github.com/anthill/pheromon/pull/39#discussion_r40088245","pull_request_url":"https://api.github.com/repos/anthill/pheromon/pulls/39","_links":{"self":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/comments/40088245"},"html":{"href":"https://github.com/anthill/pheromon/pull/39#discussion_r40088245"},"pull_request":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/39"}}},"pull_request":{"url":"https://api.github.com/repos/anthill/pheromon/pulls/39","id":45607264,"html_url":"https://github.com/anthill/pheromon/pull/39","diff_url":"https://github.com/anthill/pheromon/pull/39.diff","patch_url":"https://github.com/anthill/pheromon/pull/39.patch","issue_url":"https://api.github.com/repos/anthill/pheromon/issues/39","number":39,"state":"open","locked":false,"title":"MQTT implementation","user":{"login":"vallettea","id":371303,"avatar_url":"https://avatars.githubusercontent.com/u/371303?v=3","gravatar_id":"","url":"https://api.github.com/users/vallettea","html_url":"https://github.com/vallettea","followers_url":"https://api.github.com/users/vallettea/followers","following_url":"https://api.github.com/users/vallettea/following{/other_user}","gists_url":"https://api.github.com/users/vallettea/gists{/gist_id}","starred_url":"https://api.github.com/users/vallettea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vallettea/subscriptions","organizations_url":"https://api.github.com/users/vallettea/orgs","repos_url":"https://api.github.com/users/vallettea/repos","events_url":"https://api.github.com/users/vallettea/events{/privacy}","received_events_url":"https://api.github.com/users/vallettea/received_events","type":"User","site_admin":false},"body":"This is a start.\r\nThe code is not neat and things should be refactored a bit in some places, but a least we get a feeling of what if tends to be.\r\nTODO\r\n- [ ] dashboard should subscribe to `measurement`\r\n- [ ] admin backend should subscribe to `command` and `status`\r\n- [ ] socket.io should propagate message to frontends","created_at":"2015-09-21T22:15:43Z","updated_at":"2015-09-22T13:53:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"24d391e6702341ace9d83f700c5b0a446f60554c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/anthill/pheromon/pulls/39/commits","review_comments_url":"https://api.github.com/repos/anthill/pheromon/pulls/39/comments","review_comment_url":"https://api.github.com/repos/anthill/pheromon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/anthill/pheromon/issues/39/comments","statuses_url":"https://api.github.com/repos/anthill/pheromon/statuses/b7ebe7218ef0fab3100663bd2f80e5e45a6fce33","head":{"label":"vallettea:master","ref":"master","sha":"b7ebe7218ef0fab3100663bd2f80e5e45a6fce33","user":{"login":"vallettea","id":371303,"avatar_url":"https://avatars.githubusercontent.com/u/371303?v=3","gravatar_id":"","url":"https://api.github.com/users/vallettea","html_url":"https://github.com/vallettea","followers_url":"https://api.github.com/users/vallettea/followers","following_url":"https://api.github.com/users/vallettea/following{/other_user}","gists_url":"https://api.github.com/users/vallettea/gists{/gist_id}","starred_url":"https://api.github.com/users/vallettea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vallettea/subscriptions","organizations_url":"https://api.github.com/users/vallettea/orgs","repos_url":"https://api.github.com/users/vallettea/repos","events_url":"https://api.github.com/users/vallettea/events{/privacy}","received_events_url":"https://api.github.com/users/vallettea/received_events","type":"User","site_admin":false},"repo":{"id":42448649,"name":"pheromon","full_name":"vallettea/pheromon","owner":{"login":"vallettea","id":371303,"avatar_url":"https://avatars.githubusercontent.com/u/371303?v=3","gravatar_id":"","url":"https://api.github.com/users/vallettea","html_url":"https://github.com/vallettea","followers_url":"https://api.github.com/users/vallettea/followers","following_url":"https://api.github.com/users/vallettea/following{/other_user}","gists_url":"https://api.github.com/users/vallettea/gists{/gist_id}","starred_url":"https://api.github.com/users/vallettea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vallettea/subscriptions","organizations_url":"https://api.github.com/users/vallettea/orgs","repos_url":"https://api.github.com/users/vallettea/repos","events_url":"https://api.github.com/users/vallettea/events{/privacy}","received_events_url":"https://api.github.com/users/vallettea/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vallettea/pheromon","description":"","fork":true,"url":"https://api.github.com/repos/vallettea/pheromon","forks_url":"https://api.github.com/repos/vallettea/pheromon/forks","keys_url":"https://api.github.com/repos/vallettea/pheromon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vallettea/pheromon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vallettea/pheromon/teams","hooks_url":"https://api.github.com/repos/vallettea/pheromon/hooks","issue_events_url":"https://api.github.com/repos/vallettea/pheromon/issues/events{/number}","events_url":"https://api.github.com/repos/vallettea/pheromon/events","assignees_url":"https://api.github.com/repos/vallettea/pheromon/assignees{/user}","branches_url":"https://api.github.com/repos/vallettea/pheromon/branches{/branch}","tags_url":"https://api.github.com/repos/vallettea/pheromon/tags","blobs_url":"https://api.github.com/repos/vallettea/pheromon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vallettea/pheromon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vallettea/pheromon/git/refs{/sha}","trees_url":"https://api.github.com/repos/vallettea/pheromon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vallettea/pheromon/statuses/{sha}","languages_url":"https://api.github.com/repos/vallettea/pheromon/languages","stargazers_url":"https://api.github.com/repos/vallettea/pheromon/stargazers","contributors_url":"https://api.github.com/repos/vallettea/pheromon/contributors","subscribers_url":"https://api.github.com/repos/vallettea/pheromon/subscribers","subscription_url":"https://api.github.com/repos/vallettea/pheromon/subscription","commits_url":"https://api.github.com/repos/vallettea/pheromon/commits{/sha}","git_commits_url":"https://api.github.com/repos/vallettea/pheromon/git/commits{/sha}","comments_url":"https://api.github.com/repos/vallettea/pheromon/comments{/number}","issue_comment_url":"https://api.github.com/repos/vallettea/pheromon/issues/comments{/number}","contents_url":"https://api.github.com/repos/vallettea/pheromon/contents/{+path}","compare_url":"https://api.github.com/repos/vallettea/pheromon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vallettea/pheromon/merges","archive_url":"https://api.github.com/repos/vallettea/pheromon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vallettea/pheromon/downloads","issues_url":"https://api.github.com/repos/vallettea/pheromon/issues{/number}","pulls_url":"https://api.github.com/repos/vallettea/pheromon/pulls{/number}","milestones_url":"https://api.github.com/repos/vallettea/pheromon/milestones{/number}","notifications_url":"https://api.github.com/repos/vallettea/pheromon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vallettea/pheromon/labels{/name}","releases_url":"https://api.github.com/repos/vallettea/pheromon/releases{/id}","created_at":"2015-09-14T12:57:27Z","updated_at":"2015-09-14T12:57:28Z","pushed_at":"2015-09-21T22:12:35Z","git_url":"git://github.com/vallettea/pheromon.git","ssh_url":"git@github.com:vallettea/pheromon.git","clone_url":"https://github.com/vallettea/pheromon.git","svn_url":"https://github.com/vallettea/pheromon","homepage":null,"size":234,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","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":"anthill:master","ref":"master","sha":"f94360f14515a7e13f8afcd4681ecd5210601da2","user":{"login":"anthill","id":7281593,"avatar_url":"https://avatars.githubusercontent.com/u/7281593?v=3","gravatar_id":"","url":"https://api.github.com/users/anthill","html_url":"https://github.com/anthill","followers_url":"https://api.github.com/users/anthill/followers","following_url":"https://api.github.com/users/anthill/following{/other_user}","gists_url":"https://api.github.com/users/anthill/gists{/gist_id}","starred_url":"https://api.github.com/users/anthill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anthill/subscriptions","organizations_url":"https://api.github.com/users/anthill/orgs","repos_url":"https://api.github.com/users/anthill/repos","events_url":"https://api.github.com/users/anthill/events{/privacy}","received_events_url":"https://api.github.com/users/anthill/received_events","type":"Organization","site_admin":false},"repo":{"id":42229949,"name":"pheromon","full_name":"anthill/pheromon","owner":{"login":"anthill","id":7281593,"avatar_url":"https://avatars.githubusercontent.com/u/7281593?v=3","gravatar_id":"","url":"https://api.github.com/users/anthill","html_url":"https://github.com/anthill","followers_url":"https://api.github.com/users/anthill/followers","following_url":"https://api.github.com/users/anthill/following{/other_user}","gists_url":"https://api.github.com/users/anthill/gists{/gist_id}","starred_url":"https://api.github.com/users/anthill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anthill/subscriptions","organizations_url":"https://api.github.com/users/anthill/orgs","repos_url":"https://api.github.com/users/anthill/repos","events_url":"https://api.github.com/users/anthill/events{/privacy}","received_events_url":"https://api.github.com/users/anthill/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/anthill/pheromon","description":"Deploy, administrate and harvest data from a swarm of sensors","fork":false,"url":"https://api.github.com/repos/anthill/pheromon","forks_url":"https://api.github.com/repos/anthill/pheromon/forks","keys_url":"https://api.github.com/repos/anthill/pheromon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/anthill/pheromon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/anthill/pheromon/teams","hooks_url":"https://api.github.com/repos/anthill/pheromon/hooks","issue_events_url":"https://api.github.com/repos/anthill/pheromon/issues/events{/number}","events_url":"https://api.github.com/repos/anthill/pheromon/events","assignees_url":"https://api.github.com/repos/anthill/pheromon/assignees{/user}","branches_url":"https://api.github.com/repos/anthill/pheromon/branches{/branch}","tags_url":"https://api.github.com/repos/anthill/pheromon/tags","blobs_url":"https://api.github.com/repos/anthill/pheromon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/anthill/pheromon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/anthill/pheromon/git/refs{/sha}","trees_url":"https://api.github.com/repos/anthill/pheromon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/anthill/pheromon/statuses/{sha}","languages_url":"https://api.github.com/repos/anthill/pheromon/languages","stargazers_url":"https://api.github.com/repos/anthill/pheromon/stargazers","contributors_url":"https://api.github.com/repos/anthill/pheromon/contributors","subscribers_url":"https://api.github.com/repos/anthill/pheromon/subscribers","subscription_url":"https://api.github.com/repos/anthill/pheromon/subscription","commits_url":"https://api.github.com/repos/anthill/pheromon/commits{/sha}","git_commits_url":"https://api.github.com/repos/anthill/pheromon/git/commits{/sha}","comments_url":"https://api.github.com/repos/anthill/pheromon/comments{/number}","issue_comment_url":"https://api.github.com/repos/anthill/pheromon/issues/comments{/number}","contents_url":"https://api.github.com/repos/anthill/pheromon/contents/{+path}","compare_url":"https://api.github.com/repos/anthill/pheromon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/anthill/pheromon/merges","archive_url":"https://api.github.com/repos/anthill/pheromon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/anthill/pheromon/downloads","issues_url":"https://api.github.com/repos/anthill/pheromon/issues{/number}","pulls_url":"https://api.github.com/repos/anthill/pheromon/pulls{/number}","milestones_url":"https://api.github.com/repos/anthill/pheromon/milestones{/number}","notifications_url":"https://api.github.com/repos/anthill/pheromon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/anthill/pheromon/labels{/name}","releases_url":"https://api.github.com/repos/anthill/pheromon/releases{/id}","created_at":"2015-09-10T07:41:08Z","updated_at":"2015-09-20T15:57:08Z","pushed_at":"2015-09-22T09:56:45Z","git_url":"git://github.com/anthill/pheromon.git","ssh_url":"git@github.com:anthill/pheromon.git","clone_url":"https://github.com/anthill/pheromon.git","svn_url":"https://github.com/anthill/pheromon","homepage":"","size":403,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":22,"forks":3,"open_issues":22,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/39"},"html":{"href":"https://github.com/anthill/pheromon/pull/39"},"issue":{"href":"https://api.github.com/repos/anthill/pheromon/issues/39"},"comments":{"href":"https://api.github.com/repos/anthill/pheromon/issues/39/comments"},"review_comments":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/39/comments"},"review_comment":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/anthill/pheromon/pulls/39/commits"},"statuses":{"href":"https://api.github.com/repos/anthill/pheromon/statuses/b7ebe7218ef0fab3100663bd2f80e5e45a6fce33"}}}}