Пример #1
0
        private void NotificationHandler()
        {
            //osae.AddToLog("Notification: " + m_notification.GetType().ToString(), false);
            switch (m_notification.GetType())
            {
                case ZWNotification.Type.ValueAdded:
                    {
                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                        using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                        {
                            device d = GetDevices(db).FirstOrDefault(o => o.node_id == node.ID);
                            if (d != null)
                            {
                                ZWValueID vid = m_notification.GetValueID();
                                Value value = new Value();
                                value.ValueID = vid;
                                value.Label = m_manager.GetValueLabel(vid);
                                value.Genre = vid.GetGenre().ToString();
                                value.Index = vid.GetIndex().ToString();
                                value.Type = vid.GetType().ToString();
                                value.CommandClassID = vid.GetCommandClassId().ToString();
                                value.Help = m_manager.GetValueHelp(vid);
                                bool read_only = m_manager.IsValueReadOnly(vid);
                                node.AddValue(value);

                                string data = "";
                                bool b = m_manager.GetValueAsString(vid, out data);

                                WriteToLog(Urgency.INFO, "[ValueAdded] Node:" + node.ID + ", Label:" + value.Label + ", Data:" + data + ", result: " + b.ToString());

                                //Values are 'unknown' at this point so dont report a value change.
                                DefineOrUpdateDeviceValue(new device_values
                                {
                                    device_id = d.id,
                                    value_id = vid.GetId().ToString(),
                                    label_name = value.Label,
                                    genre = value.Genre,
                                    index = value.Index,
                                    type = value.Type,
                                    commandClassId = value.CommandClassID,
                                    value = data,
                                    read_only = read_only
                                }, true);

                                #region Install Dynamic Commands

                                if (!read_only)
                                {
                                    Data_Types pType = Data_Types.NONE;

                                    //Set param types for command
                                    switch (vid.GetType())
                                    {
                                        case ZWValueID.ValueType.List:
                                            pType = Data_Types.LIST;
                                            break;
                                        case ZWValueID.ValueType.Byte:
                                            pType = Data_Types.BYTE;
                                            break;
                                        case ZWValueID.ValueType.Decimal:
                                            pType = Data_Types.DECIMAL;
                                            break;
                                        case ZWValueID.ValueType.Int:
                                            pType = Data_Types.INTEGER;
                                            break;
                                        case ZWValueID.ValueType.String:
                                            pType = Data_Types.STRING;
                                            break;
                                        case ZWValueID.ValueType.Short:
                                            pType = Data_Types.SHORT;
                                            break;
                                        case ZWValueID.ValueType.Bool:
                                            pType = Data_Types.BOOL;
                                            break;
                                    }

                                    //Install the Node Specific Command
                                    int order;
                                    switch (value.Genre)
                                    {
                                        case "User":
                                            order = 1;
                                            break;
                                        case "Config":
                                            order = 2;
                                            break;
                                        default:
                                            order = 99;
                                            break;
                                    }

                                    device_commands dynamic_dc = new device_commands
                                    {
                                        device_id = d.id,
                                        name = "DYNAMIC_CMD_" + value.Label.ToUpper(),
                                        friendly_name = "Set " + value.Label,
                                        arg_data_type = (int)pType,
                                        help = value.Help,
                                        custom_data1 = value.Label,
                                        custom_data2 = vid.GetId().ToString(),
                                        sort_order = order
                                    };

                                    //Special case for lists add additional info
                                    if (vid.GetType() == ZWValueID.ValueType.List)
                                    {
                                        //Install the allowed options/values
                                        String[] options;
                                        if (m_manager.GetValueListItems(vid, out options))
                                            foreach (string option in options)
                                                dynamic_dc.device_command_options.Add(new device_command_options { name = option });
                                    }

                                    DefineOrUpdateDeviceCommand(dynamic_dc);
                                }
                                #endregion
                            }
                        }
                        break;
                    }

                case ZWNotification.Type.ValueRemoved:
                    {
                        try
                        {
                            Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                            ZWValueID vid = m_notification.GetValueID();
                            Value val = node.GetValue(vid);

                            WriteToLog(Urgency.INFO, "[ValueRemoved] Node:" + node.ID + ",Label:" + m_manager.GetValueLabel(vid));

                            node.RemoveValue(val);
                            //TODO: Remove from values and command table
                        }
                        catch (Exception ex)
                        {
                            WriteToLog(Urgency.ERROR, "ValueRemoved error: " + ex.Message);
                        }
                        break;
                    }

                case ZWNotification.Type.ValueChanged:
                    {
                        //try
                        //{
                            Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                            ZWValueID vid = m_notification.GetValueID();
                            Value value = new Value();
                            value.ValueID = vid;
                            value.Label = m_manager.GetValueLabel(vid);
                            value.Genre = vid.GetGenre().ToString();
                            value.Index = vid.GetIndex().ToString();
                            value.Type = vid.GetType().ToString();
                            value.CommandClassID = vid.GetCommandClassId().ToString();
                            value.Help = m_manager.GetValueHelp(vid);
                            bool read_only = m_manager.IsValueReadOnly(vid);

                            string data = GetValue(vid);
                            //m_manager.GetValueAsString(vid, out data);

                            WriteToLog(Urgency.INFO,"[ValueChanged] Node:" + node.ID + ", Label:" + value.Label + ", Data:" + data);
                            using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                            {
                                device d = GetDevices(db).FirstOrDefault(o => o.node_id == node.ID);

                                if (d != null)
                                {
                                   // d.last_heard_from = DateTime.Now;
                                    //db.SaveChanges();

                                    //Update Device Commands
                                   if (!read_only)
                                    {
                                        //User commands are more important so lets see them first in the GUIs
                                        int order;
                                        switch (value.Genre)
                                        {
                                            case "User":
                                                order = 1;
                                                break;
                                            case "Config":
                                                order = 2;
                                                break;
                                            default:
                                                order = 99;
                                                break;
                                        }

                                        device_commands dc = d.device_commands.FirstOrDefault(c => c.custom_data2 == vid.GetId().ToString());

                                        if (dc != null)
                                        {
                                            //After Value is Added, Value Name other value properties can change so update.
                                            dc.friendly_name = "Set " + value.Label;
                                            dc.help = value.Help;
                                            dc.custom_data1 = value.Label;
                                            dc.sort_order = order;
                                        }
                                    }

                                    //Some dimmers take x number of seconds to dim to desired level.  Therefor the level recieved here initially is a
                                    //level between old level and new level. (if going from 0 to 100 we get 84 here).
                                    //To get the real level repoll the device a second or two after a level change was recieved.
                                    bool EnableDimmerRepoll = false;
                                    bool.TryParse(device_property_values.GetDevicePropertyValue(d.id, "ENABLEREPOLLONLEVELCHANGE"), out EnableDimmerRepoll);

                                    if (FinishedInitialPoll && EnableDimmerRepoll)
                                    {
                                        switch (node.Label)
                                        {
                                            case "Multilevel Switch":
                                            case "Multilevel Power Switch":
                                                {

                                                    switch (value.Label)
                                                    {
                                                        case "Basic":
                                                            device_values dv_basic = d.device_values.FirstOrDefault(v => v.value_id == vid.GetId().ToString());
                                                            if (dv_basic != null)
                                                            {
                                                                string prevVal = dv_basic.value;
                                                                //If it is truely new
                                                                if (!prevVal.Equals(data))
                                                                {
                                                                    System.Timers.Timer t = new System.Timers.Timer();
                                                                    t.Interval = 5000;
                                                                    t.Elapsed += (sender, e) =>
                                                                    {
                                                                        m_manager.RefreshNodeInfo(m_homeId, (byte)d.node_id);
                                                                        t.Enabled = false;
                                                                    };
                                                                    t.Enabled = true;
                                                                }
                                                            }
                                                            break;
                                                    }
                                                    break;
                                                }
                                        }
                                    }

                                    DefineOrUpdateDeviceValue(new device_values
                                    {
                                        device_id = d.id,
                                        value_id = vid.GetId().ToString(),
                                        label_name = value.Label,
                                        genre = value.Genre,
                                        index = value.Index,
                                        type = value.Type,
                                        commandClassId = value.CommandClassID,
                                        value = data,
                                        read_only = read_only
                                    });
                                }
                                else
                                {
                                    WriteToLog(Urgency.WARNING, "Getting changes on an unknown device!");
                                }

                            }
                        //}
                        //catch (Exception ex)
                        //{
                        //    WriteToLog(Urgency.ERROR, "error: " + ex.Message);
                        //}
                        break;
                    }

                case ZWNotification.Type.Group:
                    {
                        WriteToLog(Urgency.INFO, "[Group]"); ;
                        break;
                    }

                case ZWNotification.Type.NodeAdded:
                    {
                        // if this node was in zwcfg*.xml, this is the first node notification
                        // if not, the NodeNew notification should already have been received
                        //if (GetNode(m_notification.GetHomeId(), m_notification.GetNodeId()) == null)
                        //{
                            Node node = new Node();
                            node.ID = m_notification.GetNodeId();
                            node.HomeID = m_notification.GetHomeId();
                            m_nodeList.Add(node);

                            WriteToLog(Urgency.INFO, "[NodeAdded] ID:" + node.ID.ToString() + " Added");
                        //}
                        break;
                    }

                case ZWNotification.Type.NodeNew:
                    {
                        // Add the new node to our list (and flag as uninitialized)
                        Node node = new Node();
                        node.ID = m_notification.GetNodeId();
                        node.HomeID = m_notification.GetHomeId();
                        m_nodeList.Add(node);

                        WriteToLog(Urgency.INFO, "[NodeNew] ID:" + node.ID.ToString() + " Added");
                        break;
                    }

                case ZWNotification.Type.NodeRemoved:
                    {
                        foreach (Node node in m_nodeList)
                        {
                            if (node.ID == m_notification.GetNodeId())
                            {
                                WriteToLog(Urgency.INFO, "[NodeRemoved] ID:" + node.ID.ToString());
                                m_nodeList.Remove(node);
                                break;
                            }
                        }
                        break;
                    }

                case ZWNotification.Type.NodeProtocolInfo:
                    {
                        using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                        {
                            Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                            if (node != null)
                            {
                                node.Label = m_manager.GetNodeType(m_homeId, node.ID);
                            }
                            string deviceName = "UNKNOWN";
                            device_types device_type = null;

                            if (node != null)
                            {
                                WriteToLog(Urgency.INFO, "[Node Protocol Info] " + node.Label);

                                switch (node.Label)
                                {
                                    case "Toggle Switch":
                                    case "Binary Toggle Switch":
                                    case "Binary Switch":
                                    case "Binary Power Switch":
                                    case "Binary Scene Switch":
                                    case "Binary Toggle Remote Switch":
                                        deviceName = "OpenZWave Switch " + node.ID;
                                        device_type = GetDeviceType("SWITCH", db);
                                        break;
                                    case "Multilevel Toggle Remote Switch":
                                    case "Multilevel Remote Switch":
                                    case "Multilevel Toggle Switch":
                                    case "Multilevel Switch":
                                    case "Multilevel Power Switch":
                                    case "Multilevel Scene Switch":
                                        deviceName = "OpenZWave Dimmer " + node.ID;
                                        device_type = GetDeviceType("DIMMER", db);
                                        break;
                                    case "Multiposition Motor":
                                    case "Motor Control Class A":
                                    case "Motor Control Class B":
                                    case "Motor Control Class C":
                                        deviceName = "Variable Motor Control " + node.ID;
                                        device_type = GetDeviceType("DIMMER", db);
                                        break;
                                    case "General Thermostat V2":
                                    case "Heating Thermostat":
                                    case "General Thermostat":
                                    case "Setback Schedule Thermostat":
                                    case "Setpoint Thermostat":
                                    case "Setback Thermostat":
                                    case "Thermostat":
                                        deviceName = "OpenZWave Thermostat " + node.ID;
                                        device_type = GetDeviceType("THERMOSTAT", db);
                                        break;
                                    case "Static PC Controller":
                                    case "Static Controller":
                                    case "Portable Remote Controller":
                                    case "Portable Installer Tool":
                                    case "Static Scene Controller":
                                    case "Static Installer Tool":
                                        deviceName = "OpenZWave Controller " + node.ID;
                                        device_type = GetDeviceType("CONTROLLER", db);
                                        break;
                                    case "Secure Keypad Door Lock":
                                    case "Advanced Door Lock":
                                    case "Door Lock":
                                    case "Entry Control":
                                        deviceName = "OpenZWave Door Lock " + node.ID;
                                        device_type = GetDeviceType("DOORLOCK", db);
                                        break;
                                    case "Alarm Sensor":
                                    case "Basic Routing Alarm Sensor":
                                    case "Routing Alarm Sensor":
                                    case "Basic Zensor Alarm Sensor":
                                    case "Zensor Alarm Sensor":
                                    case "Advanced Zensor Alarm Sensor":
                                    case "Basic Routing Smoke Sensor":
                                    case "Routing Smoke Sensor":
                                    case "Basic Zensor Smoke Sensor":
                                    case "Zensor Smoke Sensor":
                                    case "Advanced Zensor Smoke Sensor":
                                    case "Routing Binary Sensor":
                                    case "Routing Multilevel Sensor":
                                        deviceName = "OpenZWave Sensor " + node.ID;
                                        device_type = GetDeviceType("SENSOR", db);
                                        break;
                                    default:
                                        {
                                            WriteToLog(Urgency.INFO, "[Node Label] " + node.Label);
                                            break;
                                        }
                                }
                                if (device_type != null)
                                {

                                    device ozw_device = GetDevices(db).FirstOrDefault(d => d.node_id == node.ID);
                                    //If we dont already have the device
                                    if (ozw_device == null)
                                    {
                                        ozw_device = new device
                                        {
                                            node_id = node.ID,
                                            device_types = device_type,
                                            friendly_name = deviceName
                                        };

                                        db.devices.AddObject(ozw_device);
                                        db.SaveChanges();

                                        ozw_device.CallAdded(new EventArgs());
                                    }

                                    #region Last Event Value Storeage
                                    //Node event value placeholder
                                    DefineOrUpdateDeviceValue(new device_values
                                    {
                                        device_id = ozw_device.id,
                                        value_id = LaastEventNameValueId,
                                        label_name = "Last Node Event Value",
                                        genre = "Custom",
                                        index = "0",
                                        type = "Byte",
                                        commandClassId = "0",
                                        value = "0",
                                        read_only = true
                                    });

                                    #endregion
                                }
                                else
                                    WriteToLog(Urgency.WARNING, string.Format("Found unknown device '{0}', node #{1}!", node.Label, node.ID));

                            }
                        }
                        break;
                    }

                case ZWNotification.Type.NodeNaming:
                    {
                        string ManufacturerNameValueId = "9999058723211334120";
                        string ProductNameValueId = "9999058723211334121";
                        string NodeLocationValueId = "9999058723211334122";
                        string NodeNameValueId = "9999058723211334123";

                        using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                {

                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                        if (node != null)
                        {
                            node.Manufacturer = m_manager.GetNodeManufacturerName(m_homeId, node.ID);
                            node.Product = m_manager.GetNodeProductName(m_homeId, node.ID);
                            node.Location = m_manager.GetNodeLocation(m_homeId, node.ID);
                            node.Name = m_manager.GetNodeName(m_homeId, node.ID);

                            device d = GetDevices(db).FirstOrDefault(o => o.node_id == node.ID);
                            if (d != null)
                            {
                                //lets store the manufacturer name and product name in the values table.
                                //Giving ManufacturerName a random value_id 9999058723211334120
                                DefineOrUpdateDeviceValue(new device_values
                                {
                                    device_id = d.id,
                                    value_id = ManufacturerNameValueId,
                                    label_name = "Manufacturer Name",
                                    genre = "Custom",
                                    index = "0",
                                    type = "String",
                                    commandClassId = "0",
                                    value = node.Manufacturer,
                                    read_only = true
                                });
                                DefineOrUpdateDeviceValue(new device_values
                                {
                                    device_id = d.id,
                                    value_id = ProductNameValueId,
                                    label_name = "Product Name",
                                    genre = "Custom",
                                    index = "0",
                                    type = "String",
                                    commandClassId = "0",
                                    value = node.Product,
                                    read_only = true
                                });
                                DefineOrUpdateDeviceValue(new device_values
                                {
                                    device_id = d.id,
                                    value_id = NodeLocationValueId,
                                    label_name = "Node Location",
                                    genre = "Custom",
                                    index = "0",
                                    type = "String",
                                    commandClassId = "0",
                                    value = node.Location,
                                    read_only = true
                                });
                                DefineOrUpdateDeviceValue(new device_values
                                {
                                    device_id = d.id,
                                    value_id = NodeNameValueId,
                                    label_name = "Node Name",
                                    genre = "Custom",
                                    index = "0",
                                    type = "String",
                                    commandClassId = "0",
                                    value = node.Name,
                                    read_only = true
                                });
                            }
                        }
                        WriteToLog(Urgency.INFO, "[NodeNaming] Node:" + node.ID + ", Product:" + node.Product + ", Manufacturer:" + node.Manufacturer + ")");
                        }
                        break;
                    }

                case ZWNotification.Type.NodeEvent:
                    {
                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());
                        byte gevent = m_notification.GetEvent();

                        if (node != null)
                        {
                            WriteToLog(Urgency.INFO, string.Format("[NodeEvent] Node: {0}, Event Byte: {1}", node.ID, gevent));

                            using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                            {
                                #region Last Event Value Storeage
                                device d = GetDevices(db).FirstOrDefault(o => o.node_id == node.ID);
                                if (d != null)
                                {
                                    //Node event value placeholder
                                    device_values dv = d.device_values.FirstOrDefault(v => v.value_id == LaastEventNameValueId);
                                    if (dv != null)
                                    {
                                        dv.value = gevent.ToString();
                                        db.SaveChanges();

                                        //Since events are differently than values fire the value change event every time we recieve the event regardless if
                                        //it is the same value or not.
                                        dv.DeviceValueDataChanged(new device_values.ValueDataChangedEventArgs { device_value_id  = dv.id, previousValue = string.Empty});
                                    }
                                }
                                #endregion
                            }
                        }
                        break;

                    }

                case ZWNotification.Type.PollingDisabled:
                    {
                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());

                        if (node != null)
                        {
                            WriteToLog(Urgency.INFO, "[PollingDisabled] Node:" + node.ID);
                        }

                        break;
                    }

                case ZWNotification.Type.PollingEnabled:
                    {
                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());

                        if (node != null)
                        {
                            WriteToLog(Urgency.INFO, "[PollingEnabled] Node:" + node.ID);
                        }
                        break;
                    }

                case ZWNotification.Type.DriverReady:
                    {
                        m_homeId = m_notification.GetHomeId();
                        WriteToLog(Urgency.INFO, "Initializing: Driver with Home ID 0x" + m_homeId);
                        WriteToLog(Urgency.INFO, "[DriverReady] Initializing...driver with Home ID 0x" + m_homeId);
                        break;
                    }

                case ZWNotification.Type.NodeQueriesComplete:
                    {

                        Node node = GetNode(m_notification.GetHomeId(), m_notification.GetNodeId());

                        if (node != null)
                        {
                            using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                            {
                                device d = GetDevices(db).FirstOrDefault(o => o.node_id == node.ID);
                                if (d != null)
                                {
                                    d.last_heard_from = DateTime.Now;
                                }
                                db.SaveChanges();

                                zvsEntityControl.CallDeviceModified(d, "last_heard_from");
                            }

                            WriteToLog(Urgency.INFO, "Initializing: Node " + node.ID + " query complete.");
                            WriteToLog(Urgency.INFO, "[NodeQueriesComplete] Initializing...node " + node.ID + " query complete.");
                        }

                        break;
                    }

                case ZWNotification.Type.AllNodesQueried:
                    {
                        using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                        {
                            foreach (Node n in m_nodeList)
                            {
                                device d = GetDevices(db).FirstOrDefault(o => o.node_id == n.ID);

                                if (d != null)
                                {
                                    if (device_property_values.GetDevicePropertyValue(d.id, "ENABLEPOLLING").ToUpper().Equals("TRUE"))
                                        EnablePolling(n.ID);
                                }
                            }
                        }

                        WriteToLog(Urgency.INFO, "Ready:  All nodes queried. Plug-in now ready.");
                        IsReady = true;

                        FinishedInitialPoll = true;
                        break;
                    }

                case ZWNotification.Type.AwakeNodesQueried:
                    {
                        using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                        {
                            foreach (Node n in m_nodeList)
                            {
                                device d = GetDevices(db).FirstOrDefault(o => o.node_id == n.ID);

                                if (d != null)
                                {
                                    if (device_property_values.GetDevicePropertyValue(d.id, "ENABLEPOLLING").ToUpper().Equals("TRUE"))
                                        EnablePolling(n.ID);
                                }
                            }
                        }

                        WriteToLog(Urgency.INFO, "Ready:  Awake nodes queried (but not some sleeping nodes).");
                        IsReady = true;

                        FinishedInitialPoll = true;

                        break;
                    }
            }
        }
