Ejemplo n.º 1
0
        private static void _udpListener_PacketReceived(object sender, PacketReceivedArgs e)
        {
            if (e.Packet.Length > 0)
            {
                var s = Encoding.ASCII.GetString(e.Packet);
                Debug.WriteLine(s);
                WeatherPacket wp = null;
                try
                {
                    wp = JsonSerializer.DeserializeFromString <WeatherPacket>(s);
                    if (wp != null)
                    {
                        wp.FullString = s;
                        wp.SetMessageType();
                        if (wp.MsgType != WeatherPacket.MessageType.Unknown)
                        {
                            WeatherPacketReceived?.Invoke(wp);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                if ((wp?.timestamp ?? 0) != 0)
                {
                    Debug.WriteLine(wp?.PacketTime.ToString());
                }
            }
        }
Ejemplo n.º 2
0
        public DeviceStatus(WeatherPacket packet)
        {
            SerialNumber = packet.serial_number;
            HubSN        = packet.hub_sn;
            Timestamp    = WeatherPacket.FromUnixTimeSeconds(packet.timestamp);
            Uptime       = packet.uptime;
            Voltage      = packet.voltage;

            try
            {
                if (!int.TryParse(packet.firmware_revision.ToString(), out var i))
                {
                    i = -1;
                }
                FirmwareRevision = i;
            }
            catch (Exception e)
            {
                var ex = e.Message;
            }

            RSSI         = packet.rssi;
            HubRSSI      = packet.hub_rssi;
            SensorStatus = packet.sensor_status;
            Debug        = packet.debug;
        }
Ejemplo n.º 3
0
        public LightningStrike(WeatherPacket packet)
        {
            SerialNumber = packet.serial_number;
            HubSN        = packet.hub_sn;

            if (packet.evt.Count == 3)
            {
                Timestamp = WeatherPacket.FromUnixTimeSeconds(packet.evt[0]);
                Distance  = Convert.ToInt32(packet.evt[1]);
                Energy    = Convert.ToInt32(packet.evt[2]);
            }
        }
Ejemplo n.º 4
0
 public RapidWind(WeatherPacket packet)
 {
     //string sn, string hubsn, List<decimal> ob
     SerialNumber = packet.serial_number;
     HubSN        = packet.hub_sn;
     if (packet.ob.Count == 3)
     {
         Timestamp     = WeatherPacket.FromUnixTimeSeconds((long)packet.ob[0]);
         WindSpeed     = packet.ob[1];
         WindDirection = (int)packet.ob[2];
     }
 }
Ejemplo n.º 5
0
        private static void LoadObservation(Observation o, decimal?[] ob)
        {
            o.Timestamp       = WeatherPacket.FromUnixTimeSeconds(WeatherPacket.Getlong(ob[0]));
            o.WindLull        = WeatherPacket.GetDecimal(ob[1]);
            o.WindAverage     = WeatherPacket.GetDecimal(ob[2]);
            o.WindGust        = WeatherPacket.GetDecimal(ob[3]);
            o.WindDirection   = WeatherPacket.GetInt(ob[4]);
            o.WindSampleInt   = WeatherPacket.GetInt(ob[5]);
            o.StationPressure = WeatherPacket.GetDecimal(ob[6]);
            o.Temperature     = WeatherPacket.GetDecimal(ob[7]);
            o.Humidity        = WeatherPacket.GetDecimal(ob[8]);
            o.Illuminance     = WeatherPacket.GetInt(ob[9]);
            o.UV             = WeatherPacket.GetDecimal(ob[10]);
            o.SolarRadiation = WeatherPacket.GetInt(ob[11]);
            o.Precipitation  = WeatherPacket.GetDecimal(ob[12]);

            switch (WeatherPacket.GetInt(ob[13]))
            {
            case 0:
                o.PrecipType = WeatherPacket.PrecipType.None;
                break;

            case 1:
                o.PrecipType = WeatherPacket.PrecipType.Rain;
                break;

            case 2:
                o.PrecipType = WeatherPacket.PrecipType.Hail;
                break;

            default:
                o.PrecipType = WeatherPacket.PrecipType.None;
                break;
            }
            o.LightningAvgDist = WeatherPacket.GetInt(ob[14]);
            o.LightningCount   = WeatherPacket.GetInt(ob[15]);
            o.BatteryVoltage   = WeatherPacket.GetDecimal(ob[16]);
            o.ReportInterval   = WeatherPacket.GetInt(ob[17]);

            if (ob.Length >= 21)
            {
                // these are only available for history data, not from the UDP message
                o.LocalDayRain     = WeatherPacket.GetInt(ob[18]);
                o.FinalRainChecked = WeatherPacket.GetInt(ob[19]);
                o.LocalRainChecked = WeatherPacket.GetInt(ob[20]);
            }
        }
Ejemplo n.º 6
0
        public Observation(WeatherPacket packet)
        {
            SerialNumber = packet.serial_number;
            HubSN        = packet.hub_sn;
            try
            {
                int i;
                if (!int.TryParse(packet.firmware_revision.ToString(), out i))
                {
                    i = -1;
                }
                FirmwareRevision = i;
            }
            catch {}

            if (packet.obs[0].Length >= 18)
            {
                //var ob = packet.obs[0];
                LoadObservation(this, packet.obs[0]);
            }
        }
Ejemplo n.º 7
0
        public HubStatus(WeatherPacket packet)
        {
            //string sn, string hubsn, List<decimal> ob
            SerialNumber = packet.serial_number;
            Timestamp    = WeatherPacket.FromUnixTimeSeconds(packet.timestamp);

            if (packet.radio_stats.Count == 5)
            {
                RadioVersion     = packet.radio_stats[0];
                RadioReboots     = packet.radio_stats[1];
                RadioBusErrors   = packet.radio_stats[2];
                FirmwareRevision = packet.firmware_revision.ToString();
                Uptime           = packet.uptime;
                Rssi             = packet.rssi;
                ResetFlags       = packet.reset_flags;
                Seq = packet.seq;


                switch (packet.radio_stats[3])
                {
                case 0:
                    RadioStatus = WeatherPacket.RadioStatus.RadioOff;
                    break;

                case 1:
                    RadioStatus = WeatherPacket.RadioStatus.RadioOn;
                    break;

                case 3:
                    RadioStatus = WeatherPacket.RadioStatus.RadioActive;
                    break;

                default:
                    RadioStatus = WeatherPacket.RadioStatus.RadioOff;
                    break;
                }
            }
        }
Ejemplo n.º 8
0
        public static List <Observation> GetRestPacket(string url, string token, int deviceId, DateTime start, DateTime end, Cumulus c)
        {
            List <Observation> ret = new List <Observation>();

            cumulus = c;

            using (var httpClient = new HttpClient())
            {
                var    tpStart = start;
                var    tpEnd   = end;
                double ts      = tpEnd.Subtract(tpStart).TotalDays;

                while (ts > 0)
                {
                    long st;
                    long end_time;
                    st       = WeatherPacket.ToUnixTimeSeconds(tpStart);
                    end_time = WeatherPacket.ToUnixTimeSeconds(end);
                    if (ts > 4)                    // load max 4 days at a time
                    {
                        tpStart  = tpStart.AddDays(4);
                        end_time = WeatherPacket.ToUnixTimeSeconds(tpStart) - 1;                      // subtract a second so we don't overlap
                        ts       = tpEnd.Subtract(tpStart).TotalDays;
                    }
                    else
                    {
                        ts = 0;
                    }


                    using (var response =
                               httpClient.GetAsync($"{url}device/{deviceId}?token={token}&time_start={st}&time_end={end_time}")
                           )
                    {
                        string apiResponse = response.Result.Content.ReadAsStringAsync().Result;
                        var    rp          = JsonSerializer.DeserializeFromString <RestPacket>(apiResponse);
                        if (rp != null && rp.status.status_message.Equals("SUCCESS") && rp.obs != null)
                        {
                            foreach (var ob in rp.obs)
                            {
                                ret.Add(new Observation(ob));
                            }
                        }
                        else if (rp != null && rp.status.status_message.Equals("SUCCESS"))
                        {
                            // no data for time period, ignore
                            //cumulus.LogConsoleMessage($"No data for time period from {tpStart} to {end}");
                        }
                        else
                        {
                            var msg = $"Error downloading tempest history: {apiResponse}";
                            cumulus.LogMessage(msg);
                            cumulus.LogConsoleMessage(msg);
                            if (rp.status.status_code == 404)
                            {
                                cumulus.LogConsoleMessage("Normally indicates incorrect Device ID");
                                ts = -1;                                // force a stop, fatal error
                            }

                            if (rp.status.status_code == 401)
                            {
                                cumulus.LogConsoleMessage("Normally indicates incorrect Token");
                                ts = -1;                                // force a stop, fatal error
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Ejemplo n.º 9
0
 public PrecipEvent(WeatherPacket packet)
 {
     SerialNumber = packet.serial_number;
     HubSN        = packet.hub_sn;
     Timestamp    = WeatherPacket.FromUnixTimeSeconds(packet.evt[0]);
 }