예제 #1
0
        private void LoadCurrentStateAndSendNotification(String groupController, int group)
        {
            List <SendingData> listOfSendingData = null;

            //TODO: think about try. How many of them we need here and what to do with errors.
            try
            {
                SqlConnection conn = GetDBConnection();

                const string SQL_LOAD_CURRENT_STATE =
                    @"UPDATE entereddatatable
                      SET ActualStatusID = @ProcessedActualStatus, 
                          EditDate = GETUTCDATE()
                      OUTPUT inserted.RoomID, inserted.RoomName, inserted.StatusID, inserted.SuplementData, inserted.AdditionalData, inserted.CreationDate
                      WHERE GroupController = @GroupController
                            AND [Group] = @Group
                            AND ActualStatusID = @ActiveActualStatus";

                using (SqlCommand cmd = new SqlCommand(SQL_LOAD_CURRENT_STATE, conn))
                {
                    cmd.Parameters.Add("@GroupController", SqlDbType.NVarChar, 2).Value = groupController;
                    cmd.Parameters.Add("@Group", SqlDbType.TinyInt).Value             = group;
                    cmd.Parameters.Add("@ProcessedActualStatus", SqlDbType.Int).Value = ActualStatus.Processed;
                    cmd.Parameters.Add("@ActiveActualStatus", SqlDbType.Int).Value    = ActualStatus.Active;

                    conn.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            listOfSendingData = new List <SendingData>();

                            while (reader.Read())
                            {
                                SendingData data = new SendingData
                                {
                                    roomID          = Int32.Parse((reader["RoomID"].ToString())),
                                    roomName        = (reader["RoomName"].ToString()),
                                    statusID        = Int32.Parse((reader["StatusID"].ToString())),
                                    suplementData   = (reader["SuplementData"].ToString()),
                                    roomDescription = (reader["AdditionalData"].ToString()),
                                    date            = DateTime.Parse(reader["CreationDate"].ToString())
                                };

                                listOfSendingData.Add(data);
                            }
                        }
                    }

                    if (listOfSendingData != null)
                    {
                        LoadAllActiveDevicesAndSendNotification(group, listOfSendingData, false, conn);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #2
0
        private void DataProcessing(string data)
        {
            string[] dataBlocks = Regex.Split(data, RECORD_SEPARATOR);
            foreach (var block in dataBlocks)
            {
                if (block.Length > 0)
                {
                    var startIndex = 0;
                    //In case when we have <00> at the beggining of the data string.
                    if (block.Substring(0, 4) == "<00>")
                    {
                        if (block.Length == 4)
                        {
                            continue;
                        }
                        startIndex = 4;
                    }

                    string[] separatedBlock = Regex.Split(block, UNIT_SEPARATOR);

                    var roomID_1        = Int32.Parse(separatedBlock[0].Substring(startIndex, 4).Replace("<", "").Replace(">", ""));
                    var roomID_2        = Int32.Parse(separatedBlock[0].Substring(startIndex + 4, 4).Replace("<", "").Replace(">", ""));
                    var roomID          = Int32.Parse(roomID_1.ToString() + roomID_2.ToString());
                    var groupController = separatedBlock[0].Substring(startIndex + 8, 2);
                    var group           = separatedBlock[0].Substring(startIndex + 10, 1);
                    var roomName        = separatedBlock[0].Substring(startIndex + 11, 4);

                    int status = -1;
                    try
                    {
                        status = Int32.Parse(separatedBlock[0].Substring(startIndex + 15, 4).Replace("<", "").Replace(">", ""));
                    }
                    catch (Exception)
                    {
                        //TODO: Sometimes here breaks!!! TEST IT
                    }

                    if (status != -1 && status != (int)RoomStatuses.sb)
                    {
                        String suplementData   = null;
                        String additionalData  = null;
                        String roomDescription = null;

                        try
                        {
                            suplementData = separatedBlock[0].Substring(startIndex + 19, 2);
                        }
                        catch (Exception)
                        {
                            //just continue the process of data processing
                        }

                        //If length of data is greater than 21, that mean that we have some additional data
                        if (separatedBlock[0].Length > startIndex + 21)
                        {
                            additionalData = separatedBlock[0].Substring(startIndex + 21, separatedBlock[0].Length - (startIndex + 21));
                        }

                        if (separatedBlock.Length > 1)
                        {
                            roomDescription = separatedBlock[1];
                        }

                        if (status != (int)RoomStatuses.aw && status != (int)RoomStatuses.aw2)
                        {
                            int         enteredID   = EnterData(roomID, groupController, Int32.Parse(group), roomName, status, suplementData, additionalData, roomDescription);
                            SendingData sendingData = new SendingData
                            {
                                roomID          = roomID,
                                roomName        = roomName,
                                statusID        = status,
                                suplementData   = suplementData,
                                roomDescription = roomDescription,
                                date            = DateTime.Now
                            };
                            //If data was already sent, don't send again.
                            if (enteredID == 0)
                            {
                                LoadAllActiveDevicesAndSendNotification(Int32.Parse(group), new List <SendingData> {
                                    sendingData
                                }, true, null);
                            }
                        }
                        else if (status == (int)RoomStatuses.aw)
                        {
                            LoadCurrentStateAndSendNotification(groupController, Int32.Parse(group));
                        }
                        else if (status == (int)RoomStatuses.aw2)
                        {
                            //TODO: ? ... discuss about this!
                            DisableSendingToClientDevices(Int32.Parse(group), roomID, null);
                        }
                    }
                }
            }
        }