Follow

Create simple job example

Description

This sample shows how to create a simple job and assigned it to a driver of the company, here hinting the implementation with a nearest available driver arround an actual GPS position.
It is assumed that a valid api key is provided into the function.
With successful operation the job is created and annotated with a unique job identificator (jid). This jid can further be used with e.g. driver reassignment or retrieval of job status.
Please note that in this example we are using only minimum set of attributes for job and task (calling it lite), which are generally richer.

C#.NET

        ...
        string lat = "48.85297";
        string lon = "2.32099";
        string startTime = "2015-07-08T08:30:00";
        string endTime = "2015-07-08T09:30:00";
        int driverId = FindNearestDriver(lat, lon);
        int jid = CreateSimpleJob(apikey, driverId, "Pickup Art.2462", startTime, endTime, lat, lon);
        ...

        public class LiteJob
        {
            public string Name { get; set; }
            public int? AssignedUserId { get; set; }
            public string StartDate { get; set; }
            public string EndDate { get; set; }
            public List<LiteTask> Tasks { get; set; }
        }

        public class LiteTask
        {
            public string Name { get; set; }
            public string Longitude { get; set; }
            public string Latitude { get; set; }
            public string StartDate { get; set; }
            public string EndDate { get; set; }
        }

        public class LiteJobResult
        {
            public int Id { get; set; }
        }

        protected int CreateSimpleJob(string apikey, int driverId, string jobname, string startTime, string endTime, string lat, string lon)
        {
            LiteJob job = new LiteJob();
            job.Name = jobname;
            job.AssignedUserId = driverId;
            job.StartDate = startTime;
            job.EndDate = endTime;
            job.Tasks = new List<LiteTask>();

            LiteTask task = new LiteTask();
            task.Name = "Single task";
            task.Latitude = lat;
            task.Longitude = lon;
            task.StartDate = null;
            task.EndDate = null;
            job.Tasks.Add(task);

            var cli = new RestClient("https://api.jobdispatch.sygic.com/api/v1");
            var request = new RestRequest("jobs", Method.POST);
            request.AddHeader("X-api_key", apikey);
            request.RequestFormat = DataFormat.Json;
            request.AddBody(job);
            int jid = 0;
            try
            {
                var response = (RestResponse)cli.Execute(request);
                var jsonConverter = new RestSharp.Deserializers.JsonDeserializer();
                var resultJob = jsonConverter.Deserialize<LiteJobResult>(response);
                if (resultJob.Id > 0)
                {
                    jid = resultJob.Id;
                }
            }
            catch (Exception e)
            {}

            return jid;

        }

        int FindNearestDriver(string lat, string lon)
        {
            ...
        }


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments