var MessageHandler = function(TripSvc){ this.handleMessages = function(messages){ var instance = this; //TODO: messages are specific to jobs... so we probably need to check that the message applies to the // current jobId before taking action angular.forEach(messages, function(message){ console.log("Handling: " + message.type); console.log(JSON.stringify(message, null, 4)); switch(message.type.toUpperCase()){ case "DRIVER_PAYMENT_OTHER": instance.handleDriverPaymentOther(message); break; case "DRIVER_COLLECTED": instance.handleDriverCollected(message); break; case "JOB_ACCEPTED": instance.handleJobAccepted(message); break; case "DRIVER_REJECTED": instance.handleDriverRejected(message); break; case "JOB_FLAG_COUNT": instance.handleJobFlagCount(message); break; case "JOB_VIEWED_COUNT": instance.handleJobViewedCount(message); break; case "JOB_NO_SHOW": instance.handleJobNoShow(message); break; case "DRIVER_ABANDONED": instance.handleDriverAbandoned(message); break; default: // DISPATCH_TRASHED instance.defaultMessageHandler(message); } }); }; this.handleDriverPaymentOther = function(message){ // AKA completeJob // EXAMPLE // { // "_id": "53b6252ae4b06c3e735073b8", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_PAYMENT_OTHER", // "data": { // "job_id": 715749, // "state": "DRIVER_PAYMENT_OTHER" // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_PAYMENT_OTHER" // }, // "time_in": 1404445994 // } if(message.data.job_id != TripSvc.jobId){ return; } TripSvc.startGetActiveTaxisPolling(); TripSvc.eventScope.$broadcast(driverCompleteJobEvent, message.data.job_id); }; this.handleDriverCollected = function(message){ // EXAMPLE // { // "_id": "53b62473e4b06c3e735073b2", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_COLLECTED", // "data": { // "job_id": 715749, // "state": "DRIVER_COLLECTED" // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_COLLECTED" // }, // "time_in": 1404445811 // } if(message.data.job_id != TripSvc.jobId){ var unexpected = String.format("We've been collected by a driver with a jobId ({0}) that is not from our current job ({1})...", message.data.job_id, TripSvc.jobId); console.log(unexpected); return; } TripSvc.eventScope.$broadcast(driverCollectedEvent, message.data.job_id); }; this.handleJobAccepted = function(message){ // EXAMPLE // { // "_id": "53b622d7e4b06c3e735073aa", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_ACCEPTED", // "data": { // "driver_device_id": "71bef55e9d5fd24f8da9105cd090bc260061c34c" // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_ACCEPTED" // }, // "time_in": 1404445399 // } // CASE: haven't started any job yet; poll shows a driver accepting a job // we allow the user to recover or abandon the old job if(!TripSvc.jobId){ var confirmString = "A taxi driver has accepted a previously created trip. Click OK to try to recover or click cancel to abandon that job."; var recover = confirm(confirmString); TripSvc.getPassengerFlagsAndStatus().then(function(response){ if(!response.data.result || !response.data.data){ return; } if(recover){ TripSvc.recoveryHandler.recoverState(response.data); } else{ var jobId = response.data.data.job.fare.jobId; TripSvc.abandonJob(jobId); } }); return; } // CASE: We have created/are in a job, but an accepted message arrives for an old job. // we silently abandon the older job // NOTE: don't think we can actually do this because the getPassengerFlagsAndStatus call probably will only // return the most recent job? I've asked Eric if we can get the JobId on the JOB_ACCEPTED message as that will // help a lot // if(TripSvc.jobId){ // TripSvc.getPassengerFlagsAndStatus().then(function(response){ // if(!response.data.result || !response.data.data){ // return; // } // // var jobId = response.data.data.job.fare.jobId; // TripSvc.abandonJob(jobId); // }); // } TripSvc.driverDeviceId = message.data.driver_device_id; // reset viewed by count as soon as job is accepted TripSvc.resetJobViewedByCounters(); TripSvc.startGetFlaggedTaxiPolling(); TripSvc.getDriverPhotoUrl(); TripSvc.getVehiclePhotoUrl(); TripSvc.getDriverDetails(); TripSvc.eventScope.$broadcast(jobAcceptedEvent, message.data.driver_device_id); }; this.handleDriverRejected = function(message){ // EXAMPLE // { // "_id": "53b61f28e4b06c3e73507390", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_REJECTED", // "data": { // "job_id": 715746, // "driver_device_id": "71bef55e9d5fd24f8da9105cd090bc260061c34c", // "pid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "count": 1 // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_REJECTED" // }, // "time_in": 1404444456 // } if(message.data.job_id != TripSvc.jobId){ // silently cancel older job as we have no good way to recover to it. TripSvc.cancelJob(message.data.job_id); return; } TripSvc.jobDriversRejected = message.data.count; if (ga){ ga('send', 'event', 'Rejected by Drivers', 'click'); } }; this.handleJobFlagCount = function(message){ // EXAMPLE // { // "_id": "53b61e23e4b06c3e7350733b", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_FLAG_COUNT", // "data": { // "jid": 715746, // "pid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "count": 1 // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_FLAG_COUNT" // }, // "time_in": 1404444195 // } if(message.data.jid != TripSvc.jobId){ // silently cancel older job as we have no good way to recover to it. TripSvc.cancelJob(message.data.jid); return; } TripSvc.jobDriversSentTo = message.data.count; if (ga){ ga('send', 'event', 'Sent to Drivers', 'click'); } }; this.handleJobViewedCount = function(message){ // EXAMPLE // { // "_id": "53b61e338e874f6b0400004b", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_VIEWED_COUNT", // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_VIEWED_COUNT" // }, // "data": { // "jid": "715746", // "pid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "did": "all", // "count": "1" // }, // "time_in": { // "sec": 1404444211 // } // } if(message.data.jid != TripSvc.jobId){ // silently cancel older job as we have no good way to recover to it. TripSvc.cancelJob(message.data.jid); return; } TripSvc.jobDriversViewedBy = message.data.count; //TODO: consider also broadcasting this event and having this count kept on the controller, not the service if (ga){ ga('send', 'event', 'Drivers Viewed', 'click'); } }; this.handleJobNoShow = function(message){ // EXAMPLE // { // "_id": "53b627c1e4b06c3e73507422", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_NO_SHOW", // "data": { // "job_id": 715750, // "state": "NO_SHOW" // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "JOB_NO_SHOW" // } if(message.data.job_id != TripSvc.jobId){ return; } TripSvc.driverDeviceId = null; TripSvc.jobId = null; TripSvc.startGetActiveTaxisPolling(); TripSvc.eventScope.$broadcast(jobNoShowEvent, message.data.job_id); }; this.handleDriverAbandoned = function(message){ // EXAMPLE // { // "_id": "53b628e8e4b06c3e73507435", // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_ABANDONED", // "data": { // "job_id": 715753, // "state": "DRIVER_ABANDONED", // "substate": "OTHER" // }, // "message": { // "udid": "6a50726f29e8a9d657994ff337a4e372065a1458", // "type": "DRIVER_ABANDONED" // }, // "time_in": 1404446952 // } if(message.data.job_id != TripSvc.jobId){ return; } TripSvc.driverDeviceId = null; TripSvc.jobId = null; TripSvc.startGetActiveTaxisPolling(); TripSvc.eventScope.$broadcast(driverAbandonedEvent, message.data.job_id); }; // this.handleDispatchTrashed = function(message){ // EXAMPLE // { // "udid": "5e581acaac8852c0535d88e1f0378b8731698bbe", // "time_in": 1406202711, // "type": "DISPATCH_TRASHED", // "message": { // "type": "DISPATCH_TRASHED", // "udid": "5e581acaac8852c0535d88e1f0378b8731698bbe" // }, // "data": { // "dispatchId": 2766, // "jobId": 2766 // }, // "_id": "53d0f357e4b00f55c926a1d4" // } // // We are not using this message for anything yet // }; this.defaultMessageHandler = function(message){ console.log("Unknown message type: " + message.type); }; };