Esempio n. 1
0
        /// <summary>
        /// Save the preset position
        /// </summary>
        /// <param name="Position"></param>
        /// <returns></returns>
        public bool SavePresetPosition(PresetPosition Position)
        {
            using (SQLiteCommand cmd = new SQLiteCommand(conn))
            {
                StringBuilder sb = new StringBuilder();

                foreach (var item in Position.Items)
                {
                    sb.Append(string.Format("('{0}', '{1}', '{2}', {3}, {4}, {5}, '{6}')",
                                            new object[]
                    {
                        Position.MotionComponentHashCode.ToString("X"),
                        item.HashCode.ToString("X"),
                        Position.Name,
                        item.MoveOrder,
                        item.Speed,
                        item.Position,
                        item.IsAbsMode ? "1" : "0"
                    }));

                    sb.Append(", ");
                }

                // delete the last comma
                sb.Remove(sb.Length - 2, 2);

                cmd.CommandText = string.Format("insert into {0} ([motioncomponent_hashcode], [logicalaxis_hashcode], [name], [move_order], [speed], [position], [absmode]) VALUES {1}", TBL_PRESETPOSITION, sb.ToString());
                cmd.ExecuteNonQuery();
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Get the detail of preset position
        /// </summary>
        /// <param name="MotionComponentHashCode"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool ReadPresetPosition(int MotionComponentHashCode, string Name, out PresetPosition Preset)
        {
            bool ret = false;

            List <PresetPositionItem> detail = new List <PresetPositionItem>();

            Preset = new PresetPosition()
            {
                MotionComponentHashCode = MotionComponentHashCode,
                Name = Name
            };

            try
            {
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = string.Format("select * from {0} where motioncomponent_hashcode = '{1:X}' and name = '{2}'",
                                                    TBL_PRESETPOSITION, MotionComponentHashCode, Name);
                    SQLiteDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        detail.Add(new PresetPositionItem()
                        {
                            Id        = 0,
                            IsAbsMode = dr["absmode"].ToString() == "1" ? true : false,
                            Position  = double.Parse(dr["position"].ToString()),
                            Speed     = int.Parse(dr["speed"].ToString()),
                            MoveOrder = int.Parse(dr["move_order"].ToString()),
                            HashCode  = int.Parse(dr["logicalaxis_hashcode"].ToString(), System.Globalization.NumberStyles.HexNumber)
                        });
                    }

                    dr.Close();

                    if (detail.Count == 0)
                    {
                        Preset = null;
                    }
                    else
                    {
                        Preset.Items = detail.ToArray();
                    }

                    ret = true;
                }
            }
            catch (Exception ex)
            {
                LastError = ex.Message;
                Preset    = null;
                ret       = false;
            }

            return(ret);
        }
        public void SavePresetPosition(string Name)
        {
            try
            {
                PresetPosition pos = new PresetPosition()
                {
                    Name = Name,
                    MotionComponentHashCode = MotionComponent.GetHashCode(),
                    Items = new PresetPositionItem[AxisControlCollection.Count]
                };
                for (int i = 0; i < AxisControlCollection.Count; i++)
                {
                    pos.Items[i] = AxisControlCollection[i].PresetAggregation;
                }

                using (SqliteDb db = new SqliteDb())
                {
                    // check if the preset exists
                    if (db.ReadPresetPosition(pos.MotionComponentHashCode, Name, out PresetPosition preset))
                    {
                        if (preset == null)
                        {
                            // save the preset position
                            db.SavePresetPosition(pos);

                            Messenger.Default.Send <NotificationMessage <string> >(new NotificationMessage <string>(
                                                                                       this,
                                                                                       string.Format("The preset position saved successfully."),
                                                                                       "NOTIFY"));
                        }
                        else
                        {
                            // overwrite
                            Messenger.Default.Send <NotificationMessageAction <MessageBoxResult> >(new NotificationMessageAction <MessageBoxResult>(
                                                                                                       this,
                                                                                                       "AskForOverwrite",
                                                                                                       (ret) =>
                            {
                                if (ret == MessageBoxResult.Yes)
                                {
                                    // overwrite the current preset position
                                    if (db.DeletePresetPosition(pos.MotionComponentHashCode, pos.Name))
                                    {
                                        // save the preset position
                                        db.SavePresetPosition(pos);

                                        Messenger.Default.Send <NotificationMessage <string> >(new NotificationMessage <string>(
                                                                                                   this,
                                                                                                   string.Format("The preset position saved successfully."),
                                                                                                   "NOTIFY"));
                                    }
                                    else
                                    {
                                        Messenger.Default.Send <NotificationMessage <string> >(new NotificationMessage <string>(
                                                                                                   this,
                                                                                                   string.Format("Unable to delete the preset position, {0}", db.LastError),
                                                                                                   "ERROR"));
                                    }
                                }
                            }
                                                                                                       ));
                        }
                    }
                    else
                    {
                        // error while reading preset position from database
                        Messenger.Default.Send <NotificationMessage <string> >(new NotificationMessage <string>(
                                                                                   this,
                                                                                   string.Format("Unable to check the existence of the preset position, {0}", db.LastError),
                                                                                   "ERROR"));
                    }
                }
            }
            catch (Exception ex)
            {
                Messenger.Default.Send <NotificationMessage <string> >(new NotificationMessage <string>(
                                                                           this,
                                                                           string.Format("Unable to save the preset position, {0}", ex.Message),
                                                                           "ERROR"));
            }
        }