private void button6_Click(object sender, EventArgs e) { if (ddlScheduleCommand.SelectedIndex > -1 && ddlScheduleCommand.SelectedIndex > -1) { var cmd = ddlScheduleCommand.SelectedItem.ToString(); var time = dtpSchedulePTZ.Value; var s = new objectsCameraPtzscheduleEntry { command = cmd, time = time }; List <objectsCameraPtzscheduleEntry> scheds = CameraControl.Camobject.ptzschedule.entries.ToList(); scheds.Add(s); CameraControl.Camobject.ptzschedule.entries = scheds.ToArray(); ShowPTZSchedule(); } }
private void button6_Click(object sender, EventArgs e) { if (ddlScheduleCommand.SelectedIndex > -1) { if (ddlScheduleCommand.SelectedIndex > -1) { var cmd = ddlScheduleCommand.SelectedItem.ToString(); var time = dtpSchedulePTZ.Value; var s = new objectsCameraPtzscheduleEntry { command = cmd, time = time }; List<objectsCameraPtzscheduleEntry> scheds = CameraControl.Camobject.ptzschedule.entries.ToList(); scheds.Add(s); CameraControl.Camobject.ptzschedule.entries = scheds.ToArray(); ShowPTZSchedule(); } } }
private void SaveJson(String sPhysicalFilePath, string sHttpVersion, string sBuffer, ref HttpRequest req) { string resp = ""; int ot, oid; int.TryParse(GetVar(sPhysicalFilePath, "oid"), out oid); int.TryParse(GetVar(sPhysicalFilePath, "ot"), out ot); bool saveObjects = true; try { var d = getJSONObject(sBuffer); if (d == null) return; String n = d.name; resp = "{\"actionResult\":\"ok\"}"; bool apply = false; string nl = n.ToLowerInvariant(); switch (nl) { default: { resp = "{\"error\":\"" + nl + " not recognized\"}"; saveObjects = false; } break; case "liveupdate": { switch (ot) { case 1: { var c = MainForm.Microphones.FirstOrDefault(p => p.id == oid); if (c != null) { PopulateObject(d, c); } } break; case 2: { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { PopulateObject(d, c); } } break; } apply = true; saveObjects = false; } break; case "editpelco": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { PopulateObject(d, c); } } break; case "settings": { PopulateObject(d, MainForm.Conf); if (!string.IsNullOrEmpty(MainForm.Conf.Archive)) { MainForm.Conf.Archive = MainForm.Conf.Archive.Replace("/", @"\"); if (!MainForm.Conf.Archive.EndsWith(@"\")) MainForm.Conf.Archive += @"\"; } ReloadAllowedIPs(); ReloadAllowedReferrers(); MainForm.SaveConfig(); saveObjects = false; } break; case "editftpserver": { resp = "{\"actionResult\":\"reloadFTPServers\"}"; if (d.ident == "new") { d.ident = Guid.NewGuid().ToString(); var cfgs = new configurationServer {ident = d.ident}; var l = MainForm.Conf.FTPServers.ToList(); l.Add(cfgs); MainForm.Conf.FTPServers = l.ToArray(); } PopulateObject(d, MainForm.Conf.FTPServers.First(p => p.ident == d.ident.ToString())); MainForm.SaveConfig(); saveObjects = false; } break; case "editstorage": { resp = "{\"actionResult\":\"reloadStorage\"}"; if (d.ident.ToString() == "new") { d.ident = MainForm.Conf.MediaDirectories.Max(p => p.ID) + 1; } int idnew = Convert.ToInt32(d.ident); d.ident = idnew; var md = MainForm.Conf.MediaDirectories.FirstOrDefault(p => p.ID == idnew); bool ndir = false; if (md == null) { md = new configurationDirectory {ID = d.ident, Entry = ""}; ndir = true; } var exdir = md.Entry; PopulateObject(d, md); md.Entry = md.Entry.Replace("/", @"\"); if (!md.Entry.EndsWith(@"\")) md.Entry += @"\"; try { if (!Directory.Exists(md.Entry)) { throw new Exception("Invalid Directory"); } } catch (Exception ex) { if (exdir != "") md.Entry = exdir; resp = "{\"actionResult\":\"reloadStorage\",\"error\":\"" + ex.Message.JsonSafe() + "\"}"; break; } if (ndir) { var l = MainForm.Conf.MediaDirectories.ToList(); l.Add(md); MainForm.Conf.MediaDirectories = l.ToArray(); } else { var di = new DirectoryInfo(exdir); var di2 = new DirectoryInfo(md.Entry); if (di.ToString() != di2.ToString()) { var t = new Thread(() => Helper.CopyFolder(exdir, md.Entry)) {IsBackground = true}; t.Start(); resp = "{\"actionResult\":\"reloadStorage\",\"message\":\"Media is being copied to the new location.\"}"; } } MainForm.SaveConfig(); saveObjects = false; } break; case "editcamera": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { var olddir = c.directory; PopulateObject(d, c); apply = true; var newdir = c.directory; if (olddir.ToLowerInvariant() != newdir.ToLowerInvariant()) { if (!Helper.IsAlphaNumeric(newdir)) { c.directory = olddir; resp = "{\"error\":\"Directory invalid. Should be alphanumeric eg: ABCD.\"}"; } else { var fullolddir = Helper.GetMediaDirectory(2, c.id) + "video\\" + olddir + "\\"; var fullnewdir = Helper.GetMediaDirectory(2, c.id) + "video\\" + newdir + "\\"; try { Directory.Move(fullolddir, fullnewdir); } catch (Exception ex) { c.directory = olddir; resp = "{\"error\":\""+ex.Message.JsonSafe()+"\"}"; } } } MainForm.InstanceReference.SaveObjectList(false); } } break; case "motionzones": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { var lz = new List<objectsCameraDetectorZone>(); if (d.zones != null) { foreach (var z in d.zones) { var x = Convert.ToInt32(z["x"].Value); var y = Convert.ToInt32(z["y"].Value); var w = Convert.ToInt32(z["w"].Value); var h = Convert.ToInt32(z["h"].Value); lz.Add(new objectsCameraDetectorZone { height = h, left = x, top = y, width = w }); } } c.detector.motionzones = lz.ToArray(); var cw = MainForm.InstanceReference.GetCameraWindow(oid); cw?.Camera?.SetMotionZones(lz.ToArray()); } saveObjects = false; } break; case "screenarea": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { if (d.zones != null) { var z = d.zones[0]; int si; int.TryParse(c.settings.videosourcestring, out si); if (Screen.AllScreens.Length <= si) si = 0; var s = Screen.AllScreens[si]; var x = Convert.ToInt32((Convert.ToDecimal(z["x"].Value) / 100) * s.WorkingArea.Width); var y = Convert.ToInt32((Convert.ToDecimal(z["y"].Value) / 100) * s.WorkingArea.Height); var w = Convert.ToInt32((Convert.ToDecimal(z["w"].Value) / 100) * s.WorkingArea.Width); var h = Convert.ToInt32((Convert.ToDecimal(z["h"].Value) / 100) * s.WorkingArea.Height); //even height and width if (w % 2 != 0) w -= 1; if (h % 2 != 0) h -= 1; c.settings.desktoparea = x + "," + y + "," + w + "," + h; } else c.settings.desktoparea = ""; } saveObjects = false; } break; case "pip": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { string cfg = ""; if (d.zones != null) { foreach (var z in d.zones) { var id = Convert.ToInt32(z["id"].Value); var x = Convert.ToInt32(z["x"].Value); var y = Convert.ToInt32(z["y"].Value); var w = Convert.ToInt32(z["w"].Value); var h = Convert.ToInt32(z["h"].Value); cfg += id + "," + x + "," + y + "," + w + "," + h + "|"; } } c.settings.pip.config = cfg.Trim('|'); var cw = MainForm.InstanceReference.GetCameraWindow(oid); if (cw?.Camera != null) { cw.Camera.PiPConfig = c.settings.pip.config; } } saveObjects = false; } break; case "editmicrophone": { var c = MainForm.Microphones.FirstOrDefault(p => p.id == oid); if (c != null) { var olddir = c.directory; PopulateObject(d, c); apply = true; var newdir = c.directory; if (olddir.ToLowerInvariant() != newdir.ToLowerInvariant()) { if (!Helper.IsAlphaNumeric(newdir)) { c.directory = olddir; resp = "{\"error\":\"Directory invalid. Should be alphanumeric eg: ABCD.\"}"; } else { var fullolddir = Helper.GetMediaDirectory(2, c.id) + "video\\" + olddir + "\\"; var fullnewdir = Helper.GetMediaDirectory(2, c.id) + "video\\" + newdir + "\\"; try { Directory.Move(fullolddir, fullnewdir); } catch (Exception ex) { c.directory = olddir; resp = "{\"error\":\"" + ex.Message.JsonSafe() + "\"}"; } } } MainForm.InstanceReference.SaveObjectList(false); } } break; case "editschedule": { resp = "{\"actionResult\":\"reloadSchedule\"}"; objectsScheduleEntry s = null; if (d.ident.ToString() == "new") { s = new objectsScheduleEntry { active = true, time = 0, daysofweek = "", objectid = oid, objecttypeid = ot, parameter = "", typeid = 0 }; MainForm.Schedule.Add(s); } else { var l = MainForm.Schedule.Where(p => p.objectid == oid && p.objecttypeid == ot).ToList(); int i = Convert.ToInt32(d.ident); if (i < l.Count) { s = l[i]; } } if (s != null) { PopulateObject(d, s); } } break; case "editptzschedule": { resp = "{\"actionResult\":\"reloadPTZSchedule\"}"; var oc = MainForm.Cameras.First(p => p.id == oid); objectsCameraPtzscheduleEntry pe; if (d.ident.ToString() == "new") { pe = new objectsCameraPtzscheduleEntry { command = "", time = new DateTime() }; PopulateObject(d, pe); var l = oc.ptzschedule.entries.ToList(); l.Add(pe); oc.ptzschedule.entries = l.ToArray(); } else { int i = Convert.ToInt32(d.ident); var l = oc.ptzschedule.entries.ToList(); if (i < l.Count) { pe = l[i]; PopulateObject(d, pe); } } } break; case "editaction": resp = "{\"actionResult\":\"reloadActions\"}"; objectsActionsEntry a; if (d.ident.ToString() == "new") { a = new objectsActionsEntry { type = "", mode = "", active = true, objectid = oid, objecttypeid = ot, ident = Guid.NewGuid().ToString() }; MainForm.Actions.Add(a); resp = resp.Replace("PARAMS", ""); } else { a = MainForm.Actions.FirstOrDefault(p => p.ident == d.ident.ToString()); } if (a != null) { var p1 = a.param1; PopulateObject(d, a); if (Helper.WebRestrictedAlertTypes.Contains(a.type)) { a.param1 = a.param1.Replace("/", "\\"); if (a.param1.IndexOf("\\", StringComparison.Ordinal)!=-1) { resp = "{\"error\":\"Only filenames (not paths) are allowed in actions via the website\"}"; a.param1 = p1; } } MainForm.InstanceReference.SaveObjectList(false); } break; case "editaudiosource": { var c = MainForm.Microphones.FirstOrDefault(p => p.id == oid); if (c != null) { PopulateObject(d, c); var vl = MainForm.InstanceReference.GetVolumeLevel(c.id); if (vl != null) { vl.Disable(); vl.Enable(); } } resp = "{\"actionResult\":\"waiteditobject\"}"; } break; case "editvideosource": { var c = MainForm.Cameras.FirstOrDefault(p => p.id == oid); if (c != null) { PopulateObject(d, c); if (c.settings.sourceindex == 9) { string url = c.settings.onvifident; Uri u; if (!Uri.TryCreate(url, UriKind.Absolute, out u)) { resp = "{\"error\":\"URI invalid\"}"; break; } string addr = u.DnsSafeHost; var dev = MainForm.ONVIFDevices.FirstOrDefault(p => p.Address == addr); if (dev == null) { if (NV(c.settings.namevaluesettings, "profileid") == "") c.settings.namevaluesettings = NVSet(c.settings.namevaluesettings, "profileid", "0"); } else { TransportProtocol tp; if (!Enum.TryParse(NV(c.settings.namevaluesettings, "transport"), true, out tp)) tp = TransportProtocol.rtsp; dev.Account = new NetworkCredential { UserName = c.settings.login, Password = c.settings.password }; var sessionFactory = new NvtSessionFactory(dev.Account); var profilename = NV(c.settings.namevaluesettings, "profilename"); foreach (var uri in dev.Uris) { var f = sessionFactory.CreateSession(uri); dev.URL = uri.ToString(); Profile[] profiles; try { profiles = f.GetProfiles().RunSynchronously(); } catch (Exception ex) { Logger.LogExceptionToFile(ex, "ONVIF"); continue; } dev.Profiles = profiles; var strSetup = new StreamSetup { transport = new Transport() }; strSetup.transport.protocol = tp; int i = 0; foreach (var p in profiles) { try { if (p.token == profilename) { var strUri = f.GetStreamUri(strSetup, p.token).RunSynchronously(); string urlAuth = strUri.uri.Replace("://", "://[USERNAME]:[PASSWORD]@"); string streamSize = p.videoEncoderConfiguration.resolution.width + "x" + p.videoEncoderConfiguration.resolution.height; c.settings.videosourcestring = urlAuth; c.settings.namevaluesettings = NVSet(c.settings.namevaluesettings, "profileid", i.ToString(CultureInfo.InvariantCulture)); c.settings.namevaluesettings = NVSet(c.settings.namevaluesettings, "streamsize", streamSize); string[] sz = streamSize.Split('x'); c.settings.desktopresizewidth = Convert.ToInt32(sz[0]); c.settings.desktopresizeheight = Convert.ToInt32(sz[1]); break; } i++; } catch (Exception ex) { Logger.LogExceptionToFile(ex, "ONVIF"); } } } } c.ptz = -5; } var cw = MainForm.InstanceReference.GetCameraWindow(c.id); if (cw != null) { cw.Disable(); cw.Enable(); } resp = "{\"actionResult\":\"waiteditobject\"}"; } } break; } if (apply) { var io = MainForm.InstanceReference.GetISpyControl(ot, oid); io?.Apply(); } } catch (Exception ex) { Logger.LogExceptionToFile(ex, "JSON Parser"); } if (saveObjects) { try { MainForm.InstanceReference.SaveObjectList(false); } catch (Exception ex) { Logger.LogExceptionToFile(ex, "JSON Parser"); } } SendHeader(sHttpVersion, "application/json", resp.Length, " 200 OK", 0, ref req); SendToBrowser(resp, req); }