/// <summary> /// Posts the parametised Device object directly into the database. /// </summary> /// <param name="device"></param> public void AddNewDevice(Device device) { //If Mongo unaccessible, ignore! if (!_mongoAccessible) return; var processObj = new Process() { ObjectId = ObjectId.GenerateNewId(), DeviceId = device.Id, Custodian = device.Custodian, Enabled = device.Enabled, Location = device.Location, Name = device.Name, Values = new List<Value>() }; _db.GetCollection<Process>("ProcessData").InsertOne(processObj); }
/// <summary> /// Internal conversion between a basic Device object and the logical-parent /// Process. /// </summary> /// <param name="device">Basic Device object.</param> /// <returns>Updated Process object.</returns> private Process ConvertDeviceToProcess(Device device) { var _id = device.ObjectId == null ? ObjectId.GenerateNewId() : device.ObjectId; return new Process() { ObjectId = _id, DeviceId = device.Id, Custodian = device.Custodian, Enabled = device.Enabled, Location = device.Location, Name = device.Name, Values = new List<Value>() }; }
/// <summary> /// Repalces the current Device in the database with the parametised Device's ID /// with the parametised Device. /// </summary> /// <param name="device"></param> public void UpdateDevice(Device device) { //If Mongo unaccessible, ignore! if (!_mongoAccessible) return; var collection = _db.GetCollection<Process>("ProcessData"); var filter = Builders<Process>.Filter.Eq("DeviceId", device.Id); var result = collection.Find(filter).ToList(); if (result.Count > 0) { var entity = result[0]; device.ObjectId = entity.ObjectId; } var process = ConvertDeviceToProcess(device); var update = collection.ReplaceOne ( item => item.DeviceId == device.Id, process, new UpdateOptions { IsUpsert = true } ); }