using System;
|
using System.Collections.Generic;
|
using System.Net;
|
using System.IO;
|
using System.Text;
|
|
namespace Shared.SimpleControl.Phone
|
{
|
public class DoorLockAlarmScene : FrameLayout
|
{
|
DoorLock doorLock;
|
DoorLockSceneTargets sceneTargetsInfo;
|
Button btn;
|
|
public DoorLockAlarmScene (DoorLock doorLock)
|
{
|
this.doorLock = doorLock;
|
BackgroundColor = SkinStyle.Current.MainColor;
|
}
|
|
public int SceneID;//场景类型
|
public string SceneName = Language.StringByID (R.MyInternationalizationString.NotConfigured);//场景名称
|
Button btnEditedName;
|
|
/// <summary>
|
/// 读门锁所有报警场景信息
|
/// </summary>
|
/// <param name="door_lock">Door lock.</param>
|
|
public void DoorLockAlarmSceneShow ()
|
{
|
#region 标题
|
var topView = new FrameLayout () {
|
Height = Application.GetRealHeight (130),
|
BackgroundColor = SkinStyle.Current.MainColor,
|
};
|
AddChidren (topView);
|
|
var title = new Button () {
|
TextAlignment = TextAlignment.Center,
|
TextID = R.MyInternationalizationString.SceneAlarmSetting,
|
TextSize = 19,
|
Y = Application.GetRealHeight (10),
|
TextColor = SkinStyle.Current.TextColor1,
|
};
|
topView.AddChidren (title);
|
|
var back = new Button () {
|
Height = Application.GetRealHeight (90),
|
Width = Application.GetRealWidth (85),
|
UnSelectedImagePath = "Item/Back.png",
|
SelectedImagePath = "Item/BackSelected.png",
|
Y = Application.GetRealHeight (30),
|
};
|
topView.AddChidren (back);
|
back.MouseUpEventHandler += (sender, e) => {
|
this.RemoveFromParent ();
|
};
|
|
#endregion
|
|
var middleVerticalScrolViewLayout = new VerticalScrolViewLayout () {
|
Height = Application.GetRealHeight (Application.DesignHeight - 126),
|
Y = topView.Bottom,
|
BackgroundColor = SkinStyle.Current.ViewColor,
|
};
|
AddChidren (middleVerticalScrolViewLayout);
|
|
for (int i = 0; i < 5; i++) {
|
var RowView = new FrameLayout () {
|
Height = Application.GetRealHeight (110),
|
};
|
middleVerticalScrolViewLayout.AddChidren (RowView);
|
|
var btnIcon = new Button () {
|
Width = Application.GetRealHeight (13),
|
Height = Application.GetRealHeight (13),
|
X = Application.GetRealWidth (40),
|
UnSelectedImagePath = "Item/Point.png",
|
SelectedImagePath = "Item/PointSelected.png",
|
Gravity = Gravity.CenterVertical,
|
};
|
RowView.AddChidren (btnIcon);
|
|
var sceneAlarmType = new Button () {
|
X = btnIcon.Right + Application.GetRealWidth (20),
|
Width = Application.GetRealWidth (300),
|
Height = Application.GetRealHeight (110),
|
TextAlignment = TextAlignment.CenterLeft,
|
TextColor = SkinStyle.Current.TextColor1,
|
};
|
RowView.AddChidren (sceneAlarmType);
|
|
var btnSwitch = new Button () {
|
X = sceneAlarmType.Right + Application.GetRealWidth (145),
|
Gravity = Gravity.CenterVertical,
|
Width = Application.GetMinRealAverage (90),
|
Height = Application.GetMinRealAverage (53),
|
UnSelectedImagePath = "Item/SwitchClose.png",
|
SelectedImagePath = "Item/SwitchOpen.png",
|
};
|
RowView.AddChidren (btnSwitch);
|
|
if (i == 0) {
|
sceneAlarmType.TextID = R.MyInternationalizationString.DoorUncloseAlarm;
|
} else if (i == 1) {
|
sceneAlarmType.TextID = R.MyInternationalizationString.LowVoltageAlarm;
|
} else if (i == 2) {
|
sceneAlarmType.TextID = R.MyInternationalizationString.ErrorAlarm;
|
} else if (i == 3) {
|
sceneAlarmType.TextID = R.MyInternationalizationString.KeyUnlockAlarm;
|
} else if (i == 4) {
|
sceneAlarmType.TextID = R.MyInternationalizationString.TamperResistantAlarm;
|
}
|
|
btn = new Button () {
|
Y = RowView.Height - 1,
|
Height = 1,
|
BackgroundColor = SkinStyle.Current.LineColor,
|
};
|
RowView.AddChidren (btn);
|
|
int sceneType = 0;//当前场景类型
|
string alarmMessage = null;
|
int currentTypeIndex = i;
|
if (currentTypeIndex == 0) {
|
sceneType = 0x02;
|
alarmMessage = Language.StringByID (R.MyInternationalizationString.DoorUncloseAlarm);
|
} else if (currentTypeIndex == 1) {
|
sceneType = 0x03;
|
alarmMessage = Language.StringByID (R.MyInternationalizationString.LowVoltageAlarm);
|
} else if (currentTypeIndex == 2) {
|
sceneType = 0x04;
|
alarmMessage = Language.StringByID (R.MyInternationalizationString.ErrorAlarm);
|
} else if (currentTypeIndex == 3) {
|
sceneType = 0x05;
|
alarmMessage = Language.StringByID (R.MyInternationalizationString.KeyUnlockAlarm);
|
} else if (currentTypeIndex == 4) {
|
sceneType = 0x06;
|
alarmMessage = Language.StringByID (R.MyInternationalizationString.TamperResistantAlarm);
|
}
|
|
int alarmID = currentTypeIndex;
|
|
|
readStatus ((obj) => {
|
Application.RunOnMainThread (() => {
|
if (obj) {
|
btnSwitch.IsSelected = true;
|
|
} else {
|
btnSwitch.IsSelected = false;
|
}
|
});
|
},sceneType);
|
|
EventHandler<MouseEventArgs> eHandler = (sender, e) => {
|
btnSwitch.IsSelected = !btnSwitch.IsSelected;
|
if (btnSwitch.IsSelected) {
|
var alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureToOpenAlarmNotice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
|
alert.ResultEventHandler += (sender1, e1) => {
|
if (e1) {
|
System.Threading.Tasks.Task.Run (() => {
|
var sendBytes = new byte [12];
|
sendBytes [0] = doorLock.PhysicsLoopID;
|
sendBytes [1] = 2;//报警
|
sendBytes [2] = (byte)sceneType;
|
sendBytes [3] = 0;//用户ID
|
sendBytes [4] = 0;
|
sendBytes [5] = 0x58;
|
sendBytes [6] = doorLock.SubnetID;
|
sendBytes [7] = 0;
|
sendBytes [8] = (byte)(alarmID + 210);//密码10以下,指纹ID+10,感应卡id+100
|
sendBytes [9] = 0;
|
sendBytes [10] = 0;
|
sendBytes [11] = 0;
|
var result = Control.ControlBytesSendHasReturn (Command.SetDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, sendBytes);
|
if (result == null) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
|
});
|
return;
|
} else {
|
var result1 = result [0];
|
if (result1 == 0xF8) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.OpenDoorLockNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 1 }.Show (btnSwitch);
|
});
|
NoticePushServer (alarmMessage, alarmID, btnSwitch );
|
} else {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.OpenDoorLockNotificationsUnSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
|
});
|
btnSwitch.IsSelected = false;
|
}
|
}
|
});
|
}
|
};
|
alert.Show ();
|
} else {
|
var alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureToCloseAlarmNotice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
|
alert.ResultEventHandler += (sender1, e1) => {
|
if (e1) {
|
System.Threading.Tasks.Task.Run (() => {
|
var sendBytes = new byte [12];
|
sendBytes [0] = doorLock.PhysicsLoopID;
|
sendBytes [1] = 2;//报警
|
sendBytes [2] = (byte)sceneType;
|
sendBytes [3] = 0;//用户ID
|
sendBytes [4] = 0;
|
sendBytes [5] = 0;
|
sendBytes [6] = doorLock.SubnetID;
|
sendBytes [7] = 0;
|
sendBytes [8] = (byte)(alarmID + 210);//密码10以下,指纹ID+10,感应卡id+100
|
sendBytes [9] = 0;
|
sendBytes [10] = 0;
|
sendBytes [11] = 0;
|
var result = Control.ControlBytesSendHasReturn (Command.SetDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, sendBytes);
|
if (result == null) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
|
});
|
return;
|
} else {
|
var result1 = result [0];
|
if (result1 == 0xF8) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.CloseDoorLockNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
|
});
|
} else {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.CloseDoorLockNotificationsUnSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
|
});
|
btnSwitch.IsSelected = true;
|
|
}
|
}
|
});
|
}
|
};
|
alert.Show ();
|
}
|
};
|
btnSwitch.MouseUpEventHandler += eHandler;
|
|
}
|
}
|
|
public void readStatus (Action<bool> action,int type)
|
{
|
System.Threading.Tasks.Task.Run (() => {
|
var sceneTargetInfoBytes = Control.ControlBytesSendHasReturn (Command.ReadDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, new byte [] { doorLock.PhysicsLoopID, 2, (byte)type, 0, 0 });
|
if (sceneTargetInfoBytes == null) {
|
action (false);
|
} else {
|
var sceneTargetsInfo = new DoorLockSceneTargets ();
|
sceneTargetsInfo.TargetSerialNum = sceneTargetInfoBytes [4];
|
sceneTargetsInfo.ControlType = sceneTargetInfoBytes [5];//通用开关0x58,无效0
|
sceneTargetsInfo.TargetSunbetID = sceneTargetInfoBytes [6];
|
sceneTargetsInfo.TargetDeviceID = sceneTargetInfoBytes [7];
|
sceneTargetsInfo.TargetLoopID = sceneTargetInfoBytes [8];
|
|
var command = sceneTargetInfoBytes [5];
|
if (sceneTargetsInfo.TargetSunbetID == doorLock.SubnetID &&
|
sceneTargetsInfo.TargetDeviceID == 0 &&
|
command == 0x58) {
|
action (true);
|
} else {
|
action (false);
|
}
|
}
|
});
|
}
|
|
/// <summary>
|
/// 服务器请求
|
/// </summary>
|
/// <returns>The web.</returns>
|
/// <param name="url">URL.</param>
|
/// <param name="data">Data.</param>
|
string requestWeb (string data)
|
{
|
var webClient = new WebClient { };
|
webClient.Headers.Remove ("ContentType");
|
webClient.Headers.Add ("ContentType", "application/json");
|
var bytes = webClient.UploadData ("https://developer.hdlcontrol.com/api/AddPushMessage", "POST", Encoding.UTF8.GetBytes (data));
|
if (bytes == null) {
|
return null;
|
} else {
|
return Encoding.UTF8.GetString (bytes);
|
}
|
}
|
|
public static string messageJson (PushMessageEntity pushMessage)
|
{
|
if (pushMessage == null) {
|
return null;
|
}
|
return Newtonsoft.Json.JsonConvert.SerializeObject (pushMessage);
|
}
|
|
/// <summary>
|
/// 消息推送到服务器
|
/// </summary>
|
/// <param name="message">Message.</param>
|
public async void NoticePushServer (string message,int alarmID,Button btnSwitch)
|
{
|
string result = null;
|
await System.Threading.Tasks.Task.Run (() => {
|
//获取一端口交换机的mac
|
var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (UserConfig.Instance.RemoteModeFile));
|
if (gateWayString == null || UserConfig.Instance.RemoteModeFile.Split ('_').Length < 2) {
|
result = Language.StringByID (R.MyInternationalizationString.NoGateway);
|
return;
|
}
|
var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (gateWayString);
|
if (common == null) {
|
result = Language.StringByID (R.MyInternationalizationString.NoGateway);
|
return;
|
}
|
string mac = null;
|
if (common.Type == DeviceType.OnePortWirelessFR) {
|
var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortWirelessFR> (gateWayString);
|
mac = gateWay.MAC.Replace (".", "");
|
} else if (common.Type == DeviceType.OnePortBus) {
|
var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortBus> (gateWayString);
|
mac = gateWay.MAC.Replace (".", "");
|
} else {
|
result = Language.StringByID (R.MyInternationalizationString.NoGateway);
|
return;
|
}
|
|
var pushMessage = new PushMessageEntity ();
|
pushMessage.Mac = mac;
|
pushMessage.SubnetID = doorLock.SubnetID;
|
pushMessage.DeviceID = doorLock.DeviceID;
|
pushMessage.SwitchNum = alarmID + 210;
|
pushMessage.MessageMode = 1;
|
pushMessage.AlarmEmail = new byte [] { 2 };
|
pushMessage.DetailMessage = message;
|
|
var mess = messageJson (pushMessage);
|
try {
|
var recevieResult = requestWeb (mess);
|
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<PushMessageResult> (recevieResult);
|
if (obj.StateCode == "SUCCESS") {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
|
});
|
} else {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsUnsuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
|
//btnSwitch.IsSelected = false;跟服务器状态没有关系,这里状态是有发到门锁的数据确定
|
});
|
}
|
} catch (Exception ex) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsUnsuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
|
});
|
}
|
});
|
if (result != null) {
|
Application.RunOnMainThread (() => {
|
new Tip () { MaxWidth = 150, Text = result, Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
|
});
|
}
|
}
|
|
}
|
}
|