public ContentResult ExportData(string asset, int year, int month, int day, int page = 0, int maxItemPerPage = 20)
        {
            JsonExportData result = new JsonExportData();

            if (!String.IsNullOrEmpty(asset))
            {
                DateTime    date   = new DateTime(year, month, day);
                DeviceModel device = RepositoryFactory.Instance.DeviceDb.GetDevice(asset);
                if (device != null)
                {
                    List <TrackingModel> tracks = RepositoryFactory.Instance.DataTrackingDB.GetData(device, date).OrderBy(x => x.Data.Recorded_at).ToList();

                    Dictionary <string, MD.CloudConnect.Data.Field> previousFields = null;
                    foreach (TrackingModel t in tracks)
                    {
                        if (previousFields == null)
                        {
                            previousFields = new Dictionary <string, MD.CloudConnect.Data.Field>();
                            foreach (KeyValuePair <string, MD.CloudConnect.Data.Field> item in t.Data.fields)
                            {
                                if (item.Value.b64_value != null)
                                {
                                    previousFields.Add(item.Key, item.Value);
                                }
                            }
                            t.Data.fields = previousFields;
                        }
                        else
                        {
                            Dictionary <string, MD.CloudConnect.Data.Field> tmp = new Dictionary <string, MD.CloudConnect.Data.Field>();
                            //remove not usefull fields to simulate the cloud alogrithm
                            foreach (KeyValuePair <string, MD.CloudConnect.Data.Field> item in t.Data.fields)
                            {
                                if ((previousFields.ContainsKey(item.Key) && previousFields[item.Key].b64_value != item.Value.b64_value) &&
                                    t.Data.fields[item.Key].b64_value != null)
                                {
                                    tmp.Add(item.Key, item.Value);
                                    previousFields[item.Key].b64_value = item.Value.b64_value;
                                }
                            }
                            t.Data.fields = tmp;
                        }
                    }

                    result.TotalItems = tracks.Count;
                    result.TotalPages = tracks.Count / maxItemPerPage;

                    result.Page = page;
                    List <TrackingModel> tracksCurrentPage = tracks.Skip(page * maxItemPerPage).Take(maxItemPerPage).ToList();
                    result.NumberOfItems = tracksCurrentPage.Count;

                    List <MD.CloudConnect.MDData> data = new List <MD.CloudConnect.MDData>();

                    foreach (TrackingModel t in tracksCurrentPage)
                    {
                        MD.CloudConnect.MDData current = new MD.CloudConnect.MDData()
                        {
                            Meta = new MD.CloudConnect.Meta()
                            {
                                Account = "webdemo",
                                Event   = "track",
                            },
                            Payload = Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(t.Data, Formatting.None))
                        };

                        data.Add(current);
                    }
                    result.Content = data.ToArray();
                }
            }
            return(new ContentResult()
            {
                Content = JsonConvert.SerializeObject(result, Formatting.None),
                ContentType = "application/json"
            });
        }
        public ContentResult ExportData(string asset, int year, int month, int day, int page = 0, int maxItemPerPage = 20)
        {
            JsonExportData result = new JsonExportData();

            if (!String.IsNullOrEmpty(asset))
            {
                DateTime date = new DateTime(year, month, day);
                DeviceModel device = RepositoryFactory.Instance.DeviceDb.GetDevice(asset);
                if (device != null)
                {
                    List<TrackingModel> tracks = RepositoryFactory.Instance.DataTrackingDB.GetData(device, date).OrderBy(x => x.Data.Recorded_at).ToList();

                    Dictionary<string, MD.CloudConnect.Data.Field> previousFields = null;
                    foreach (TrackingModel t in tracks)
                    {
                        if (previousFields == null)
                        {
                            previousFields = new Dictionary<string, MD.CloudConnect.Data.Field>();
                            foreach (KeyValuePair<string, MD.CloudConnect.Data.Field> item in t.Data.fields)
                            {
                                if (item.Value.b64_value != null)
                                    previousFields.Add(item.Key, item.Value);
                            }
                            t.Data.fields = previousFields;
                        }
                        else
                        {
                            Dictionary<string, MD.CloudConnect.Data.Field> tmp = new Dictionary<string, MD.CloudConnect.Data.Field>();
                            //remove not usefull fields to simulate the cloud alogrithm
                            foreach (KeyValuePair<string, MD.CloudConnect.Data.Field> item in t.Data.fields)
                            {
                                if ((previousFields.ContainsKey(item.Key) && previousFields[item.Key].b64_value != item.Value.b64_value)
                                    && t.Data.fields[item.Key].b64_value != null)
                                {
                                    tmp.Add(item.Key, item.Value);
                                    previousFields[item.Key].b64_value = item.Value.b64_value;
                                }
                            }
                            t.Data.fields = tmp;
                        }
                    }

                    result.TotalItems = tracks.Count;
                    result.TotalPages = tracks.Count / maxItemPerPage;

                    result.Page = page;
                    List<TrackingModel> tracksCurrentPage = tracks.Skip(page * maxItemPerPage).Take(maxItemPerPage).ToList();
                    result.NumberOfItems = tracksCurrentPage.Count;

                    List<MD.CloudConnect.MDData> data = new List<MD.CloudConnect.MDData>();

                    foreach (TrackingModel t in tracksCurrentPage)
                    {
                        MD.CloudConnect.MDData current = new MD.CloudConnect.MDData()
                        {
                            Meta = new MD.CloudConnect.Meta()
                            {
                                Account = "webdemo",
                                Event = "track",
                            },
                            Payload = Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(t.Data, Formatting.None))
                        };

                        data.Add(current);
                    }
                    result.Content = data.ToArray();
                }
            }
            return new ContentResult()
            {
                Content = JsonConvert.SerializeObject(result, Formatting.None),
                ContentType = "application/json"
            };
        }