Exemplo n.º 1
0
        public async void CanInsertIntoInflux()
        {
            var config = new InfluxConfig(new Uri("http://influx:8086"), "mydb", "my_user", "my_pass");

            using (var writer = new InfluxWriter(config, "my-pc"))
                using (var client = new HttpClient())
                {
                    for (int attempts = 0; ; attempts++)
                    {
                        try
                        {
                            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                            var resp = await client.GetAsync(
                                "http://influx:8086/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");

                            Assert.True(resp.IsSuccessStatusCode);
                            var content = await resp.Content.ReadAsStringAsync();

                            Assert.Contains("/intelcpu/0/temperature/0", content);
                            break;
                        }
                        catch (Exception ex)
                        {
                            if (attempts >= 10)
                            {
                                throw;
                            }

                            Thread.Sleep(TimeSpan.FromSeconds(1));
                        }
                    }
                }
        }
Exemplo n.º 2
0
        public async void CanInsertIntoPasswordLessInfluxdb()
        {
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("influxdb:1.8-alpine")
                                        .WithEnvironment("INFLUXDB_DB", "mydb")
                                        .WithEnvironment("INFLUXDB_USER", "my_user")
                                        .WithEnvironment("INFLUXDB_HTTP_AUTH_ENABLED", "false")
                                        .WithPortBinding(8086, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));

            await using var container = testContainersBuilder.Build();
            await container.StartAsync();

            var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
            var config  = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", null);

            using var writer = new InfluxWriter(config, "my-pc");
            using var client = new HttpClient();
            for (int attempts = 0;; attempts++)
            {
                try
                {
                    await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                    var resp = await client.GetAsync(
                        $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");

                    Assert.True(resp.IsSuccessStatusCode);
                    var content = await resp.Content.ReadAsStringAsync();

                    Assert.Contains("/intelcpu/0/temperature/0", content);
                    break;
                }
                catch (Exception)
                {
                    if (attempts >= 10)
                    {
                        throw;
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Exemplo n.º 3
0
        public static async Task Main(string[] args)
        {
            Configuration            execonfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            TibberPulseConfigSection config    = execonfig.GetSection("tibberPulse") as TibberPulseConfigSection;

            if (string.IsNullOrEmpty(config.InfluxDatabase) || string.IsNullOrEmpty(config.InfluxHost) || string.IsNullOrEmpty(config.InfluxMeasurement))
            {
                Console.WriteLine("Invalid database parameters");
                return;
            }
            if (string.IsNullOrEmpty(config.AuthToken) || string.IsNullOrEmpty(config.HomeId))
            {
                Console.WriteLine("Invalid pulse parameters");
                return;
            }

            InfluxWriter influx = new InfluxWriter(config.InfluxHost, config.InfluxDatabase, config.InfluxMeasurement);

            var             cancelall   = new CancellationTokenSource();
            bool            softexiting = false;
            bool            receiving   = false;
            int             curid       = 0;
            ClientWebSocket ws          = null;

            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
                e.Cancel    = true;
                softexiting = true;
                cancelall.CancelAfter(10000);
                string message = JsonConvert.SerializeObject(new { type = "stop", id = curid.ToString() });
                Console.WriteLine(message);
                var buffer = Encoding.UTF8.GetBytes(message);
                if (receiving)
                {
                    try {
                        ws?.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, cancelall.Token);
                    }
                    catch (WebSocketException wse) {
                        Console.WriteLine($"WebSocketException when stopping {wse.Message}");
                    }
                }
            };

            CancellationTokenSource timeout;

            while (!softexiting)
            {
                using (timeout = CancellationTokenSource.CreateLinkedTokenSource(cancelall.Token))
                    using (ws = new ClientWebSocket())
                        try {
                            ws.Options.AddSubProtocol("graphql-ws");
                            ws.Options.SetRequestHeader("Authorization", config.AuthToken);
                            // Timeout for connecting and registering = 30s
                            timeout.CancelAfter(30000);
                            await ws.ConnectAsync(new Uri("wss://api.tibber.com/v1-beta/gql/subscriptions"), timeout.Token);

                            string    message;
                            byte[]    buffer;
                            const int MAXLEN   = 1024 * 16;
                            byte[]    inbuffer = new byte[MAXLEN];
                            WebSocketReceiveResult result;
                            string json;

                            message = JsonConvert.SerializeObject(new { type = "connection_init" });
                            Console.WriteLine(message);
                            buffer = Encoding.UTF8.GetBytes(message);
                            await ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, timeout.Token);

                            result = await ws.ReceiveAsync(new ArraySegment <byte>(inbuffer), timeout.Token);

                            if (result.Count == 0)
                            {
                                Console.WriteLine("Socket closed, {0}: '{1}'", result.CloseStatus, result.CloseStatusDescription);
                                throw new WebSocketException("Socket closed while expecting data");
                            }
                            json = Encoding.UTF8.GetString(inbuffer, 0, result.Count);
                            Console.WriteLine(json);


                            bool needsubscribe = true;
                            while (true)
                            {
                                if (needsubscribe)
                                {
                                    curid++;
                                    var subscriptionpayload = new
                                    {
                                        query = $"subscription {{ liveMeasurement(homeId: \"{config.HomeId}\") {{ timestamp power powerProduction accumulatedConsumption accumulatedProduction lastMeterConsumption lastMeterProduction powerFactor voltagePhase1 voltagePhase2 voltagePhase3 currentPhase1 currentPhase2 currentPhase3 }} }}"
                                    };
                                    message = JsonConvert.SerializeObject(new { type = "start", id = curid.ToString(), payload = subscriptionpayload });
                                    Console.WriteLine(message);
                                    buffer = Encoding.UTF8.GetBytes(message);
                                    await ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, timeout.Token);

                                    needsubscribe = false;
                                }

                                // Timeout for receiving next message = 15s
                                timeout.CancelAfter(15000);
                                try {
                                    result = await ws.ReceiveAsync(new ArraySegment <byte>(inbuffer), timeout.Token);
                                }
                                catch (OperationCanceledException) when(receiving && !cancelall.IsCancellationRequested)
                                {
                                    // Timeout after at least one message received - stop and restart
                                    receiving     = false;
                                    needsubscribe = true;
                                    timeout.Dispose();
                                    timeout = CancellationTokenSource.CreateLinkedTokenSource(cancelall.Token);
                                    timeout.CancelAfter(30000);
                                    message = JsonConvert.SerializeObject(new { type = "stop", id = curid.ToString() });
                                    Console.WriteLine(message);
                                    buffer = Encoding.UTF8.GetBytes(message);
                                    await ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, cancelall.Token);

                                    continue;
                                }
                                if (result.Count == 0)
                                {
                                    Console.WriteLine("Socket closed, {0}: '{1}'", result.CloseStatus, result.CloseStatusDescription);
                                    throw new WebSocketException("Socket closed while expecting data");
                                }
                                json = Encoding.UTF8.GetString(inbuffer, 0, result.Count);
                                var data = JsonConvert.DeserializeObject <PulseData <LiveMeasurementData> >(json);
                                if (data.type == "complete" && data.id == curid.ToString())
                                {
                                    receiving = false;
                                    Console.WriteLine(json);
                                    break;
                                }
                                var lm = data.payload.data.liveMeasurement;
                                receiving = true;
                                double?calcpower  = (lm.voltagePhase1 * lm.currentPhase1 + lm.voltagePhase2 * lm.currentPhase2 + lm.voltagePhase3 * lm.currentPhase3) * lm.powerFactor;
                                double?calcpower2 = (lm.currentPhase1 + lm.currentPhase2 + lm.currentPhase3) * lm.powerFactor * 230;
                                Console.Write($"{lm.timestamp}: {lm.power} {lm.powerProduction}            \r");

                                DateTime date = DateTime.Parse(lm.timestamp);
                                Dictionary <string, object> values = new Dictionary <string, object>();
                                values.Add("powerConsumption", lm.power.Value);
                                if (lm.powerProduction.HasValue)
                                {
                                    values.Add("powerProduction", lm.powerProduction.Value);
                                }
                                if (lm.accumulatedConsumption.HasValue)
                                {
                                    values.Add("accumulatedConsumption", lm.accumulatedConsumption.Value);
                                }
                                if (lm.accumulatedProduction.HasValue)
                                {
                                    values.Add("accumulatedProduction", lm.accumulatedProduction.Value);
                                }
                                if (lm.lastMeterConsumption.HasValue)
                                {
                                    values.Add("lastMeterConsumption", lm.lastMeterConsumption.Value);
                                }
                                if (lm.lastMeterProduction.HasValue)
                                {
                                    values.Add("lastMeterProduction", lm.lastMeterProduction.Value);
                                }
                                if (lm.powerFactor.HasValue)
                                {
                                    values.Add("powerFactor", lm.powerFactor.Value);
                                }
                                if (lm.voltagePhase1.HasValue)
                                {
                                    values.Add("voltagePhase1", lm.voltagePhase1.Value);
                                }
                                if (lm.voltagePhase2.HasValue)
                                {
                                    values.Add("voltagePhase2", lm.voltagePhase2.Value);
                                }
                                if (lm.voltagePhase3.HasValue)
                                {
                                    values.Add("voltagePhase3", lm.voltagePhase3.Value);
                                }
                                if (lm.currentPhase1.HasValue)
                                {
                                    values.Add("currentPhase1", lm.currentPhase1.Value);
                                }
                                if (lm.currentPhase2.HasValue)
                                {
                                    values.Add("currentPhase2", lm.currentPhase2.Value);
                                }
                                if (lm.currentPhase3.HasValue)
                                {
                                    values.Add("currentPhase3", lm.currentPhase3.Value);
                                }
                                influx.Write(date, values);
                            }
                            message = JsonConvert.SerializeObject(new { type = "connection_terminate" });
                            Console.WriteLine(message);
                            buffer = Encoding.UTF8.GetBytes(message);
                            await ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, timeout.Token);

                            await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "Done", timeout.Token);
                        }
                        catch (WebSocketException wse) {
                            Console.WriteLine($"WebsocketException {wse.Message}");
                        }
            }