Follow

Create job with itinerary example

Description

This sample shows how to create a job from an itinerary defined as a list of waypoints, while each represented by an address description, GPS coordinates and a timewindow.
This example demonstrates how to translate itineraries from an external system into the Sygic FleetWork system.
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 be further be used with e.g. driver assignment 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

        ...
        List<Waypoint> itinerary = new List<Waypoint>
        {
                new Waypoint("Paris Bastille", "48.85297", "2.36899", "2015-07-09T08:30:00", "2015-07-09T09:30:00"),
                new Waypoint("Paris Montparnasse", "48.84239", "2.32099", "2015-07-09T09:30:00", "2015-07-09T10:30:00"),
                new Waypoint("Paris Clichy", "48.88356", "2.32733", "2015-07-09T10:30:00", "2015-07-09T11:30:00")
        };

        int jid = CreateJobFromItinerary(apikey, "Roundtrip job 62", itinerary);
        ...


        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; }
        }

        public class Waypoint
        {
            public string Address { get; set; }
            public string Lat { get; set; }
            public string Lon { get; set; }
            public string StartTime { get; set; }
            public string EndTime { get; set; }
            public Waypoint(string address, string lat, string lon, string startTime, string endTime)
            {
                this.Address = address;
                this.Lat = lat;
                this.Lon = lon;
                this.StartTime = startTime;
                this.EndTime = endTime;
            }
        }


        protected int CreateJobFromItinerary(string apikey, string jobname, List<Waypoint> itinerary)
        {
            LiteJob job = new LiteJob();
            job.Name = jobname;
            job.AssignedUserId = null;
            job.Tasks = new List<LiteTask>();

            for (int i = 0; i < itinerary.Count; i++)
            {
                LiteTask task = new LiteTask();
                task.Name = itinerary[i].Address;
                task.Latitude = itinerary[i].Lat;
                task.Longitude = itinerary[i].Lon;
                task.StartDate = itinerary[i].StartTime;
                task.EndDate = itinerary[i].EndTime;
                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;

        }


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

Comments