HDL_ON/DAL/FileUtils.cs
@@ -1,19 +1,25 @@
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared;
using HDL_ON.UI;
using HDL_ON.DAL.Net;
using HDL_ON.DAL;
namespace HDL_ON.MyIO
namespace HDL_ON
{
    public static class FileUtils
    {
        public static string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/";
        /// <summary>
        /// 是否存在文件夹
        /// </summary>
        /// <returns></returns>
        public static bool IsExisFolder(string folderName)
        {
            bool result = false;
            var path = Path.Combine(RootPath, folderName);
            result = Directory.Exists(path);
            return result;
        }
        /// <summary>
        /// 创建一个住宅的备份文件夹
        /// </summary>
@@ -43,8 +49,7 @@
        {
            foreach (var fileName in ReadRegionFiles(path))
            {
                System.IO.File.Delete(System.IO.Path.Combine(path, fileName));
                MainPage.Log("Del region file :" + fileName);
                File.Delete(Path.Combine(path, fileName));
            }
        }
@@ -74,11 +79,15 @@
            foreach (FileInfo NextFile in thFolders)
            {
                index++;
                if (NextFile.Name == "null" || NextFile.Name == "UserConfig" || NextFile.Name == "Register_File" || NextFile.Name == "AccountListDB")
                if (NextFile.Name == "headImage.png")
                {
                    continue;
                }
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(regionRootPath + NextFile.Name);
                if (NextFile.Name == "null" || NextFile.Name == "UserConfig" || NextFile.Name == "UserInfo_File" || NextFile.Name == "AccountListDB")
                {
                    continue;
                }
                FileInfo fileInfo = new FileInfo(regionRootPath + NextFile.Name);
                if (fileInfo.Exists)
                {
                    fileInfo.MoveTo(Application.RootPath + NextFile.Name);
@@ -86,13 +95,11 @@
                }
            }
        }
        /// <summary>
        /// 遍历文件
        /// </summary>
        public static List<string> ReadFiles(bool reInit = true)
        public static List<string> ReadFiles()
        {
            List<string> listFiles = new List<string>();
            DirectoryInfo TheFolder = new DirectoryInfo(RootPath);
@@ -100,14 +107,6 @@
            {
                listFiles.Add(NextFile.Name);
            }
            try
            {
                if (reInit)
                {
                    MainPage.LocationFiles = listFiles;
                }
            }
            catch { }
            return listFiles;
        }
@@ -123,14 +122,11 @@
                return;
            }
            DeleteFile(newFilePath);
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(Application.RootPath + oldFilePath);
            FileInfo fileInfo = new FileInfo(Application.RootPath + oldFilePath);
            if (fileInfo.Exists)
            {
                fileInfo.MoveTo(Application.RootPath + newFilePath);
            }
            //if (!UserConfig.Instance.LocalFiles.Contains (newFilePath)) {
            //    UserConfig.Instance.LocalFiles.Add (newFilePath);
            //}
        }
@@ -147,9 +143,9 @@
                MainPage.Log ("文件夹:" + d.Name);
            }
#endif
            if (System.IO.Directory.Exists(srcPath))
            if (Directory.Exists(srcPath))
            {
                System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(srcPath);
                DirectoryInfo folder = new DirectoryInfo(srcPath);
                var path = Path.Combine(RootPath, destFolderPath);
                if (Directory.Exists(path))
                {
@@ -188,8 +184,8 @@
        /// <param name="filePath">File path.</param>
        public static bool Exists(string filePath)
        {
            //System.MainPage.WriteLog ("Exists==" + System.IO.Path.Combine (RootPath, filePath));
            return System.IO.File.Exists(System.IO.Path.Combine(RootPath, filePath));
            //System.MainPage.WriteLog ("Exists==" + Path.Combine (RootPath, filePath));
            return File.Exists(Path.Combine(RootPath, filePath));
        }
        /// <summary>
@@ -202,11 +198,8 @@
            {
                return;
            }
            System.IO.File.Delete(System.IO.Path.Combine(RootPath, fileName));
            File.Delete(Path.Combine(RootPath, fileName));
            MainPage.Log("删除文件名为:" + fileName);
            //if (UserConfig.Instance.LocalFiles.Contains (fileName)) {
            //    UserConfig.Instance.LocalFiles.Remove (fileName);
            //}
        }
        /// <summary>
@@ -218,57 +211,10 @@
            for (int j = 0; j < filesList.Count; j++)
            {
                var f = filesList[j];
                if (f == "linphonerc" || f == "AccountListDB")
                if (f == "linphonerc" || f == "AccountListDB" || f == "UserInfo_File")
                    continue;
                DeleteFile(f);
            }
        }
        /// <summary>
        /// Reads the equipment message.
        /// </summary>
        /// <returns>The equipment json data.</returns>
        /// <param name="common">Common.</param>
        /// <param name="isList">If set to <c>true</c> is list.</param>
        ///
        /// <param name="commonType">
        /// Commons是所有设备的基类,但是当传过来的Common是LightDevice时,LightDevice不是真正的设备,
        /// 真正的设备是它下面的调光器(LightDimming)或者继电器.
        /// 所以Common.GetType()获取不到真实的设备类型
        /// 所以这里要手动传入设备的类型
        /// </param>
        public static string ReadEquipmentMessage(Common common)
        {
            string fileName = "Equipment_";
            if (common.Type.ToString() == "WirelessPanel")
            {
                fileName += DeviceType.ButtonPanel.ToString() + "_" + common.SubnetID.ToString() + "_" + common.DeviceID.ToString();
            }
            else
            {
                fileName += common.Type + "_" + common.SubnetID.ToString() + "_" + common.DeviceID.ToString();
            }
            byte[] bytes1 = ReadFile(fileName);
            string one = System.Text.Encoding.UTF8.GetString(bytes1);
            return one;
        }
        /// <summary>
        /// Saves the equipment message.
        /// </summary>
        public static void SaveEquipmentMessage(Common common, string loopID = "")
        {
            string deviceType = common.Type.ToString();
            if (common.Type == DeviceType.LightSwitchSocket)
                deviceType = DeviceType.LightSwitch.ToString();
            string fileName = "Equipment_" + deviceType + "_" + common.SubnetID + "_" + common.DeviceID;
            if (loopID != "")
            {
                fileName += "_" + (loopID.Length < 2 ? "0" + loopID : loopID);
            }
            common.SavePath = fileName;
            MainPage.Log(fileName);
            WriteFileByBytes(fileName, CommonPage.MyEncodingUTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(common)));
        }
        /// <summary>
