public void reload_table() { dataSet1.tmp_data.Clear(); string[] avg_pins = new string[8]; tooltip_lines = "Fire monitor\n"; for (int i = 0, pin = 1; i <= 7; i++, pin++) { int sensor_type = (int)dataSet1.Tables["sensors"].Rows[i]["type"]; if (sensor_type == 0) { avg_pins[i] = "0"; continue; } bool show_tray = (bool)dataSet1.Tables["sensors"].Rows[i]["tray"]; double warn = (double)dataSet1.Tables["sensors"].Rows[i]["warn"]; double alarm = (double)dataSet1.Tables["sensors"].Rows[i]["alarm"]; bool sounds = (bool)dataSet1.Tables["settings"].Rows[0]["sounds"]; string descr = dataSet1.Tables["sensors"].Rows[i]["descr"].ToString(); string sensor_cdef = dataSet1.Tables["sensor_types"].Rows[sensor_type]["cdef"].ToString(); string symbol = dataSet1.Tables["sensor_types"].Rows[sensor_type]["symbol"].ToString(); string cdef = dataSet1.Tables["sensors"].Rows[i]["cdef"].ToString(); string sensor_color = dataSet1.Tables["sensors"].Rows[i]["color"].ToString(); int avg_pin = 0; for (int s = 0; s < pins[i].Count; s++) { avg_pin += pins[i][s]; } if(pins[i].Count > 0){ avg_pin = avg_pin / pins[i].Count; } DataRow row = dataSet1.tmp_data.NewRow(); double pin_result = Convert.ToDouble(avg_pin); if (cdef.Length > 0) { pin_result = calculate_cdef(pin_result, cdef); } if (sensor_cdef.Length > 0) { pin_result = calculate_cdef(pin_result, sensor_cdef); } Bitmap color_rect = new Bitmap(16, 16); Color bgcolor = System.Drawing.ColorTranslator.FromHtml(sensor_color); Color bordercolor = ContrastColor(bgcolor); SolidBrush bru = new SolidBrush(bgcolor); Graphics graphicss = Graphics.FromImage(color_rect); graphicss.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; graphicss.FillRectangle(bru, 0, 0, 16, 16); try { byte[] color_img = new byte[0]; MemoryStream mss = new MemoryStream(); color_rect.Save(mss, System.Drawing.Imaging.ImageFormat.Gif); color_img = mss.ToArray(); row["color"] = color_img; } catch (Exception) { //toolStripStatusLabel2.Text = "Грешка " + e; } pin_result = Math.Round(pin_result, 1); if (sensor_type == 1 || sensor_type == 2) { byte[] img = new byte[0]; MemoryStream ms = new MemoryStream(); Properties.Resources.temperature.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); img = ms.ToArray(); row["image"] = img; row["sensor"] = descr; dataSet1.tmp_data.Rows.Add(row); if (show_tray) { Bitmap bitmap = new Bitmap(16, 16); SolidBrush brush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(sensor_color)); Graphics graphics = Graphics.FromImage(bitmap); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; ; graphics.DrawString(Math.Round(pin_result).ToString(), new Font("Tahoma", 8, FontStyle.Regular), brush, 0, 0); IntPtr hIcon = new IntPtr(); try { hIcon = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hIcon); pin_icons[i].Icon = icon; if (!pin_icons[i].Visible) { pin_icons[i].Visible = true; } } catch (Exception) { //toolStripStatusLabel2.Text = "Грешка " + e; } } row["result"] = pin_result.ToString() + symbol; tooltip_lines += row["sensor"] + ": " + row["result"] + "\n"; if (pin_icons[i] != null) pin_icons[i].Text = row["sensor"] + ": " + row["result"]; if (pin_result >= warn && pin_result < alarm && pin_warnings[i]) { show_baloon(false, descr + ": Температура " + row["result"], show_tray, i); pin_warnings[i] = false; } else if (pin_result >= alarm && pin_alarms[i]) { show_baloon(true, descr + ": Температура " + row["result"], show_tray, i); if (show_popups) { Form4 f4 = new Form4(); f4.Descr = descr; f4.Sound = sounds; f4.Value = pin_result.ToString(); f4.Show(); } pin_alarms[i] = false; } if (!pin_warnings[i] && pin_result < warn - hist) { pin_warnings[i] = true; } if (!pin_alarms[i] && pin_result < alarm - hist) { Form4 f4 = new Form4(); f4.Close(); pin_alarms[i] = true; } avg_pins[i] = avg_pin.ToString(); } else if (sensor_type == 3) { byte[] img = new byte[0]; MemoryStream ms = new MemoryStream(); Properties.Resources.hum_old.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); img = ms.ToArray(); row["image"] = img; row["sensor"] = descr; row["result"] = pin_result.ToString() + symbol; dataSet1.tmp_data.Rows.Add(row); if (pin_icons[i] != null) pin_icons[i].Text = row["sensor"] + ": " + row["result"]; if (show_tray) { Bitmap bitmap = new Bitmap(16, 16); SolidBrush brush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(sensor_color)); Graphics graphics = Graphics.FromImage(bitmap); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; ; graphics.DrawString(Math.Round(pin_result).ToString(), new Font("Tahoma", 8, FontStyle.Regular), brush, 0, 0); IntPtr hIcon = new IntPtr(); try { hIcon = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hIcon); pin_icons[i].Icon = icon; if (!pin_icons[i].Visible) { pin_icons[i].Visible = true; } } catch (Exception) { //toolStripStatusLabel2.Text = "Грешка " + e; } pin_icons[i].Text = row["sensor"] + ": " + row["result"]; } if (pin_result >= warn && pin_result < alarm && pin_warnings[i]) { show_baloon(false, descr + ": Влажност " + row["result"], show_tray, i); pin_warnings[i] = false; } else if (pin_result >= alarm && pin_alarms[i]) { if (show_popups) { Form4 f4 = new Form4(); f4.Descr = descr; f4.Sound = sounds; f4.Value = pin_result.ToString(); f4.Show(); } pin_alarms[i] = false; } if (!pin_warnings[i] && pin_result < warn - hist) { pin_warnings[i] = true; } if (!pin_alarms[i] && pin_result < alarm - hist) { Form4 f4 = new Form4(); f4.Close(); pin_alarms[i] = true; } avg_pins[i] = avg_pin.ToString(); } else if (sensor_type == 5) { byte[] img = new byte[0]; MemoryStream ms = new MemoryStream(); Properties.Resources.icon_battery.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); img = ms.ToArray(); row["image"] = img; row["sensor"] = descr; row["result"] = pin_result.ToString() + symbol; dataSet1.tmp_data.Rows.Add(row); if (pin_icons[i] != null) pin_icons[i].Text = row["sensor"] + ": " + row["result"]; if (show_tray) { Bitmap bitmap = new Bitmap(16, 16); SolidBrush brush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(sensor_color)); Graphics graphics = Graphics.FromImage(bitmap); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; brush.Color = Color.Orange; graphics.DrawString(Math.Round(pin_result).ToString(), new Font("Tahoma", 8, FontStyle.Regular), brush, 0, 0); IntPtr hIcon = new IntPtr(); try { hIcon = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hIcon); pin_icons[i].Icon = icon; if (!pin_icons[i].Visible) { pin_icons[i].Visible = true; } } catch (Exception) { //toolStripStatusLabel2.Text = "Грешка " + e; } pin_icons[i].Text = row["sensor"] + ": " + row["result"]; } avg_pins[i] = avg_pin.ToString(); } else if (sensor_type == 4) { if (show_tray) { Bitmap bitmap = new Bitmap(16, 16); SolidBrush brush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(sensor_color)); Graphics graphics = Graphics.FromImage(bitmap); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; ; Font fnt = new Font("tahoma", 8, FontStyle.Regular); if (pins[i].Count > 0) { if (pins[i][pins[i].Count - 1] >= 1000) { graphics.DrawString("On", fnt, brush, 0, 0); } else { graphics.DrawString("Off", fnt, brush, 0, 0); } } IntPtr hIcon = new IntPtr(); try { hIcon = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hIcon); pin_icons[i].Icon = icon; if (!pin_icons[i].Visible) { pin_icons[i].Visible = true; } } catch (Exception) { //toolStripStatusLabel2.Text = "Грешка " + e; } } byte[] img = new byte[0]; MemoryStream ms = new MemoryStream(); if (pins[i].Count > 0) { if (pins[i][pins[i].Count - 1] >= 1000) { Properties.Resources.switch_on.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); row["result"] = "Работи"; avg_pins[i] = "10"; } else { Properties.Resources.switch_off.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); row["result"] = "Не работи"; avg_pins[i] = "0"; } } row["sensor"] = descr; tooltip_lines += row["sensor"] + ": " + row["result"] + "\n"; img = ms.ToArray(); row["image"] = img; if (pin_icons[i] != null) pin_icons[i].Text = row["sensor"] + ": " + row["result"]; dataSet1.tmp_data.Rows.Add(row); } else { avg_pins[i] = "0"; } } //notifyIcon1.Text = tooltip_lines.Substring(0, tooltip_lines.Length - 1 run = 0; tmpdataBindingSource.DataSource = dataSet1.tmp_data; rrdtool rt = new rrdtool(); rt.setExe(Application.StartupPath + "\\rrdtool.exe"); if (!rt.rrdExsists(Application.StartupPath + "\\sensors.rrd")) { string command = "--start N "; command += "--step=20 "; command += "DS:pin1:GAUGE:80:-1000:U "; command += "DS:pin2:GAUGE:80:-1000:U "; command += "DS:pin3:GAUGE:80:-1000:U "; command += "DS:pin4:GAUGE:80:-1000:U "; command += "DS:pin5:GAUGE:80:-1000:U "; command += "DS:pin6:GAUGE:80:-1000:U "; command += "DS:pin7:GAUGE:80:-1000:U "; command += "DS:pin8:GAUGE:80:-1000:U "; command += "RRA:AVERAGE:0.5:1:60540 "; command += "RRA:AVERAGE:0.5:3:43800 "; command += "RRA:AVERAGE:0.5:15:52560 "; command += "RRA:AVERAGE:0.5:30:52596 "; command += "RRA:AVERAGE:0.5:180:43830 "; rt.create(Application.StartupPath + "\\sensors.rrd", command); } else { string command = "N:" + avg_pins[0] + ":" + avg_pins[1] + ":" + avg_pins[2] + ":" + avg_pins[3] + ":" + avg_pins[4] + ":" + avg_pins[5] + ":" + avg_pins[6] + ":" + avg_pins[7]; rt.update(Application.StartupPath + "\\sensors.rrd", command); } bool dtcheck = dateTimePicker2.Checked; if (!dtcheck) { dateTimePicker2.Enabled = false; dateTimePicker2.Value = DateTime.Now; dateTimePicker2.Checked = dtcheck; dateTimePicker2.Enabled = true; } bool dtcheck1 = dateTimePicker1.Checked; if (!dtcheck1) { dateTimePicker1.Enabled = false; dateTimePicker1.Value = DateTime.Now.AddHours(-2.0); dateTimePicker1.Checked = dtcheck1; dateTimePicker1.Enabled = true; } }
public void draw_graph() { rrdtool rt = new rrdtool(); rt.setExe(Application.StartupPath + "\\rrdtool.exe"); string graph_command = "--imgformat=PNG "; if (!dateTimePicker1.Checked) { graph_command += "--start=-7200 "; } else { int unixTime = (int)(dateTimePicker1.Value.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds; graph_command += "--start=" + unixTime.ToString() + " "; } if (!dateTimePicker2.Checked) { graph_command += "--end=N "; } else { int unixTime = (int)(dateTimePicker2.Value.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds; graph_command += "--end=" + unixTime.ToString() + " "; } int graph_width = pictureBox1.Width - 68; int graph_height = pictureBox1.Height - 61; graph_command += "--height=" + graph_height.ToString() + " "; graph_command += "--width=" + graph_width.ToString() + " "; if (rigid_graph) { graph_command += "--rigid "; graph_command += "--upper-limit=" + upper_limit + " "; graph_command += "--lower-limit=" + lower_limit + " "; } else { graph_command += "--alt-autoscale-max "; graph_command += "--lower-limit=0 "; } graph_command += "--slope-mode "; graph_command += "--no-gridfit "; graph_command += "--font DEFAULT:8:\"" + Application.StartupPath.Replace("\\", "/").ToLower() + "/consola.ttf\" "; graph_command += "--color BACK#" + string.Format("{0:X2}{1:X2}{2:X2}", System.Drawing.SystemColors.Control.R, System.Drawing.SystemColors.Control.G, System.Drawing.SystemColors.Control.B) + " "; graph_command += "--color FONT#" + string.Format("{0:X2}{1:X2}{2:X2}", System.Drawing.SystemColors.ControlText.R, System.Drawing.SystemColors.ControlText.G, System.Drawing.SystemColors.ControlText.B) + " "; graph_command += "--color SHADEA#" + string.Format("{0:X2}{1:X2}{2:X2}", System.Drawing.SystemColors.Control.R, System.Drawing.SystemColors.Control.G, System.Drawing.SystemColors.Control.B) + " "; graph_command += "--color SHADEB#" + string.Format("{0:X2}{1:X2}{2:X2}", System.Drawing.SystemColors.Control.R, System.Drawing.SystemColors.Control.G, System.Drawing.SystemColors.Control.B) + " "; for (int i = 0, pin = 1; i <= 7; i++, pin++) { if (Convert.ToInt32(dataSet1.Tables["sensors"].Rows[i]["type"]) > 0 && Convert.ToBoolean(dataSet1.Tables["sensors"].Rows[i]["graphing"])) { string defname = ""; switch (i) { case 0: defname = "a"; break; case 1: defname = "b"; break; case 2: defname = "c"; break; case 3: defname = "d"; break; case 4: defname = "e"; break; case 5: defname = "f"; break; case 6: defname = "g"; break; case 7: defname = "h"; break; } int sensor_type = Convert.ToInt32(dataSet1.Tables["sensors"].Rows[i]["type"]); string sensor_cdef = dataSet1.Tables["sensor_types"].Rows[sensor_type]["cdef"].ToString(); string extra_cdef = dataSet1.Tables["sensors"].Rows[i]["cdef"].ToString(); int line_area = Convert.ToInt32(dataSet1.Tables["settings"].Rows[0]["line_area"]); string line_area_string; graph_command += "DEF:" + defname + "=\"" + Application.StartupPath.Replace("\\", "/").Replace(":", "\\:").ToLower() + "/sensors.rrd\":pin" + pin + ":AVERAGE "; sensor_cdef = (sensor_cdef == "") ? "" : "," + sensor_cdef; extra_cdef = (extra_cdef == "") ? "" : "," + extra_cdef; graph_command += "CDEF:cdef" + defname + "=" + defname + extra_cdef + sensor_cdef + " "; graph_command += "LINE" + dataSet1.Tables["settings"].Rows[0]["line_width"].ToString() + ":cdef" + defname + dataSet1.Tables["sensors"].Rows[i]["color"] + " "; if (line_area > 0) { if(line_area < 10) { line_area_string = "0" + line_area.ToString(); }else { line_area_string = line_area.ToString(); } graph_command += "AREA:cdef" + defname + dataSet1.Tables["sensors"].Rows[i]["color"].ToString() + line_area_string + " "; } } } rt.graph(Application.StartupPath + "\\graph.png", graph_command); if (File.Exists(Application.StartupPath + "\\graph.png")) { FileStream fs = new FileStream(Application.StartupPath + "\\graph.png", FileMode.Open, FileAccess.Read); Image graph_img = Image.FromStream(fs); fs.Close(); pictureBox1.Image = graph_img; } pictureBox1.Refresh(); }