Пример #2
0
 /// <summary>
 /// Deprecated Method for adding a new object to the device_commands EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTodevice_commands(device_commands device_commands)
 {
     base.AddObject("device_commands", device_commands);
 }
Пример #3
0
        public void DefineOrUpdateDeviceCommand(device_commands dc)
        {
            using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
            {
                device d = db.devices.FirstOrDefault(o => o.id == dc.device_id);
                if (d != null)
                {
                    //Does device type exist?
                    device_commands existing_dc = d.device_commands.FirstOrDefault(c => c.name == dc.name);

                    if (existing_dc == null)
                    {
                        d.device_commands.Add(dc);
                    }
                    else
                    {
                        existing_dc.help = dc.help;
                        existing_dc.friendly_name = dc.friendly_name;
                        existing_dc.description = dc.description;
                        existing_dc.custom_data2 = dc.custom_data2;
                        existing_dc.custom_data1 = dc.custom_data1;
                        existing_dc.arg_data_type = dc.arg_data_type;

                        existing_dc.device_command_options.Clear();

                        foreach (var option in db.device_command_options.Where(o => o.device_command_id == existing_dc.id).ToArray())
                        {
                            db.DeleteObject(option);
                        }

                        foreach (device_command_options o in dc.device_command_options)
                            existing_dc.device_command_options.Add(new device_command_options { name = o.name });

                        existing_dc.sort_order = dc.sort_order;
                    }
                    db.SaveChanges();
                }
            }
        }
Пример #4
0
 /// <summary>
 /// Create a new device_commands object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="device_id">Initial value of the device_id property.</param>
 /// <param name="name">Initial value of the name property.</param>
 /// <param name="friendly_name">Initial value of the friendly_name property.</param>
 /// <param name="arg_data_type">Initial value of the arg_data_type property.</param>
 public static device_commands Createdevice_commands(global::System.Int64 id, global::System.Int64 device_id, global::System.String name, global::System.String friendly_name, global::System.Int64 arg_data_type)
 {
     device_commands device_commands = new device_commands();
     device_commands.id = id;
     device_commands.device_id = device_id;
     device_commands.name = name;
     device_commands.friendly_name = friendly_name;
     device_commands.arg_data_type = arg_data_type;
     return device_commands;
 }