// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; using System.Net; public partial class DVB : System.Web.UI.Page { private Spludlow.Video.VideoData _VideoData = null; private DataTable _RecordSchedule = null; protected void Page_Load(object sender, EventArgs e) { Spludlow.WebControls.DisableOnClick(this.ButtonUpdate); Spludlow.WebControls.DisableOnClick(this.ButtonStop); if (this.IsPostBack == false) { string clientAddress = this.Request.UserHostAddress; if (clientAddress.Contains(":") == true) clientAddress = "127.0.0.1"; this.TextBoxClientAddress.Text = clientAddress; this.DropDownListMode.Items.Add(new ListItem("0 - Tuner Local", "0")); this.DropDownListMode.Items.Add(new ListItem("1 - Local Network UDP", "1")); this.DropDownListMode.Items.Add(new ListItem("2 - Local Network TCP", "2")); this.DropDownListScale.Items.Add(new ListItem("Original", "1.0")); this.DropDownListScale.Items.Add(new ListItem("90%", "0.9")); this.DropDownListScale.Items.Add(new ListItem("80%", "0.8")); this.DropDownListScale.Items.Add(new ListItem("70%", "0.7")); this.DropDownListScale.Items.Add(new ListItem("60%", "0.6")); this.DropDownListScale.Items.Add(new ListItem("50%", "0.5")); this.DropDownListScale.Items.Add(new ListItem("40%", "0.4")); this.DropDownListScale.Items.Add(new ListItem("30%", "0.3")); this.LiteralWindowStart.Text = DateTime.Now.ToString(); try { this._ShowData(); } catch (Exception ee) { this.LabelError.Text = "The DVB page requires setup: " + ee.Message; Spludlow.Log.Error("DVB Intranet Page: ", ee); } } } private void _ShowData() { this._VideoData = new Spludlow.Video.VideoData(); Spludlow.Video.Recorder recorder = new Spludlow.Video.Recorder(); this._RecordSchedule = recorder.Schedule; string tunerHost = Spludlow.Config.Get("VideoRecorder.TunerHost"); string edgeHost = ""; // Not working string publicAddress = ""; string clientAddress = this.TextBoxClientAddress.Text; this.Literal1.Text = tunerHost + "->" + edgeHost + "->" + clientAddress; DataTable channelsTable = this._VideoData.Channels.Copy(); DataTable programmeTable = this._VideoData.Programmes; channelsTable.Columns.Add("CommandLink", typeof(string)); channelsTable.Columns.Add("GuideChannelId", typeof(int)); channelsTable.Columns.Add("OnNow", typeof(string)); channelsTable.Columns.Add("CommandLinkHD", typeof(string)); channelsTable.Columns.Add("NameHD", typeof(string)); int nextCount = 3; for (int next = 0; next < nextCount; ++next) channelsTable.Columns.Add("OnNext" + next, typeof(string)); DateTime now = DateTime.Now; foreach (DataRow channelRow in channelsTable.Rows) { string name = (string)channelRow["Name"]; bool clientZoom = true; bool clientInterlace = true; if (name.EndsWith(" HD") == true) clientZoom = false; channelRow["CommandLink"] = this._MakeCommand(channelRow, tunerHost, edgeHost, clientAddress, publicAddress, clientZoom, clientInterlace); DataRow hdChannelRow = this._VideoData.HdEquivalent(name); if (hdChannelRow != null) { channelRow["NameHD"] = "HD"; channelRow["CommandLinkHD"] = this._MakeCommand(hdChannelRow, tunerHost, edgeHost, clientAddress, publicAddress, false, clientInterlace); } } DataView view = new DataView(channelsTable); view.Sort = "Number, Frequency, ServiceId"; this.GridViewChannels.DataSource = view; this.GridViewChannels.DataBind(); DataTable frquencyTable = Spludlow.Data.TextTable.ReadText(new string[] { "UHFChannel CentreFrequency AntennaCM Channels", "Int32* String Decimal String" }); int offset = 474; int step = 8; for (int uhfChan = 21; uhfChan <= 60; ++uhfChan) { int freq = offset + (uhfChan - 21) * step; string frequency = freq + " 000 000"; freq *= 1000; DataRow[] channels = channelsTable.Select("Frequency = " + freq); StringBuilder text = new StringBuilder(); foreach (DataRow row in channels) { if (text.Length > 0) text.Append(", "); text.Append((string)row["name"]); } freq *= 1000; decimal cm = Math.Round(((299792458.0M / freq) / 2.0M) * 100.0M, 0); frquencyTable.Rows.Add(new object[] { uhfChan, frequency, cm, text.ToString() }); } this.GridView1.DataSource = frquencyTable; this.GridView1.DataBind(); DataTable reportTable = Spludlow.Data.TextTable.ReadText(new string[] { "ChannelId Number Name Frequency ServiceId TotalDuration TotalCount", "Int32* Int32 String Int32 Int32 Int32 Int32", }); int importColumnCount = 5; List dates = new List(); foreach (DataRow progRow in programmeTable.Rows) { DateTime startDate = (DateTime)progRow["StartDate"]; startDate = startDate.Date; if (dates.Contains(startDate) == false) dates.Add(startDate); } dates.Sort(); foreach (DateTime date in dates) reportTable.Columns.Add(date.ToShortDateString(), typeof(int)); foreach (DataRow progRow in programmeTable.Rows) { int channelId = (int)progRow["ChannelId"]; DataRow reportRow = reportTable.Rows.Find(channelId); if (reportRow == null) { DataRow chanRow = channelsTable.Rows.Find(channelId); reportRow = reportTable.NewRow(); for (int colIndex = 0; colIndex < importColumnCount; ++colIndex) { string colName = reportTable.Columns[colIndex].ColumnName; reportRow[colName] = chanRow[colName]; } reportRow["TotalDuration"] = 0; reportRow["TotalCount"] = 0; reportTable.Rows.Add(reportRow); } DateTime startDate = (DateTime)progRow["StartDate"]; string columnName = startDate.Date.ToShortDateString(); if (reportRow.IsNull(columnName) == true) reportRow[columnName] = 0; reportRow[columnName] = (int)reportRow[columnName] + 1; reportRow["TotalDuration"] = (int)reportRow["TotalDuration"] + (int)progRow["Duration"]; reportRow["TotalCount"] = (int)reportRow["TotalCount"] + 1; } reportTable.PrimaryKey = null; reportTable.Columns.Remove("ChannelId"); view = new DataView(reportTable); view.Sort = "Number"; this.GridViewReport.DataSource = view; this.GridViewReport.DataBind(); } private string _MakeCommand(DataRow channelRow, string tunerHost, string edgeHost, string clientAddress, string publicAddress, bool clientZoom, bool clientInterlace) { int frequency = (int)channelRow["Frequency"] * 1000; int serviceId = (int)channelRow["ServiceId"]; string mime = "application/bat"; string filename = "DVB-T-" + frequency + "-" + serviceId + ".bat"; StringBuilder link = new StringBuilder(); link.Append("Generic.aspx?A=Spludlow&T=Spludlow.VLC&M=StartServer"); link.Append("&P0=" + frequency); link.Append("&P1=" + serviceId); link.Append("&P2=" + Int32.Parse(this.DropDownListMode.SelectedValue)); link.Append("&P3String=" + tunerHost); link.Append("&P4String=" + edgeHost); link.Append("&P5String=" + clientAddress); link.Append("&P6String=" + publicAddress); link.Append("&P7=" + clientZoom); link.Append("&P8=" + clientInterlace); link.Append("&P9=" + Decimal.Parse(this.DropDownListScale.SelectedValue)); link.Append("&mime="); link.Append(HttpUtility.UrlEncode(mime)); link.Append("&filename="); link.Append(HttpUtility.UrlEncode(filename)); return link.ToString(); } private static string TimeDescription(DateTime nowTime, DateTime startTime, DateTime endTime, int duration) { StringBuilder timeString = new StringBuilder(); if (nowTime > endTime) { timeString.Append("Finished "); timeString.Append(Spludlow.Text.TimeTook(nowTime - endTime)); timeString.Append(" ago."); } else { if (nowTime > startTime) { timeString.Append("On now, started "); timeString.Append(Spludlow.Text.TimeTook(nowTime - startTime)); timeString.Append(" ago, finishes in "); timeString.Append(Spludlow.Text.TimeTook(endTime - nowTime)); timeString.Append("."); } else { timeString.Append("Starts in "); timeString.Append(Spludlow.Text.TimeTook(startTime - nowTime)); timeString.Append("."); } } //timeString.Append(" Start Time: "); //timeString.Append(startTime.ToString()); timeString.Append(" Duration: "); timeString.Append(Spludlow.Text.TimeTook(TimeSpan.FromMinutes(duration))); return timeString.ToString(); } protected void ButtonStop_Click(object sender, EventArgs e) { string tunerHost = Spludlow.Config.Get("VideoRecorder.TunerHost"); Spludlow.VLC.Stop(tunerHost); this.LiteralWindowStart.Text = DateTime.Now.ToString(); this._ShowData(); } protected void ButtonUpdate_Click(object sender, EventArgs e) { this.LiteralWindowStart.Text = DateTime.Now.ToString(); this._ShowData(); } protected void GridViewChannels_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return; int minutesWidth = 3 * 60 + 15; double percentPerMinute = 100.0 / minutesWidth; DateTime windowStart = DateTime.Parse(this.LiteralWindowStart.Text); DateTime windowEnd = windowStart.AddMinutes(minutesWidth); DataRow chanRow = ((DataRowView)e.Row.DataItem).Row; int channelId = (int)chanRow["ChannelId"]; int number = (int)chanRow["Number"]; DataRow[] progRows = this._VideoData.Programmes.Select("ChannelId=" + channelId, "StartDate"); StringBuilder html = new StringBuilder(); html.Append(""); for (int pass = 0; pass < 2; ++pass) { html.Append(""); foreach (DataRow progRow in progRows) { DateTime startTime = (DateTime)progRow["StartDate"]; DateTime endTime = (DateTime)progRow["EndDate"]; long progId = (long)progRow["ProgramId"]; if ((endTime < windowStart) || (startTime > windowEnd)) continue; double duration = (endTime - startTime).TotalMinutes; string title = (string)progRow["Title"]; string description = (string)progRow["Description"]; string timeDesc = TimeDescription(DateTime.Now, startTime, endTime, (int)duration); if (startTime < windowStart) startTime = windowStart; if (endTime > windowEnd) endTime = windowEnd; duration = (endTime - startTime).TotalMinutes; double width = duration * percentPerMinute; if (pass == 0) { bool onNow = false; bool record = false; if (timeDesc.StartsWith("On now") == true) onNow = true; if (this._RecordSchedule != null && this._RecordSchedule.Rows.Find(new object[] { number, startTime }) != null) record = true; string colour = ""; if (onNow == true && record == true) // never happerns as scedule row removed when starts !!!! { colour = "background-color:yellow;"; } else { if (onNow == true) colour = "background-color:palegreen;"; if (record == true) colour = "background-color:pink;"; } html.Append(""); } else { html.Append(""); } } html.Append(""); } html.Append("
"); html.Append("
"); html.Append(""); html.Append(title); html.Append(""); html.Append("
"); html.Append(timeDesc); html.Append("
"); html.Append("
"); html.Append("
"); html.Append("
"); html.Append(description); html.Append("
"); html.Append("
"); Literal literal = (Literal)e.Row.FindControl("LiteralProgs"); literal.Text = html.ToString(); } protected void ButtonPrev_Click(object sender, EventArgs e) { this.LiteralWindowStart.Text = this._MoveTime(-3); this._ShowData(); } protected void ButtonNext_Click(object sender, EventArgs e) { this.LiteralWindowStart.Text = this._MoveTime(3); this._ShowData(); } private string _MoveTime(int hours) { DateTime time = DateTime.Parse(this.LiteralWindowStart.Text); time = time.AddHours(hours); time = new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0); return time.ToString(); } }