Example #1
0
        /// <summary>
        /// Gửi yêu cầu thêm thông báo tới cho server
        /// </summary>
        /// <param name="notifyObject">Đối tượng dữ liệu cần gửi</param>
        /// <param name="receivers">Danh sách người nhận</param>
        /// <returns>Phương thức này sẽ trả về một đối tượng AddNotificationFSPacketData giúp mô tả kết quả từ server.
        /// Trong trường hợp gặp sự cố kết nối, nó sẽ bắn ra một SocketException nếu lỗi khi gửi hoặc trả về null nếu lỗi xảy ra sau khi gửi hoàn thành</returns>
        public async Task <AddNotificationFSPacketData> AddNotificationAsync(T notifyObject, List <IReceiver> receivers)
        {
            AddNotificationPacketData PacketData =
                new AddNotificationPacketData(_NotifyContentConverter.ObjectToBytes(notifyObject),
                                              receivers.Select(r => r.RNReceiverOldID).ToList());

            FromClientPacket SendPacket = new FromClientPacket(FromClientPacketType.AddNotification, PacketData);

            TCPClientForDotNetFramework.SendAndReceiveResult result = await _Client.SendAndReceiveAsync(_FromClientConverter.ObjectToBytes(SendPacket));

            if (result.IsSuccess) // thành công thì trả về dữ liệu
            {
                try
                {
                    return(FromServerConverter.BytesToObject(result.Data).Data as AddNotificationFSPacketData);
                }
                catch (Exception ex)
                {   // nhận về thành công rồi mà mở ra bị lỗi thì là do hệ thống có vấn đề
                    throw ex;
                }
            }
            else
            {// lỗi khi đang gửi thì trả về null
                if (result.ErrorPoint == TCPClientForDotNetFramework.ErrorPoint.Sending)
                {
                    throw new System.Net.Sockets.SocketException();
                }
                else
                { // lỗi trong khi nhận dữ liệu
                    return(null);
                }
            }
        }
Example #2
0
        internal AddNotificationFSPacketData AddNotification(AddNotificationPacketData addNotification)
        {
            Notification_DBDataContext db = new Notification_DBDataContext();

            Notification notifi = new Notification {
                NotificationContent = new System.Data.Linq.Binary(addNotification.NotificationContent)
            };

            db.Notifications.InsertOnSubmit(notifi);

            #region thêm thông báo vào cho các receiver
            // lấy ra tất cả các người nhận trong Notification_DB theo các ID của người nhận có được ở bước trước
            var recievers = db.Receivers.Where(rc => addNotification.ReceiversOldID.Contains(rc.OldID));

            try
            {
                foreach (var receiver in recievers)
                {   // tạo thông báo cho mỗi người nhận
                    ReceiverNotification newreceivernoti = new ReceiverNotification {
                        Notification = notifi
                    };
                    // thêm thông báo cho người nhận
                    receiver.ReceiverNotifications.Add(newreceivernoti);
                    foreach (var device in receiver.Devices)
                    { // với mỗi thiết bị của người đó, thêm thông báo tới thiết bị
                        Console.WriteLine("add device notification");
                        device.DeviceNotifications.Add(new DeviceNotification {
                            ReceiverNotification = newreceivernoti
                        });
                    }
                }


                db.SubmitChanges();
                return(new AddNotificationFSPacketData(true, ComminucateServerErrorType.None, null));
            }
            catch (System.Data.SqlClient.SqlException Sqlex)
            {
                return(new AddNotificationFSPacketData(false, ComminucateServerErrorType.SqlError, Sqlex));
            }
            catch (Exception ex)
            {
                return(new AddNotificationFSPacketData(false, ComminucateServerErrorType.Unknow, ex));
            }

            #endregion
        }