aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/DataStore/DefaultDataStoreProvider.cs
blob: 9136658772c26f9c28aa886212a95c6974faeb1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.ActionLogs;
using Tango.BL.DTO;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.Core.DI;
using Tango.DataStore;
using Tango.DataStore.Editing;
using Tango.DataStore.EF;
using Tango.DataStore.Remote;
using Tango.FSE.BL;
using Tango.FSE.Common.Authentication;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.DataStore;

namespace Tango.FSE.UI.DataStore
{
    public class DefaultDataStoreProvider : ExtendedObject, IDataStoreProvider
    {
        private DataStoreModel _lastModel;
        private IMachineProvider MachineProvider { get; set; }

        [TangoInject]
        private FSEServicesContainer Services { get; set; }

        [TangoInject]
        protected IActionLogManager ActionLogManager { get; set; }

        [TangoInject]
        protected IAuthenticationProvider AuthenticationProvider { get; set; }

        private bool _acceptFirmwareChanges;
        public bool AcceptFirmwareChanges
        {
            get { return _acceptFirmwareChanges; }
            set { _acceptFirmwareChanges = value; RaisePropertyChangedAuto(); }
        }

        public DefaultDataStoreProvider(IMachineProvider machineProvider)
        {
            MachineProvider = machineProvider;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

        private void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e)
        {
            _lastModel = null;

            if (MachineProvider.IsPPCAvailable)
            {
                LogManager.Log("Starting listening for data store changes...");

                MachineProvider.MachineOperator.SendGenericContinuousRequest<RemoteDataStoreStartListenRequest, RemoteDataStoreStartListenResponse>(new RemoteDataStoreStartListenRequest()).Subscribe((response) =>
                {
                    if (response.ChangeType != RemoteDataStoreChangeType.None)
                    {
                        LogManager.Log($"Data store change received for '{response.CollectionName}.{response.Item.Key}'...");
                        OnDataStoreItemChanged(response);
                    }
                }, (ex) =>
                {
                    if (!(ex is Transport.TransporterDisconnectedException))
                    {
                        LogManager.Log(ex, "Error occurred on data store changes listener.");
                    }
                }, () =>
                {
                    //Nothing.
                });
            }
        }

