HMDevice.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Xml;
  2. using System.Collections.Generic;
  3. namespace System.Net.HomeMatic {
  4. public class HMDevice : HMBase {
  5. /*
  6. Thermostat Error F2
  7. <device name="Thermo_Eins" address="PEQ1316939" ise_id="2826" interface="BidCos-RF" device_type="HM-CC-RT-DN" ready_config="true">
  8. <channel name="HM-CC-RT-DN PEQ1316939:1" type="22" address="PEQ1316939:1" ise_id="2856" direction="RECEIVER" parent_device="2826" index="1" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  9. <channel name="HM-CC-RT-DN PEQ1316939:2" type="17" address="PEQ1316939:2" ise_id="2857" direction="RECEIVER" parent_device="2826" index="2" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  10. <channel name="HM-CC-RT-DN PEQ1316939:3" type="26" address="PEQ1316939:3" ise_id="2858" direction="RECEIVER" parent_device="2826" index="3" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  11. <channel name="HM-CC-RT-DN PEQ1316939:4" type="17" address="PEQ1316939:4" ise_id="2859" direction="SENDER" parent_device="2826" index="4" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  12. <channel name="HM-CC-RT-DN PEQ1316939:5" type="17" address="PEQ1316939:5" ise_id="2903" direction="RECEIVER" parent_device="2826" index="5" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  13. <channel name="HM-CC-RT-DN PEQ1316939:6" type="17" address="PEQ1316939:6" ise_id="2904" direction="RECEIVER" parent_device="2826" index="6" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  14. </device>
  15. Thermostat Fehlerfrei
  16. <device name="Thermo_Eins" address="PEQ1316939" ise_id="2826" interface="BidCos-RF" device_type="HM-CC-RT-DN" ready_config="true">
  17. <channel name="HM-CC-RT-DN PEQ1316939:1" type="22" address="PEQ1316939:1" ise_id="2856" direction="RECEIVER" parent_device="2826" index="1" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  18. <channel name="HM-CC-RT-DN PEQ1316939:2" type="17" address="PEQ1316939:2" ise_id="2857" direction="RECEIVER" parent_device="2826" index="2" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  19. <channel name="HM-CC-RT-DN PEQ1316939:3" type="26" address="PEQ1316939:3" ise_id="2858" direction="RECEIVER" parent_device="2826" index="3" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  20. <channel name="HM-CC-RT-DN PEQ1316939:4" type="17" address="PEQ1316939:4" ise_id="2859" direction="SENDER" parent_device="2826" index="4" group_partner="" aes_available="false" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  21. <channel name="HM-CC-RT-DN PEQ1316939:5" type="17" address="PEQ1316939:5" ise_id="2903" direction="RECEIVER" parent_device="2826" index="5" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  22. <channel name="HM-CC-RT-DN PEQ1316939:6" type="17" address="PEQ1316939:6" ise_id="2904" direction="RECEIVER" parent_device="2826" index="6" group_partner="" aes_available="true" transmission_mode="DEFAULT" visible="true" ready_config="true" operate="true"/>
  23. </device>
  24. */
  25. private string _address;
  26. private string _interface;
  27. private string _deviceType;
  28. private bool _readyConfig;
  29. private bool _unreach;
  30. private bool _stickyUnreach;
  31. private bool _configPending;
  32. //private List<HMChannel> _channels;
  33. private int _systemChannel = 0;
  34. public HMDevice(HMApi Api) : base(Api) {
  35. }
  36. public HMDevice(HMApi Api, XmlNode Data) : base(Api, Data) {
  37. this.Init(Data);
  38. }
  39. public void Init(XmlNode Data) {
  40. this._address = this.Attr_String(Data, "address");
  41. this._interface = this.Attr_String(Data, "interface");
  42. this._deviceType = this.Attr_String(Data, "device_type");
  43. this._readyConfig = this.Attr_Bool(Data, "ready_config");
  44. this._unreach = this.Attr_Bool(Data, "unreach");
  45. this._stickyUnreach = this.Attr_Bool(Data, "sticky_unreach");
  46. this._configPending = this.Attr_Bool(Data, "config_pending");
  47. }
  48. public string Address {
  49. get { return this._address; }
  50. }
  51. public string Interface {
  52. get { return this._interface; }
  53. }
  54. public string DeviceType {
  55. get { return this._deviceType; }
  56. }
  57. public bool ReadyConfig {
  58. get { return this._readyConfig; }
  59. }
  60. public bool Unreach {
  61. get { return this._unreach; }
  62. }
  63. public bool StickyUnreach {
  64. get { return this._stickyUnreach; }
  65. }
  66. public bool ConfigPending {
  67. get { return this._configPending; }
  68. }
  69. private HMChannel[] _channelList;
  70. private bool _initChannelList = false;
  71. public HMChannel[] Channels {
  72. get {
  73. if (this._initChannelList == false)
  74. this.InitChannelList();
  75. return this._channelList;
  76. }
  77. }
  78. private void InitChannelList() {
  79. List<HMChannel> res = new List<HMChannel>();
  80. foreach(HMChannel ch in this._api.Channels) {
  81. if (ch.Device.IseID == this._ise_id) {
  82. res.Add(ch);
  83. }
  84. }
  85. this._channelList = res.ToArray();
  86. this._initChannelList = true;
  87. }
  88. public void UpdateChannels() {
  89. this.InitChannelList();
  90. }
  91. public int SystemChannel {
  92. get {
  93. if(this._systemChannel<1) {
  94. this.UpdateDeviceInfo();
  95. }
  96. return this._systemChannel;
  97. }
  98. }
  99. private DateTime _lastDeviceInfoUpdate;
  100. private DeviceInfo _lastDeviceInfo;
  101. public DeviceInfo Info {
  102. get {
  103. TimeSpan ts = DateTime.Now - this._lastDeviceInfoUpdate;
  104. if (ts.TotalSeconds > 3) {
  105. this.UpdateDeviceInfo();
  106. }
  107. return this._lastDeviceInfo;
  108. }
  109. }
  110. public void UpdateDeviceInfo() {
  111. XmlDocument doc = this._api.GetApiData(ApiMethode.State, new Dictionary<string, object>() {["device_id"] = this._ise_id });
  112. XmlNodeList childs = doc.ChildNodes[1].ChildNodes;
  113. int c = childs.Count;
  114. this._lastDeviceInfoUpdate = DateTime.Now;
  115. if (this._systemChannel < 1) {
  116. if (c > 0)
  117. for (int i = 0; i < c; i++)
  118. if (childs[i].Name == "channel")
  119. if (childs[i].HasChildNodes) {
  120. int cc = childs[i].ChildNodes.Count;
  121. int matches = 0;
  122. for (int j = 0; j < cc; j++) {
  123. if (childs[i].ChildNodes[j].Name == "datapoint") {
  124. if (
  125. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "UNREACH" ||
  126. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "STICKY_UNREACH" ||
  127. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "CONFIG_PENDING" ||
  128. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "RSSI_DEVICE" ||
  129. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "LOWBAT" ||
  130. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "RSSI_PEER" ||
  131. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "DEVICE_IN_BOOTLOADER" ||
  132. childs[i].ChildNodes[j].Attributes["type"].Value.ToUpper() == "UPDATE_PENDING"
  133. ) {
  134. matches++;
  135. }
  136. }
  137. }
  138. if (matches > 1) {
  139. DeviceInfo nfo = new DeviceInfo();
  140. nfo.Data(childs[i]);
  141. this._systemChannel = Convert.ToInt32(childs[i].Attributes["ise_id"].Value);
  142. this._lastDeviceInfo = nfo;
  143. return;
  144. }
  145. }
  146. } else {
  147. if (c > 0) {
  148. for (int k = 0; k < c; k++) {
  149. if (childs[k].Name == "channel" && Convert.ToInt32(childs[k].Attributes["ise_id"].Value) == this._systemChannel) {
  150. DeviceInfo nfo = new DeviceInfo();
  151. nfo.Data(childs[k]);
  152. this._lastDeviceInfo = nfo;
  153. return;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. protected override string GetClassName() {
  160. return "HMDevice";
  161. }
  162. protected override string ValuesToString() {
  163. return this._address + ";" +
  164. this._interface + ";" +
  165. this._deviceType + ";" +
  166. (this._readyConfig ? "true" : "false") + ";" +
  167. (this._unreach ? "true" : "false") + ";" +
  168. (this._stickyUnreach ? "true" : "false") + ";" +
  169. (this._configPending ? "true" : "false") + ";";
  170. }
  171. // exp: Device ID: 1502, Channel ID: 1531
  172. }
  173. }