private void btn_Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_name.Text))
            {
                MessageBox.Show("Please enter a name for this trigger.", zvsEntityControl.zvsNameAndVersion, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                ActiveControl = txt_name;
                return;
            }

            if (string.IsNullOrEmpty(txTriggertValue.Text))
            {
                MessageBox.Show("Please enter a trigger value for this trigger.", zvsEntityControl.zvsNameAndVersion, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                ActiveControl = txt_name;
                return;
            }

            device_values selected_device_value = (device_values)cmbo_devicevalues.SelectedItem;
            if (selected_device_value == null)
            {
                MessageBox.Show("Please select a device value for this trigger.", zvsEntityControl.zvsNameAndVersion, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                ActiveControl = cmbo_devicevalues;
                return;
            }

            scene selected_scene = (scene)cmbo_scene.SelectedItem;
            if (selected_scene == null)
            {
                MessageBox.Show("Please select a scene for this trigger.", zvsEntityControl.zvsNameAndVersion, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                ActiveControl = cmbo_scene;
                return;
            }

            using (zvsEntities2 db = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
            {
                device_value_triggers trigger_to_edit;
                if (trigger_to_edit_id.HasValue)
                {
                    trigger_to_edit = db.device_value_triggers.FirstOrDefault(o => o.id == trigger_to_edit_id.Value);
                }
                else
                {
                    trigger_to_edit = new device_value_triggers();
                }

                trigger_to_edit.device_value_id = selected_device_value.id;
                trigger_to_edit.enabled = true;
                trigger_to_edit.trigger_value = txTriggertValue.Text;
                trigger_to_edit.trigger_operator = (int)cmbo_operator.SelectedItem;
                trigger_to_edit.scene_id = selected_scene.id;
                trigger_to_edit.Name = txt_name.Text;
                trigger_to_edit.enabled = checkBoxEnabled.Checked;
                trigger_to_edit.trigger_type = (int)device_value_triggers.TRIGGER_TYPE.Basic;

                //if we are not editing add new trigger to trigger list
                if (!trigger_to_edit_id.HasValue)
                    db.device_value_triggers.AddObject(trigger_to_edit);

                db.SaveChanges();
            }
            zvsEntityControl.CallTriggerModified(this, "modified");
            this.Close();
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the device_value_triggers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTodevice_value_triggers(device_value_triggers device_value_triggers)
 {
     base.AddObject("device_value_triggers", device_value_triggers);
 }
        public static void RunScript(device_value_triggers trigger)
        {
            Logger.WriteToLog(zVirtualScenesCommon.Urgency.INFO, "Running Advanced Script - " + trigger.FriendlyName, _FriendlyName);

            // Step 1: Split up the lines of the script
            String[] splitScript = trigger.trigger_script.ToLower().Split('\n');
            List<bool> ifStatements = new List<bool>();
            bool waitingForOpen = false;

            // Step 2: Start iterating through all lines of the script
            foreach (String scriptLine in splitScript)
            {
                // Step 3: Detect what command the line is using (eg. RunScene, if, else)
                if (waitingForOpen)
                {
                    if (!scriptLine.StartsWith("{"))
                    {
                        Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "Script " + trigger.FriendlyName + " started 'if' statement without the { on the next line", _FriendlyName);
                        throw new Exception();
                    }

                    waitingForOpen = false;
                }
                else if (ifStatements.Count > 0 && ifStatements[ifStatements.Count - 1] == false)
                {
                    if (scriptLine.StartsWith("}"))
                    {
                        ifStatements.RemoveAt(ifStatements.Count - 1);
                    }
                }
                else if (scriptLine.StartsWith("runscene"))
                {
                    // Run this scene!
                    using (zvsEntities2 context = new zvsEntities2(zvsEntityControl.GetzvsConnectionString))
                    {
                        String scene_name = scriptLine.Substring(scriptLine.IndexOf(" ") + 1);
                        scene _scene = context.scenes.FirstOrDefault(s => s.friendly_name.ToLower() == scene_name);

                        // Make sure the user specified a correct scene name
                        if (_scene != null)
                        {
                            _scene.RunScene(context);
                            Logger.WriteToLog(zVirtualScenesCommon.Urgency.INFO, "Script " + trigger.FriendlyName + " ran scene '" + scene_name + "'", _FriendlyName);
                        }
                        else
                        {
                            Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "Script " + trigger.FriendlyName + " specified a invalid scene name '" + scene_name + "'", _FriendlyName);
                            return;
                        }
                    }
                }
                else if (scriptLine.StartsWith("if"))
                {
                    // Calculate this if statement!
                    try
                    {
                        String parameters = scriptLine.Substring(scriptLine.IndexOf("(") + 1, scriptLine.LastIndexOf(")") - scriptLine.IndexOf("(") - 1);

                        // TODO: Add ability to have multiple comparisions - if (Dimmer1.Level == 20 && Dimmer2.Level == 30)

                        device_value_triggers.TRIGGER_OPERATORS operatorType;
                        String strOperator;
                        String[] strValues = new String[2];
                        int[] intValues = new int[2];

                        if (parameters.Contains("=="))
                        {
                            operatorType = device_value_triggers.TRIGGER_OPERATORS.EqualTo;
                            strOperator = "==";
                        }
                        else if (parameters.Contains("!="))
                        {
                            operatorType = device_value_triggers.TRIGGER_OPERATORS.NotEqualTo;
                            strOperator = "!=";
                        }
                        else if (parameters.Contains("<"))
                        {
                            operatorType = device_value_triggers.TRIGGER_OPERATORS.LessThan;
                            strOperator = "<";
                        }
                        else if (parameters.Contains(">"))
                        {
                            operatorType = device_value_triggers.TRIGGER_OPERATORS.GreaterThan;
                            strOperator = ">";
                        }
                        else
                        {
                            // ??
                            Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "Script " + trigger.FriendlyName + " unkown operator in '" + parameters + "'", _FriendlyName);
                            return;
                        }

                        strValues[0] = parameters.Substring(0, parameters.IndexOf(strOperator));
                        strValues[1] = parameters.Substring(parameters.IndexOf(strOperator) + strOperator.Length);

                        intValues[0] = GetValue(trigger.FriendlyName, strValues[0]);
                        intValues[1] = GetValue(trigger.FriendlyName, strValues[1]);

                        switch (operatorType)
                        {
                            case device_value_triggers.TRIGGER_OPERATORS.EqualTo:
                                if (intValues[0] == intValues[1])
                                    ifStatements.Add(true);
                                else
                                    ifStatements.Add(false);
                                break;
                            case device_value_triggers.TRIGGER_OPERATORS.NotEqualTo:
                                if (intValues[0] != intValues[1])
                                    ifStatements.Add(true);
                                else
                                    ifStatements.Add(false);
                                break;
                            case device_value_triggers.TRIGGER_OPERATORS.LessThan:
                                if (intValues[0] < intValues[1])
                                    ifStatements.Add(true);
                                else
                                    ifStatements.Add(false);
                                break;
                            case device_value_triggers.TRIGGER_OPERATORS.GreaterThan:
                                if (intValues[0] > intValues[1])
                                    ifStatements.Add(true);
                                else
                                    ifStatements.Add(false);
                                break;
                        }

                        waitingForOpen = true;
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "Script " + trigger.FriendlyName + " - Unable to get parameters for if statement from line", _FriendlyName);
                        Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "'" + scriptLine + "'.", _FriendlyName);
                        Logger.WriteToLog(zVirtualScenesCommon.Urgency.ERROR, "Did you include both ( and )?", _FriendlyName);
                        return;
                    }
                }
            }

            Logger.WriteToLog(zVirtualScenesCommon.Urgency.INFO, "Finished Running Advanced Script - " + trigger.FriendlyName, _FriendlyName);
        }
 /// <summary>
 /// Create a new device_value_triggers object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="device_value_id">Initial value of the device_value_id property.</param>
 /// <param name="enabled">Initial value of the enabled property.</param>
 public static device_value_triggers Createdevice_value_triggers(global::System.Int64 id, global::System.Int64 device_value_id, global::System.Boolean enabled)
 {
     device_value_triggers device_value_triggers = new device_value_triggers();
     device_value_triggers.id = id;
     device_value_triggers.device_value_id = device_value_id;
     device_value_triggers.enabled = enabled;
     return device_value_triggers;
 }