using System;
using System.Collections.Generic;
namespace Shared.SimpleControl.Phone
{
public class SystemMusicPanel : FrameLayout
{
///
/// 显示绑定音乐面板界面
///
public void ShowSystemMusicPanel (MusicPanel musicPanel, Common bp)
{
#region top
RowLayout frameLayout = new RowLayout () {
Height = Application.GetRealHeight (100),
Width = LayoutParams.MatchParent,
BackgroundColor = SkinStyle.Current.MainColor
};
AddChidren (frameLayout);
Button backButton = new Button () {
Height = Application.GetRealHeight (90),
Width = Application.GetRealWidth (85),
UnSelectedImagePath = "Item/Back.png",
SelectedImagePath = "Item/BackSelected.png",
Gravity = Gravity.CenterVertical,
};
backButton.MouseUpEventHandler += (sender, e) => {
(Parent as PageLayout).PageIndex -= 1;
//SystemEquipmentBase.RefreshView (bp);
};
frameLayout.AddChidren (backButton);
EditText textButton = new EditText () {
X = Application.GetRealWidth (30) + backButton.Right,
Height = Application.GetRealHeight (50),
Width = Application.GetRealWidth (400),
Text = musicPanel.Name,
Gravity = Gravity.CenterVertical,
TextAlignment = TextAlignment.CenterLeft,
TextColor = SkinStyle.Current.TextColor1,
SelectedBackgroundColor = SkinStyle.Current.SysEditBox,
Enable = false,
TextSize = 15,
IsSelected = false,
};
frameLayout.AddChidren (textButton);
Button editor = new Button () {
X = Application.GetRealWidth (0),
Height = Application.GetRealHeight (90),
Width = Application.GetRealWidth (70),
UnSelectedImagePath = "Item/Editor.png",
SelectedImagePath = "Item/EditorSelected.png",
Gravity = Gravity.CenterVertical,
};
frameLayout.AddChidren (editor);
editor.MouseUpEventHandler += (sender, e) => {
if (editor.IsSelected) {
editor.IsSelected = textButton.IsSelected = textButton.Enable = false;
MainPage.Loading.Start ();
byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (textButton.Text.Trim ());
System.Threading.Tasks.Task.Run (() => {
byte [] updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, musicPanel.SubnetID, musicPanel.DeviceID, new byte [] {
musicPanel.BigClass,musicPanel.MinClass,musicPanel.LoopID});
if (updateBytes == null) {
Application.RunOnMainThread (() => {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
MainPage.Loading.Hide ();
});
return;
}
byte [] uBytes = new byte [20];
Array.Copy (remakeBytes, 0, uBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
Array.Copy (uBytes, 0, updateBytes, 3, 20 < uBytes.Length ? 20 : uBytes.Length);
var reBytes = Control.ControlBytesSendHasReturn (Command.SetDeviceLoopInfo, musicPanel.SubnetID, musicPanel.DeviceID, updateBytes);
if (reBytes != null) {
Application.RunOnMainThread (() => {
musicPanel.Name = textButton.Text.Trim ();
IO.FileUtils.SaveEquipmentMessage (musicPanel, musicPanel.LoopID.ToString ());
MainPage.Loading.Hide ();
});
} else {
Application.RunOnMainThread (() => {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
MainPage.Loading.Hide ();
});
}
});
} else {
textButton.Enable = textButton.IsSelected = editor.IsSelected = true;
}
};
#endregion
byte [] acBytes = new byte[26];
Button btnOutput = new Button () {
Width = Application.GetRealWidth (303),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (60),
Y = Application.GetRealHeight (100),
TextID = R.MyInternationalizationString.MusicModel,
TextAlignment = TextAlignment.CenterLeft,
};
AddChidren (btnOutput);
VerticalScrolViewLayout verticalScrolView = new VerticalScrolViewLayout {
Width = Application.GetRealWidth (508),
Height = Application.GetRealHeight (700),
Y = btnOutput.Bottom + Application.GetRealHeight (5),
Gravity = Gravity.CenterHorizontal,
BackgroundColor = SkinStyle.Current.SysSubtitleView
};
AddChidren (verticalScrolView);
List willBeShowList = IO.FileUtils.ReadFiles ().FindAll ((fileName) => {
return fileName.Contains ("Equipment_") && (fileName.Contains (DeviceType.MusicModel.ToString ()) || fileName.Contains (DeviceType.A31MusicModel.ToString ()) ) && (fileName.Split ('_').Length == 5);
});
byte [] sendData = new byte [25];
int num = 0;
foreach (string equipment in willBeShowList)//显示在本地找到的所有设备
{
var v = IO.FileUtils.ReadFile (equipment);
string data = CommonPage.MyEncodingUTF8.GetString (v);
MusicModel ac = Newtonsoft.Json.JsonConvert.DeserializeObject (data);
Button btnEquipment = new Button () {
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (90),
TextAlignment = TextAlignment.Center,
Text = ac.Name,
Tag = ac,
BackgroundColor = SkinStyle.Current.ButtonColor,
SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
Gravity = Gravity.CenterHorizontal
};
verticalScrolView.AddChidren (btnEquipment);
btnEquipment.Y = Application.GetRealHeight (10) + num * (btnEquipment.Height + Application.GetRealHeight (30));
num++;
btnEquipment.MouseUpEventHandler += (sender, e) => {
if (btnEquipment.IsSelected) {
Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.CancelBinding), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
alert.Show ();
alert.ResultEventHandler += (sender2, e2) => {
if (e2) {
sendData [0] = 0;
sendData [1] = 255;
sendData [2] = 255;
System.Threading.Tasks.Task.Run (() => {
byte [] reFalg = Control.ControlBytesSendHasReturn (Command.SetMusicPanelSetting, musicPanel.SubnetID, musicPanel.DeviceID, sendData);
Application.RunOnMainThread (() => {
if (reFalg == null) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
} else if (reFalg [0] == 0xF8) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.ThePerationWasSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
}
this.ShowSystemMusicPanel (musicPanel, bp);
});
});
}
};
} else {
Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureBindDevice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
alert.Show ();
alert.ResultEventHandler += (sender2, e2) => {
if (e2) {
Common h = (Common)btnEquipment.Tag;
sendData [0] = 0;
sendData [1] = h.SubnetID;
sendData [2] = h.DeviceID;
System.Threading.Tasks.Task.Run (() => {
Control.ControlBytesSendHasReturn (Command.SetMusicPanelEnable, musicPanel.SubnetID, musicPanel.DeviceID, new byte [] { 1, 1, 0 });
byte [] reFalg = Control.ControlBytesSendHasReturn (Command.SetMusicPanelSetting, musicPanel.SubnetID, musicPanel.DeviceID, sendData);
Application.RunOnMainThread (() => {
if (reFalg == null) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
} else if (reFalg [0] == 0xF8) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.ThePerationWasSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
}
this.ShowSystemMusicPanel (musicPanel, bp);
});
});
}
};
}
};
Button btnLine = new Button () {
Width = LayoutParams.MatchParent,
Height = 1,
BackgroundColor = SkinStyle.Current.MainColor
};
verticalScrolView.AddChidren (btnLine);
}
//--------------bottom--------------
System.Threading.Tasks.Task.Run (() => {
acBytes = Control.ControlBytesSendHasReturn (Command.ReadMusicPanelSetting, musicPanel.SubnetID, musicPanel.DeviceID, new byte [] { 0});
Application.RunOnMainThread (() => {
for (int i = 0; i < verticalScrolView.ChildrenCount; i++) {
if (verticalScrolView.GetChildren (i).GetType () == typeof (Button)) {
Button btn = (Button)verticalScrolView.GetChildren (i);
if (btn.Tag != null) {
var ac = (Common)btn.Tag;
if (acBytes != null) {
if (acBytes [2] == ac.SubnetID && acBytes [3] == ac.DeviceID) {
btn.IsSelected = true;
}
}
}
}
}
});
});
}
}
}