Exemple #1
0
        /// <summary>
        /// DC updates new value.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GXValuesUpdateResponse Post(GXValuesUpdateRequest request)
        {
            List <GXEventsItem> events = new List <GXEventsItem>();
            bool partOfTable           = request.Values[0] is GXAmiDataRow;

            lock (Db)
            {
                if (request.Values != null && request.Values.Length != 0)
                {
                    using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted))
                    {
                        if (partOfTable)
                        {
                            uint maxRow = 0;
                            //Find last row number.
#if !SS4
                            SqlExpressionVisitor <GXAmiDataRow> ev = OrmLiteConfig.DialectProvider.ExpressionVisitor <GXAmiDataRow>();
#else
                            SqlExpression <GXAmiDataRow> ev = OrmLiteConfig.DialectProvider.SqlExpression <GXAmiDataRow>();
#endif
                            ev.Limit(1);
                            ev.Where(q => q.TableID == (request.Values[0] as GXAmiDataRow).TableID);
                            ev.OrderByDescending(q => q.RowIndex);
                            List <GXAmiDataRow> items = Db.Select <GXAmiDataRow>(ev);
                            if (items.Count == 1)
                            {
                                maxRow = items[0].RowIndex;
                            }
                            else
                            {
                                maxRow = 0;
                            }
                            foreach (GXAmiDataRow it in request.Values)
                            {
                                //Increase row count.
                                if (it.RowIndex == 0 && it.ColumnIndex == 0)
                                {
                                    ++maxRow;
                                }
                                it.TimeStamp = DateTime.Now;
                                if (it.RowIndex == 0)
                                {
                                    it.RowIndex = maxRow;
                                    Db.Insert(it);
#if !SS4
                                    it.Id = (ulong)Db.GetLastInsertId();
#else
                                    it.Id = (ulong)Db.LastInsertId();
#endif
                                }
                                else
                                {
                                    Db.Update(it);
                                }
                                events.Add(new GXEventsItem(ActionTargets.TableValueChanged, Actions.Add, it));
                            }
                        }
                        else
                        {
                            foreach (GXAmiDataValue it in request.Values)
                            {
                                //Delete old value from latest value.
                                Db.Delete <GXAmiLatestValue>(q => q.Id == it.PropertyID);
                                GXAmiLatestValue v = new GXAmiLatestValue();
                                v.TimeStamp  = DateTime.Now;
                                v.PropertyID = it.PropertyID;
                                ulong mask = 0xFFFF;
                                v.DeviceID = v.PropertyID & ~mask;
                                v.UIValue  = it.UIValue;
                                GXAmiValueLog lv = new GXAmiValueLog();
                                lv.PropertyID = it.PropertyID;
                                lv.DeviceID   = v.DeviceID;
                                lv.UIValue    = v.UIValue;
                                lv.TimeStamp  = v.TimeStamp;
                                Db.Insert(v);
                                Db.Insert(lv);
                                events.Add(new GXEventsItem(ActionTargets.ValueChanged, Actions.Add, v));
                            }
                        }
                        trans.Commit();
                    }
                }
            }
            AppHost host = this.ResolveService <AppHost>();
            host.SetEvents(Db, this.Request, 0, events);
            return(new GXValuesUpdateResponse());
        }
        /// <summary>
        /// DC updates new value.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GXValuesUpdateResponse Post(GXValuesUpdateRequest request)
        {
            List<GXEventsItem> events = new List<GXEventsItem>();
            bool partOfTable = request.Values[0] is GXAmiDataRow;
            lock (Db)
            {
                if (request.Values != null && request.Values.Length != 0)
                {
                    using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted))
                    {
                        if (partOfTable)
                        {
                            uint maxRow = 0;
                            //Find last row number.
#if !SS4
                            SqlExpressionVisitor<GXAmiDataRow> ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<GXAmiDataRow>();
#else
                            SqlExpression<GXAmiDataRow> ev = OrmLiteConfig.DialectProvider.SqlExpression<GXAmiDataRow>();
#endif                                                                
                            ev.Limit(1);
                            ev.Where(q => q.TableID == (request.Values[0] as GXAmiDataRow).TableID);
                            ev.OrderByDescending(q => q.RowIndex);
                            List<GXAmiDataRow> items = Db.Select<GXAmiDataRow>(ev);
                            if (items.Count == 1)
                            {
                                maxRow = items[0].RowIndex;
                            }
                            else
                            {
                                maxRow = 0;
                            }
                            foreach (GXAmiDataRow it in request.Values)
                            {
                                //Increase row count.
                                if (it.RowIndex == 0 && it.ColumnIndex == 0)
                                {
                                    ++maxRow;
                                }
                                it.TimeStamp = DateTime.Now;
                                if (it.RowIndex == 0)
                                {
                                    it.RowIndex = maxRow;
                                    Db.Insert(it);
#if !SS4
                                    it.Id = (ulong) Db.GetLastInsertId();
#else
                                    it.Id = (ulong) Db.LastInsertId();
#endif                                    
                                }
                                else
                                {
                                    Db.Update(it);
                                }
                                events.Add(new GXEventsItem(ActionTargets.TableValueChanged, Actions.Add, it));
                            }
                        }
                        else
                        {
                            foreach (GXAmiDataValue it in request.Values)
                            {
                                //Delete old value from latest value.
                                Db.Delete<GXAmiLatestValue>(q => q.Id == it.PropertyID);
                                GXAmiLatestValue v = new GXAmiLatestValue();
                                v.TimeStamp = DateTime.Now;
                                v.PropertyID = it.PropertyID;
                                ulong mask = 0xFFFF;
                                v.DeviceID = v.PropertyID & ~mask;
                                v.UIValue = it.UIValue;
                                GXAmiValueLog lv = new GXAmiValueLog();
                                lv.PropertyID = it.PropertyID;
                                lv.DeviceID = v.DeviceID;
                                lv.UIValue = v.UIValue;
                                lv.TimeStamp = v.TimeStamp;
                                Db.Insert(v);
                                Db.Insert(lv);
                                events.Add(new GXEventsItem(ActionTargets.ValueChanged, Actions.Add, v));
                            }
                        }
                        trans.Commit();
                    }
                }
            }
            AppHost host = this.ResolveService<AppHost>();
            host.SetEvents(Db, this.Request, 0, events);
            return new GXValuesUpdateResponse();
        }