using Edge.Core.Parser.BinaryParser.Attributes;
using Edge.Core.Parser.BinaryParser.MessageEntity;
using Edge.Core.Parser.BinaryParser.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HengShan_Pump_TQC_IFSF.MessageEntity
{
///
/// LNAO (Originator Subnet + Node) + IFSF_MsgCode = 1 + DEVICE_STATUS
///
///
public class LonWorksHeartbeat : IfsfMessageBase
{
public LonWorksHeartbeat(byte ifsfDomain, byte originatorSubnet, byte originatorNode)
{
this.IfsfDomain = ifsfDomain;
base.OriginatorSubnet = originatorSubnet;
base.OriginatorNode = originatorNode;
this.MessageCode = MessageCode.IFSF_MESSAGE_CODE_HEARTBEAT;
}
[Format(1, EncodingType.BIN, -19990)]
[Range(0, 255, "IfsfDomaint must range in 0 to 255, but actual is: {0}")]
public byte IfsfDomain { get; set; }
[Format(1, EncodingType.BIN, 3)]
public byte RawDeviceState { get; set; }
public IEnumerable DeviceStates
{
get
{
var result = new List();
if (this.RawDeviceState == 0)
result.Add(HeartbeatDeviceStatus.EverythingIsFine);
if (this.RawDeviceState.GetBit(0) == 1)
result.Add(HeartbeatDeviceStatus.ConfigurationNeeded);
if (this.RawDeviceState.GetBit(7) == 1)
result.Add(HeartbeatDeviceStatus.SoftwareRefreshRequired);
return result;
}
set
{
foreach (var e in value)
{
if (e == HeartbeatDeviceStatus.ConfigurationNeeded)
this.RawDeviceState = this.RawDeviceState.SetBit(0, 0, 1);
if (e == HeartbeatDeviceStatus.SoftwareRefreshRequired)
this.RawDeviceState = this.RawDeviceState.SetBit(7, 7, 1);
}
}
}
public override string ToLogString()
{
return base.ToLogString() + this.DeviceStates.Select(s => s.ToString()).Aggregate((acc, n) => acc + ", " + n);
}
}
}