Ejemplo n.º 1
0
        public static string ToLineProtocolString(this MyStrom device, string measurement, long unixTimeSeconds)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append(measurement);
            stringBuilder.Append(",");
            stringBuilder.Append($"{nameof(device.Name).ToLowerInvariant()}={device.Name} ");
            stringBuilder.Append($"{nameof(device.Report.Power).ToLowerInvariant()}={device.Report.Power.ToString("0.000000", CultureInfo.InvariantCulture)},");
            stringBuilder.Append($"{nameof(device.Report.Relay).ToLowerInvariant()}={device.Report.Relay.ToString().ToLowerInvariant()} ");
            stringBuilder.Append(unixTimeSeconds);

            return(stringBuilder.ToString());
        }
Ejemplo n.º 2
0
        private static IEnumerable <MyStrom> ParseDevicesFromEnv(string envString)
        {
            var list       = new List <MyStrom>();
            var splitPairs = envString.Split(';');

            foreach (var splitPair in splitPairs)
            {
                var keyValue      = splitPair.Split('=');
                var myStromDevice = new MyStrom
                {
                    Name    = keyValue[0],
                    Address = keyValue[1]
                };
                list.Add(myStromDevice);
            }

            return(list);
        }
Ejemplo n.º 3
0
        public void SendMetric(MyStrom deviceReport, DateTime timeStamp)
        {
            var webClient = new WebClient
            {
                QueryString = new NameValueCollection
                {
                    { "db", _configuration.InfluxDbDatabase },
                    { "u", _configuration.InfluxDbUsername },
                    { "p", _configuration.InfluxDbPassword },
                    { "precision", "s" }
                }
            };

            var uri = new Uri(_configuration.InfluxDbAddress + "/write");

            Console.WriteLine($"[{DateTime.UtcNow}] sending metric to InfluxDB for {deviceReport.Name}");
            webClient.UploadStringAsync(uri, deviceReport.ToLineProtocolString(_configuration.InfluxDbMeasurement,
                                                                               new DateTimeOffset(timeStamp).ToUnixTimeSeconds()));
        }
Ejemplo n.º 4
0
        public static async Task <MyStrom> GetReport(MyStrom device)
        {
            Console.WriteLine($"[{DateTime.UtcNow}] GET report of {device.Name} on {device.Address}");
            var client   = new HttpClient();
            var response = await client.GetAsync(device.ReportUrl);

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            var deserializerSettings = new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            var result = JsonConvert.DeserializeObject <Report>(content, deserializerSettings);

            return(new MyStrom
            {
                Name = device.Name,
                Report = result
            });
        }