软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

284 lines
12 KiB

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestHttp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;
namespace HardWareInteface.Common
{
public class hikAlarmQueueTaskXZ
{
private static FangYar.BLL.TBL_ALARMDATA aBll = new FangYar.BLL.TBL_ALARMDATA();
//
public static ConcurrentQueue<string> infoQueue = new ConcurrentQueue<string>();
private static CancellationTokenSource m_DealToken = new CancellationTokenSource();
private static Task m_DealTask = Task.Factory.StartNew(DealInfoList, m_DealToken.Token);
private static int i = 0;
public static void infoQueueFun(string model)
{
try
{
infoQueue.Enqueue(model); //添加
//DealInfoList();
}
catch (Exception e)
{
WriteLog(e.Message);
}
WriteLog("西藏队列长度:" + infoQueue.Count.ToString());
}
private static void DealInfoList()
{
while (!m_DealToken.IsCancellationRequested)
{
string dataInfo = null;
if (infoQueue.TryDequeue(out dataInfo))
{
try
{
WriteLog("开始处理...");
DealInfo(dataInfo);
WriteLog("结束处理...");
}
catch (Exception ex)
{
WriteLog(ex.Source + ex.StackTrace + ex.Message + "DealInfoList_Error");
}
}
else
{
WriteLog("西藏队列长度:" + infoQueue.Count.ToString() + ",正在等待...");
Thread.Sleep(100);
}
}
}
private static string DealInfo(string data)
{
try
{
//data = "{\"alarmTime\":1627919606000,\"channel\":\"11\",\"channelType\":\"10300\",\"devSerial\":\"G99356847\",\"alarmId\":\"20210802235327-E35064652-11-10014\",\"alarmType\":\"10014\",\"channelName\":\"东南1号向西\",\"pictureUrl\":[\"https://i.ys7.com/streamer/alarm/url/get?fileId=20210802235327-E35064652-11-10014-2-1&deviceSerialNo=E35064652&cn=11&isEncrypted=0&isCloudStored=0&ct=1&lc=7&bn=1_hikalarm&isDevVideo=0\"]}";
//data = "{\"alarmTime\":1627919606000,\"channel\":\"8\",\"channelType\":\"10300\",\"devSerial\":\"G16833582\",\"alarmId\":\"20210802235327-E35064652-11-10014\",\"alarmType\":\"10014\",\"channelName\":\"东南1号向西\",\"pictureUrl\":[\"https://dn-od4ir4su9.qbox.me/7/G16833582_8_20210822020009-G16833582-8-10014-2-1?e=1630395821&token=5JNHy55nNlr5InRTUltuUGUbGkZQb9Vpzf_Ko-XH:0Bjgn_WFVsYQA0nTkVpy_3V4XN8=\"]}";
data = System.Web.HttpUtility.UrlDecode(data, System.Text.Encoding.UTF8);
WriteLog(data.ToString());
string redata_string = data.Replace(Environment.NewLine, "");
string redata_string_json = "[";
if (redata_string.Contains("} {"))
{
redata_string = redata_string.Replace("} {", "},{");
}
else if (redata_string.Contains("}{"))
{
redata_string = redata_string.Replace("}{", "},{");
}
redata_string_json += redata_string;
redata_string_json += "]";
try
{
JArray jsonStr = JsonConvert.DeserializeObject<JArray>(redata_string_json);
}
catch (Exception e)
{
WriteLog("\r\n 数据有误: 字符串不是json格式");
}
List<FangYar.Model.AlarmReception_DATA> ar_data = JsonConvert.DeserializeObject<List<FangYar.Model.AlarmReception_DATA>>(redata_string_json);
FangYar.BLL.TBL_CAMERA bll_cam = new FangYar.BLL.TBL_CAMERA();
for (int i = 0; i < ar_data.Count; i++)
{
//判断是否该条报警为重复上报,已存在于缓存中
object cameraAlarmTime = FangYar.Common.CacheHelper.GetCache("XZ_" + ar_data[i].devSerial + "_" + ar_data[i].channel + "_" + ar_data[i].alarmTime + "_" + ar_data[i].alarmType);
if (cameraAlarmTime != null)
{
return ""; //
}
else
{
FangYar.Common.CacheHelper.SetCache("XZ_" + ar_data[i].devSerial + "_" + ar_data[i].channel + "_" + ar_data[i].alarmTime + "_" + ar_data[i].alarmType, true, Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0, 0));
DataWarehousing(ar_data[i]);
}
}
}
catch (Exception e)
{
WriteLog(e.Message);
}
return "";
}
public static void DataWarehousing(FangYar.Model.AlarmReception_DATA dataInfo)
{
FangYar.Model.OA.TBL_ALARMDATA amodel = new FangYar.Model.OA.TBL_ALARMDATA();
amodel.ID = Guid.NewGuid().ToString("N");
amodel.DVC_ID = dataInfo.devSerial;
amodel.TYPE = "0";
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddTicks(dataInfo.alarmTime * 10000);
amodel.TIME = dt;
amodel.ALARM_TYPE = "0";
amodel.CHANNEL = dataInfo.channel;
amodel.CHANNELTYPE = dataInfo.channelType;
amodel.CUSTOMINFO = dataInfo.customInfo;
amodel.DEVSERIAL = dataInfo.devSerial;
amodel.ALARMID = dataInfo.alarmId;
amodel.ALARMTYPE = dataInfo.alarmType;
amodel.CHANNELNAME = dataInfo.channelName;
string picsrcs = "";
for (int y = 0; y < dataInfo.pictureUrl.Length; y++)
{
try
{
WebClient client = new WebClient();
byte[] byteArray = client.DownloadData(dataInfo.pictureUrl[y]);
if (y == 0)
{
picsrcs = GetFilePath(byteArray, dataInfo.devSerial);
}
else
{
picsrcs += "," + GetFilePath(byteArray, dataInfo.devSerial);
}
}
catch (Exception e)
{
WriteLog(e.Message);
}
}
amodel.PICSRC = picsrcs;
//判断设备机构ID是否存在缓存中
object dvcOrg = FangYar.Common.CacheHelper.GetCache("XZ_" + dataInfo.devSerial);
if (dvcOrg != null)
{
amodel.ORG_ID = dvcOrg.GetType().GetProperty("ORG_ID").GetValue(dvcOrg).ToString();
amodel.ADDR = dvcOrg.GetType().GetProperty("ADDR").GetValue(dvcOrg).ToString();
}
else
{
DataTable orgDt = FangYar.Common.MySqlHelper.QueryTable("select ORG_ID,ORG_NAME from tbl_device_org where devSerial = '" + dataInfo.devSerial + "'");
if (orgDt.Rows.Count > 0)
{
amodel.ORG_ID = orgDt.Rows[0]["ORG_ID"].ToString();
amodel.ADDR = orgDt.Rows[0]["ORG_NAME"].ToString();
}
FangYar.Common.CacheHelper.SetCache("XZ_" + dataInfo.devSerial, new { ORG_ID = amodel.ORG_ID, ADDR = amodel.ADDR }, Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0, 0));
}
bool isadd = aBll.Add(amodel);
WriteLog("结果:" + isadd + "_" + amodel.DVC_ID + "_" + amodel.TIME);
}
/// <summary>
/// base转图片保存到服务器
/// </summary>
/// <param name="msg"></param>
private static string GetFilePath(byte[] bytes, string devSerial)
{
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();//年月
string ImageFilePath = "/Attachment" + "/hikAlarmReception/" + devSerial + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString();
//图片保存地址
string imgSavePath = System.Configuration.ConfigurationManager.AppSettings["imgSavePath"] + "";
if (HttpContext.Current != null) //web应用程序 单线程
{
if (System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(ImageFilePath)) == false)//如果不存在就创建文件夹
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath));
}
}
else //非web程序引用 多线程
{
//ImageFilePath = ImageFilePath.Replace("/", "\\");
string filePath = imgSavePath + ImageFilePath;
if (System.IO.Directory.Exists(filePath) == false)//如果不存在就创建文件夹
{
System.IO.Directory.CreateDirectory(filePath);
}
}
string ImagePath;
string str = System.DateTime.Now.ToString("yyyyHHddHHmmssfff") + Guid.NewGuid().ToString();
ImagePath = imgSavePath + ImageFilePath + "/" + str;//定义图片名称
File.WriteAllBytes(ImagePath + ".png", bytes); //保存图片到服务器,然后获取路径
return ImageFilePath + "/" + str + ".png";//获取保存后的路径
}
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
private static void WriteLog(string msg)
{
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "hikAlarmReceptionXZ" });
//try
//{
// LogWriteLock.EnterWriteLock();
// string path = AppDomain.CurrentDomain.BaseDirectory + "log\\hikAlarmReceptionXZ\\";
// string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt";
// if (Directory.Exists(path) == false)//如果不存在就创建文件夹
// {
// Directory.CreateDirectory(path);
// }
// FileInfo file = new FileInfo(path + filename);
// if (!file.Exists)
// {
// try
// {
// FileStream fs;
// fs = File.Create(path + filename);
// fs.Close();
// }
// catch
// {
// }
// }
// using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write))
// {
// using (StreamWriter sw = new StreamWriter(fs))
// {
// sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg);
// }
// }
//}
//catch (Exception e)
//{
//}
//finally
//{
// LogWriteLock.ExitWriteLock();
//}
}
}
}