internal void Reload(Rainmeter.API api, ref double maxValue)
        {
            try
            {
                number = api.ReadInt("Value", 0);
            }
            catch (Exception e) {}
            try
            {
                Arc.Thickness = Convert.ToSingle(api.ReadDouble("Width", Arc.Thickness));
            }
            catch (Exception e) {}
            try
            {
                Digit.Size = Convert.ToInt32(api.ReadInt("Size", Digit.Size) * ScaleFactor);
            }
            catch (Exception e) {}
            try
            {
                outFilePath = api.ReadString("Out", string.Format("{0}.png", number.ToString()));
            }
            catch (Exception e) {}
            try
            {
                var colorVals = api.ReadString("Color", string.Format("{0},{1},{2},{3}", Arc.Red, Arc.Green, Arc.Blue, Arc.Opacity)).Split(',').Select(n => Int32.Parse(n)).ToArray();
                switch (colorVals.Length)
                {
                case 4:
                {
                    Arc.Opacity = colorVals[3];
                    goto case 3;
                }

                case 3:
                {
                    Arc.Red   = colorVals[0];
                    Arc.Green = colorVals[1];
                    Arc.Blue  = colorVals[2];
                    break;
                }
                }
            }
            catch (Exception e) {}
            try
            {
                Digit.GlobalAngle = Convert.ToSingle(api.ReadDouble("Angle", Digit.GlobalAngle));
            }
            catch (Exception e) { }
        }
        internal MqttClientMeasure(Rainmeter.API api)
        {
            ParentMeasures.Add(this);
            ParentRainmeterApis.Add(api);
            this.Rainmeter = api;
            this.Name      = api.GetMeasureName();
            Skin           = api.GetSkin();
            DebugLevel     = (ushort)api.ReadInt("DebugLevel", 0);

            Server        = api.ReadString("Server", "localhost");
            Port          = (ushort)api.ReadInt("Port", 1883);
            RetryInterval = (ushort)api.ReadDouble("RetryInterval", 5.0);
            ClientId      = api.ReadString("ClientId", Guid.NewGuid().ToString());
            Username      = api.ReadString("Username", "");
            Password      = new SecureString();
            foreach (char ch in api.ReadString("Password", ""))
            {
                Password.AppendChar(ch);
            }

            /* Mqtt Server Bangs */
            OnConnectBangs    = SplitBangs(api.ReadString("OnConnect", ""));
            OnDisconnectBangs = SplitBangs(api.ReadString("OnConnect", ""));
            OnReloadBangs     = SplitBangs(api.ReadString("OnReload", ""));
            OnMessageBangs    = SplitBangs(api.ReadString("OnMessage", ""));

            MqttClient = Factory.CreateManagedMqttClient();

            /* Setup Event Handlers */
            MqttClient.UseConnectedHandler(e =>
            {
                if (!MqttClientMeasure.ParentRainmeterApis.Contains(Rainmeter))
                {
                    return;
                }

                Log(API.LogType.Notice, "Connected to " + Server + " : " + Port);

                if (OnConnectBangs.Length > 0)
                {
                    Log(API.LogType.Notice, "Executing OnConnect Bangs");
                    ExecuteBangs(OnConnectBangs);
                }
            });

            MqttClient.UseApplicationMessageReceivedHandler(e =>
            {
                if (!MqttClientMeasure.ParentRainmeterApis.Contains(Rainmeter))
                {
                    return;
                }

                e.GetType();
                String topic   = e.ApplicationMessage.Topic;
                String payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                try
                {
                    Debug("### RECEIVED APPLICATION MESSAGE ###", 3);
                    Debug($" >> Topic = {e.ApplicationMessage.Topic}", 4);
                    Debug($" >> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}", 4);
                    Debug($" >> QoS = {e.ApplicationMessage.QualityOfServiceLevel}", 5);
                    Debug($" >> Retain = {e.ApplicationMessage.Retain}", 5);

                    if (Topics.Contains(topic))
                    {
                        Topics[topic] = payload;
                        Log(API.LogType.Notice, "Received update for " + topic);
                    }
                    else
                    {
                        Topics.Add(topic, payload);
                        Log(API.LogType.Warning, "Received payload for unknown topic " + topic);
                    }

                    if (OnMessageBangs.Length > 0)
                    {
                        Log(API.LogType.Notice, "Executing OnMessage Bangs");
                        ExecuteBangs(OnMessageBangs);
                    }
                }
                catch
                {
                    // Error Application
                }
            });

            MqttClient.UseDisconnectedHandler(e =>
            {
                if (!MqttClientMeasure.ParentRainmeterApis.Contains(Rainmeter))
                {
                    return;
                }

                Log(API.LogType.Error, e.Exception?.Message);
                Log(API.LogType.Error, e.AuthenticateResult?.ReasonString);
                Log(API.LogType.Error, e.ClientWasConnected.ToString());

                if (!MqttClient.IsConnected)
                {
                    Log(API.LogType.Warning, "Lost previous connection to " + Server + " : " + Port);
                }

                if (OnDisconnectBangs.Length > 0)
                {
                    Log(API.LogType.Notice, "Executing OnDisconnect Bangs");
                    ExecuteBangs(OnDisconnectBangs);
                }
            });

            try
            {
                Log(API.LogType.Warning, "Connecting to " + Server + " : " + Port + "...");
                ConnectAsync(Server, Port, Username, Password, ClientId).Wait();
            }
            catch (Exception ex)
            {
                Log(API.LogType.Error, "Exception trying to connect: " + ex);
                return;
            }
        }
