private bool ArePropertiesSet(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, Dictionary <string, string> props)
        {
            string json_response = clp_connection.GetCLPObject(clp_user, clp_token, clp_auid);

            CLP_Parameters clp_parameters = new CLP_Parameters();

            clp_parameters.Parse(json_response);
            Dictionary <string, string> dict = clp_parameters.GetParams(clp_auid);


            foreach (KeyValuePair <string, string> item in props)
            {
                if (!dict.ContainsKey(item.Key))
                {
                    return(false);
                }

                if (!dict[item.Key].ToUpper().Equals(item.Value.ToUpper()))
                {
                    return(false);
                }
            }


            return(true);
        }
        public void Oven_PUT_SetMeatProbeTemp(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, int temp)
        {
            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("SET_MEAT_PROBE_TEMP", "0");

            if (!ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
            {
                props.Clear();
                props.Add("SET_MEAT_PROBE_TEMP", temp.ToString());
                bool properties_set = false;
                int  count_repeats  = 0;
                while (count_repeats < 5)
                {
                    clp_connection.OV_SetMeatProbeTemp(clp_user, clp_token, clp_auid, temp);

                    int count_check = 0;
                    while (count_check < 2)
                    {
                        if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                        {
                            Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetMeatProbeTemp -> properties OK", LogAuthor.Adapter);
                            break;
                        }

                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetMeatProbeTemp -> properties recheck required", LogAuthor.Adapter);

                        count_check++;

                        Thread.Sleep(750);
                    }

                    if (count_check < 2)
                    {
                        properties_set = true;
                        break;
                    }

                    count_repeats++;

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetMeatProbeTemp -> action re-initiating", LogAuthor.Adapter);

                    Thread.Sleep(750);
                }

                if (!properties_set)
                {
                    Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetMeatProbeTemp -> action not executed", LogAuthor.Adapter);

                    ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
                }
            }
            else
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetMeatProbeTemp -> action not executed(meat probe not present)", LogAuthor.Adapter);
            }
        }
        public void Refrigerator_PUT_Fastfreeze(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, string property_name)
        {
            // Set fastfreeze property

            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("FASTFREEZE_FUNCTION", property_name);

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.RF_SetFastFreeze(clp_user, clp_token, clp_auid, property_name);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFastfreeze -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFastfreeze -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFastfreeze -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetFastfreeze -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public void Refrigerator_PUT_FreezerTemp(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, int temp)
        {
            // Set freezer temperature property

            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("SET_FREEZER_TEMPERATURE", temp.ToString());

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.RF_SetFreezerTemp(clp_user, clp_token, clp_auid, temp);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFreezerTemp -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFreezerTemp -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetFreezerTemp -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetFreezerTemp -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public void Oven_PUT_ChildLock(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, string property_name)
        {
            // Set child lock property

            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("CHILD_LOCK", property_name);

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.OV_SetChildLock(clp_user, clp_token, clp_auid, property_name);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetChildLock -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetChildLock -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetChildLock -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetChildLock -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public void Oven_PUT_BakingStartTimeMinute(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, int minute)
        {
            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("BAKING_START_TIME_MINUTE", minute.ToString());

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.OV_SetBakingStartTimeMinute(clp_user, clp_token, clp_auid, minute);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] BakingStartTimeMinute -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] BakingStartTimeMinute -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] BakingStartTimeMinute -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] BakingStartTimeMinute -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public void Oven_PUT_HeaterSystem(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, string heater)
        {
            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("HEATER_SYSTEM_SUB", heater);

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.OV_SetHeaterSystem(clp_user, clp_token, clp_auid, heater);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] HeaterSystem -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] HeaterSystem -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] HeaterSystem -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] HeaterSystem -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public void Oven_POST_ActionStop(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid)
        {
            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("DEVICE_STATUS", "IDLE");

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.OV_ActionStop(clp_user, clp_token, clp_auid);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] ActionStop -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] ActionStop -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] ActionStop -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] ActionStop -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);
            }
        }
        public ServiceWorker()
        {
            clp_connection = new CLP_Connection();
            _devices       = new List <Device>();
            _eventThreads  = new List <Thread>();
            _actions       = new List <string>()
            {
                "delayed_baking", "baking", "stop"
            };

            XmlDocument xmldoc = new XmlDocument();

            try
            {
                xmldoc.Load(@"C:\VICINITY\Files\ClpInfo.xml");
            }
            catch
            {
            }

            XmlNode node = xmldoc.SelectSingleNode("/User");

            if (node != null)
            {
                try
                {
                    clp_user = node["username"].InnerText;
                    clp_pass = node["password"].InnerText;
                }
                catch
                {
                }
            }

            clp_login_token        = null;
            clp_login_token_expire = DateTime.Now;

            exception = null;

            app_methods = new ApplianceMethods();

            //events
            List <DeviceEvent> ovenEvents         = new List <DeviceEvent>();
            List <DeviceEvent> refrigeratorEvents = new List <DeviceEvent>();
            XmlDocument        xmldoc2            = new XmlDocument();

            try
            {
                xmldoc2.Load(@"C:\VICINITY\Files\Events.xml");
                XmlNodeList xmlOvenEvents         = xmldoc2.SelectNodes("/Appliances/Appliance[@type='oven']/Events/Event");
                XmlNodeList xmlRefrigeratorEvents = xmldoc2.SelectNodes("/Appliances/Appliance[@type='refrigerator']/Events/Event");
                foreach (XmlNode ovenEvent in xmlOvenEvents)
                {
                    string eid   = ovenEvent.Attributes["eid"].Value;
                    string clpID = ovenEvent["clpID"].InnerText;
                    List <EmergencyLevel> emergencyLevel = null;
                    try
                    {
                        string lvl1 = ovenEvent["level1"].InnerText;
                        string lvl2 = ovenEvent["level2"].InnerText;
                        string lvl3 = ovenEvent["level3"].InnerText;
                        if (!string.IsNullOrEmpty(lvl1) && !string.IsNullOrEmpty(lvl2) && !string.IsNullOrEmpty(lvl3))
                        {
                            emergencyLevel = new List <EmergencyLevel>();
                            int no = int.Parse(lvl3);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.Three));
                            no = int.Parse(lvl2);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.Two));
                            no = int.Parse(lvl1);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.One));
                        }
                    }
                    catch
                    { }
                    ovenEvents?.Add(new DeviceEvent(clpID, eid, emergencyLevel));
                }
                foreach (XmlNode refrigeratorEvent in xmlRefrigeratorEvents)
                {
                    string eid   = refrigeratorEvent.Attributes["eid"].Value;
                    string clpID = refrigeratorEvent["clpID"].InnerText;
                    List <EmergencyLevel> emergencyLevel = null;
                    try
                    {
                        string lvl1 = refrigeratorEvent["level1"].InnerText;
                        string lvl2 = refrigeratorEvent["level2"].InnerText;
                        string lvl3 = refrigeratorEvent["level3"].InnerText;
                        if (!string.IsNullOrEmpty(lvl1) && !string.IsNullOrEmpty(lvl2) && !string.IsNullOrEmpty(lvl3))
                        {
                            emergencyLevel = new List <EmergencyLevel>();
                            int no = int.Parse(lvl3);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.Three));
                            no = int.Parse(lvl2);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.Two));
                            no = int.Parse(lvl1);
                            emergencyLevel?.Add(new EmergencyLevel(no, EmergencyStage.One));
                        }
                    }
                    catch
                    { }
                    refrigeratorEvents?.Add(new DeviceEvent(clpID, eid, emergencyLevel));
                }
            }
            catch
            { }

            XmlDocument xmldoc3 = new XmlDocument();

            try
            {
                xmldoc3.Load(@"C:\VICINITY\Files\Appliances.xml");
                XmlNodeList xmlAppliances = xmldoc3.SelectNodes("/items/item");
                foreach (XmlNode appliance in xmlAppliances)
                {
                    string type = appliance["type"].InnerText;
                    if (type.Equals("oven"))
                    {
                        _devices?.Add(new Device(appliance["clp"].InnerText, appliance.Attributes["oid"].Value, CopyEvents(ovenEvents), DeviceType.oven));
                    }
                    else if (type.Equals("refrigerator"))
                    {
                        _devices?.Add(new Device(appliance["clp"].InnerText, appliance.Attributes["oid"].Value, CopyEvents(refrigeratorEvents), DeviceType.refrigerator));
                    }
                }
            }
            catch
            { }
        }
        public void Oven_POST_DelayedBaking(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid, int duration, int delay, int temperature, string heater)
        {
            /* string response = clp_connection.ActionStop(clp_user, CLPtokenToString(), AUIDs[0]);
             * Thread.Sleep(1000); */


            // Set step bake 1 baking properties

            Dictionary <string, string> props = new Dictionary <string, string>();

            props.Add("SET_BAKE_TIME_MINUTES", duration.ToString());
            props.Add("SET_OVEN_TEMPERATURE", temperature.ToString());
            props.Add("HEATER_SYSTEM_SUB", heater);

            bool properties_set = false;
            int  count_repeats  = 0;

            while (count_repeats < 5)
            {
                clp_connection.OV_SetBakeSettings(clp_user, clp_token, clp_auid, "OVEN_SET", duration, heater, temperature);

                int count_check = 0;

                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetStepBakeSettingsStep1 -> action executed", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetStepBakeSettingsStep1 -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetStepBakeSettingsStep1 -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetStepBakeSettingsStep1 -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);

                return;
            }


            Thread.Sleep(750);


            // Set bake delay
            DateTime delay_time = DateTime.Now.AddMinutes(delay);

            clp_connection.OV_SetBakeStartTime(clp_user, clp_token, clp_auid, delay_time.Minute, delay_time.Hour);

            props.Clear();
            props.Add("BAKING_START_TIME_HOUR", delay_time.Hour.ToString());
            props.Add("BAKING_START_TIME_MINUTE", delay_time.Minute.ToString());

            properties_set = false;
            count_repeats  = 0;
            while (count_repeats < 5)
            {
                clp_connection.OV_SetBakeStartTime(clp_user, clp_token, clp_auid, delay_time.Minute, delay_time.Hour);

                int count_check = 0;
                while (count_check < 2)
                {
                    if (ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props))
                    {
                        Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetBakeStartTime -> properties OK", LogAuthor.Adapter);
                        break;
                    }

                    Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetBakeStartTime -> properties recheck required", LogAuthor.Adapter);

                    count_check++;

                    Thread.Sleep(750);
                }

                if (count_check < 2)
                {
                    properties_set = true;
                    break;
                }

                count_repeats++;

                Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetBakeStartTime -> action re-initiating", LogAuthor.Adapter);

                Thread.Sleep(750);
            }

            if (!properties_set)
            {
                Logger.Log(LogMsgType.ERROR, "[" + clp_auid + "] SetBakeStartTime -> action not executed", LogAuthor.Adapter);

                ArePropertiesSet(clp_connection, clp_user, clp_token, clp_auid, props);

                return;
            }
            Thread.Sleep(30000);


            // Start bake action

            clp_connection.OV_ActionStart(clp_user, clp_token, clp_auid);

            Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetBake ACTION SET", LogAuthor.Adapter);
        }
        public void Oven_POST_ActionStart(CLP_Connection clp_connection, string clp_user, string clp_token, string clp_auid)
        {
            clp_connection.OV_ActionStart(clp_user, clp_token, clp_auid);

            Logger.Log(LogMsgType.INFO, "[" + clp_auid + "] SetBake ACTION SET", LogAuthor.Adapter);
        }