static void Main(string[] args) { // this starts MQTTnet client Task.Run(() => ManagedClientTest.RunAsync("config")); //Initialise inverter Inverter x = new Inverter(); //open serial port x.OpenPort(); //x.PollTest(); //Test data x.Poll(); //Live port while (true) { Console.ReadLine(); } }
public static async Task RunAsync1(string type) { try { if (managedClient.IsConnected) { await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_inverter_mode").WithPayload(InverterData.Mode)); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_grid_voltage").WithPayload(InverterData.QPIGS.voltage_grid.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_grid_frequency").WithPayload(InverterData.QPIGS.freq_grid.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_out_voltage").WithPayload(InverterData.QPIGS.voltage_out.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_out_frequency").WithPayload(InverterData.QPIGS.freq_out.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_pv_in_voltage").WithPayload(InverterData.QPIGS.pv_input_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_pv_in_current").WithPayload(InverterData.QPIGS.pv_input_current.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_pv_in_watts").WithPayload(InverterData.QPIGS.pv_input_watts.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_pv_in_watthour").WithPayload(InverterData.QPIGS.pv_input_watthour.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_scc_voltage").WithPayload(InverterData.QPIGS.scc_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_pct").WithPayload(InverterData.QPIGS.load_percent.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_watt").WithPayload(InverterData.QPIGS.load_watt.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_watthour").WithPayload(InverterData.QPIGS.load_watthour.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_va").WithPayload(InverterData.QPIGS.load_va.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_bus_voltage").WithPayload(InverterData.QPIGS.voltage_bus.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_heatsink_temperature").WithPayload(InverterData.QPIGS.temp_heatsink.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_capacity").WithPayload(InverterData.QPIGS.batt_capacity.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_voltage").WithPayload(InverterData.QPIGS.voltage_batt.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_charge_current").WithPayload(InverterData.QPIGS.batt_charge_current.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_discharge_current").WithPayload(InverterData.QPIGS.batt_discharge_current.ToString())); try { await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_status_on").WithPayload(InverterData.QPIGS.device_status.Substring(3, 1))); // need to fix await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_scc_charge_on").WithPayload(InverterData.QPIGS.device_status.Substring(6, 1))); // need to fix await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_charge_on").WithPayload(InverterData.QPIGS.device_status.Substring(7, 1))); // need to fix } catch { await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_load_status_on").WithPayload("0")); // need to fix await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_scc_charge_on").WithPayload("0")); // need to fix await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_ac_charge_on").WithPayload("0")); // need to fix } await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_recharge_voltage").WithPayload(InverterData.QPIRI.batt_recharge_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_under_voltage").WithPayload(InverterData.QPIRI.batt_under_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_bulk_voltage").WithPayload(InverterData.QPIRI.batt_bulk_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_float_voltage").WithPayload(InverterData.QPIRI.batt_float_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_max_grid_charge_current").WithPayload(InverterData.QPIRI.max_grid_charge_current.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_max_charge_current").WithPayload(InverterData.QPIRI.max_charge_current.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_out_source_priority").WithPayload(InverterData.QPIRI.out_source_priority.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_charger_source_priority").WithPayload(InverterData.QPIRI.charger_source_priority.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_battery_redischarge_voltage").WithPayload(InverterData.QPIRI.batt_redischarge_voltage.ToString())); await managedClient.PublishAsync(builder => builder.WithTopic("homeassistant/sensor/" + Settings.MQTT.MQTTClientId + "_warnings").WithPayload("NA"));//need to fix } else { Task.Run(() => ManagedClientTest.RunAsync("config")); } } catch (Exception e) { Logging.DebugWrite("Error", e.ToString()); } }