Example #1
0
        public DAL.Communicator ConvertToData(DOM.Communicator domainModel)
        {
            {
                if (domainModel is DOM.SerialCommunicator)
                {
                    return ConvertToData((DOM.SerialCommunicator)domainModel);
                }
                else if (domainModel is DOM.FileCommunicator)
                {
                    return ConvertToData((DOM.FileCommunicator)domainModel);
                }
                else if (domainModel is DOM.DatabaseCommunicator)
                {
                    return ConvertToData((DOM.DatabaseCommunicator)domainModel);
                }

                return null;
            }
        }
Example #2
0
 /// <summary>
 /// Adds a new Device to the MongoDB database.
 /// </summary>
 /// <param name="device"></param>
 public void AddNewDevice(DOM.Device device)
 {
     _controller.AddNewDevice(_mapper.Map(device, new MON.Device()));
 }
 /// <summary>
 /// Updates the Value object on the database.
 /// </summary>
 /// <param name="value"></param>
 public void UpdateValue(DOM.Value value)
 {
     var dbValue = _db.Values.FirstOrDefault(v => v.Id == value.Id);
     if (dbValue == null) return;
     _mapper.Map(value, dbValue);
     _db.Entry(dbValue).State = EntityState.Modified;
     _db.SaveChanges();
 }
        /// <summary>
        /// Updates the database copy of the parametised domain model.
        /// </summary>
        /// <param name="device"></param>
        public DOM.Device UpdateDevice(DOM.Device device)
        {
            var dbCurrent = _db.Devices.FirstOrDefault(dev => dev.Id == device.Id);
            _mapper.Map(device, dbCurrent);

            _db.Entry(dbCurrent).State=EntityState.Modified;
            _db.SaveChanges();

            return _mapper.Map(dbCurrent, new DOM.Device());
        }
        /// <summary>
        /// Updates the Alarm object on the database.
        /// </summary>
        /// <param name="alarm"></param>
        /// <returns></returns>
        public DOM.Alarm UpdateAlarm(DOM.Alarm alarm)
        {
            var dbCurrent = _db.Alarms.FirstOrDefault(alm => alm.Id == alarm.Id);
            _mapper.Map(alarm, dbCurrent);

            _db.Entry(dbCurrent).State = EntityState.Modified;
            _db.SaveChanges();

            return _mapper.Map(dbCurrent, new DOM.Alarm());
        }
        /// <summary>
        /// Set the App Data for future use.
        /// </summary>
        /// <param name="ai"></param>
        public void SetApplicationInformation(DOM.AppData ai)
        {
            var dbValue = _db.AppData.First();
            if (dbValue == null)
            {
                _db.AppData.Add(_mapper.Map(ai, new DAL.AppData()));
            }
            else
            {
                dbValue = _mapper.Map(ai, new DAL.AppData());
            }

            _db.SaveChanges();
        }
 /// <summary>
 /// Removes the Rule from the database.
 /// </summary>
 /// <param name="rule"></param>
 public void DeleteRule(DOM.Rule rule)
 {
     var dbValue = _db.Rules.FirstOrDefault(r => r.Id == rule.Id);
     if (dbValue == null) return;
     _db.Rules.Remove(dbValue);
     _db.SaveChanges();
 }
 /// <summary>
 /// Removes the action from the database.
 /// </summary>
 /// <param name="action">Action to remove.</param>
 public void DeleteAction(DOM.Action action)
 {
     var dbValue = RetrieveAction(action.Id);
     if (dbValue == null) return;
     _db.Actions.Remove(_mapper.Map(action, new DAL.Action()));
     _db.SaveChanges();
 }
Example #9
0
 public DAL.CommunicatorType ConvertToData(DOM.CommunicatorType domainModel)
 {
     throw new NotImplementedException();
 }
Example #10
0
 public DAL.DatabaseType ConvertToData(DOM.DatabaseType dataModel)
 {
     throw new NotImplementedException();
 }
Example #11
0
 public DAL.RuleType ConvertToData(DOM.RuleType domainModel)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public DAL.Value ConvertToData(DOM.Value domainModel)
 {
     throw new NotImplementedException();
 }
Example #13
0
 public DAL.SerialCommunicator ConvertToData(DOM.SerialCommunicator domainModel)
 {
     throw new NotImplementedException();
 }