@@ -284,17 +230,14 @@
                return false;
            }
            System.IO.FileStream fs = null;
            FileStream fs = null;
            try
            {
                fs = new FileStream(System.IO.Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
                fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                MainPage.Log("SaveFile:" + fileName);
                //if (!UserConfig.Instance.LocalFiles.Contains (fileName)) {
                //    UserConfig.Instance.LocalFiles.Add (fileName);
                //}
                return true;
            }
            catch (Exception ex)
@@ -325,13 +268,13 @@
        /// <param name="fileName">File name.</param>
        /// <param name="inputStream">Input stream.</param>
        /// <param name="isNeedRootPath">If set to <c>true</c> is need root path.</param>
        public static bool WriteFileByInputStream(string fileName, System.IO.Stream inputStream)
        public static bool WriteFileByInputStream(string fileName, Stream inputStream)
        {
            if (fileName == null || (fileName = fileName.Trim()) == (""))
            {
                return false;
            }
            System.IO.FileStream fs = null;
            FileStream fs = null;
            try
            {
@@ -341,7 +284,7 @@
                {
                    if (fs == null)
                    {
                        fs = new FileStream(System.IO.Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
                        fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
                    }
                    fs.Write(buffer, 0, length);
                    fs.Flush();
@@ -383,14 +326,14 @@
            {
                fileName = "Phone/" + fileName;
            }
            System.IO.FileStream fs = null;
            FileStream fs = null;
            try
            {
                if (System.IO.File.Exists(System.IO.Path.Combine(RootPath, fileName)))
                if (File.Exists(Path.Combine(RootPath, fileName)))
                {
                    fs = new FileStream(System.IO.Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
                    fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
                }
                else if (System.IO.File.Exists(fileName))
                else if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                }
@@ -424,14 +367,14 @@
        public static byte[] ReadFile(string fileName)
        {
            System.IO.FileStream fs = null;
            FileStream fs = null;
            try
            {
                if (System.IO.File.Exists(System.IO.Path.Combine(RootPath, fileName)))
                if (File.Exists(Path.Combine(RootPath, fileName)))
                {
                    fs = new FileStream(System.IO.Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
                    fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
                }
                else if (System.IO.File.Exists(fileName))
                else if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                }
@@ -464,107 +407,122 @@
        }
        /// <summary>
        /// 下载电台、音乐、DLNA等等网络图片
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="url"></param>
        public static void DownLoadImage(string fileName, string url)
        /// <param name="imagePath"></param>
        public static void FileCopyToRootPath(string oldPath, string fileName)
        {
            #region 如果当前目录不存在,就初始化
            string directioryPath = null;
            if (Shared.Application.IsPad)
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(oldPath);
            if (fileInfo.Exists)
            {
                directioryPath = "Pad/" + "MusicImage";
            }
            else
            {
                directioryPath = "Phone/" + "MusicImage";
            }
            directioryPath = System.IO.Path.Combine(RootPath, directioryPath);
            if (!System.IO.Directory.Exists(directioryPath))
            {
                System.IO.Directory.CreateDirectory(directioryPath);
            }
            #endregion
            if (!System.IO.File.Exists(fileName))
            {
                System.Threading.Tasks.Task.Run(() =>
                {
                    FileStream fs = null;
                    try
                    {
                        HDL_ON.DAL.Net.MyWebClient webClient = new HDL_ON.DAL.Net.MyWebClient();
                        byte[] recevieBytes = webClient.DownloadData(new Uri(url));
                        fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                        fs.Write(recevieBytes, 0, recevieBytes.Length);
                        fs.Flush();
                    }
                    catch { }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                });
                fileInfo.CopyTo(Path.Combine(RootPath, fileName), true);
                //Utlis.WriteLine("FileCopyToRootPath : " + Path.Combine(RootPath, fileName));
            }
        }
        /// <summary>
        /// 下载电台、音乐、DLNA等等网络图片
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="url"></param>
        public static string DownLoadImageFormBaidu(string album, string artist)
        {
            var url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=" + artist;
            string directioryPath = "MusicImage";
            directioryPath = System.IO.Path.Combine(RootPath, directioryPath);
            if (!System.IO.Directory.Exists(directioryPath))
            {
                System.IO.Directory.CreateDirectory(directioryPath);
            }
            try
            {
                var song = Newtonsoft.Json.JsonConvert.DeserializeObject<Song>(new HDL_ON.DAL.Net.MyWebClient().DownloadString(new Uri(url)));
                if (song == null)
                {
                    return null;
                }
                foreach (var tempAlbum in song.album)
                {
                    if (tempAlbum.albumname == album && artist == tempAlbum.artistname)
                    {
                        var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempAlbum.artistpic.Split('@')[0] + "@s_1,w_160,h_160"));
                        var filePath = System.IO.Path.Combine(directioryPath, "AlbumArtistImage_" + album);
                        var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Flush();
                        fs.Close();
                        return filePath;
                    }
                }
        ///// <summary>
        ///// 下载电台、音乐、DLNA等等网络图片
        ///// </summary>
        ///// <param name="fileName"></param>
        ///// <param name="url"></param>
        //public static void DownLoadImage(string fileName, string url)
        //{
        //    #region 如果当前目录不存在,就初始化
        //    string directioryPath = null;
        //    if (Shared.Application.IsPad)
        //    {
        //        directioryPath = "Pad/" + "MusicImage";
        //    }
        //    else
        //    {
        //        directioryPath = "Phone/" + "MusicImage";
        //    }
        //    directioryPath = Path.Combine(RootPath, directioryPath);
        //    if (!Directory.Exists(directioryPath))
        //    {
        //        Directory.CreateDirectory(directioryPath);
        //    }
        //    #endregion
                foreach (var tempArtist in song.artist)
                {
                    if (artist == tempArtist.artistname)
                    {
                        var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempArtist.artistpic.Split('@')[0] + "@s_1,w_160,h_160"));
                        var filePath = System.IO.Path.Combine(directioryPath, "AlbumArtistImage_" + artist);
                        var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Flush();
                        fs.Close();
                        return filePath;
                    }
                }
        //    if (!File.Exists(fileName))
        //    {
        //        System.Threading.Tasks.Task.Run(() =>
        //        {
        //            FileStream fs = null;
        //            try
        //            {
        //                MyWebClient webClient = new MyWebClient();
        //                byte[] recevieBytes = webClient.DownloadData(new Uri(url));
        //                fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        //                fs.Write(recevieBytes, 0, recevieBytes.Length);
        //                fs.Flush();
        //            }
        //            catch { }
        //            finally
        //            {
        //                if (fs != null)
        //                {
        //                    fs.Close();
        //                }
        //            }
        //        });
        //    }
        //}
            }
            catch { }
            return null;
        }
        ///// <summary>
        ///// 下载电台、音乐、DLNA等等网络图片
        ///// </summary>
        ///// <param name="fileName"></param>
        ///// <param name="url"></param>
        //public static string DownLoadImageFormBaidu(string album, string artist)
        //{
        //    var url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=" + artist;
        //    string directioryPath = "MusicImage";
        //    directioryPath = Path.Combine(RootPath, directioryPath);
        //    if (!Directory.Exists(directioryPath))
        //    {
        //        Directory.CreateDirectory(directioryPath);
        //    }
        //    try
        //    {
        //        var song = Newtonsoft.Json.JsonConvert.DeserializeObject<Song>(new HDL_ON.DAL.Net.MyWebClient().DownloadString(new Uri(url)));
        //        if (song == null)
        //        {
        //            return null;
        //        }
        //        foreach (var tempAlbum in song.album)
        //        {
        //            if (tempAlbum.albumname == album && artist == tempAlbum.artistname)
        //            {
        //                var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempAlbum.artistpic.Split('@')[0] + "@s_1,w_160,h_160"));
        //                var filePath = Path.Combine(directioryPath, "AlbumArtistImage_" + album);
        //                var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
        //                fs.Write(bytes, 0, bytes.Length);
        //                fs.Flush();
        //                fs.Close();
        //                return filePath;
        //            }
        //        }
        //        foreach (var tempArtist in song.artist)
        //        {
        //            if (artist == tempArtist.artistname)
        //            {
        //                var bytes = new HDL_ON.DAL.Net.MyWebClient().DownloadData(new Uri(tempArtist.artistpic.Split('@')[0] + "@s_1,w_160,h_160"));
        //                var filePath = Path.Combine(directioryPath, "AlbumArtistImage_" + artist);
        //                var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
        //                fs.Write(bytes, 0, bytes.Length);
        //                fs.Flush();
        //                fs.Close();
        //                return filePath;
        //            }
        //        }
        //    }
        //    catch { }
        //    return null;
        //}
    }
    [System.Serializable]