using Microsoft.Extensions.Configuration.Ini; using Newtonsoft.Json; using…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
using Microsoft.Extensions.Configuration.Ini; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SorboxBack.Database; using System; using System.Data.SQLite; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.ServiceProcess; using System.Text.Json.Nodes; using static SQLite.SQLite3;
namespace SorboxBack { public class CameraConfig { public static bool[] cameraFlags = Enumerable.Repeat(true, 4).ToArray();
public static int maxCam = 4;
public enum CameraType
{
TYPE_ACQ_IP_RTSP_FFMPEG_TCP = 0,
TYPE_ACQ_IP_RTSP_FFMPEG,
TYPE_ACQ_IP_H264_MPEG4,
TYPE_ACQ_IP_MJPEG,
ERROR
};
public enum TypeAcq
{
GENERIC = 0,
AXIS_MJPEG,
AXIS_H264,
SAMSUNG,
FLIR_F_FC,
FLIR_FB,
DAHUA,
HIKVISION_MJPEG,
HIKVISION_H264
};
public enum ModeAcq
{
PRIOR_IMAGE = 0,
PRIOR_SPEED,
ACQ_ROBUST,
FRAME_BY_FRAME,
}
private readonly string filePath;
public CameraConfig(string filePath)
{
this.filePath = filePath;
}
public static bool setCamConfig(int camId, JsonObject cameraConf)
{
// Fetch the existing camera config from the database
CameraConf existingCameraConf = DataBaseManagement.GetCameraConfigurationById(camId);
if (existingCameraConf == null)
{
Console.WriteLine("No existing camera config found with ID: " + camId);
return false;
}
// Convert the provided JsonObject into a dictionary
Dictionary<string, object> updates = JsonConvert.DeserializeObject<Dictionary<string, object>>(cameraConf.ToString());
// Use reflection to update the fields in the existing CameraConf object
foreach (KeyValuePair<string, object> update in updates)
{
// Get the property in the existing object
PropertyInfo property = typeof(CameraConf).GetProperty(update.Key);
// If the property doesn't exist, skip this update
if (property == null)
{
Console.WriteLine("No such property in CameraConf: " + update.Key);
continue;
}
// Convert the new value to the property type and set the property
object convertedValue = Convert.ChangeType(update.Value, property.PropertyType);
property.SetValue(existingCameraConf, convertedValue);
}
// Update the existing camera config in the database
bool ret = DataBaseManagement.UpdateCameraConfiguration(camId, existingCameraConf);
// Set flag
cameraFlags[camId - 1] = false;
return ret;
}
private static CameraType handleGenericTypeConfig(CameraConf configCam)
{
ModeAcq acqMode;
CameraType camType;
Enum.TryParse(configCam.modeAcq, out acqMode);
switch (acqMode)
{
case ModeAcq.PRIOR_IMAGE:
camType = CameraType.TYPE_ACQ_IP_RTSP_FFMPEG_TCP; break;
case ModeAcq.PRIOR_SPEED:
camType = CameraType.TYPE_ACQ_IP_RTSP_FFMPEG; break;
case ModeAcq.ACQ_ROBUST:
camType = CameraType.TYPE_ACQ_IP_H264_MPEG4; break;
case ModeAcq.FRAME_BY_FRAME:
camType = CameraType.TYPE_ACQ_IP_MJPEG; break;
default:
camType = CameraType.ERROR; break;
}
return camType;
}
private static CameraType handleTypeCongfig(CameraConf configCam)
{
TypeAcq acqType;
CameraType camType;
Enum.TryParse(configCam.acqType, out acqType);
switch (acqType)
{
case TypeAcq.AXIS_MJPEG:
camType = CameraType.TYPE_ACQ_IP_MJPEG; break;
case TypeAcq.AXIS_H264:
case TypeAcq.SAMSUNG:
case TypeAcq.FLIR_F_FC:
case TypeAcq.FLIR_FB:
case TypeAcq.DAHUA:
case TypeAcq.HIKVISION_MJPEG:
case TypeAcq.HIKVISION_H264:
camType = CameraType.TYPE_ACQ_IP_H264_MPEG4; break;
default:
camType = CameraType.ERROR; break;
}
return camType;
}
private static CameraType getCameraType(CameraConf configCam)
{
CameraType camType;
if (configCam.acqType == TypeAcq.GENERIC.ToString())
{
camType = handleGenericTypeConfig(configCam);
}
else
{
camType = handleTypeCongfig(configCam);
}
return camType;
}
public static bool writeRtspFfmpegTcpFields(CameraConf configCam)
{
if (configCam == null)
{
throw new ArgumentNullException(nameof(configCam), "configCam cannot be null.");
}
IniFile file = new IniFile("C:\\Program Files\\Foxstream\\FoxVigi\\Camera.ini");
try
{
string prefix = "Camera_";
if (configCam.Id <= 0) // Assuming Id should be a positive number
{
throw new ArgumentException("Camera ID must be greater than 0.", nameof(configCam.Id));
}
int camIndex = configCam.Id - 1;
string camField = $"{prefix}{camIndex}";
file.DeleteKey("Adresse IP", camField);
file.DeleteKey("Url", camField);
file.DeleteKey("Password", camField);
file.DeleteKey("Login", camField);
file.DeleteKey("Port", camField);
file.DeleteKey("user", camField);
file.DeleteKey("pass", camField);
file.DeleteKey("transport", camField);
// Before writing each value, check if the property is null
if (configCam.name != null)
{
file.Write("Nom", configCam.name, camField);
}
if (configCam.user != null)
{
file.Write("user", configCam.user, camField);
}
file.Write("TypeName", "RTSP FFMPEG", camField);
if (configCam.password != null)
{
file.Write("pass", configCam.password, camField);
}
// Assuming configCam.activation is a non-nullable value type; otherwise, add a null check.
file.Write("Activation", (configCam.activation ? "1" : "0"), camField);
file.Write("Type", "23", camField);
file.Write("Sleep", "10", camField);
if (configCam.url != null)
{
file.Write("address", configCam.url.ToString(), camField);
}
// Assuming configCam.fixingIframes is a non-nullable value type; otherwise, add a null check.
file.Write("buffercomposer", (configCam.fixingIframes == 1 ? "1" : "0"), camField);
file.Write("transport", "2", camField); // Always writing "2" for transport, assuming this is intentional and not a nullable field.
CameraConfig.RestartService("FoxVigiSrv"); // Restart fox vigi service
return true;
}
catch (Exception e)
{
// Consider logging the exception here
throw new Exception($"Failed to set RTSP FFMPEG TCP configuration. {e.Message}", e);
}
}
/*public static bool writeRtspFfmpegTcpFields(CameraConf configCam)
{
IniFile file = new IniFile("C:\\Program Files\\Foxstream\\FoxVigi\\Camera.ini");
try
{
string prefix = "Camera_";
int camIndex = configCam.Id - 1;
string camField = $"{prefix}{camIndex}";
file.DeleteKey("Adresse IP", camField);
file.DeleteKey("Url", camField);
file.DeleteKey("Password", camField);
file.DeleteKey("Login", camField);
file.DeleteKey("Port", camField);
file.DeleteKey("user", camField);
file.DeleteKey("pass", camField);
file.DeleteKey("transport", camField);
file.Write("Nom", configCam.name, camField);
file.Write("user", configCam.user, camField);
file.Write("TypeName", "RTSP FFMPEG", camField);
file.Write("pass", configCam.password, camField);
file.Write("Activation", (configCam.activation ? "1" : "0"), camField);
file.Write("Type", "23", camField);
file.Write("Sleep", "10", camField);
file.Write("address", configCam.url.ToString(), camField);
file.Write("buffercomposer", (configCam.fixingIframes == 1 ? "1" : "0"), camField);
file.Write("transport", "2", camField);
CameraConfig.RestartService("FoxVigiSrv"); // Restart fox vigi servic
return true;
}
catch (Exception e)
{
throw new Exception($"Failed to set RTSP FFMPEG configuration. {e.Message}");
}
}*/
public static bool writeRtspFfmpegFields(CameraConf configCam)
{
if (configCam == null)
{
throw new ArgumentNullException(nameof(configCam), "configCam cannot be null.");
}
IniFile file = new IniFile("C:\\Program Files\\Foxstream\\FoxVigi\\Camera.ini");
try
{
string prefix = "Camera_";
// Assuming configCam.Id is a non-nullable value type; otherwise, add a null check.
int camIndex = configCam.Id - 1;
string camField = $"{prefix}{camIndex}";
file.DeleteKey("Adresse IP", camField);
file.DeleteKey("Url", camField);
file.DeleteKey("Password", camField);
file.DeleteKey("Login", camField);
file.DeleteKey("Port", camField);
file.DeleteKey("user", camField);
file.DeleteKey("pass", camField);
file.DeleteKey("transport", camField);
// Before writing each value, check if the property is null
if (configCam.name != null)
{
file.Write("Nom", configCam.name, camField);
}
if (configCam.user != null)
{
file.Write("user", configCam.user, camField);
}
file.Write("TypeName", "RTSP FFMPEG", camField);
if (configCam.password != null)
{
file.Write("pass", configCam.password, camField);
}
file.Write("Activation", (configCam.activation ? "1" : "0"), camField);
file.Write("Type", "23", camField);
file.Write("Sleep", "10", camField);
if (configCam.url != null)
{
file.Write("address", configCam.url.ToString(), camField);
}
// Assuming configCam.fixingIframes is a non-nullable value type; otherwise, add a null check.
file.Write("buffercomposer", (configCam.fixingI
frames == 1 ? "1" : "0"), camField); file.Write("transport", "2", camField); // Always writing "2" for transport, assuming this is intentional and not a nullable field.
CameraConfig.RestartService("FoxVigiSrv"); // Restart fox vigi service
return true;
}
catch (Exception e)
{
// Consider logging the exception here
throw new Exception($"Failed to set RTSP FFMPEG configuration. {e.Message}", e);
}
}
public static bool writeIpMjpegFields(CameraConf configCam)
{
if (configCam == null)
{
throw new ArgumentNullException(nameof(configCam), "configCam cannot be null.");
}
IniFile file = new IniFile("C:\\Program Files\\Foxstream\\FoxVigi\\Camera.ini");
try
{
string prefix = "Camera_";
// Assuming configCam.Id is a non-nullable value type; otherwise, add a null check.
int camIndex = configCam.Id - 1;
string camField = $"{prefix}{camIndex}";
file.DeleteKey("Adresse IP", camField);
file.DeleteKey("Url", camField);
file.DeleteKey("Password", camField);
file.DeleteKey("Login", camField);
file.DeleteKey("Port", camField);
file.DeleteKey("user", camField);
file.DeleteKey("pass", camField);
file.DeleteKey("transport", camField);
// Before writing each value, check if the property is null
if (configCam.name != null)
{
file.Write("Nom", configCam.name, camField);
}
if (configCam.user != null)
{
file.Write("user", configCam.user, camField);
}
file.Write("TypeName", "IP MJPEG", camField);
if (configCam.password != null)
{
file.Write("pass", configCam.password, camField);
}
// Assuming configCam.activation is a non-nullable value type; otherwise, add a null check.
file.Write("Activation", (configCam.activation ? "1" : "0"), camField);
file.Write("Type", "1", camField);
file.Write("Sleep", "10", camField);
if (configCam.url != null)
{
file.Write("address", configCam.url.ToString(), camField);
}
CameraConfig.RestartService("FoxVigiSrv"); // Restart fox vigi service
return true;
}
catch (Exception e)
{
// Consider logging the exception here
throw new Exception($"Failed to set IP MJPEG configuration. {e.Message}", e);
}
}
public static bool writeIpH264Mpeg4Fields(CameraConf configCam)
{
if (configCam == null)
{
throw new ArgumentNullException(nameof(configCam), "configCam cannot be null.");
}
IniFile file = new IniFile("C:\\Program Files\\Foxstream\\FoxVigi\\Camera.ini");
try
{
string prefix = "Camera_";
// Assuming configCam.Id is a non-nullable value type; otherwise, add a null check.
int camIndex = configCam.Id - 1;
string camField = $"{prefix}{camIndex}";
file.DeleteKey("Adresse IP", camField);
file.DeleteKey("Url", camField);
file.DeleteKey("Password", camField);
file.DeleteKey("Login", camField);
file.DeleteKey("Port", camField);
file.DeleteKey("user", camField);
file.DeleteKey("pass", camField);
file.DeleteKey("transport", camField);
// Before writing each value, check if the property is null
if (configCam.name != null)
{
file.Write("Nom", configCam.name, camField);
}
if (configCam.user != null)
{
file.Write("user", configCam.user, camField);
}