HMDataPoint.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Xml;
  2. using System.Collections.Generic;
  3. using System.Net.HomeMatic.Events;
  4. namespace System.Net.HomeMatic {
  5. public class HMDataPoint : HMBase {
  6. public delegate void ValueChangedHandler(object sender, ValueChangedEventArgs e);
  7. public event ValueChangedHandler OnValueChanged;
  8. public delegate void BeforeValueChangedHandler(object sender, ValueChangedEventArgs e);
  9. public event BeforeValueChangedHandler OnBeforeValueChanged;
  10. private DataPoint_Type _type;
  11. private string _value;
  12. private int _valueType;
  13. private string _valueUnit;
  14. private int _timeStamp;
  15. private HMChannel _channel;
  16. private DateTime lastUpdateTime;
  17. private bool bInit = false;
  18. public HMDataPoint(HMApi Api, HMChannel Channel) :base(Api) {
  19. this._channel = Channel;
  20. }
  21. public HMDataPoint(HMApi Api, HMChannel Channel, XmlNode Data) : base(Api, Data) {
  22. this._channel = Channel;
  23. this.Init(Data);
  24. this.bInit = true;
  25. }
  26. public void Init(XmlNode Data) {
  27. this._type = this.Attr_DataPointType(Data, "type");
  28. if (this.bInit) {
  29. this.Value = this.Attr_String(Data, "value");
  30. } else {
  31. this._value = this.Attr_String(Data, "value");
  32. }
  33. this._valueType = this.Attr_Int32(Data, "value_type");
  34. this._valueUnit = this.Attr_String(Data, "value_unit");
  35. this._timeStamp = this.Attr_Int32(Data, "timestamp");
  36. this.lastUpdateTime = DateTime.Now;
  37. }
  38. public DataPoint_Type DataType {
  39. get {
  40. return this._type;
  41. }
  42. }
  43. public void Update() {
  44. XmlDocument doc = this.Api.GetApiData(ApiMethode.State, new Dictionary<string, object>() {["device_id"] = this.Channel.Device.IseID });
  45. XmlNodeList child_devices = doc.ChildNodes[1].ChildNodes;
  46. int cdc = child_devices.Count;
  47. if (cdc > 0) {
  48. for (int i = 0; i < cdc; i++) {
  49. if (child_devices[i].Name == "device") {
  50. XmlNodeList child_channels = child_devices[i].ChildNodes;
  51. int ccc = child_channels.Count;
  52. if (ccc > 0) {
  53. for (int j = 0; j < ccc; j++) {
  54. if (child_channels[j].Name == "channel" && this.Attr_Int32(child_channels[j],"ise_id") == this._channel.IseID) {
  55. XmlNodeList child_datapoints = child_channels[j].ChildNodes;
  56. int cdpc = child_datapoints.Count;
  57. if (cdpc > 0) {
  58. for (int k = 0; k < cdpc; k++) {
  59. if (child_datapoints[k].Name == "datapoint") {
  60. if (this.Attr_Int32(child_datapoints[k],"ise_id") == this.IseID) {
  61. this._timeStamp = this.Attr_Int32(child_datapoints[k], "timestamp");
  62. string newVal = this.Attr_String(child_datapoints[k], "value");
  63. this.changeValue(newVal, false);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public string Value {
  76. get {
  77. if ((DateTime.Now - this.lastUpdateTime).TotalSeconds > 10) {
  78. //this.Update();
  79. this.Api.UpdateDevices();
  80. }
  81. return this._value;
  82. }
  83. set {
  84. System.Diagnostics.Debug.WriteLine("Change Value of Datapoint " + this.Channel.Name + "." + this.GetDataPointType_To_String(this._type) + " From '" + this._value + "' => '" + value + "'");
  85. this.changeValue(value, true);
  86. }
  87. }
  88. protected bool changeValue(string newValue, bool bSendApi = false) {
  89. if (this._value != newValue) {
  90. bool bChange = true;
  91. if (this.OnBeforeValueChanged != null) {
  92. BeforeValueChangedEventArgs args = new BeforeValueChangedEventArgs(this, this._value, newValue);
  93. this.OnBeforeValueChanged(this, args);
  94. bChange = args.Accept;
  95. }
  96. if (bChange) {
  97. string oldVal = this._value;
  98. this._value = newValue;
  99. if (this.OnValueChanged != null) {
  100. ValueChangedEventArgs args = new ValueChangedEventArgs(this, oldVal, this._value);
  101. this.OnValueChanged(this, args);
  102. }
  103. Dictionary<string, object> val = new Dictionary<string, object>() {
  104. ["ise_id"] = this._ise_id,
  105. ["new_value"] = newValue
  106. };
  107. this.lastUpdateTime = DateTime.Now;
  108. if (bSendApi) {
  109. Api.GetApiData(ApiMethode.StateChange, val);
  110. }
  111. }
  112. return bChange;
  113. }
  114. return false;
  115. }
  116. public int ValueType {
  117. get { return this._valueType; }
  118. }
  119. public string Unit {
  120. get { return this._valueUnit; }
  121. }
  122. public int TimeStamp {
  123. get { return this._timeStamp; }
  124. }
  125. public HMChannel Channel {
  126. get { return this._channel; }
  127. }
  128. protected override string GetClassName() {
  129. return "HMDataPoint";
  130. }
  131. protected override string ValuesToString() {
  132. return this._type.ToString() + ";" +
  133. this._value.ToString() + ";" +
  134. this._valueType.ToString() + ";" +
  135. this._valueUnit + ";" +
  136. this._timeStamp.ToString();
  137. }
  138. }
  139. }