        private void OnDataStoreItemChanged(RemoteDataStoreStartListenResponse response)
        {
            try
            {
                if (_lastModel != null && AcceptFirmwareChanges)
                {
                    DataStoreCollectionModel collectionModel = _lastModel.Collections.FirstOrDefault(x => x.Name == response.CollectionName);

                    if (collectionModel == null)
                    {
                        collectionModel = new DataStoreCollectionModel();
                        collectionModel.Name = response.CollectionName;
                        _lastModel.Collections.Add(collectionModel);
                    }

                    var remoteItem = response.Item;

                    var itemModel = collectionModel.Items.FirstOrDefault(x => x.Guid == remoteItem.Guid);

                    if (itemModel == null)
                    {
                        itemModel = DataStoreItemModel.FromLocalDataStoreItem(remoteItem, null);
                        itemModel.ExistsOnMachine = true;
                        collectionModel.Items.Add(itemModel);
                    }
                    else
                    {
                        itemModel.Value = remoteItem.Value;
                        itemModel.Type = remoteItem.Type;
                        itemModel.Date = remoteItem.Date;
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error occurred while processing a data store item remote change.");
            }
        }

        public Task<DataStoreModel> GetDataStoreModel(String machineGuid)
        {
            return Task.Factory.StartNew<DataStoreModel>(() =>
            {
                DataStoreModel model = new DataStoreModel();

                List<GlobalDataStoreItem> globalItems = Services.DataStoreService.GetGlobalDataStoreItems().Result;
                List<DataStoreItem> localItems = Services.DataStoreService.GetMachinelDataStoreItems(machineGuid).Result;

                List<DataStoreItemModel> itemsModels = new List<DataStoreItemModel>();

                //Get machine items from db.
                foreach (var collection in localItems.GroupBy(x => x.CollectionName))
                {
                    DataStoreCollectionModel collectionModel = new DataStoreCollectionModel();
                    collectionModel.Name = collection.First().CollectionName;

                    foreach (var item in collection)
                    {
                        GlobalDataStoreItem globalItem = globalItems.FirstOrDefault(x => x.CollectionName == item.CollectionName && x.Key == item.Key);

                        if (globalItem != null)
                        {
                            globalItems.Remove(globalItem);
                        }

                        DataStoreItemModel itemModel = DataStoreItemModel.FromLocalDataStoreItem(item.ToDataStoreItem(), globalItem?.ToDataStoreItem());
                        collectionModel.Items.Add(itemModel);
                    }

                    model.Collections.Add(collectionModel);
                }

                //Get machine items from connected machine.
                if (MachineProvider.IsPPCAvailable && machineGuid == MachineProvider.Machine.Guid)
                {
                    var response = MachineProvider.MachineOperator.SendGenericRequest<RemoteDataStoreGetAllItemsRequest, RemoteDataStoreGetAllItemsResponse>(new RemoteDataStoreGetAllItemsRequest(), new Transport.TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(30) }).Result;

                    foreach (var collection in response.Collections)
                    {
                        DataStoreCollectionModel collectionModel = model.Collections.FirstOrDefault(x => x.Name == collection.Name);

                        if (collectionModel == null)
                        {
                            collectionModel = new DataStoreCollectionModel();
                            collectionModel.Name = collection.Name;
                            model.Collections.Add(collectionModel);
                        }

                        foreach (var remoteItem in collection.Items)
                        {
                            var localItem = collectionModel.Items.FirstOrDefault(x => x.Key == remoteItem.Key);

                            if (localItem != null)
                            {
                                localItem.ExistsOnMachine = true;
                            }

                            if (localItem != null && remoteItem.Date > localItem.Date)
                            {
                                localItem.Value = remoteItem.Value;
                                localItem.OriginalValue = remoteItem.Value;
                                localItem.Type = remoteItem.Type;
                                localItem.Date = remoteItem.Date;
                            }
                            else if (localItem == null)
                            {
                                GlobalDataStoreItem globalItem = globalItems.FirstOrDefault(x => x.CollectionName == collection.Name && x.Key == remoteItem.Key);

                                if (globalItem != null)
                                {
                                    globalItems.Remove(globalItem);
                                }

                                DataStoreItemModel itemModel = DataStoreItemModel.FromLocalDataStoreItem(remoteItem, globalItem?.ToDataStoreItem());
                                itemModel.ExistsOnMachine = true;
                                collectionModel.Items.Add(itemModel);
                            }

                        }
                    }
                }

                //Get global items without overrides from db.
                foreach (var collection in globalItems.GroupBy(x => x.CollectionName))
                {
                    DataStoreCollectionModel collectionModel = model.Collections.FirstOrDefault(x => x.Name == collection.First().CollectionName);

                    if (collectionModel == null)
                    {
                        collectionModel = new DataStoreCollectionModel();
                        collectionModel.Name = collection.First().CollectionName;
                        model.Collections.Add(collectionModel);
                    }

                    foreach (var item in collection)
                    {
                        DataStoreItemModel itemModel = DataStoreItemModel.FromGlobalDataStoreItem(item.ToDataStoreItem());
                        collectionModel.Items.Add(itemModel);
                    }
                }


                _lastModel = model;
                return model;
            });
        }

        private class SaveModel
        {
            public String CollectionName { get; set; }
            public DataStoreItemModel Item { get; set; }
        }

        public Task<DataStoreModel> UpdateDataStoreModel(DataStoreModel model, String machineGuid)
        {
            return Task.Factory.StartNew<DataStoreModel>(() =>
            {
                List<SaveModel> globals = new List<SaveModel>();
                List<SaveModel> locals = new List<SaveModel>();
                List<SaveModel> deleted = new List<SaveModel>();

                List<DataStoreItemDTO> actionLogInserted = new List<DataStoreItemDTO>();
                List<Tuple<DataStoreItemDTO, DataStoreItemDTO>> actionLogModified = new List<Tuple<DataStoreItemDTO, DataStoreItemDTO>>();
                List<DataStoreItemDTO> actionLogDeleted = new List<DataStoreItemDTO>();

                UpdateDataStoreRequest updateRequest = new UpdateDataStoreRequest();

                foreach (var collection in model.Collections.Where(x => !x.IsDeleted))
                {
                    foreach (var item in collection.Items)
                    {
                        if (item.IsGlobal)
                        {
                            globals.Add(new SaveModel() { CollectionName = collection.Name, Item = item });
                        }
                        else if (item.IsDeleted)
                        {
                            deleted.Add(new SaveModel() { CollectionName = collection.Name, Item = item });
                        }
                        else if (item.HasDifference || (MachineProvider.IsPPCAvailable && !item.ExistsOnMachine))
                        {
                            locals.Add(new SaveModel() { CollectionName = collection.Name, Item = item });
                        }
                    }
                }

                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    var machine = db.Machines.SingleOrDefault(x => x.Guid == machineGuid);

                    var allItems = db.DataStoreItems.Where(x => x.MachineGuid == machineGuid).ToList();

                    //Deleted collections
                    foreach (var deletedCollection in model.Collections.Where(x => x.IsDeleted))
                    {
                        foreach (var itemDb in allItems.ToList())
                        {
                            if (itemDb.CollectionName == deletedCollection.Name)
                            {
                                itemDb.IsDeleted = true;
                                itemDb.LastUpdated = DateTime.UtcNow;
                                itemDb.IsSynchronized = false;
                                updateRequest.ToDelete.Add(itemDb.Guid);
                                actionLogDeleted.Add(DataStoreItemDTO.FromObservable(itemDb));
                            }
                        }
                    }

                    //Deleted items
                    foreach (var item in deleted)
                    {
                        var itemDb = allItems.FirstOrDefault(x => x.CollectionName == item.CollectionName && x.Key == item.Item.Key);
                        if (itemDb != null)
                        {
                            itemDb.IsDeleted = true;
                            itemDb.LastUpdated = DateTime.UtcNow;
                            itemDb.IsSynchronized = false;
                            updateRequest.ToDelete.Add(itemDb.Guid);
                            try
                            {
                                actionLogDeleted.Add(DataStoreItemDTO.FromObservable(itemDb));
                            }
                            catch { }
                        }
                    }

                    //locals
                    foreach (var item in locals)
                    {
                        DataStoreItem itemDb = allItems.FirstOrDefault(x => x.CollectionName == item.CollectionName && x.Key == item.Item.Key);

                        if (itemDb == null) //new local item.
                        {
                            itemDb = new DataStoreItem();
                            itemDb.MachineGuid = machineGuid;
                            itemDb.CollectionName = item.CollectionName;
                            itemDb.Key = item.Item.Key;
                            itemDb.LastUpdated = DateTime.UtcNow;
                            itemDb.DataType = (int)item.Item.Type;
                            itemDb.Value = EFDataStoreHelper.CreateBytes(item.Item.Type, item.Item.Value);
                            db.DataStoreItems.Add(itemDb);
                            updateRequest.ToUpsert.Add(DataStoreItemDTO.FromObservable(itemDb));
                            actionLogInserted.Add(DataStoreItemDTO.FromObservable(itemDb));
                        }
                        else //update local item only if changed...
                        {
                            bool upsert = MachineProvider.IsPPCAvailable && !item.Item.ExistsOnMachine;

                            var itemDbDTO = DataStoreItemDTO.FromObservable(itemDb);

                            if (itemDb.IsDeleted) //restore if item was deleted..
                            {
                                itemDb.IsDeleted = false;
                                itemDb.LastUpdated = DateTime.UtcNow;
                                itemDb.IsSynchronized = false;
                                upsert = true;
                            }

                            //update item only if it has difference although "locals" already contains only differences.
                            var bytes = EFDataStoreHelper.CreateBytes(item.Item.Type, item.Item.Value);

                            if (itemDb.DataType != (int)item.Item.Type || !Enumerable.SequenceEqual(itemDb.Value, bytes))
                            {
                                itemDb.DataType = (int)item.Item.Type;
                                itemDb.Value = bytes;
                                itemDb.LastUpdated = DateTime.UtcNow;
                                itemDb.IsSynchronized = false;
                                upsert = true;
                            }

                            if (upsert)
                            {
                                updateRequest.ToUpsert.Add(DataStoreItemDTO.FromObservable(itemDb));
                                actionLogModified.Add(new Tuple<DataStoreItemDTO, DataStoreItemDTO>(itemDbDTO, DataStoreItemDTO.FromObservable(itemDb)));
                            }
                        }
                    }

                    if (MachineProvider.IsPPCAvailable && machineGuid == MachineProvider.Machine.Guid)
                    {
                        //Direct Sync Here.
                        //Make all items "IsSynchronized = true" if success.
                        var response = MachineProvider.MachineOperator.SendGenericRequest<UpdateDataStoreRequest, UpdateDataStoreResponse>(updateRequest, new Transport.TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(30) }).Result;

                        allItems.ForEach(x => x.IsSynchronized = true);
                    }

                    db.SaveChanges();

                    try
                    {
                        //Save action logs..
                        actionLogDeleted = actionLogDeleted.DistinctBy(x => x.Guid).ToList();
                        actionLogInserted = actionLogInserted.DistinctBy(x => x.Guid).ToList();
                        actionLogModified = actionLogModified.DistinctBy(x => x.Item1.Guid).ToList();

                        foreach (var item in actionLogDeleted)
                        {
                            ActionLogManager.InsertLog(ActionLogType.DataStoreItemDeleted, AuthenticationProvider.CurrentUser, $"{machine.SerialNumber} => {item.Key}", item, "Data store item deleted via FSE.", true);
                        }

                        foreach (var item in actionLogInserted)
                        {
                            ActionLogManager.InsertLog(ActionLogType.DataStoreItemCreated, AuthenticationProvider.CurrentUser, $"{machine.SerialNumber} => {item.Key}", item, "Data store item created via FSE.", false);
                        }

                        foreach (var item in actionLogModified)
                        {
                            ActionLogManager.InsertLog(ActionLogType.DataStoreItemModified, AuthenticationProvider.CurrentUser, $"{machine.SerialNumber} => {item.Item1.Key}", item.Item1, item.Item2, "Data store item modified via FSE.");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error saving action logs for data store updates.");
                    }
                }

                return GetDataStoreModel(machineGuid).Result;
            });
        }

        private void ValidateCollectionAndKey(String collection = null, String key = null)
        {
            if (collection != null)
            {
                if (!DataStoreHelper.ValidateCollectionOrKeyName(collection))
                {
                    throw new ArgumentException("Collection name contains invalid characters.");
                }
            }

            if (key != null)
            {
                if (!DataStoreHelper.ValidateCollectionOrKeyName(key))
                {
                    throw new ArgumentException("Item key contains invalid characters.");
                }
            }
        }
    }
}