Example #14
0
 /// <summary>
 /// Adds a new value to the MongoDB database.
 /// </summary>
 /// <param name="value"></param>
 public void AddNewValue(DOM.Value value)
 {
     _controller.AddNewValue(_mapper.Map(value, new MON.Value()));
 }
        /// <summary>
        /// Creates a new instance of a Rule object. The returned
        /// object includes all created Identity information.
        /// </summary>
        /// <param name="rule"></param>
        /// <returns></returns>
        public DOM.Rule CreateRule(DOM.Rule rule)
        {
            //Map to DAL object.
            var dbVal = _mapper.Map(rule, new DAL.Rule());

            //The Device does not need individual mapping for Rules.

            try
            {
                //Save to Database.
                _db.Rules.Add(dbVal);

                //Save the changes made to the database.
                _db.SaveChanges();

                //Return the updated Domain object.
                return _mapper.Map(dbVal, new DOM.Rule());
            }
            catch (Exception e)
            {
                //Report fault back up the chain.
                DebugOutput.Print("Rule insertion to database failed. ", e.Message);
            }

            //Return null on failure.
            return null;
        }
        /// <summary>
        /// Creates a new Value object on the database. The returned
        /// object includes all created Identity information.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public DOM.Value CreateValue(DOM.Value value)
        {
            var dbVal = _mapper.Map(value, new DAL.Value());
            dbVal.DeviceId = value.Device.Id;

            try
            {
                _db.Values.Add(dbVal);
                _db.SaveChanges();

                DebugOutput.Print($"Communicator #{dbVal.DeviceId}'s new value, '{dbVal.StringValue}', was captured!");

                return _mapper.Map(dbVal, new DOM.Value());
            }
            catch (Exception)
            {
                var msg = $"Communicator #{value.Device.Id}'s new value, '{value.StringValue}', could not be captured!";
                DebugOutput.Print("Could not store the new value!", msg);
            }

            //Return null on fail.
            return null;
        }
