using Shared.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.Specialized;
|
using System.IO;
|
using System.Net;
|
using System.Text;
|
using System.Threading.Tasks;
|
using ZigBee.Device;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// 分享的逻辑
|
/// </summary>
|
public class HdlShardLogic
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 分享的逻辑
|
/// </summary>
|
private static HdlShardLogic m_Current = null;
|
/// <summary>
|
/// 分享的逻辑
|
/// </summary>
|
public static HdlShardLogic Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new HdlShardLogic();
|
}
|
return m_Current;
|
}
|
set
|
{
|
m_Current = value;
|
}
|
}
|
#endregion
|
|
#region ■ 获取分享文件_______________________
|
|
/// <summary>
|
/// 将分享的数据存入本地,返回的是文件名字(异常时返回null),调用 GetShardFileContent 可以获取文件内的内容
|
/// </summary>
|
/// <param name="listDistributedMark"></param>
|
/// <returns></returns>
|
public async Task<List<string>> SetShardFileToLocation(List<string> listDistributedMark)
|
{
|
if (listDistributedMark.Count == 0)
|
{
|
return new List<string>();
|
}
|
ProgressBar.SetMaxValue(listDistributedMark.Count);
|
|
List<string> listFile = new List<string>();
|
//文件夹
|
string strDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory);
|
|
//不允许按系统的返回键
|
Shared.Common.CommonPage.BackKeyCanClick = false;
|
UserCenterResourse.Option.AppCanSignout = false;
|
|
foreach (string keys in listDistributedMark)
|
{
|
var dataPra = new { DistributedMark = keys, HouseDistributedMark = Common.Config.Instance.Home.Id, IsOtherAccountControl = Common.Config.Instance.isAdministrator };
|
var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeDataShare/GetOneShareData", false, dataPra);
|
if (result == null)
|
{
|
//允许按系统的返回键
|
Shared.Common.CommonPage.BackKeyCanClick = true;
|
UserCenterResourse.Option.AppCanSignout = true;
|
return null;
|
}
|
var dataResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetShardInfoResult>(result);
|
//文件名字
|
listFile.Add(dataResult.ShareName);
|
//保存到指定文件夹下
|
Global.WriteFileToDirectoryByBytes(strDir, dataResult.ShareName, dataResult.ShareDataBytes);
|
//设置进度值
|
ProgressBar.SetValue(1);
|
}
|
|
//允许按系统的返回键
|
Shared.Common.CommonPage.BackKeyCanClick = true;
|
UserCenterResourse.Option.AppCanSignout = true;
|
|
return listFile;
|
}
|
|
/// <summary>
|
/// 获取分享文件的内容
|
/// </summary>
|
/// <param name="fileName">文件名称(不是全路径)</param>
|
/// <returns></returns>
|
public byte[] GetShardFileContent(string fileName)
|
{
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, fileName);
|
if (System.IO.File.Exists(fullName) == false)
|
{
|
return null;
|
}
|
string path = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory);
|
var varByte = Global.ReadFileByDirectory(path, fileName);
|
return varByte;
|
}
|
|
/// <summary>
|
/// 获取本地全部的共享文件
|
/// </summary>
|
/// <returns></returns>
|
public List<string> GetLocalAllShardFile()
|
{
|
string path = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory);
|
var listFile = HdlAutoBackupLogic.GetFileFromDirectory(path);
|
|
return listFile;
|
}
|
|
/// <summary>
|
/// 文件是否存在
|
/// </summary>
|
/// <param name="fileName"></param>
|
/// <returns></returns>
|
public bool IsFileExists(string fileName)
|
{
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, fileName);
|
if (System.IO.File.Exists(fullName) == false)
|
{
|
return false;
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 添加分享文件_______________________
|
|
/// <summary>
|
/// 添加分享文件到缓存
|
/// </summary>
|
/// <param name="fileName">原根目录下的文件名</param>
|
public void AddShardFile(string fileName)
|
{
|
if (Global.IsExistsByHomeId(fileName) == false)
|
{
|
return;
|
}
|
string oldName = UserCenterLogic.CombinePath(fileName);
|
string newName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, fileName);
|
System.IO.File.Copy(oldName, newName, true);
|
}
|
|
/// <summary>
|
/// 添加房间对象到缓存
|
/// </summary>
|
/// <param name="room">房间对象</param>
|
public void AddShardFile(Common.Room room)
|
{
|
room.IsSharedRoom = true;
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(room);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, room.FileName);
|
//写入内容
|
Shared.IO.FileUtils.WriteFileByBytes(fullName, byteData);
|
}
|
|
/// <summary>
|
/// 添加场景对象到缓存
|
/// </summary>
|
/// <param name="scene">场景</param>
|
public void AddShardFile(Common.SceneUI scene)
|
{
|
scene.IsSharedScene = true;
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(scene);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, scene.FileName);
|
//写入内容
|
Shared.IO.FileUtils.WriteFileByBytes(fullName, byteData);
|
}
|
|
/// <summary>
|
/// 添加楼层对象到缓存
|
/// </summary>
|
/// <param name="dic">楼层</param>
|
public void AddShardFile(Dictionary<string, string> dic)
|
{
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, DirNameResourse.ShardFloorFile);
|
//写入内容
|
Shared.IO.FileUtils.WriteFileByBytes(fullName, byteData);
|
}
|
|
#endregion
|
|
#region ■ 删除分享文件_______________________
|
|
/// <summary>
|
/// 添加分享文件到缓存
|
/// </summary>
|
/// <param name="fileName">指定文件名字</param>
|
public void DeleteShardFile(string fileName)
|
{
|
fileName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, fileName);
|
if (System.IO.File.Exists(fileName) == true)
|
{
|
System.IO.File.Delete(fileName);
|
}
|
}
|
|
/// <summary>
|
/// 清空共享文件夹
|
/// </summary>
|
public void ClearShardDirectory()
|
{
|
//创建文件夹
|
string strDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory);
|
Global.CreateEmptyDirectory(strDir, true);
|
}
|
|
#endregion
|
|
#region ■ 获取成员共享列表___________________
|
|
/// <summary>
|
/// 从本地获取成员的分享列表
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据的缓存(调用这个函数会刷新这个变量的信息)</param>
|
public void GetMemberShardContentListFromLocal(MemberShardInfoData memberShardInfo)
|
{
|
memberShardInfo.dicShardRoom = new Dictionary<string, Common.Room>();
|
|
var listDeviceFile = new HashSet<string>();
|
var listSceneFile = new HashSet<string>();
|
|
var listFile = this.GetLocalAllShardFile();
|
//先初始化房间
|
foreach (string fileName in listFile)
|
{
|
if (fileName.StartsWith("Room_") == true)
|
{
|
//房间文件
|
var byteData = this.GetShardFileContent(fileName);
|
if (byteData != null)
|
{
|
string valueData = System.Text.Encoding.UTF8.GetString(byteData);
|
var roomTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.Room>(valueData);
|
memberShardInfo.dicShardRoom[fileName] = roomTemp;
|
}
|
}
|
else if (fileName == DirNameResourse.ShardFloorFile)
|
{
|
//楼层文件
|
var byteData = this.GetShardFileContent(fileName);
|
if (byteData != null)
|
{
|
string valueData = System.Text.Encoding.UTF8.GetString(byteData);
|
memberShardInfo.dicShardFloor = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(valueData);
|
}
|
}
|
else if (fileName.StartsWith("Device_") == true)
|
{
|
//设备文件
|
listDeviceFile.Add(fileName);
|
}
|
else if (fileName.StartsWith("Scene_") == true)
|
{
|
//场景文件
|
listSceneFile.Add(fileName);
|
}
|
}
|
|
//设置房间里面设备的UI对象(因为这个东西是反序列化出来的,设备UI对象是不序列化对象)
|
foreach (var tempRoom in memberShardInfo.dicShardRoom.Values)
|
{
|
//还原设备对象
|
tempRoom.DeviceUIList.Clear();
|
foreach (string uiPath in tempRoom.DeviceUIFilePathList)
|
{
|
string deviceFile = uiPath.Replace("DeviceUI_", string.Empty);
|
//这个设备文件匹配得到房间
|
listDeviceFile.Remove(deviceFile);
|
if (this.IsFileExists(deviceFile) == false)
|
{
|
continue;
|
}
|
tempRoom.DeviceUIList.Add(Common.LocalDevice.Current.GetDeviceUI(uiPath));
|
}
|
//还原场景对象
|
tempRoom.SceneUIList.Clear();
|
foreach (string uiPath in tempRoom.SceneUIFilePathList)
|
{
|
//这个场景文件匹配得到房间
|
listSceneFile.Remove(uiPath);
|
var byteData = this.GetShardFileContent(uiPath);
|
if (byteData == null)
|
{
|
continue;
|
}
|
string valueData = System.Text.Encoding.UTF8.GetString(byteData);
|
var tempUi = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(valueData);
|
tempRoom.SceneUIList.Add(tempUi);
|
}
|
}
|
//如果存在匹配不上的文件
|
if (listDeviceFile.Count != 0 || listSceneFile.Count != 0)
|
{
|
//创建一个临时房间来存储
|
var roomTemp = new Room();
|
roomTemp.Id = "Other";
|
roomTemp.FloorId = "Other";
|
memberShardInfo.dicShardRoom[roomTemp.FileName] = roomTemp;
|
roomTemp.Name = Language.StringByID(R.MyInternationalizationString.uSharedRoom);
|
//还原设备对象
|
foreach (string deviceFile in listDeviceFile)
|
{
|
roomTemp.DeviceUIList.Add(Common.LocalDevice.Current.GetDeviceUI(deviceFile));
|
}
|
//还原场景对象
|
foreach (string uiPath in listSceneFile)
|
{
|
var byteData = this.GetShardFileContent(uiPath);
|
string valueData = System.Text.Encoding.UTF8.GetString(byteData);
|
var tempUi = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(valueData);
|
roomTemp.SceneUIList.Add(tempUi);
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 同步服务器的分享内容_______________
|
|
/// <summary>
|
/// 同步服务器的分享内容(里面只负责把东西存入本地)
|
/// </summary>
|
public async Task<bool> SynchronizeDbSharedContent()
|
{
|
if (UserCenterResourse.UserInfo.AuthorityNo != 3)
|
{
|
//只有成员才有这个概念
|
return true;
|
}
|
|
//获取主人分享给成员的文件列表
|
var infoPra = new { DistributedMark = Config.Instance.Guid, HouseDistributedMark = Common.Config.Instance.Home.Id, IsOtherAccountControl = Common.Config.Instance.isAdministrator };
|
var listNotShow = new List<string>() { "NotSetAgain" };
|
var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeDataShare/GetShareDataBySubAccount", false, infoPra, listNotShow);
|
if (string.IsNullOrEmpty(result) == true)
|
{
|
return false;
|
}
|
var listShardInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GetShardInfoResult>>(result);
|
|
//主键
|
var listAddMark = new List<string>();
|
//检测取消了共享的操作使用
|
var listShardFile = new HashSet<string>();
|
//获取前回共享文件的更新日期(keys:文件名字,value:更新日期)
|
var dicUpdateTime = this.GetAllShardFileAgoUpdateTime();
|
|
//先看看这分享数据里面有什么鬼
|
foreach (var info in listShardInfo)
|
{
|
string updateTime = info.TimeSpan;
|
string fileName = info.ShareName;
|
listShardFile.Add(fileName);
|
|
//如果本地没有,或者更新日期大于前回更新日期的时候
|
if (Global.IsExistsByHomeId(fileName) == false
|
|| dicUpdateTime.ContainsKey(fileName) == false
|
|| updateTime.CompareTo(dicUpdateTime[fileName]) > 0)
|
{
|
listAddMark.Add(info.DistributedMark);
|
//更新日更新
|
dicUpdateTime[fileName] = updateTime;
|
}
|
}
|
|
//从云端获取下来文件
|
if (listAddMark.Count > 0)
|
{
|
//强制指定文本附加信息:共享数据同步中
|
string msg = Language.StringByID(R.MyInternationalizationString.uShardDataIsSynchronizing);
|
//开启进度条
|
ProgressBar.Show(msg);
|
ProgressBar.SetAppendText(msg);
|
|
//将分享的数据存入本地(获取的是本地没有的)
|
this.ClearShardDirectory();
|
var listDbFile = await this.SetShardFileToLocation(listAddMark);
|
if (listDbFile == null)
|
{
|
//关闭
|
ProgressBar.Close();
|
return false;
|
}
|
|
//生成文件
|
foreach (string fileName in listDbFile)
|
{
|
var data = this.GetShardFileContent(fileName);
|
Global.WriteFileByBytesByHomeId(fileName, data);
|
if (fileName.StartsWith("Room_") == true)
|
{
|
//房间文件特殊处理
|
Config.Instance.Home.AddRoomListFilePath(fileName);
|
}
|
}
|
//清空共享文件夹
|
this.ClearShardDirectory();
|
}
|
|
//同步服务器的取消了的分享内容
|
return await this.SynchronizeDeleteSharedContent(listShardFile, dicUpdateTime);
|
}
|
|
/// <summary>
|
/// 同步服务器的取消了的分享内容
|
/// </summary>
|
/// <param name="listShardFile">云端上面分享的文件,为Null时从新获取</param>
|
/// <param name="dicUpdateTime">分享文件的更新时间,为Null时从新获取</param>
|
/// <returns></returns>
|
public async Task<bool> SynchronizeDeleteSharedContent(HashSet<string> listShardFile = null, Dictionary<string, string> dicUpdateTime = null)
|
{
|
if (UserCenterResourse.UserInfo.AuthorityNo != 3)
|
{
|
//只有成员才有这个概念
|
return true;
|
}
|
//强制指定文本附加信息:共享数据同步中
|
string msg = Language.StringByID(R.MyInternationalizationString.uShardDataIsSynchronizing);
|
//开启进度条
|
ProgressBar.Show(msg);
|
ProgressBar.SetAppendText(msg);
|
|
if (dicUpdateTime == null)
|
{
|
//获取前回共享文件的更新日期(keys: 文件名字, value: 更新日期)
|
dicUpdateTime = this.GetAllShardFileAgoUpdateTime();
|
}
|
|
//重新获取
|
if (listShardFile == null)
|
{
|
listShardFile = new HashSet<string>();
|
//获取主人分享给成员的文件列表
|
var infoPra = new { DistributedMark = Config.Instance.Guid, HouseDistributedMark = Common.Config.Instance.Home.Id, IsOtherAccountControl = Common.Config.Instance.isAdministrator };
|
var listNotShow = new List<string>() { "NotSetAgain" };
|
var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeDataShare/GetShareDataBySubAccount", false, infoPra, listNotShow);
|
if (string.IsNullOrEmpty(result) == true)
|
{
|
//关闭
|
ProgressBar.Close();
|
return false;
|
}
|
var listShardData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GetShardInfoResult>>(result);
|
//先看看这分享数据里面有什么鬼
|
foreach (var info in listShardData)
|
{
|
listShardFile.Add(info.ShareName);
|
}
|
}
|
|
//然后检测本地的文件
|
var listLocalFile = Global.FileListByHomeId();
|
foreach (string fileName in listLocalFile)
|
{
|
if (listShardFile.Contains(fileName) == true)
|
{
|
//本地的这个文件还分享着,没有问题
|
continue;
|
}
|
//检测本地设备文件,是否存在已经取消了共享了的
|
if (fileName.StartsWith(Common.LocalDevice.deviceFirstName) == true)
|
{
|
//删除掉这个设备文件
|
Global.DeleteFilebyHomeId(fileName);
|
dicUpdateTime.Remove(fileName);
|
}
|
//检测本地房间文件,是否存在已经取消了共享了的
|
else if (fileName.StartsWith("Room_") == true)
|
{
|
var roomData = Global.ReadFileByHomeId(fileName);
|
var nowRoom = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(Encoding.UTF8.GetString(roomData));
|
if (nowRoom.IsSharedRoom == false)
|
{
|
//这个房间是他自己创建的
|
continue;
|
}
|
|
//删除掉这个房间文件
|
Global.DeleteFilebyHomeId(fileName);
|
dicUpdateTime.Remove(fileName);
|
if (Config.Instance.Home.RoomFilePathList.Contains(fileName) == true)
|
{
|
Config.Instance.Home.RemoveRoomListFilePath(fileName);
|
}
|
}
|
//检测本地场景文件,是否存在已经取消了共享了的
|
else if (fileName.StartsWith("Scene_") == true)
|
{
|
var sceneData = Global.ReadFileByHomeId(fileName);
|
var nowScene = Newtonsoft.Json.JsonConvert.DeserializeObject<SceneUI>(Encoding.UTF8.GetString(sceneData));
|
if (nowScene.IsSharedScene == false)
|
{
|
//这个场景是他自己创建的
|
continue;
|
}
|
//删除掉这个场景文件
|
Global.DeleteFilebyHomeId(fileName);
|
dicUpdateTime.Remove(fileName);
|
}
|
}
|
//网关文件非合法性是刷新设备列表的函数里面实现
|
|
//保存全部分享文件的更新日期
|
this.SaveAllShardFileAgoUpdateTime(dicUpdateTime);
|
//关闭
|
ProgressBar.Close();
|
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 同步内容更新时间___________________
|
|
/// <summary>
|
/// 获取全部分享文件的前回更新日期
|
/// </summary>
|
/// <returns></returns>
|
private Dictionary<string, string> GetAllShardFileAgoUpdateTime()
|
{
|
var dicTime = new Dictionary<string, string>();
|
var data = Global.ReadFileByDirectory(DirNameResourse.LocalMemoryDirectory, DirNameResourse.ShardFileUpdateFile);
|
if (data == null)
|
{
|
//目标文件不存在
|
return dicTime;
|
}
|
dicTime = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(System.Text.Encoding.UTF8.GetString(data));
|
return dicTime;
|
}
|
|
/// <summary>
|
/// 保存全部分享文件的更新日期
|
/// </summary>
|
/// <param name="dicTime"></param>
|
private void SaveAllShardFileAgoUpdateTime(Dictionary<string, string> dicTime)
|
{
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(dicTime);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
Global.WriteFileToDirectoryByBytes(DirNameResourse.LocalMemoryDirectory, DirNameResourse.ShardFileUpdateFile, byteData);
|
}
|
|
#endregion
|
|
//----------------------------------分割线(数据预备)-------------------------------------------------
|
|
#region ■ 数据预备___________________________
|
|
/// <summary>
|
/// 获取成员共享列表然后保存到本地(用完之后最好调用清空 ClearShardDirectory函数清空)
|
/// </summary>
|
/// <param name="SubAccountDistributedMark">成员列表接口返回的SubAccountDistributedMark</param>
|
/// <param name="memberShardInfo">
|
/// <para>成员的分享数据</para>
|
/// <para>如果不知道这是什么东西,就New一个,然后缓存起来,调用其他函数都需要这个东西</para>
|
/// <para>New的时候记得对SubAccountDistributedMark赋值,它是成员列表接口返回的SubAccountDistributedMark</para>
|
/// </param>
|
/// <returns></returns>
|
public async Task<bool> GetMemberShardContentListAndSetToLocation(MemberShardInfoData memberShardInfo)
|
{
|
if (memberShardInfo.Refresh == false)
|
{
|
return true;
|
}
|
memberShardInfo.Refresh = false;
|
|
//打开进度条
|
ProgressBar.Show();
|
|
var infoPra = new
|
{
|
DistributedMark = memberShardInfo.SubAccountDistributedMark,
|
HouseDistributedMark = Shared.Common.Config.Instance.HomeId,
|
IsOtherAccountControl = Common.Config.Instance.isAdministrator
|
};
|
var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeDataShare/GetShareDataBySubAccount", false, infoPra);
|
if (result == null)
|
{
|
//关闭进度条
|
ProgressBar.Close();
|
return false;
|
}
|
var listData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ReceiveShardInfoResult>>(result);
|
//先清空共享文件夹
|
this.ClearShardDirectory();
|
|
var listMark = new List<string>();
|
foreach (var data in listData)
|
{
|
//性能优化:如果本地拥有这个文件的话,直接使用那个文件
|
string fileName = data.ShareName;
|
//记录起它的主键
|
memberShardInfo.dicAllShardKeys[fileName] = data.DistributedMark;
|
|
if (fileName.StartsWith("Room_") == true || fileName.StartsWith("Scene_") == true)
|
{
|
//房间文件和场景文件需要重新反序列化
|
listMark.Add(data.DistributedMark);
|
continue;
|
}
|
if (Shared.Common.Global.IsExistsByHomeId(fileName) == true)
|
{
|
//其他文件的话,如果本地存在,则以本地文件为准
|
this.AddShardFile(fileName);
|
continue;
|
}
|
listMark.Add(data.DistributedMark);
|
}
|
|
//将分享的数据存入本地,返回的是文件名字(异常时返回null)
|
var listFile = await this.SetShardFileToLocation(listMark);
|
if (listFile == null)
|
{
|
//关闭进度条
|
ProgressBar.Close();
|
return false;
|
}
|
//关闭进度条
|
ProgressBar.Close();
|
|
return true;
|
}
|
|
#endregion
|
|
//----------------------------------分割线(上传新的分享)---------------------------------------------
|
|
#region ■ 执行上传新的分享___________________
|
|
/// <summary>
|
/// 执行上传新的分享
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据</param>
|
/// <param name="nowRoom">当前的房间对象</param>
|
/// <param name="listDevice">选择上传的设备</param>
|
/// <param name="listScene">选择上传的场景(不要获取它里面的绑定列表)</param>
|
/// <param name="BarMaxValue">进度条的最大值,如果不为-1,则内部不会自动弹出进度条</param>
|
/// <param name="listCheckFile">文件重复上传检测</param>
|
public async Task<bool> DoUploadSharedContent(MemberShardInfoData memberShardInfo, Common.Room nowRoom,
|
List<CommonDevice> listDevice, List<Common.SceneUI> listScene, int BarMaxValue = -1, HashSet<string> listCheckFile = null)
|
{
|
if (listDevice.Count == 0 && listScene.Count == 0)
|
{
|
return true;
|
}
|
var listCheck = new HashSet<string>();
|
|
//获取场景里面嵌套的子设备和子场景
|
var listChirdDevice = new List<CommonDevice>();
|
var listChirdScene = new List<Common.SceneUI>();
|
foreach (var SceneTemp in listScene)
|
{
|
//从缓存获取场景的执行目标
|
this.GetSceneDeviceList(SceneTemp, listCheck, listChirdScene, listChirdDevice);
|
}
|
|
//这个也是备份
|
int BackgroundImageType = 0;
|
string BackgroundImage = string.Empty;
|
if (memberShardInfo.dicShardRoom.ContainsKey(nowRoom.FileName) == true)
|
{
|
BackgroundImageType = memberShardInfo.dicShardRoom[nowRoom.FileName].BackgroundImageType;
|
BackgroundImage = memberShardInfo.dicShardRoom[nowRoom.FileName].BackgroundImage;
|
}
|
//防止出现错误,暂时保存两个列表(虽然没那么麻烦,不过既然已经这样写了就这样了)
|
var dicBackDevice = new Dictionary<string, List<string>>();
|
var dicBackScene = new Dictionary<string, List<string>>();
|
//临时备份房间设备列表
|
this.BackupRoomDevicelistTemporary(memberShardInfo, dicBackDevice, dicBackScene);
|
|
//获取上传的文件,然后将文件复制到指定文件夹
|
var listDelPic = new List<string>();
|
var listFile = this.GetUploadListFile(memberShardInfo, nowRoom, listDevice, listScene, listChirdDevice, listChirdScene, listDelPic);
|
|
//不允许按系统的返回键
|
Shared.Common.CommonPage.BackKeyCanClick = false;
|
UserCenterResourse.Option.AppCanSignout = false;
|
|
if (BarMaxValue == -1)
|
{
|
//打开进度条
|
ProgressBar.Show();
|
//设置最大值
|
ProgressBar.SetMaxValue(listFile.Count);
|
}
|
else
|
{
|
//设置最大值
|
ProgressBar.SetMaxValue(BarMaxValue);
|
}
|
|
//上传分享
|
var result = await this.DoUploadShardContent(memberShardInfo, listFile, listCheckFile);
|
if (result == true)
|
{
|
//删除指定自定义图片
|
result = await this.DoDeleteSharedContent(memberShardInfo, listDelPic);
|
}
|
if (BarMaxValue == -1)
|
{
|
//关闭进度条
|
ProgressBar.Close();
|
}
|
|
//允许按系统的返回键
|
Shared.Common.CommonPage.BackKeyCanClick = true;
|
UserCenterResourse.Option.AppCanSignout = true;
|
|
if (result == false)
|
{
|
//恢复原数据
|
if (memberShardInfo.TempRoom == null)
|
{
|
memberShardInfo.dicShardRoom[nowRoom.FileName].BackgroundImageType = BackgroundImageType;
|
memberShardInfo.dicShardRoom[nowRoom.FileName].BackgroundImage = BackgroundImage;
|
}
|
this.RecoverRoomDevicelistTemporary(memberShardInfo, dicBackDevice, dicBackScene, listFile);
|
}
|
else
|
{
|
//覆盖数据
|
if (memberShardInfo.TempRoom != null)
|
{
|
memberShardInfo.dicShardRoom[nowRoom.FileName] = memberShardInfo.TempRoom;
|
}
|
}
|
memberShardInfo.TempRoom = null;
|
|
return result;
|
}
|
|
/// <summary>
|
/// 获取上传的文件名
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据</param>
|
/// <param name="nowRoom">需要上传到云端的房间对象(注意,这个房间是主人的房间对象,而不是虚拟的)</param>
|
/// <param name="listDevice">分享的设备列表(注意,这个东西是主人的)</param>
|
/// <param name="listScene">分享的场景列表(注意,这个东西是主人的)</param>
|
/// <param name="listChirdDevice">场景里面递归获取的设备</param>
|
/// <param name="listChirdScene">场景里面递归获取的场景</param>
|
/// <param name="listDelPic">需要删除的图片</param>
|
/// <returns></returns>
|
private List<string> GetUploadListFile(MemberShardInfoData memberShardInfo, Common.Room nowRoom, List<CommonDevice> listDevice, List<Common.SceneUI> listScene,
|
List<CommonDevice> listChirdDevice, List<Common.SceneUI> listChirdScene, List<string> listDelPic)
|
{
|
var listFile = new List<string>();
|
|
Common.Room roomTemp = null;
|
if (memberShardInfo.dicShardRoom.ContainsKey(nowRoom.FileName) == false)
|
{
|
//克隆一个对象
|
roomTemp = nowRoom.CloneRoomClass();
|
memberShardInfo.TempRoom = roomTemp;
|
//新分享的房间,如果图片是自定义的,则上传图片
|
if (roomTemp.BackgroundImageType != 0)
|
{
|
//自定义的图片,它存在于本地,但是它是全路径
|
string[] Arry = roomTemp.BackgroundImage.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
listFile.Add(Arry[Arry.Length - 1]);
|
this.AddShardFile(Arry[Arry.Length - 1]);
|
}
|
}
|
else
|
{
|
roomTemp = memberShardInfo.dicShardRoom[nowRoom.FileName];
|
//如果云端分享的房间的图片和主人的不一样的时候
|
if (roomTemp.BackgroundImage != nowRoom.BackgroundImage)
|
{
|
if (roomTemp.BackgroundImageType != 0)
|
{
|
//删除自定义图片
|
string[] Arry = roomTemp.BackgroundImage.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
listDelPic.Add(Arry[Arry.Length - 1]);
|
}
|
if (nowRoom.BackgroundImageType != 0)
|
{
|
//自定义的图片,它存在于本地,但是它是全路径
|
string[] Arry = nowRoom.BackgroundImage.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
listFile.Add(Arry[Arry.Length - 1]);
|
this.AddShardFile(Arry[Arry.Length - 1]);
|
}
|
//变更图片设置
|
roomTemp.BackgroundImageType = nowRoom.BackgroundImageType;
|
roomTemp.BackgroundImage = nowRoom.BackgroundImage;
|
}
|
}
|
|
foreach (var device in listDevice)
|
{
|
//设备
|
listFile.Add(device.FilePath);
|
this.AddShardFile(device.FilePath);
|
|
//设备的UI
|
var deviceUi = $"DeviceUI_{device.FilePath}";
|
if (roomTemp.DeviceUIFilePathList.Contains(deviceUi) == false)
|
{
|
roomTemp.DeviceUIFilePathList.Add(deviceUi);
|
}
|
}
|
//递归获取的设备不需要加入到房间
|
foreach (var device in listChirdDevice)
|
{
|
//设备
|
listFile.Add(device.FilePath);
|
this.AddShardFile(device.FilePath);
|
}
|
|
//场景
|
foreach (var secene in listScene)
|
{
|
if (secene.IconPathType != 0)
|
{
|
//自定义的图片,它存在于本地,但是它是全路径
|
string[] Arry = secene.IconPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
listFile.Add(Arry[Arry.Length - 1]);
|
this.AddShardFile(Arry[Arry.Length - 1]);
|
}
|
|
listFile.Add(secene.FileName);
|
this.AddShardFile(secene);
|
|
if (roomTemp.SceneUIFilePathList.Contains(secene.FileName) == false)
|
{
|
roomTemp.SceneUIFilePathList.Add(secene.FileName);
|
}
|
}
|
//递归获取的场景不需要加入到房间
|
foreach (var secene in listChirdScene)
|
{
|
listFile.Add(secene.FileName);
|
this.AddShardFile(secene);
|
}
|
//房间文件也给过去
|
listFile.Add(roomTemp.FileName);
|
this.AddShardFile(roomTemp);
|
|
//反正楼层文件很小,一起给过去吧
|
if (roomTemp.FloorId != string.Empty)
|
{
|
memberShardInfo.dicShardFloor[roomTemp.FloorId] = roomTemp.FloorName; ;
|
}
|
this.AddShardFile(memberShardInfo.dicShardFloor);
|
listFile.Add(DirNameResourse.ShardFloorFile);
|
|
return listFile;
|
}
|
|
/// <summary>
|
/// 从网关获取场景的目标设备列表
|
/// </summary>
|
/// <param name="scene">场景</param>
|
/// <param name="listCheck">重复检测用</param>
|
/// <param name="listSceneUI">添加分享的场景列表</param>
|
/// <param name="listDevice">添加分享的设备列表</param>
|
/// <returns></returns>
|
public void GetSceneDeviceList(Common.SceneUI scene, HashSet<string> listCheck, List<Common.SceneUI> listSceneUI, List<CommonDevice> listDevice)
|
{
|
foreach (var data in scene.AddSceneMemberDataList)
|
{
|
//设备
|
if (data.Type == 0)
|
{
|
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.DeviceAddr, data.Epoint);
|
if (listCheck.Contains(mainKeys) == true)
|
{
|
//不重复添加
|
continue;
|
}
|
listCheck.Add(mainKeys);
|
|
var device = Common.LocalDevice.Current.GetDevice(data.DeviceAddr, data.Epoint);
|
if (device != null)
|
{
|
listDevice.Add(device);
|
}
|
}
|
//场景
|
else if (data.Type == 1)
|
{
|
string mainKeys = data.ElseScenesId.ToString();
|
if (listCheck.Contains(mainKeys) == true)
|
{
|
//不重复添加
|
continue;
|
}
|
listCheck.Add(mainKeys);
|
|
//获取场景对象
|
var sceneUi = Room.CurrentRoom.GetSceneUIBySceneId(data.ElseScenesId);
|
if (sceneUi != null)
|
{
|
listSceneUI.Add(sceneUi);
|
}
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 上传新的分享_______________________
|
|
/// <summary>
|
/// 上传分享(返回云端分享文件的主键:DistributedMark)
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据</param>
|
/// <param name="listFile">上传的文件名</param>
|
/// <param name="listCheckFile">重复文件上传检测</param>
|
/// <returns></returns>
|
private async Task<bool> DoUploadShardContent(MemberShardInfoData memberShardInfo, List<string> listFile, HashSet<string> listCheckFile)
|
{
|
var dicKeys = new Dictionary<string, string>();
|
for (int i = 0; i < listFile.Count; i++)
|
{
|
string fileName = listFile[i];
|
if (listCheckFile != null)
|
{
|
if (listCheckFile.Contains(fileName) == true)
|
{
|
//重复文件不再上传
|
continue;
|
}
|
listCheckFile.Add(fileName);
|
}
|
if (fileName.EndsWith(".png") == true)
|
{
|
//上传图片
|
var result = this.UpLoadBigBackupFileToDB(memberShardInfo, fileName);
|
if (result == null)
|
{
|
return false;
|
}
|
dicKeys[fileName] = result;
|
}
|
else
|
{
|
var info = new UploadShardContent();
|
info.SubAccountDistributedMark = memberShardInfo.SubAccountDistributedMark;
|
info.ShareName = fileName;
|
info.ShareDataBytes = HdlShardLogic.Current.GetShardFileContent(fileName);
|
|
//追加共享
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(fileName) == false)
|
{
|
var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeDataShare/AddShareData", false, info);
|
if (string.IsNullOrEmpty(result) == true)
|
{
|
return false;
|
}
|
//这里有点特殊,接口是直接返回主键回来的,而不是Jsoin
|
dicKeys[fileName] = result;
|
}
|
else
|
{
|
info.DistributedMark = memberShardInfo.dicAllShardKeys[fileName];
|
var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeDataShare/EditShareData", false, info);
|
if (result == false)
|
{
|
return false;
|
}
|
dicKeys[fileName] = info.DistributedMark;
|
}
|
}
|
//设置进度值
|
ProgressBar.SetValue(1);
|
}
|
//如果没有错误,则添加临时缓存
|
foreach (var keys in dicKeys.Keys)
|
{
|
memberShardInfo.dicAllShardKeys[keys] = dicKeys[keys];
|
}
|
|
return true;
|
}
|
|
/// <summary>
|
/// 上传图片文件到云端
|
/// </summary>
|
/// <param name="fullDir">文件夹的全路径</param>
|
/// <param name="listPicFile">图片文件列表</param>
|
/// <returns></returns>
|
private string UpLoadBigBackupFileToDB(MemberShardInfoData memberShardInfo, string fileName)
|
{
|
string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadShardDirectory, fileName);
|
if (System.IO.File.Exists(fullName) == false)
|
{
|
return null;
|
}
|
var nvc = new NameValueCollection();
|
nvc.Add("ShareName", fileName);
|
nvc.Add("HouseDistributedMark", Common.Config.Instance.Home.Id);
|
nvc.Add("SubAccountDistributedMark", memberShardInfo.SubAccountDistributedMark);
|
nvc.Add("ShareDataBytes", Convert.ToBase64String(this.GetShardFileContent(fileName)));
|
nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.isAdministrator.ToString());
|
|
//追加共享
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(fileName) == false)
|
{
|
var result = this.UpLoadBigBackupFileToDB("ZigbeeDataShare/AddShareData", fullName, nvc);
|
if (string.IsNullOrEmpty(result) == true)
|
{
|
return null;
|
}
|
//这里有点特殊,接口是直接返回主键回来的,而不是Jsoin
|
return result;
|
}
|
else
|
{
|
nvc.Add("DistributedMark", memberShardInfo.dicAllShardKeys[fileName]);
|
var result = this.UpLoadBigBackupFileToDB("ZigbeeDataShare/EditShareData", fullName, nvc);
|
if (result != "1")
|
{
|
return null;
|
}
|
return memberShardInfo.dicAllShardKeys[fileName];
|
}
|
}
|
|
/// <summary>
|
/// 上传大文件
|
/// </summary>
|
/// <param name="RequestName">请求接口</param>
|
/// <param name="fullFileName">文件名字(含路径)</param>
|
/// <param name="nvc"></param>
|
/// <returns></returns>
|
private string UpLoadBigBackupFileToDB(string RequestName, string fullFileName, NameValueCollection nvc)
|
{
|
string paramName = "file";
|
string contentType = "application/octet-stream";
|
string requestUrl = $"{CommonPage.RequestHttpsHost}/{RequestName}";
|
//启用管理员权限
|
//if (Config.Instance.isAdministrator == true)
|
//{
|
// requestUrl = $"{Config.Instance.AdminRequestBaseUrl}/{RequestName}";
|
//}
|
//else
|
//{
|
// requestUrl = $"{CommonPage.RequestHttpsHost}/{RequestName}";
|
//}
|
|
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
|
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
|
var wr = (HttpWebRequest)WebRequest.Create(requestUrl);
|
wr.ContentType = "multipart/form-data; boundary=" + boundary;
|
wr.Method = "POST";
|
wr.KeepAlive = true;
|
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
|
|
var rs = wr.GetRequestStream();
|
|
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
foreach (string key in nvc.Keys)
|
{
|
rs.Write(boundarybytes, 0, boundarybytes.Length);
|
string formitem = string.Format(formdataTemplate, key, nvc[key]);
|
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
|
rs.Write(formitembytes, 0, formitembytes.Length);
|
}
|
rs.Write(boundarybytes, 0, boundarybytes.Length);
|
|
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
|
string header = string.Format(headerTemplate, paramName, fullFileName, contentType);
|
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
|
rs.Write(headerbytes, 0, headerbytes.Length);
|
|
var fileStream = new FileStream(fullFileName, FileMode.Open, FileAccess.Read);
|
byte[] buffer = new byte[4096];
|
int bytesRead = 0;
|
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
{
|
rs.Write(buffer, 0, bytesRead);
|
}
|
fileStream.Close();
|
|
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
rs.Write(trailer, 0, trailer.Length);
|
rs.Close();
|
|
WebResponse wresp = null;
|
try
|
{
|
wresp = wr.GetResponse();
|
Stream stream2 = wresp.GetResponseStream();
|
StreamReader reader2 = new StreamReader(stream2);
|
|
string result = reader2.ReadToEnd();
|
if (RequestName == "ZigbeeDataShare/AddShareData")
|
{
|
return result;
|
}
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResponsePack>(result);
|
|
bool flage = UserCenterLogic.CheckNotEorrorMsg(data, requestUrl);
|
if (flage == true)
|
{
|
return "1";
|
}
|
return "0";
|
}
|
catch
|
{
|
return "-1";
|
}
|
finally
|
{
|
if (wresp != null)
|
{
|
wresp.Close();
|
wresp = null;
|
}
|
wr = null;
|
}
|
}
|
|
|
#endregion
|
|
#region ■ 临时备份房间设备列表_______________
|
|
/// <summary>
|
/// 临时备份房间设备列表
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据</param>
|
/// <param name="dicBackDevice">空的设备备份列表</param>
|
/// <param name="dicBackScene">空的场景备份列表</param>
|
private void BackupRoomDevicelistTemporary(MemberShardInfoData memberShardInfo, Dictionary<string, List<string>> dicBackDevice,
|
Dictionary<string, List<string>> dicBackScene)
|
{
|
foreach (var roomKeys in memberShardInfo.dicShardRoom.Keys)
|
{
|
dicBackDevice[roomKeys] = new List<string>();
|
dicBackScene[roomKeys] = new List<string>();
|
|
dicBackDevice[roomKeys].AddRange(memberShardInfo.dicShardRoom[roomKeys].DeviceUIFilePathList);
|
dicBackScene[roomKeys].AddRange(memberShardInfo.dicShardRoom[roomKeys].SceneUIFilePathList);
|
}
|
}
|
|
/// <summary>
|
/// 还原临时备份的房间设备列表
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享数据</param>
|
/// <param name="dicBackDevice">临时备份的设备备份列表</param>
|
/// <param name="dicBackScene">临时备份的场景备份列表</param>
|
/// <param name="listFile">上传的文件名</param>
|
private void RecoverRoomDevicelistTemporary(MemberShardInfoData memberShardInfo, Dictionary<string, List<string>> dicBackDevice,
|
Dictionary<string, List<string>> dicBackScene, List<string> listFile)
|
{
|
foreach (string fileName in listFile)
|
{
|
//生成的文件全部删除,房间文件的话,下面会重新生成
|
this.DeleteShardFile(fileName);
|
}
|
|
foreach (var roomKeys in memberShardInfo.dicShardRoom.Keys)
|
{
|
if (dicBackDevice.ContainsKey(roomKeys) == false)
|
{
|
continue;
|
}
|
memberShardInfo.dicShardRoom[roomKeys].DeviceUIFilePathList.Clear();
|
memberShardInfo.dicShardRoom[roomKeys].DeviceUIFilePathList.AddRange(dicBackDevice[roomKeys]);
|
|
memberShardInfo.dicShardRoom[roomKeys].SceneUIFilePathList.Clear();
|
memberShardInfo.dicShardRoom[roomKeys].SceneUIFilePathList.AddRange(dicBackScene[roomKeys]);
|
|
//覆盖房间文件
|
this.AddShardFile(memberShardInfo.dicShardRoom[roomKeys]);
|
}
|
}
|
|
#endregion
|
|
//----------------------------------分割线(删除指定分享)---------------------------------------------
|
|
#region ■ 移除指定分享内容___________________
|
|
/// <summary>
|
/// 移除指定分享内容
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享信息</param>
|
/// <param name="nowRoom">当前房间</param>
|
/// <param name="listDevice">要取消的设备</param>
|
/// <param name="listSceneUI">要取消的场景</param>
|
/// <returns></returns>
|
public async Task<bool> DoDeleteSharedContent(MemberShardInfoData memberShardInfo, Common.Room nowRoom,
|
List<CommonDevice> listDevice, List<SceneUI> listSceneUI)
|
{
|
var roomTemp = memberShardInfo.dicShardRoom[nowRoom.FileName];
|
//防止出错,先备份列表
|
var listBackDevice = new List<string>();
|
listBackDevice.AddRange(roomTemp.DeviceUIFilePathList);
|
var listBackScene = new List<string>();
|
listBackScene.AddRange(roomTemp.SceneUIFilePathList);
|
|
var listMark = new List<string>();
|
//要删除的文件名字
|
var listDeleteFile = new List<string>();
|
|
//除了要删除的目标外,还分享的数据
|
var listHadShard = this.GetAllShardedFileNameFromDictionary(memberShardInfo, nowRoom, listDevice, listSceneUI);
|
|
//先弄设备
|
foreach (var device in listDevice)
|
{
|
string deviceFileName = device.FilePath;
|
|
//移除路径列表
|
roomTemp.DeviceUIFilePathList.Remove($"DeviceUI_{device.FilePath}");
|
if (listHadShard.Contains(deviceFileName) == true)
|
{
|
//其他场景分享有这个设备,所以不删除这个设备的根源文件,只是把房间的设备列表路径移除
|
continue;
|
}
|
|
//设备主键
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(deviceFileName) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[deviceFileName]);
|
}
|
listDeleteFile.Add(device.FilePath);
|
}
|
|
//再弄场景
|
foreach (var sceneUI in listSceneUI)
|
{
|
//移除缓存
|
roomTemp.SceneUIFilePathList.Remove(sceneUI.FileName);
|
|
//其他场景还嵌套着这个场景,所以不删除这个场景的根源文件,只是把房间的场景列表路径移除
|
//但是它绑定的设备目标要删除
|
if (listHadShard.Contains(sceneUI.FileName) == true)
|
{
|
continue;
|
}
|
//获取要删除的分享主键
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(sceneUI.FileName) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[sceneUI.FileName]);
|
}
|
listDeleteFile.Add(sceneUI.FileName);
|
|
//删除自定义图片,它存在于本地,但是它是全路径
|
if (sceneUI.IconPathType != 0)
|
{
|
string[] Arry = sceneUI.IconPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
string sceneFile = Arry[Arry.Length - 1];
|
listDeleteFile.Add(sceneFile);
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(sceneFile) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[sceneFile]);
|
}
|
}
|
|
//执行删除嵌套的设备
|
var listTemp = new HashSet<string>();
|
var dicBindScene = new Dictionary<string, SceneUI>();
|
this.GetSceneBindFileName(sceneUI, ref listTemp, ref dicBindScene);
|
foreach (var fileName in listTemp)
|
{
|
if (listHadShard.Contains(fileName) == true)
|
{
|
continue;
|
}
|
//获取要删除的分享主键
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(fileName) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[fileName]);
|
}
|
listDeleteFile.Add(fileName);
|
|
if (dicBindScene.ContainsKey(fileName) == true && dicBindScene[fileName].IconPathType != 0)
|
{
|
//删除自定义场景图片,它存在于本地,但是它是全路径
|
string[] Arry = dicBindScene[fileName].IconPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
string sceneFile = Arry[Arry.Length - 1];
|
listDeleteFile.Add(sceneFile);
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(sceneFile) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[sceneFile]);
|
}
|
}
|
}
|
}
|
|
//如果这个房间的分享设备和场景全部删除的话,把分享房间文件也一起删除
|
if (roomTemp.DeviceUIFilePathList.Count == 0 && roomTemp.SceneUIFilePathList.Count == 0)
|
{
|
//房间删除
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(nowRoom.FileName) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[nowRoom.FileName]);
|
}
|
listDeleteFile.Add(nowRoom.FileName);
|
|
if (nowRoom.BackgroundImageType != 0)
|
{
|
//删除自定义房间图片,它存在于本地,但是它是全路径
|
string[] Arry = nowRoom.BackgroundImage.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
string roomFile = Arry[Arry.Length - 1];
|
listDeleteFile.Add(roomFile);
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(roomFile) == true)
|
{
|
listMark.Add(memberShardInfo.dicAllShardKeys[roomFile]);
|
}
|
}
|
}
|
|
//执行移除分享数据
|
var result = await this.DoDeleteSharedContent(memberShardInfo, nowRoom, listMark, listDeleteFile);
|
//同步房间文件
|
if (result == true && listDeleteFile.Contains(roomTemp.FileName) == false)
|
{
|
//执行上传房间对象
|
result = await this.DoUploadRoomObject(memberShardInfo, roomTemp);
|
}
|
//同步楼层数据
|
if (result == true && listDeleteFile.Contains(roomTemp.FileName) == true)
|
{
|
int floorCount = 0;
|
foreach (var myRoom in memberShardInfo.dicShardRoom.Values)
|
{
|
if (myRoom.FloorId == roomTemp.FloorId) { floorCount++; }
|
}
|
if (floorCount == 0)
|
{
|
//如果该楼层已经删完房间了,则更新楼层
|
memberShardInfo.dicShardFloor.Remove(roomTemp.FloorId);
|
//执行上传对象
|
result = await this.DoUploadFloorObject(memberShardInfo);
|
}
|
|
}
|
|
if (result == false)
|
{
|
//恢复
|
roomTemp.DeviceUIFilePathList.Clear();
|
roomTemp.DeviceUIFilePathList.AddRange(listBackDevice);
|
|
roomTemp.SceneUIFilePathList.Clear();
|
roomTemp.SceneUIFilePathList.AddRange(listBackScene);
|
}
|
return result;
|
}
|
|
#endregion
|
|
#region ■ 执行移除分享数据___________________
|
|
/// <summary>
|
/// 执行移除分享数据
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享内容</param>
|
/// <param name="nowRoom">当前房间</param>
|
/// <param name="listMark">要移除的主键</param>
|
/// <param name="listDeleteFile">要删除的文件(房间文件需要删除的话,必须放在最后)</param>
|
/// <returns></returns>
|
private async Task<bool> DoDeleteSharedContent(MemberShardInfoData memberShardInfo, Common.Room nowRoom,
|
List<string> listMark, List<string> listDeleteFile)
|
{
|
if (listMark.Count == 0)
|
{
|
//有可能只删除一个回路,而这个回路是分配到多个房间的
|
return true;
|
}
|
|
var info = new DeleteShardInfo();
|
//开启进度条
|
ProgressBar.Show();
|
var listCheck = new HashSet<string>();
|
foreach (var markKeys in listMark)
|
{
|
//里面似乎有可能会出现重复
|
if (listCheck.Contains(markKeys) == true) { continue; }
|
listCheck.Add(markKeys);
|
|
info.DistributedMark = markKeys;
|
//执行删除
|
var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeDataShare/DeleteShareData", false, info);
|
if (result == false)
|
{
|
//关闭进度条
|
ProgressBar.Close();
|
return false;
|
}
|
}
|
//关闭进度条
|
ProgressBar.Close();
|
;
|
for (int i = 0; i < listDeleteFile.Count; i++)
|
{
|
string fileName = listDeleteFile[i];
|
if (i == listDeleteFile.Count - 1 && fileName.StartsWith("Room_") == true)
|
{
|
//最后一个固定可能是房间文件,直接移除整个对象
|
memberShardInfo.dicShardRoom.Remove(fileName);
|
}
|
memberShardInfo.dicAllShardKeys.Remove(fileName);
|
|
this.DeleteShardFile(fileName);
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 执行移除分享数据
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享内容</param>
|
/// <param name="listDelFile">删除的文件</param>
|
/// <returns></returns>
|
private async Task<bool> DoDeleteSharedContent(MemberShardInfoData memberShardInfo, List<string> listDelFile)
|
{
|
if (listDelFile.Count == 0)
|
{
|
return true;
|
}
|
|
var info = new DeleteShardInfo();
|
foreach (var fileName in listDelFile)
|
{
|
if (memberShardInfo.dicAllShardKeys.ContainsKey(fileName) == false)
|
{
|
//我也不知道为什么会找不到主键
|
continue;
|
}
|
|
info.DistributedMark = memberShardInfo.dicAllShardKeys[fileName];
|
//执行删除
|
var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeDataShare/DeleteShareData", false, info);
|
if (result == false)
|
{
|
return false;
|
}
|
}
|
for (int i = 0; i < listDelFile.Count; i++)
|
{
|
string fileName = listDelFile[i];
|
memberShardInfo.dicAllShardKeys.Remove(fileName);
|
|
this.DeleteShardFile(fileName);
|
}
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 上传房间对象_______________________
|
|
/// <summary>
|
/// 上传房间对象
|
/// </summary>
|
/// <param name="memberShardInfo">成员的共享内容</param>
|
/// <param name="upDateRoom">需要上传到云端的房间对象</param>
|
/// <returns></returns>
|
private async Task<bool> DoUploadRoomObject(MemberShardInfoData memberShardInfo, Common.Room upDateRoom)
|
{
|
if (upDateRoom.Id == "Other")
|
{
|
return true;
|
}
|
var info = new EditorShardContent();
|
info.DistributedMark = memberShardInfo.dicAllShardKeys[upDateRoom.FileName];
|
info.ShareName = upDateRoom.FileName;
|
info.SubAccountDistributedMark = memberShardInfo.SubAccountDistributedMark;
|
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(upDateRoom);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
info.ShareDataBytes = byteData;
|
var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeDataShare/EditShareData", false, info);
|
if (result == false)
|
{
|
return false;
|
}
|
//将房间对象序列化到缓存
|
this.AddShardFile(upDateRoom);
|
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 上传楼层对象_______________________
|
|
/// <summary>
|
/// 上传楼层对象
|
/// </summary>
|
/// <param name="memberShardInfo">成员的共享内容</param>
|
/// <returns></returns>
|
private async Task<bool> DoUploadFloorObject(MemberShardInfoData memberShardInfo)
|
{
|
var info = new EditorShardContent();
|
info.DistributedMark = memberShardInfo.dicAllShardKeys[DirNameResourse.ShardFloorFile];
|
info.ShareName = DirNameResourse.ShardFloorFile;
|
info.SubAccountDistributedMark = memberShardInfo.SubAccountDistributedMark;
|
|
var data = Newtonsoft.Json.JsonConvert.SerializeObject(memberShardInfo.dicShardFloor);
|
var byteData = System.Text.Encoding.UTF8.GetBytes(data);
|
info.ShareDataBytes = byteData;
|
var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeDataShare/EditShareData", false, info);
|
if (result == false)
|
{
|
return false;
|
}
|
//将房间对象序列化到缓存
|
this.AddShardFile(memberShardInfo.dicShardFloor);
|
|
return true;
|
}
|
|
#endregion
|
|
#region ■ 移除共享的一般方法_________________
|
|
/// <summary>
|
/// 从缓存字典中获取已经分享了的全部设备文件和场景的文件名字
|
/// </summary>
|
/// <param name="memberShardInfo">成员的分享信息</param>
|
/// <param name="nowRoom">当前房间</param>
|
/// <param name="listDeleteDevice">要删除的设备列表</param>
|
/// <param name="listDeleteScene">要删除的场景列表</param>
|
/// <returns></returns>
|
private HashSet<string> GetAllShardedFileNameFromDictionary(MemberShardInfoData memberShardInfo, Common.Room nowRoom,
|
List<CommonDevice> listDeleteDevice, List<Common.SceneUI> listDeleteScene)
|
{
|
//获取全部要删除的文件名字
|
var listDeleteFile = new HashSet<string>();
|
foreach (var device in listDeleteDevice)
|
{
|
listDeleteFile.Add(device.FilePath);
|
}
|
foreach (var scene in listDeleteScene)
|
{
|
listDeleteFile.Add(scene.FileName);
|
}
|
|
var listShardFile = new HashSet<string>();
|
//获取还处于分享状态的文件
|
foreach (var roomTemp in memberShardInfo.dicShardRoom.Values)
|
{
|
//设备
|
foreach (var deviceUi in roomTemp.DeviceUIFilePathList)
|
{
|
string deviceFile = deviceUi.Replace("DeviceUI_", string.Empty);
|
if (listDeleteFile.Contains(deviceFile) == true)
|
{
|
//因为一个回路只能分配一个区域,所以可以直接判断
|
continue;
|
}
|
if (listShardFile.Contains(deviceFile) == false)
|
{
|
//这个文件还分享着
|
listShardFile.Add(deviceFile);
|
}
|
|
}
|
//场景
|
foreach (var scene in roomTemp.SceneUIList)
|
{
|
if (roomTemp.SceneUIFilePathList.Contains(scene.FileName) == false)
|
{
|
//这个场景已经被删除
|
continue;
|
}
|
if (roomTemp.FileName == nowRoom.FileName)
|
{
|
//如果循环到了当前房间
|
if (listDeleteFile.Contains(scene.FileName) == true)
|
{
|
//如果是删除目标,则不添加
|
continue;
|
}
|
}
|
//获取场景里面的全部目标
|
var listTemp = new HashSet<string>();
|
var dicTemp = new Dictionary<string, SceneUI>();
|
this.GetSceneBindFileName(scene, ref listTemp, ref dicTemp);
|
|
if (listShardFile.Contains(scene.FileName) == false)
|
{
|
//场景自身的文件
|
listShardFile.Add(scene.FileName);
|
}
|
foreach (string file in listTemp)
|
{
|
if (listShardFile.Contains(file) == false)
|
{
|
listShardFile.Add(file);
|
}
|
}
|
}
|
}
|
return listShardFile;
|
}
|
|
/// <summary>
|
/// 从场景对象里面获取全部的绑定目标的文件名字
|
/// </summary>
|
/// <param name="scene">场景</param>
|
/// <param name="listFile">文件列表(里面存的是设备UI文件和设备文件和场景文件)</param>
|
/// <param name="dicBindScene">绑定的场景对象</param>
|
private void GetSceneBindFileName(Common.SceneUI scene, ref HashSet<string> listFile,ref Dictionary<string, SceneUI> dicBindScene)
|
{
|
foreach (var data in scene.AddSceneMemberDataList)
|
{
|
//设备
|
if (data.Type == 0)
|
{
|
var device = Common.LocalDevice.Current.GetDevice(data.DeviceAddr, data.Epoint);
|
if (device != null && listFile.Contains(device.FilePath) == false)
|
{
|
listFile.Add(device.FilePath);
|
}
|
}
|
if (data.Type == 1)
|
{
|
var temp = new SceneUI();
|
temp.Id = data.ElseScenesId;
|
var byteData = this.GetShardFileContent(temp.FileName);
|
if (byteData != null)
|
{
|
string valueData = System.Text.Encoding.UTF8.GetString(byteData);
|
var roomTemp = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.SceneUI>(valueData);
|
if (roomTemp != null)
|
{
|
dicBindScene[roomTemp.FileName] = roomTemp;
|
if (listFile.Contains(roomTemp.FileName) == false)
|
{
|
listFile.Add(roomTemp.FileName);
|
}
|
}
|
}
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 结构体类___________________________
|
|
/// <summary>
|
/// 上传分享的内容
|
/// </summary>
|
private class UploadShardContent : IfacePraCommon
|
{
|
/// <summary>
|
/// 分享数据的主键
|
/// </summary>
|
public string DistributedMark = string.Empty;
|
/// <summary>
|
/// 分享名字
|
/// </summary>
|
public string ShareName = string.Empty;
|
/// <summary>
|
/// 住宅ID
|
/// </summary>
|
public string HouseDistributedMark = Common.Config.Instance.Home.Id;
|
/// <summary>
|
/// 子账号的guid
|
/// </summary>
|
public string SubAccountDistributedMark = string.Empty;
|
/// <summary>
|
/// 分享内容
|
/// </summary>
|
public byte[] ShareDataBytes = null;
|
}
|
|
/// <summary>
|
/// 删除共享的内容
|
/// </summary>
|
private class DeleteShardInfo : IfacePraCommon
|
{
|
/// <summary>
|
/// 共享数据的唯一标识
|
/// </summary>
|
public string DistributedMark = null;
|
/// <summary>
|
/// 住宅ID
|
/// </summary>
|
public string HouseDistributedMark = Common.Config.Instance.Home.Id;
|
}
|
|
/// <summary>
|
/// 编辑共享内容
|
/// </summary>
|
private class EditorShardContent : IfacePraCommon
|
{
|
/// <summary>
|
/// 主键
|
/// </summary>
|
public string DistributedMark = string.Empty;
|
/// <summary>
|
/// 分享名字
|
/// </summary>
|
public string ShareName = string.Empty;
|
/// <summary>
|
/// 住宅ID
|
/// </summary>
|
public string HouseDistributedMark = Common.Config.Instance.Home.Id;
|
/// <summary>
|
/// 分享内容
|
/// </summary>
|
public byte[] ShareDataBytes = null;
|
/// <summary>
|
/// 子账号的Guid
|
/// </summary>
|
public string SubAccountDistributedMark = string.Empty;
|
}
|
|
/// <summary>
|
/// 接收分享数据
|
/// </summary>
|
private class ReceiveShardInfoResult
|
{
|
/// <summary>
|
/// 文件名字
|
/// </summary>
|
public string ShareName = string.Empty;
|
/// <summary>
|
/// 主键
|
/// </summary>
|
public string DistributedMark = string.Empty;
|
}
|
|
#endregion
|
}
|
}
|