using System;
//using Java.Lang;
namespace Shared.SimpleControl.Phone
{
public class TemporaryPassword : Dialog
{
FrameLayout mainFrameLayout = new FrameLayout { BackgroundColor = 0xFF3b3d3c };
DoorLock doorLock;
Button passwordText1;//临时密码6位数
Button passwordText2;
Button passwordText3;
Button passwordText4;
Button passwordText5;
Button passwordText6;
TemporaryPassword obj;
private static System.Threading.Thread th;
///
/// 构造函数
///
/// Door lock.
public TemporaryPassword (DoorLock door_lock)
{
this.doorLock = door_lock;
AddChidren (mainFrameLayout);
showSetTempPassword ();
readStatus (door_lock);
th = new System.Threading.Thread ((obj) => {
ThFun ();
});
th.Start ();
}
///
/// 读门锁临时密码信息
///
/// Door lock.
void readStatus (DoorLock door_lock)
{
System.Threading.Tasks.Task.Run (() => {
var randomNum = Control.ControlBytesSendHasReturn (Command.CreatConnection, door_lock.SubnetID, door_lock.DeviceID, new byte [] { 1 });
if (randomNum != null) {
byte [] randomBytes = new byte [8];
System.Array.Copy (randomNum, 1, randomBytes, 0, 8);
byte [] inpuptBytes = { 0x1F, 0x52, door_lock.PhysicsLoopID };
var encryptionBytes = Security.Encryption (randomBytes, inpuptBytes);
var resultBytes = Control.ControlBytesSendHasReturn (Command.SendEncryptionAfterConnected, door_lock.SubnetID, door_lock.DeviceID, encryptionBytes);
if (resultBytes == null) {
Application.RunOnMainThread (() => {
//可能是设备的ID被改变了,或者加密时间超过5s后没有反馈
});
return;
} else {
var ddBytes = Security.Encryption (randomBytes, resultBytes);
door_lock.Enable = ddBytes [3].ToString ();
door_lock.Times = ddBytes [4].ToString ();
door_lock.VailidTimeL = ddBytes [5].ToString ();
door_lock.VailidTimeH = ddBytes [6].ToString ();
door_lock.tempPasswordText1 = ddBytes [7].ToString ();
door_lock.tempPasswordText2 = ddBytes [8].ToString ();
door_lock.tempPasswordText3 = ddBytes [9].ToString ();
door_lock.tempPasswordText4 = ddBytes [10].ToString ();
door_lock.tempPasswordText5 = ddBytes [11].ToString ();
door_lock.tempPasswordText6 = ddBytes [12].ToString ();
door_lock.dynamicTempPassword = ddBytes [7].ToString () + ddBytes [8].ToString () + ddBytes [9].ToString () + ddBytes [10].ToString () + ddBytes [11].ToString () + ddBytes [12].ToString ();
int VailidTimeHH = Convert.ToInt32 (door_lock.VailidTimeH);
int VailidTimeLL = Convert.ToInt32 (door_lock.VailidTimeL);
door_lock.VailidTime = Convert.ToInt32 ((VailidTimeHH) * 256 + VailidTimeLL);
}
IO.FileUtils.SaveEquipmentMessage (door_lock, door_lock.PhysicsLoopID.ToString ());
Application.RunOnMainThread (() => {
showSetTempPassword ();
});
}
});
}
void showSetTempPassword ()
{
mainFrameLayout.RemoveAll ();
#region 标题
var topView = new FrameLayout () {
Y = Application.GetRealHeight (36),
Height = Application.GetRealHeight (90),
BackgroundColor = 0xff1e1f20,
};
mainFrameLayout.AddChidren (topView);
var title = new Button () {
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.UserTempPassword,
TextSize = 19,
};
topView.AddChidren (title);
var logo = new Button () {
Width = Application.GetRealWidth (154),
Height = Application.GetRealHeight (90),
X = Application.GetRealWidth (486),
UnSelectedImagePath = MainPage.LogoString,
};
topView.AddChidren (logo);
var back = new Button () {
Height = Application.GetRealHeight (90),
Width = Application.GetRealWidth (85),
UnSelectedImagePath = "Item/Back.png",
SelectedImagePath = "Item/BackSelected.png",
Gravity = Gravity.CenterVertical,
};
topView.AddChidren (back);
back.MouseUpEventHandler += (sender, e) => {
Close ();
};
var btnl = new Button () {
Height = 1,
BackgroundColor = 0xFF000000,
Y = topView.Height - 1
};
topView.AddChidren (btnl);
#endregion
#region bodyScrolView
var bodyScrolView = new FrameLayout () {
Height = Application.GetRealHeight (Application.DesignHeight - 126 - 98),
Y = topView.Bottom,
};
mainFrameLayout.AddChidren (bodyScrolView);
Button validTimeText = new Button () {
X = Application.GetRealWidth (80),
Y = Application.GetRealHeight (50),
Width = Application.GetRealWidth (300),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
Text = Language.StringByID (R.MyInternationalizationString.ValidTime),
};
bodyScrolView.AddChidren (validTimeText);
var inputMinText = new EditText () {
X = Application.GetRealWidth (80),
Y = validTimeText.Bottom + Application.GetRealHeight (4),
Width = Application.GetRealWidth (450),
Height = Application.GetRealHeight (100),
PlaceholderText = doorLock.VailidTime.ToString (),
//Text = doorLock.VailidTime.ToString () ,
PlaceholderTextColor = 0xffc1c1c1,
TextAlignment = TextAlignment.Center,
BorderWidth = 1,
Radius = 5,
BorderColor = SkinStyle.Current.BorderColor,
};
bodyScrolView.AddChidren (inputMinText);
Button unitMin = new Button () {
Height = Application.GetRealHeight (50),
Width = Application.GetRealWidth (100),
X = inputMinText.Right,
Y = Application.GetRealHeight (190),
Text = Language.StringByID (R.MyInternationalizationString.Minute),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 12,
};
bodyScrolView.AddChidren (unitMin);
var remindPic = new Button () {
Height = Application.GetRealHeight (40),
Width = Application.GetRealWidth (40),
X = Application.GetRealWidth (80),
Y = inputMinText.Bottom + Application.GetRealHeight (18),
UnSelectedImagePath = "DoorLockPic/door_lock_alert.png",
};
bodyScrolView.AddChidren (remindPic);
var maxMinuteText = new Button () {
Width = Application.GetRealWidth (550),
Height = Application.GetRealHeight (80),
X = remindPic.Right + Application.GetRealWidth (8),
Y = inputMinText.Bottom,
Text = Language.StringByID (R.MyInternationalizationString.MaxValidTime),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 10,
};
bodyScrolView.AddChidren (maxMinuteText);
Button validCountText = new Button () {
X = Application.GetRealWidth (80),
Y = inputMinText.Bottom + Application.GetRealHeight (110),
Width = Application.GetRealWidth (300),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
Text = Language.StringByID (R.MyInternationalizationString.ValidCount),
};
bodyScrolView.AddChidren (validCountText);
var inputCountText = new EditText () {
Width = Application.GetRealWidth (450),
Height = Application.GetRealHeight (100),
X = Application.GetRealWidth (80),
Y = validCountText.Bottom + Application.GetRealHeight (10),
PlaceholderText = doorLock.Times,
PlaceholderTextColor = 0xffc1c1c1,
TextAlignment = TextAlignment.Center,
BorderWidth = 1,
Radius = 5,
BorderColor = SkinStyle.Current.BorderColor,
};
bodyScrolView.AddChidren (inputCountText);
Button unitCount = new Button () {
Height = Application.GetRealHeight (50),
Width = Application.GetRealWidth (100),
X = inputCountText.Right + Application.GetRealWidth (2),
Y = Application.GetRealHeight (490),
Text = Language.StringByID (R.MyInternationalizationString.Ci),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 12,
};
bodyScrolView.AddChidren (unitCount);
var remindPic2 = new Button () {
Height = Application.GetRealHeight (40),
Width = Application.GetRealWidth (40),
X = Application.GetRealWidth (80),
Y = inputCountText.Bottom + Application.GetRealHeight (18),
UnSelectedImagePath = "DoorLockPic/door_lock_alert.png",
};
bodyScrolView.AddChidren (remindPic2);
var maxValidCount = new Button () {
Width = Application.GetRealWidth (550),
Height = Application.GetRealHeight (80),
X = remindPic2.Right + Application.GetRealWidth (8),
Y = inputCountText.Bottom,
Text = Language.StringByID (R.MyInternationalizationString.MaxValicCount),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 10,
};
bodyScrolView.AddChidren (maxValidCount);
var remindFrameLayout = new FrameLayout () {
Height = Application.GetRealHeight (350),
Y = remindPic2.Bottom + Application.GetRealHeight (90),
};
bodyScrolView.AddChidren (remindFrameLayout);
Button TempPasswordText = new Button () {
X = Application.GetRealWidth (80),
Y = Application.GetRealHeight (10),
Width = Application.GetRealWidth (300),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
Text = Language.StringByID (R.MyInternationalizationString.DynamicTempPassword),
};
remindFrameLayout.AddChidren (TempPasswordText);
passwordText1 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
X = Application.GetRealWidth (70),
Y = TempPasswordText.Bottom + Application.GetRealHeight (10),
Text = doorLock.tempPasswordText1,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText1);
passwordText2 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
Y = Application.GetRealHeight (100),
X = passwordText1.Right + Application.GetRealWidth (2),
Text = doorLock.tempPasswordText2,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText2);
passwordText3 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
Y = Application.GetRealHeight (100),
X = passwordText2.Right + Application.GetRealWidth (2),
Text = doorLock.tempPasswordText3,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText3);
passwordText4 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
Y = Application.GetRealHeight (100),
X = passwordText3.Right + Application.GetRealWidth (2),
Text = doorLock.tempPasswordText4,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText4);
passwordText5 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
Y = Application.GetRealHeight (100),
X = passwordText4.Right + Application.GetRealWidth (2),
Text = doorLock.tempPasswordText5,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText5);
passwordText6 = new Button () {
Height = Application.GetRealHeight (100),
Width = Application.GetRealWidth (80),
TextAlignment = TextAlignment.Center,
Y = Application.GetRealHeight (100),
X = passwordText5.Right + Application.GetRealWidth (2),
Text = doorLock.tempPasswordText6,
TextSize = 28,
};
remindFrameLayout.AddChidren (passwordText6);
#endregion
#region bottomFrameLayout
var bottomFrameLayout = new FrameLayout () {
Height = Application.GetRealHeight (98),
Y = Application.GetRealHeight (Application.DesignHeight - 95),
};
mainFrameLayout.AddChidren (bottomFrameLayout);
var bottomLine = new Button () {
Height = Application.GetRealHeight (1),
BackgroundColor = 0xFF000000,
};
bottomFrameLayout.AddChidren (bottomLine);
Button generateTempPassword = new Button () {
Y = bottomLine.Bottom,
Height = Application.GetRealHeight (97),
TextID = R.MyInternationalizationString.GenerateTemporaryPassword,
TextSize = 15,
BackgroundColor = 0xFF121212,
SelectedBackgroundColor = 0xFFFC5806,
};
bottomFrameLayout.AddChidren (generateTempPassword);
generateTempPassword.MouseUpEventHandler += (sender, e) => {
var alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.GenerateTemporaryDynamicPasswordAgain),
Language.StringByID (R.MyInternationalizationString.cancel), Language.StringByID (R.MyInternationalizationString.confirm)) {
};
alert.ResultEventHandler += (sender1, e1) => {
if (e1) {
button1_Click (sender, e);
int passwrodValidTime;
int passwordValidCount;
if (string.IsNullOrEmpty (inputMinText.Text)) {
passwrodValidTime = Convert.ToInt32 (inputMinText.PlaceholderText);
} else {
passwrodValidTime = Convert.ToInt32 (inputMinText.Text);
}
if (string.IsNullOrEmpty (inputCountText.Text)) {
passwordValidCount = Convert.ToInt32 (inputCountText.PlaceholderText);
} else {
passwordValidCount = Convert.ToInt32 (inputCountText.Text);
}
if (passwordValidCount > 15 || passwordValidCount < 1) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.MaxValicCount), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
return;
}
System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.Sleep (6000);
var randomNumSet = Control.ControlBytesSendHasReturn (Command.CreatConnection, doorLock.SubnetID, doorLock.DeviceID, new byte [] { 1 });
if (randomNumSet != null) {
Console.WriteLine ("send:" + randomNumSet.ToString ());
byte [] randomBytes = new byte [8];
System.Array.Copy (randomNumSet, 1, randomBytes, 0, 8);
var sendBytes = new byte [13];
sendBytes [0] = 0x1F;
sendBytes [1] = 0x54;
sendBytes [2] = doorLock.PhysicsLoopID;
sendBytes [3] = 1;//enable,0 : unable / 1:enable / 失败 0xF5
sendBytes [4] = Convert.ToByte (passwordValidCount);//可用次数,0 ~ 15
sendBytes [5] = Convert.ToByte (passwrodValidTime % 256);//时间段低位
sendBytes [6] = Convert.ToByte (passwrodValidTime / 256);//时间段高位
sendBytes [7] = Convert.ToByte (passwordText1.Text); //0 ~ 9
sendBytes [8] = Convert.ToByte (passwordText2.Text);
sendBytes [9] = Convert.ToByte (passwordText3.Text);
sendBytes [10] = Convert.ToByte (passwordText4.Text);
sendBytes [11] = Convert.ToByte (passwordText5.Text);
sendBytes [12] = Convert.ToByte (passwordText6.Text);
var inpuptBytes = sendBytes;
var encryptionBytes = Security.Encryption (randomBytes, inpuptBytes);
var resultBytes = Control.ControlBytesSendHasReturn (Command.SendEncryptionAfterConnected, doorLock.SubnetID, doorLock.DeviceID, encryptionBytes);
if (resultBytes == null) {
Application.RunOnMainThread (() => {
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TargetsSetFail), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show ((View)sender);
});
return;
} else {
//F8= 成功; F5 = 失败
var ddBytes = Security.Encryption (randomBytes, resultBytes);
var result = ddBytes [2];
if (result == 0xF8) {
Application.RunOnMainThread (() => {
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SetSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show ((View)sender);
});
readStatus (doorLock);
}
}
}
});
}
};
alert.Show ();
#endregion
};
}
///
/// 每隔一分钟自动刷新的方法
///
//void updateStatus ()
//{
// System.Threading.Tasks.Task.Run (() => {
// if (doorLock.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now) {
// readStatus (doorLock);
// }
// });
//}
///
/// 每隔一分钟自动刷新的方法
///
private void ThFun ()
{
try {
while (true) {
//执行方法() ;
//Console.WriteLine ("ssss333333");
System.Threading.Thread.Sleep (60000);//每1分钟刷新一次 以毫秒为单位
readStatus (doorLock);
}
} catch { }
}
///
/// 生成随机数
///
public int [] getRandomNum (int num, int minValue, int maxValue)
{
Random ra = new Random (unchecked((int)DateTime.Now.Ticks));
int [] arrNum = new int [num];
int tmp = 0;
for (int i = 0; i <= num - 1; i++) {
tmp = ra.Next (minValue, maxValue); //随机取数
arrNum [i] = getNum (arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
}
return arrNum;
}
public int getNum (int [] arrNum, int tmp, int minValue, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1) {
if (arrNum [n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next (minValue, maxValue); //重新随机获取。
getNum (arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
}
n++;
}
return tmp;
}
private void button1_Click (object sender, System.EventArgs e)
{
int [] arr = getRandomNum (6, 0, 10); //从0至10中取出6个互不相同的随机数
int i = 0;
string temp = "";
while (i <= arr.Length - 1) {
// temp = arr [i].ToString ();
i++;
}
passwordText1.Text = arr [0].ToString (); //显示在label1中
passwordText2.Text = arr [1].ToString ();
passwordText3.Text = arr [2].ToString ();
passwordText4.Text = arr [3].ToString ();
passwordText5.Text = arr [4].ToString ();
passwordText6.Text = arr [5].ToString ();
}
}
}