using Container.Common; using Container.Model; using Container.Services; using Container.Viewmodel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Windows; using System.Xml; namespace Container.Business { public class ItemInfo { public int id { get; set; } public string img { get; set; } public int time { get; set; } public string effect { get; set; } //public string text { get; set; } public string screenmatch { get; set; } public string code { get; set; } public string launchtime { get; set; } public string expirydate { get; set; } public string protype { get; set; } public string xaxis { get; set; } public string yaxis { get; set; } public string devNum { get; set; } public string floorOrder { get; set; } public string applets { get; set; } public ShopInfo shopInfo { get; set; } } public class ShopInfo { public string FloorName { get; set; } public string ShopName { get; set; } public string LogoPath { get; set; } public string Angle { get; set; } public string houseNum { get; set; } public string shopFormatIcon { get; set; } public string shopFormatName { get; set; } } public class JService { App app = ((App)Application.Current); //private string localIP = app.LocalIP; //private string remoteAddress = ConfigurationManager.AppSettings["RemoteAddress"]; Class_Log Mylog = new Class_Log(); public delegate void ScreenConfigDelegate(); public static event ScreenConfigDelegate ScreenConfigReceiveEvent; ///// ///// 绑定ip ///// //public bool BindIp() //{ // string urlString = string.Format("http://{0}/shanjing/interFace/kioskBinding.do?ip={1}", remoteAddress, localIP); // string ret = GetHttpResponse(urlString); // if (string.IsNullOrEmpty(ret)) // return false; // else // return true; //} public string WriteToConfig(List itemList, string code) { try { XmlDocument doc = new XmlDocument(); // 创建dom对象 XmlElement root = doc.CreateElement("root"); // 创建根节点 doc.AppendChild(root); // 加入到xml document XmlElement FillItem; // 创建FillItem元素 FillItem = doc.CreateElement("code"); FillItem.InnerText = code; root.AppendChild(FillItem); // 添加到xml document FillItem = doc.CreateElement("items"); if (itemList != null && itemList.Count > 0) { for (int i = 0; i < itemList.Count; i++) { XmlElement Node = doc.CreateElement("item"); Node.SetAttribute("id", itemList[i].id.ToString()); Node.SetAttribute("img", itemList[i].img); Node.SetAttribute("time", itemList[i].time.ToString()); Node.SetAttribute("effect", itemList[i].effect); Node.SetAttribute("screenmatch", itemList[i].screenmatch); Node.SetAttribute("launchtime", itemList[i].launchtime); Node.SetAttribute("expirydate", itemList[i].expirydate); Node.SetAttribute("protype", itemList[i].protype); Node.SetAttribute("code", itemList[i].code); Node.SetAttribute("xaxis", itemList[i].xaxis); Node.SetAttribute("yaxis", itemList[i].yaxis); Node.SetAttribute("floorOrder", itemList[i].floorOrder); Node.SetAttribute("devNum", itemList[i].devNum); Node.SetAttribute("applets", itemList[i].applets); Node.SetAttribute("floorName", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.FloorName); Node.SetAttribute("shopName", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.ShopName); Node.SetAttribute("logoPath", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.LogoPath); Node.SetAttribute("houseNum", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.houseNum); Node.SetAttribute("shopFormatIcon", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.shopFormatIcon); Node.SetAttribute("shopFormatName", itemList[i].shopInfo == null ? "" : itemList[i].shopInfo.shopFormatName); FillItem.AppendChild(Node); } } root.AppendChild(FillItem); MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream, null); writer.Formatting = System.Xml.Formatting.Indented; doc.Save(writer); StreamReader sr = new StreamReader(stream, Encoding.UTF8); stream.Position = 0; string XMLString = sr.ReadToEnd(); Mylog.WriteLogFile(XMLString, "XMLString"); sr.Close(); writer.Flush(); writer.Close(); stream.Flush(); stream.Close(); return XMLString; } catch (Exception ex) { Mylog.WriteLogFile(ex.ToString(), "JServiceErrorLog"); return ""; } } /// /// 获取后台图片或视频资源地址 /// /// public bool GetOutLineResource() { bool flag = false; try { string MethodStr = "/Api/Programme/DevProgrammeList"; var data = new { DevCode = app.deviceid }; var ret = MyHttpClient.CryptPost(MethodStr, data, app.HttpUrl); Mylog.WriteLogFile(ret, "HttpClient"); //ret= DownLoadMethod.ReadContext(AppDomain.CurrentDomain.BaseDirectory + "/OutlineFiles/1.txt"); if (!string.IsNullOrEmpty(ret)) { app.isStartGetProg = false; string oldConfig = DownLoadMethod.ReadContext(AppDomain.CurrentDomain.BaseDirectory + "/OutlineFiles/Main/screenconfig.txt"); if (ret != oldConfig) { var Jo = JObject.Parse(ret); string code = Jo.Value("code"); if (code == "200") { if (app.IsDownScreen) { app.screenConfig = ret; } else { app.downErrorTime = 0; DownScreen(ret); } return true; //DownLoadMethod downLoadMethod = new DownLoadMethod(); //flag = downLoadMethod.DownLoadScreenConfig(itemList, ret); //if (flag) { //} } } } else { if (app.isStartGetProg) { Thread.Sleep(30000); GetOutLineResource(); } } } catch (Exception ex) { Mylog.WriteLogFile(ex.ToString(), "JServiceErrorLog"); } return flag; } private void DownScreen(string revScreen) { try { var Jo = JObject.Parse(revScreen); List itemList = Jo.Value("data").ToObject>(); Action action = new Action(() => { // List itemList = Jo.Value("data").ToObject>(); app.IsDownScreen = true; DownLoadMethod downLoadMethod = new DownLoadMethod(); bool flag = downLoadMethod.DownLoadScreenConfig(itemList, revScreen); if (flag) { ScreenConfigReceiveEvent?.Invoke(); if (!string.IsNullOrEmpty(app.screenConfig)) { DownScreen(app.screenConfig); app.screenConfig = string.Empty; app.downErrorTime = 0; } else { app.IsDownScreen = false; } } else { if (!string.IsNullOrEmpty(app.screenConfig)) { DownScreen(app.screenConfig); app.screenConfig = string.Empty; app.downErrorTime = 0; } else { if (app.downErrorTime < 100) { Thread.Sleep(1000); DownScreen(revScreen); app.downErrorTime++; } else { app.downErrorTime = 0; app.IsDownScreen = false; } } } //config.WriteToScreenConfig(); }); action.BeginInvoke(null, null); } catch (Exception e) { Mylog.WriteLogFile(e.ToString(), "HttpClientError"); } } /// /// 获取天气 /// /// public WeatherViewModel GetEnvironmental(string city) { WeatherViewModel weather = new WeatherViewModel(); try { //string urlString = string.Format("{0}/API/CDN/GetEnvironmentalData", app.kioskUrl); //string ret = GetHttpResponse(urlString); HttpClient httpclient = new HttpClient(); StringBuilder sb = new StringBuilder(); sb.Append("{\"city\":\"" + city + "\"}"); httpclient.message = sb.ToString(); httpclient.url = "http://saas.1000my.com:8999/API/Weather/GetWeather"; string ret = httpclient.PostMoths(httpclient.message, httpclient.url); if (!string.IsNullOrEmpty(ret)) { Dictionary dic = JsonConvert.DeserializeObject>(ret); if (!string.IsNullOrEmpty(dic["data"].ToString()) && dic["code"].ToString() == "200") { Dictionary dicdata = JsonConvert.DeserializeObject>(dic["data"].ToString()); weather.aqi = dicdata["realAqi"].ToString(); weather.humidity = dicdata["humidity"].ToString(); weather.pm25 = dicdata["aqi"].ToString(); weather.temperature = dicdata["temperature_Now"].ToString(); weather.windPower = dicdata["wind_Power"].ToString(); weather.status = dicdata["status"].ToString(); } } } catch (Exception ex) { Mylog.WriteLogFile("天气获取失败" + ex.ToString(), "JServiceErrorLog"); } return weather; } public void SetPlayRecord() { try { StringBuilder record = new StringBuilder(); string fpath = Directory.GetCurrentDirectory() + "\\record\\"; if (!Directory.Exists(fpath)) { return; } string fname = Directory.GetCurrentDirectory() + "\\record\\Record.txt"; FileInfo finfo = new FileInfo(fname); Mylog.WriteLogFile("文件地址" + fname, "UploadPlayRecord"); if (!finfo.Exists) { return; } else { StreamReader sr = new StreamReader(fname, Encoding.Default); String line; while ((line = sr.ReadLine()) != null) { record.Append(line.ToString()); } sr.Close(); if (!string.IsNullOrEmpty(record.ToString())) { var array = JArray.Parse("[" + record + "]"); //var list = Jo.Value("data").ToObject>(); var data = new { DevCode = app.deviceid, List = array }; Mylog.WriteLogFile("文件地址" + JsonConvert.SerializeObject(data), "UploadPlayRecord"); string MethodStr = "/API/ProgramStatistics/UploadPlayRecord"; var ret = MyHttpClient.CryptPost(MethodStr, data); if (!string.IsNullOrEmpty(ret)) { var Jo = JObject.Parse(ret); string code = Jo.Value("code"); if (code == "200") { File.Delete(fname); } else { Mylog.WriteLogFile("上传节目播放统计错误" + ret, "UploadPlayRecordError"); } } } } } catch (Exception e) { Mylog.WriteLogFile("上传节目播放统计错误" + e.ToString(), "UploadPlayRecordError"); } } public void GetSubTitle() { try { string MethodStr = "/API/Subtitle/GetDevSubtitle"; var data = new { Code = app.deviceid }; var ret = MyHttpClient.CryptPost(MethodStr, data); if (!string.IsNullOrEmpty(ret)) { var Jo = JObject.Parse(ret); string code = Jo.Value("code"); if (code == "200") { try { List itemList = Jo.Value("data").ToObject>(); //Mylog.WriteLogFile("-data---" + JsonConvert.SerializeObject(Data), "Subtitle"); //List itemList = JsonConvert.DeserializeObject<>(Data); var groupList = itemList.GroupBy(i => i.location).Select(g => (new { Name = g.Key, List = g })).ToList(); app.TopSubTitle.Clear(); app.BottomSubTitle.Clear(); foreach (var item in groupList) { if (item.Name == "top") { app.TopSubTitle = item.List.ToList(); app.TopSubTitleIndex = 0; } else { app.BottomSubTitle = item.List.ToList(); app.BottomSubTitleIndex = 0; } } } catch (Exception e) { app.TopSubTitle.Clear(); app.BottomSubTitle.Clear(); app.BottomSubTitleIndex = 0; app.TopSubTitleIndex = 0; Mylog.WriteLogFile("-----error----" + e.Message, "Subtitle"); } } } } catch (Exception e) { app.TopSubTitle.Clear(); app.BottomSubTitle.Clear(); app.BottomSubTitleIndex = 0; app.TopSubTitleIndex = 0; Mylog.WriteLogFile("-----error----" + e.Message, "Subtitle"); } } public void GetSyncDev() { Class_Log log = new Class_Log(); try { log.WriteLogFile("获取", "GetSyncDev"); string MethodStr = "/API/Dev/GetSyncDev"; var data = new { Code = app.deviceid }; var ret = MyHttpClient.CryptPost(MethodStr, data); log.WriteLogFile(ret, "GetSyncDev"); if (!string.IsNullOrEmpty(ret)) { var Jo = JObject.Parse(ret); string code = Jo.Value("code"); if (code == "200") { try { var dicData = Jo.Value("data"); if (dicData != null) { //app.deviceid = dicData["code"].ToString(); string ip = dicData["ip"].ToString(); log.WriteLogFile(ip, "GetSyncDev"); log.WriteLogFile("------" + app.websocketProgramIP, "GetSyncDev"); if (app.websocketProgramIP != ip) { app.websocketProgramIP = ip; if (app.isProgramServer) { if (app.websocketForProgramServer == null) { app.websocketForProgramServer = new WebsocketForProgramServer(); } app.websocketForProgramServer.CloasSocket(); app.isProgramServer = false; } else { string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); } } if (AddressIP == ip) { app.isProgramServer = true; if (app.websocketForProgramServer == null) { app.websocketForProgramServer = new WebsocketForProgramServer(); } app.websocketForProgramServer.openSocket(); } } app.isProgramSync = false; if (app.webSocketForProgram == null) { app.webSocketForProgram = new WebSocketForProgram(); } app.webSocketForProgram.ReloadWebSocket(); log.WriteLogFile("app.isProgramSync--" + app.isProgramSync, "GetSyncDev"); } else { if (!app.isProgramSync) { if (app.webSocketForProgram == null) { app.webSocketForProgram = new WebSocketForProgram(); } app.webSocketForProgram.ReloadWebSocket(); } } } } catch (Exception e) { Mylog.WriteLogFile("-----error----" + e.Message, "GetSyncDevError"); } } else { if (app.isProgramServer) { if (app.websocketForProgramServer == null) { app.websocketForProgramServer = new WebsocketForProgramServer(); } app.websocketForProgramServer.CloasSocket(); app.isProgramServer = false; } if (app.isProgramSync) { if (app.webSocketForProgram == null) { app.webSocketForProgram = new WebSocketForProgram(); } app.webSocketForProgram.CloseWebSocket(); app.isProgramSync = false; } } } } catch (Exception e) { log.WriteLogFile(e.ToString(), "GetSyncDevError"); } } } }