Example #17
0
 public DAL.AppData ConvertToData(DOM.AppData domainModel)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Updates the database to remove the device from the system.
 /// </summary>
 /// <param name="device"></param>
 public void DeleteDevice(DOM.Device device)
 {
     var dbValue = RetrieveDevice(device.Id);
     if (dbValue == null) return;
     _db.Devices.Remove(_mapper.Map(dbValue, new DAL.Device()));
     _db.SaveChanges();
 }
        /// <summary>
        /// Creates a new Action object within the database.
        /// </summary>
        /// <param name="action"></param>
        public DOM.Action CreateAction(DOM.Action action)
        {
            var dbVal = (_mapper.Map(action, new DAL.Action()));

            _db.Actions.Add(dbVal);
            _db.SaveChanges();

            return _mapper.Map(dbVal, new DOM.Action());
        }
 /// <summary>
 /// Removes the single Value from the database, if it exists.
 /// </summary>
 /// <param name="value"></param>
 public void DeleteValue(DOM.Value value)
 {
     var dbValue = _db.Values.FirstOrDefault(v => v.Id == value.Id);
     if (dbValue == null) return;
     _db.Values.Remove(dbValue);
     _db.SaveChanges();
 }
        /// <summary>
        /// Creates a new Alarm object on the database. The returned
        /// object includes all created Identity information.
        /// </summary>
        /// <param name="alarm"></param>
        /// <returns></returns>
        public DOM.Alarm CreateAlarm(DOM.Alarm alarm)
        {
            //Convert the main Alarm object.
            var dbVal = _mapper.Map(alarm, new DAL.Alarm());

            //Create a new Globally Unique Identifier.
            //dbVal.Id = Guid.NewGuid();

            //Add the Alarm object to the Entity Context and Save changes to the Online Store.
            _db.Alarms.Add(dbVal);
            _db.SaveChanges();

            //Return the new Alarm.
            return _mapper.Map(dbVal, new DOM.Alarm());
        }
        /// <summary>
        /// Updates the database Action entity based on the changes made in
        /// the parammetised Domain model.
        /// </summary>
        /// <param name="action">Domain model to update.</param>
        public DOM.Action UpdateAction(DOM.Action action)
        {
            var dbAction = _db.Actions.FirstOrDefault(act => act.Id == action.Id);

            if (dbAction == null) return null;

            var newAction = _mapper.Map(action, new DAL.Action());

            _mapper.Map(newAction, dbAction);
            dbAction.OutputValue = newAction.OutputValue;

            _db.SaveChanges();
            return _mapper.Map(dbAction, new DOM.Action());
        }
        /// <summary>
        /// Creates a new Communicator object on the database. Automatically
        /// handles each of the subsidary Communicator types internally.
        /// </summary>
        /// <param name="communicator">The domain model to push up to the database.</param>
        /// <returns>The new ID number</returns>
        public int? CreateCommunicator(DOM.Communicator communicator)
        {
            DAL.Communicator dbVal;

            switch (communicator.Type)
            {
                case DOM.CommunicatorType.FlatFile:
                    dbVal = _mapper.Map(communicator, new DAL.FileCommunicator());
                    break;
                case DOM.CommunicatorType.Serial:
                    dbVal= _mapper.Map(communicator, new DAL.SerialCommunicator());
                    break;
                case DOM.CommunicatorType.Database:
                    dbVal = _mapper.Map(communicator, new DAL.DatabaseCommunicator());
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var dalDevice = _mapper.Map(communicator.Device, dbVal.Device);
            dbVal.Device = dalDevice;

            try
            {
                _db.Entry(dbVal.Device).State = EntityState.Unchanged;
                _db.Communicators.Add(dbVal);

                _db.SaveChanges();
                return _db.Communicators.Find(dbVal.Id).Id;
            }
            catch (Exception e)
            {
                DebugOutput.Print("System was unable to detatch the Device from the new Communicator! ", e.Message);
            }

            return null;
        }
        /// <summary>
        /// Updates the Communicator as housed on the database.
        /// </summary>
        /// <param name="communicator"></param>
        public void UpdateCommunicator(DOM.Communicator communicator)
        {
            var dbValue = _db.Communicators.FirstOrDefault(comm => comm.Id == communicator.Id);
            if (dbValue == null) return;
            _mapper.Map(communicator, dbValue);

            //Tell EF that the object has been updated.
            _db.Entry(dbValue).State = EntityState.Modified;

            //Ensure that EF is aware this child object has not changed (and thus does not need changing/creating).
            _db.Entry(dbValue.Device).State = EntityState.Unchanged;
            _db.SaveChanges();
        }
        /// <summary>
        /// Creates a new Device object within the database.
        /// </summary>
        /// <param name="device"></param>
        public DOM.Device CreateDevice(DOM.Device device)
        {
            var dbVal = _mapper.Map(device, new DAL.Device());
            _db.Devices.Add(dbVal);
            _db.SaveChanges();

            //Update MongoDB accordingly
            var mongorepo = new MongoRepository();
            mongorepo.AddNewDevice(device);
            //MongoDB Updated!

            return _mapper.Map(dbVal, new DOM.Device());
        }
        /// <summary>
        /// Updates the Rule object on the database, the returned
        /// object includes all created Identity information.
        /// </summary>
        /// <param name="rule"></param>
        /// <returns></returns>
        public DOM.Rule UpdateRule(DOM.Rule rule)
        {
            var dbVal = _db.Rules.FirstOrDefault(r => r.Id == rule.Id);
            if (dbVal == null) return null;
            _mapper.Map(rule, dbVal);
            _db.Entry(dbVal).State = EntityState.Modified;
            _db.SaveChanges();

            //Return the updated Domain object.
            return _mapper.Map(dbVal, new DOM.Rule());
        }
 /// <summary>
 /// Decides on behalf of the user whether to create a new Device 
 /// entity or whether to update an entity that exists in its place.
 /// </summary>
 /// <param name="device"></param>
 public DOM.Device CreateOrUpdateDevice(DOM.Device device)
 {
     var dbValue = RetrieveDevice(device.Id);
     if (dbValue == null)
     {
         return CreateDevice(device);
     }
     else
     {
         return UpdateDevice(device);
     }
 }
 /// <summary>
 /// Converts a single DOM Communicator to DAL.
 /// </summary>
 /// <param name="communicator">DOM Communicator.</param>
 /// <returns>DAL Communicator.</returns>
 private DAL.Communicator ConvertDOMCommunicatorToDAL(DOM.Communicator communicator)
 {
     switch (communicator.Type)
     {
         case DOM.CommunicatorType.Database:
             return (_mapper.Map(communicator, new DAL.DatabaseCommunicator()));
         case DOM.CommunicatorType.Serial:
             return (_mapper.Map(communicator, new DAL.SerialCommunicator()));
         case DOM.CommunicatorType.FlatFile:
             return (_mapper.Map(communicator, new DAL.FileCommunicator()));
         default:
             DebugOutput.Print("Unable to convert/understand Communicator...", communicator.Id.ToString());
             return null;
     }
 }
Example #29
0
 /// <summary>
 /// Updates and existing Device within the MongoDB datbase.
 /// </summary>
 /// <param name="device"></param>
 public void UpdateDevice(DOM.Device device)
 {
     _controller.UpdateDevice(_mapper.Map(device, new MON.Device()));
 }