| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System.Xml;
- using System.Collections.Generic;
- using System.Net.HomeMatic.Events;
- namespace System.Net.HomeMatic {
- public class HMDataPoint : HMBase {
- public delegate void ValueChangedHandler(object sender, ValueChangedEventArgs e);
- public event ValueChangedHandler OnValueChanged;
- public delegate void BeforeValueChangedHandler(object sender, ValueChangedEventArgs e);
- public event BeforeValueChangedHandler OnBeforeValueChanged;
- private DataPoint_Type _type;
- private string _value;
- private int _valueType;
- private string _valueUnit;
- private int _timeStamp;
- private HMChannel _channel;
- private DateTime lastUpdateTime;
- private bool bInit = false;
- public HMDataPoint(HMApi Api, HMChannel Channel) :base(Api) {
- this._channel = Channel;
- }
- public HMDataPoint(HMApi Api, HMChannel Channel, XmlNode Data) : base(Api, Data) {
- this._channel = Channel;
- this.Init(Data);
- this.bInit = true;
- }
- public void Init(XmlNode Data) {
- this._type = this.Attr_DataPointType(Data, "type");
- if (this.bInit) {
- this.Value = this.Attr_String(Data, "value");
- } else {
- this._value = this.Attr_String(Data, "value");
- }
- this._valueType = this.Attr_Int32(Data, "value_type");
- this._valueUnit = this.Attr_String(Data, "value_unit");
- this._timeStamp = this.Attr_Int32(Data, "timestamp");
- this.lastUpdateTime = DateTime.Now;
- }
- public DataPoint_Type DataType {
- get {
- return this._type;
- }
- }
- public void Update() {
- XmlDocument doc = this.Api.GetApiData(ApiMethode.State, new Dictionary<string, object>() {["device_id"] = this.Channel.Device.IseID });
- XmlNodeList child_devices = doc.ChildNodes[1].ChildNodes;
- int cdc = child_devices.Count;
- if (cdc > 0) {
- for (int i = 0; i < cdc; i++) {
- if (child_devices[i].Name == "device") {
- XmlNodeList child_channels = child_devices[i].ChildNodes;
- int ccc = child_channels.Count;
- if (ccc > 0) {
- for (int j = 0; j < ccc; j++) {
- if (child_channels[j].Name == "channel" && this.Attr_Int32(child_channels[j],"ise_id") == this._channel.IseID) {
- XmlNodeList child_datapoints = child_channels[j].ChildNodes;
- int cdpc = child_datapoints.Count;
- if (cdpc > 0) {
- for (int k = 0; k < cdpc; k++) {
- if (child_datapoints[k].Name == "datapoint") {
- if (this.Attr_Int32(child_datapoints[k],"ise_id") == this.IseID) {
- this._timeStamp = this.Attr_Int32(child_datapoints[k], "timestamp");
- string newVal = this.Attr_String(child_datapoints[k], "value");
- this.changeValue(newVal, false);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- public string Value {
- get {
- if ((DateTime.Now - this.lastUpdateTime).TotalSeconds > 10) {
- //this.Update();
- this.Api.UpdateDevices();
- }
- return this._value;
- }
- set {
- System.Diagnostics.Debug.WriteLine("Change Value of Datapoint " + this.Channel.Name + "." + this.GetDataPointType_To_String(this._type) + " From '" + this._value + "' => '" + value + "'");
- this.changeValue(value, true);
- }
- }
- protected bool changeValue(string newValue, bool bSendApi = false) {
- if (this._value != newValue) {
- bool bChange = true;
- if (this.OnBeforeValueChanged != null) {
- BeforeValueChangedEventArgs args = new BeforeValueChangedEventArgs(this, this._value, newValue);
- this.OnBeforeValueChanged(this, args);
- bChange = args.Accept;
- }
- if (bChange) {
- string oldVal = this._value;
- this._value = newValue;
- if (this.OnValueChanged != null) {
- ValueChangedEventArgs args = new ValueChangedEventArgs(this, oldVal, this._value);
- this.OnValueChanged(this, args);
- }
- Dictionary<string, object> val = new Dictionary<string, object>() {
- ["ise_id"] = this._ise_id,
- ["new_value"] = newValue
- };
- this.lastUpdateTime = DateTime.Now;
- if (bSendApi) {
- Api.GetApiData(ApiMethode.StateChange, val);
- }
- }
- return bChange;
- }
- return false;
- }
- public int ValueType {
- get { return this._valueType; }
- }
- public string Unit {
- get { return this._valueUnit; }
- }
- public int TimeStamp {
- get { return this._timeStamp; }
- }
- public HMChannel Channel {
- get { return this._channel; }
- }
- protected override string GetClassName() {
- return "HMDataPoint";
- }
- protected override string ValuesToString() {
- return this._type.ToString() + ";" +
- this._value.ToString() + ";" +
- this._valueType.ToString() + ";" +
- this._valueUnit + ";" +
- this._timeStamp.ToString();
- }
- }
- }
|