ibms-dome/Backend/Services/Implement/webRequestService.cs

165 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Newtonsoft.Json;
using Repository.Models;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Linq;
using Repository.BackendRepository.Implement;
using Repository.BackendRepository;
using Ubiety.Dns.Core;
namespace Backend.Services.Implement
{
public class webRequestService
{
public List<Device_value> obixQuery(string urlString, string bql)
{
List<Device_value> result = new List<Device_value>();
String username = "obixUser";
String password = "Admin123456";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
String API_Url = urlString;
HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url);
Postrequest.Method = "POST";
Postrequest.Headers.Add("Authorization", "Basic " + encoded);
Postrequest.PreAuthenticate = true;
using (var streamWriter = new StreamWriter(Postrequest.GetRequestStream()))
{
string json = "<str val='" + bql + "'/>";
streamWriter.Write(json);
}
HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString);
//xmlDoc.Load("N4v1021.xml");//N4v1021
//xmlDoc.Save("N4.xml");
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);
var data = Welcome.FromJson(jsonText);
foreach (var item in data.Obj.Str)
{
Device_value row = new Device_value();
row.value = item.Val;
string[] s1 = item.Val.Split(',');
string[] s2 = s1[0].Split('/');
row.tag_name = "";
for (int i = 0; i < s2.Length; i++)
{
if (i == s2.Length - 2)
{
row.point_name = s2[i];
}
else if (i == 1)
{
row.tag_name += s2[i];
}
else if (i > 1 && i < s2.Length - 2)
{
row.tag_name += "_" + s2[i];
}
}
/*foreach (var ss in s2)
{
if (ss.Contains('_'))
{
string[] s3 = ss.Split('_');
if (s3.Count() > 3)
{
row.tag_name = ss;
row.point_name = s2[s2.Length - 2];
break;
}
}
}*/
result.Add(row);
}
return result;
}
//public void getObix()
//{
// //#region 取得obix 設定
// //var obixApiConfig = new ObixApiConfig();
// //var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
// //var variableObix = await backgroundServiceRepository.GetAllAsync<KeyValue>(sqlObix);
// //obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault();
// //obixApiConfig.UserName = ed.AESDecrypt(variableObix.Where(x => x.Name == "UserName").Select(x => x.Value).FirstOrDefault());
// //obixApiConfig.Password = ed.AESDecrypt(variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault());
// //String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password));
// //#endregion 取得obix 設定
// HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/");
// //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/");
// archiveMonthRequest.Method = "POST";
// archiveMonthRequest.Headers.Add("Authorization", "Basic " + encoded);
// archiveMonthRequest.PreAuthenticate = true;
// byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter);
// using (Stream reqStream = archiveMonthRequest.GetRequestStream())
// {
// reqStream.Write(byteArray, 0, byteArray.Length);
// }
// HttpWebResponse archiveMonthResponse = (HttpWebResponse)archiveMonthRequest.GetResponse();
// var archiveMonthResponseContent = new StreamReader(archiveMonthResponse.GetResponseStream()).ReadToEnd();
// xmlDocument.LoadXml(archiveMonthResponseContent);
// string archiveMonthJson = JsonConvert.SerializeXmlNode(xmlDocument);
// JObject archiveMonthJsonResult = (JObject)JsonConvert.DeserializeObject(archiveMonthJson);
// if (archiveMonthJsonResult.ContainsKey("err")) //抓取錯誤
// {
// //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】");
// //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】[錯誤內容]{0}", archiveMonthJsonResult);
// Dictionary<string, object> archiveMonthRawData = new Dictionary<string, object>();
// archiveMonthRawData.Add("@device_number", deviceNumberPoint.DeviceNumber);
// archiveMonthRawData.Add("@point", deviceNumberPoint.Point);
// archiveMonthRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19));
// archiveMonthRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19));
// archiveMonthRawData.Add("@is_complete", 0);
// archiveMonthRawData.Add("@repeat_times", 0);
// archiveMonthRawData.Add("@fail_reason", archiveMonthJson);
// archiveMonthRawData.Add("@count_rawdata", 0);
// archiveMonthRawData.Add("@min_rawdata", 0);
// archiveMonthRawData.Add("@max_rawdata", 0);
// archiveMonthRawData.Add("@avg_rawdata", 0);
// archiveMonthRawData.Add("@sum_rawdata", 0);
// archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
// archiveMonthRawDatas.Add(archiveMonthRawData);
// }
// if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容
// {
// var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult);
// if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0)
// {
// archiveMonthRawDatas.AddRange(ArrangeRawDatas);
// }
// }
//}
}
}