예제 #3
0
        internal static void ParseProperties(Rainmeter.API api, object obj)
        {
            LogHelper log  = new LogHelper(api);
            Type      type = obj.GetType();

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            EventInfo[]    events     = type.GetEvents(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo pinfo in properties)
            {
                if (pinfo.CanWrite)
                {
                    if (pinfo.PropertyType == typeof(string))
                    {
                        //log.LogDebug("Found string Property: " + pinfo.Name);
                        string propval = api.ReadString(pinfo.Name, "");
                        if (propval != "")
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(int))
                    {
                        int val = (int)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found int Property: " + pinfo.Name + " with value: " + val);
                        int propval = api.ReadInt(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(bool))
                    {
                        bool val = (bool)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found " + pinfo.PropertyType + " Property: " + pinfo.Name + " with value: " + val);
                        bool propval = api.ReadInt(pinfo.Name, Convert.ToInt32(val)) > 0;
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(float))
                    {
                        float val = (float)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found float Property: " + pinfo.Name + " with value: " + val);
                        float propval = (float)api.ReadDouble(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(double))
                    {
                        double val = (double)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found double Property: " + pinfo.Name + " with value: " + val);
                        double propval = api.ReadDouble(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else
                    {
                        string propval = api.ReadString(pinfo.Name, "");
                        if (propval != "")
                        {
                            object val = null;

                            if (pinfo.PropertyType == typeof(Color))
                            {
                                val = Util.ColorFromString(propval);
                            }
                            else
                            {
                                val = pinfo.PropertyType.IsEnum ?
                                      Util.EnumFromString(pinfo.PropertyType, propval) :
                                      Util.ObjectFromString(api, pinfo.PropertyType, propval);
                            }

                            if (val != null)
                            {
                                pinfo.SetValue(obj, val, null);
                            }
                            else
                            {
                                log.LogPropertyValueNotValid(propval, pinfo);
                            }
                        }
                    }
                }
            }

            foreach (EventInfo einfo in events)
            {
                // winforms syntax for events
                string evntval = api.ReadString(einfo.Name, "");
                if (evntval == "")
                {
                    // rainmeter syntax for events
                    evntval = api.ReadString("On" + einfo.Name, "");
                    if (evntval == "")
                    {
                        continue;
                    }
                }

                // remove existing event
                cEventHelper.RemoveEventHandler(obj, einfo.Name);

                // create new event
                EventHandler untypedHandler = delegate(object sender, EventArgs args)
                {
                    api.Execute(evntval);
                };

                Delegate typedHandler = Delegate.CreateDelegate(einfo.EventHandlerType,
                                                                untypedHandler.Target, untypedHandler.Method);
                einfo.AddEventHandler(obj, typedHandler);
            }
        }