public Client(MqttSettings mqttSettings, bool autoReconnect = true) { this._mqttSettings = mqttSettings; this._autoReconnect = autoReconnect; Log = LogManager.GetCurrentClassLogger("MqttClient"); client = new MqttClient(_mqttSettings.broker, _mqttSettings.port, false, null, null, MqttSslProtocols.None); client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived; client.ConnectionClosed += Client_ConnectionClosed; string clientId = _mqttSettings.deviceId; if (autoReconnect) { _reconnectTimer = new System.Timers.Timer(_mqttSettings.reconnectInterval); _reconnectTimer.Elapsed += delegate { if (client != null && !client.IsConnected) { ConnectionReconnecting?.Invoke(); _queueCancellationTokenSource.Cancel(); MqttConnect(); } }; } }
public SensorHost(IClient client, SensorManager sensorManager) { this._client = client; this._sensorManager = sensorManager; Log = LogManager.GetCurrentClassLogger("SensorHost"); IsCodeLoaded = false; }
public FakeClient(MqttSettings mqttSettings) { this._mqttSettings = mqttSettings; Log = LogManager.GetCurrentClassLogger("FakeMqtt"); Log.Warn("***"); Log.Warn("Initializing FakeClient. No actual MQTT connections will be made."); Log.Warn("Essentially a very basic, very fake MQTT internal server."); Log.Warn($"Using fake delays: {_mqttSettings.useFakeMqttDelays} | Using fake failures: {_mqttSettings.useFakeMqttFailures}"); Log.Warn("***"); }
/// <summary> /// This is called by PC2MQTT after compiling the sensor. /// </summary> /// <remarks> /// Load any databases, connect to any services, spin up any servers, etc. /// Control will be returned to the sensor in <see cref="SensorMain"/> after all sensors have loaded. /// Note that sensor scripts are non-blocking so other scripts will run as well, but if you take too long without /// returning here your sensor will be disposed because it did not initialize in a timely manner. /// </remarks> /// <param name="sensorHost"></param> /// <returns>True if initialized successfully, false if not.</returns> public bool Initialize(SensorHost sensorHost) { Log = LogManager.GetCurrentClassLogger(GetSensorIdentifier()); this.sensorHost = sensorHost; Log.Info($"Finishing initialization in {this.GetSensorIdentifier()}"); return(true); }
public SensorHost(string code, IClient client, SensorManager sensorManager) { this.code = code; this._client = client; this._sensorManager = sensorManager; Log = LogManager.GetCurrentClassLogger("SensorHost"); IsCodeLoaded = true; Compile(); }
public SensorManager(IClient client, Helpers.Settings settings) { this._client = client; this.settings = settings; Log = LogManager.GetCurrentClassLogger("SensorManager"); sensorCleanupTimer = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds); sensorCleanupTimer.Elapsed += SensorCleanupTimer_Elapsed; sensorTopics = new ConcurrentDictionary <string, SensorHost>(); sensorMultiLevelWildcardTopics = new ConcurrentDictionary <string, SensorHost>(); sensorSingleLevelWildcardTopics = new ConcurrentDictionary <string, SensorHost>(); }
private static void Main(string[] args) { Console.WriteLine($"PC2MQTT v{Version} starting"); InitializeSettings(); InitializeExtensions(); Logging.InitializeLogging(settings); Log = LogManager.GetCurrentClassLogger("PC2MQTT"); Task roslynLoading = null; if (!settings.config.useOnlyBuiltInSensors) { roslynLoading = Task.Run(() => { Log.Verbose("Pre-loading Roslyn compiler.."); CSScriptLib.RoslynEvaluator.LoadCompilers(); Log.Verbose("Roslyn finished loading."); }); } InitializeMqtt(); InitializeSensors(settings.config.useOnlyBuiltInSensors, roslynLoading); // this isn't ideal, but sometimes the mqtt server will send data before sensors // have fully initialized. foreach (var item in overflow) { sensorManager.ProcessMessage(item); } sensorManager.StartSensors(); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit); _closing.WaitOne(); sensorManager.NotifySensorsServerStatus(ServerState.Disconnecting, ServerStateReason.ShuttingDown); Log.Info("Shutting down.."); Log.Debug("Disposing of SensorManager.."); sensorManager.Dispose(); Log.Debug("Disconnecting MQTT.."); client.MqttDisconnect(); settings.SaveSettings(); Environment.Exit(0); }
public SensorHost(ISensor sensor, IClient client, SensorManager sensorManager) { this._client = client; this._sensorManager = sensorManager; this.sensor = sensor; Log = LogManager.GetCurrentClassLogger("SensorHost"); if (sensor.IsCompatibleWithCurrentRuntime()) { IsCodeLoaded = true; IsCompiled = true; } this.SensorIdentifier = sensor.GetSensorIdentifier().ToLower(); }