Beispiel #1
0
        private string Store(string d, string p, int t, string c)
        {
            string result = "0";
            var headers = String.Empty;
            foreach (var key in Request.Headers.AllKeys)
                headers += key + "=" + Request.Headers[key] + Environment.NewLine;
            headers += "UserHostName=" + Request.UserHostName + Environment.NewLine;
            headers += "UserHostAddress=" + Request.UserHostAddress + Environment.NewLine;

            string user_IP;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null)
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            else
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

            headers += "ClientIP=" + user_IP + Environment.NewLine;

            DateTime now = DateTime.Now;
            DeviceLog devicelog;
            long deviceID = -1;

            Device device = db.Devices.FirstOrDefault(i => i.DeviceCode == d && i.PassCode == p);
            if (device != null)
                deviceID = device.ID;

            // Special handling for screenshot, always store as file because of its size
            if (t == (int)LogTypeEnum.Screenshot)
            {
                devicelog = new DeviceLog()
                {
                    DeviceID = deviceID,
                    DeviceCode = d,
                    LogType = t,
                    Message = "(tmp)",
                    Request = headers,
                    CreationDate = now
                };
                logdb.DeviceLogs.Add(devicelog);

                logdb.SaveChanges(); // Need to obtain devicelog.ID first

                devicelog.Message = c;
                ArchiveTextFile(devicelog);
            }
            else
            {
                devicelog = new DeviceLog()
                {
                    DeviceID = deviceID,
                    DeviceCode = d,
                    LogType = t,
                    Message = c,
                    Request = headers,
                    CreationDate = now
                };
                logdb.DeviceLogs.Add(devicelog);

                logdb.SaveChanges();
            }

            #region Record Statistics
            // Record statistics
            bool check50Run = false;
            bool parseRunIndex = false;
            if (device != null)
            {
                switch (t)
                {
                    case (int)LogTypeEnum.Screenshot:
                        device.Screenshot = c;
                        device.ScreenshotTime = now;
                        break;
                    case (int)LogTypeEnum.TaskerVer:
                        device.TaskerVersion = c;
                        device.TaskerVersionTime = now;
                        break;
                    case (int)LogTypeEnum.RunMinute:
                        #region RunMinute
                        DateTime? lastRunMinute = device.LastRunMinute;

                        device.LastRunMinute = now;

                        bool dayChanged = true;
                        bool monthChanged = true;
                        bool yearChanged = true;
                        if (lastRunMinute.HasValue)
                        {
                            dayChanged = (lastRunMinute.Value.Day != now.Day);
                            monthChanged = (lastRunMinute.Value.Month != now.Month);
                            yearChanged = (lastRunMinute.Value.Year != now.Year);
                        }

                        DateTime nowTime = new DateTime(1900, 1, 1, now.Hour, now.Minute, now.Second);

                        if (dayChanged)
                        {
                            device.YesterdayEarliestRunMinute = device.TodayEarliestRunMinute;
                            device.TodayEarliestRunMinute = now;
                            device.YesterdayLatestRunMinute = lastRunMinute;

                            if (device.ThisMonthEarliestRunMinute.HasValue == false ||
                                new DateTime(1900, 1, 1, device.ThisMonthEarliestRunMinute.Value.Hour, device.ThisMonthEarliestRunMinute.Value.Minute, device.ThisMonthEarliestRunMinute.Value.Second) > nowTime)
                                device.ThisMonthEarliestRunMinute = now;

                            if (device.ThisMonthLatestRunMinute.HasValue == false ||
                                new DateTime(1900, 1, 1, device.ThisMonthLatestRunMinute.Value.Hour, device.ThisMonthLatestRunMinute.Value.Minute, device.ThisMonthLatestRunMinute.Value.Second) < device.YesterdayLatestRunMinute)
                                device.ThisMonthLatestRunMinute = device.YesterdayLatestRunMinute;

                            if (device.TodayRunMinuteCount.HasValue)
                                device.YesterdayRunMinuteCount = device.TodayRunMinuteCount;

                            device.TodayRunMinuteCount = 1;

                            if (device.TodayShowRate.HasValue)
                            {
                                List<string> pastShowRate = new List<string>(30);
                                if (device.Last30DaysShowRate != null)
                                    pastShowRate.AddRange(device.Last30DaysShowRate.Split(new char[] { ',' }));
                                pastShowRate.Add(device.TodayShowRate.ToString());
                                while (pastShowRate.Count > 30)
                                    pastShowRate.RemoveAt(0);

                                device.Last30DaysShowRate = string.Join(",", pastShowRate.ToArray());
                            }

                            device.TodayShowRate = 100;
                        }
                        else
                        {
                            device.TodayRunMinuteCount++;

                            int expectedRunMinutes = Convert.ToInt32(Math.Floor(now.Subtract(device.TodayEarliestRunMinute.Value).TotalMinutes)); // Assume a request every 1 minutes
                            if (expectedRunMinutes == 0)
                                expectedRunMinutes = 1;
                            device.TodayShowRate = Convert.ToInt32(Math.Floor((double)(device.TodayRunMinuteCount.Value * 100 / expectedRunMinutes)));
                        }

                        if (monthChanged)
                        {
                            device.ThisMonthEarliestRunMinute = now;
                            device.ThisMonthLatestRunMinute = null;

                            if (device.ThisMonthRunMinuteCount.HasValue)
                                device.LastMonthRunMinuteCount = device.ThisMonthRunMinuteCount;

                            device.ThisMonthRunMinuteCount = 1;
                        }
                        else
                        {
                            device.ThisMonthRunMinuteCount++;
                        }

                        if (yearChanged)
                        {
                            device.ThisYearRunMinuteCount = 1;
                        }
                        else
                        {
                            device.ThisYearRunMinuteCount++;
                        }
                        #endregion RunMinute

                        parseRunIndex = true;
                        break;
                    case (int)LogTypeEnum.RunChannel:
                        device.LastRunChannelTime = now;

                        check50Run = true;
                        parseRunIndex = true;
                        break;
                    case (int)LogTypeEnum.RunCheckUpdate:
                        device.LastRunCheckUpdateTime = now;

                        parseRunIndex = true;
                        break;
                    case (int)LogTypeEnum.RunCompleteUpdate:
                        device.LastRunCompleteUpdateTime = now;

                        parseRunIndex = true;
                        break;
                    case (int)LogTypeEnum.CommandExecuted:
                        device.LastCommandExecuted = c;
                        device.LastCommandExecuteTime = now;
                        break;
                };

                #region Parse Run index
                if (parseRunIndex)
                {
                    int runIndex = -1;
                    int i = c.IndexOf("r,");
                    if (i >= 0)
                    {
                        i += 2;
                        int j = c.IndexOf(';');
                        if (j >= 0)
                            int.TryParse(c.Substring(i, j - i), out runIndex);
                        else
                            int.TryParse(c.Substring(i), out runIndex);
                    }
                    if (runIndex != -1)
                    {
                        if (check50Run && device.LastRunIndex.HasValue && runIndex == 1)
                        {
                            // Run index just reset, store history
                            List<string> pastRun = new List<string>(50);
                            if (device.Last50Run != null)
                                pastRun.AddRange(device.Last50Run.Split(new char[] { ',' }));
                            pastRun.Add(device.LastRunIndex.ToString());
                            while (pastRun.Count > 50)
                                pastRun.RemoveAt(0);

                            device.Last50Run = string.Join(",", pastRun.ToArray());
                        }
                        device.LastRunIndex = runIndex;
                        device.LastRunIndexTime = now;
                    }
                }
                #endregion

                device.LastLogTime = now;
                device.LastLogType = t;
                device.LastLogId = devicelog.ID;

                db.Entry(device).State = EntityState.Modified;
                db.SaveChanges();
            }
            #endregion Record Statistics

            result = "1";

            return result;
        }
Beispiel #2
0
        private void ArchiveTextFile(DeviceLog devicelog)
        {
            string decoded = Server.UrlDecode(devicelog.Message);

            string file = string.Format("~/App_Data/Archive/{0}-{1}-{2}/{3}/{4}-{5}.txt", devicelog.CreationDate.Year, devicelog.CreationDate.Month, devicelog.CreationDate.Day, devicelog.CreationDate.Hour, devicelog.DeviceID, devicelog.ID);
            string filePath = Request.MapPath(file);

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath));
            System.IO.File.WriteAllText(filePath, decoded);

            devicelog.Message = file;
            devicelog.Processed = 900; // Archived

            logdb.Entry(devicelog).State = EntityState.Modified;
            logdb.SaveChanges();
        }