示例#1
0
        public static async Task ProcessQueueMessage([ServiceBusTrigger("dsbforsinketqueue")] BrokeredMessage message, TextWriter log)
        {
            log.WriteLine($"Got message: {message}");
            var isDebugMode = Convert.ToBoolean(ConfigurationManager.AppSettings["APP_DEBUG_MODE"]);
            log.WriteLine($"DEBUG MODE: {isDebugMode}");

            string stationId = (string)message.Properties["station"];
            string tag = (string)message.Properties["tag"];
            bool isTestSend = (bool)message.Properties["istest"];
            var service = new DSBLabsStationService(new Uri(BaseUrl));

            log.WriteLine($"stationId: {stationId} tag: {tag} isTestSend: {isTestSend}");
            log.WriteLine("Preparing the query.");

            var delayedDeparturesQuery =
                from departure in service.Queue
                where (departure.StationUic == stationId) &&
                      (departure.Cancelled == true || departure.DepartureDelay > 0 || isDebugMode)
                select departure;

            log.WriteLine("Executing the query.");
            var delayedDepartures = delayedDeparturesQuery.ToList();
            log.WriteLine("Query executed.");
            log.WriteLine($"Delayed departures: {delayedDepartures.Count}.");

            if (delayedDepartures.Any() || isTestSend || isDebugMode)
            {
                log.WriteLine($"Sending push message to tag: {tag}.");

                delayedDepartures = delayedDepartures.OrderBy(d => d.ScheduledDeparture).ToList();

                Dictionary<string, string> messageData = new Dictionary<string, string>
                {
                    ["delayedCount"] = delayedDepartures.Count.ToString(CultureInfo.InvariantCulture)
                };

                foreach (var departure in delayedDepartures.Take(5).Select((data, index) => new { index, data }))
                {
                    var destinationName = string.IsNullOrWhiteSpace(departure.data.Line)
                                                ?  departure.data.DestinationName
                                                : $"{departure.data.Line} <i>{departure.data.DestinationName}</i>";
                    messageData[$"departureName{departure.index}"] = destinationName;
                    messageData[$"departureTime{departure.index}"] = departure.data.ScheduledDeparture.HasValue 
                                                                        ? departure.data.ScheduledDeparture.Value.ToString("HH:mm", CultureInfo.InvariantCulture)
                                                                        : string.Empty;
                    long delayInMinutes = (departure.data.DepartureDelay / 60) ?? 0;
                    messageData[$"departureDelay{departure.index}"] = Convert.ToString(delayInMinutes);
                }

                var notificationSender = new PushNotificationSender(log.WriteLine);

                var notificationOutcome = await notificationSender.SendAsync(messageData, tag);

                log.WriteLine($"State: {notificationOutcome.State}");
                log.WriteLine($"Success: {notificationOutcome.Success}");
                log.WriteLine($"Failure: {notificationOutcome.Failure}");
                log.WriteLine($"NotificationId: {notificationOutcome.NotificationId}");
                log.WriteLine($"TrackingId: {notificationOutcome.TrackingId}");
            }
        }
示例#2
0
        public static void Main(string[] args)
        {
            string pathToOut = @"..\..\..\..\";
            string timesFileName = "times.txt";
            string timesValuesFileName = "times-values.txt";
            string stationsIdfileName = "stations-id.txt";
            string stationsNamesFileName = "stations-name.txt";

            int hoursFrom = 6;
            int hoursTo = 13;

            var hours = Enumerable.Range(hoursFrom, hoursTo - hoursFrom);
            var minutes = Enumerable.Range(0, 4).Select(i => i * 15);

            var times = from h in hours
                        from m in minutes
                        let h2 = m + 15 < 60 ? h : h + 1
                        let m2 = (m + 15) % 60
                        select new { fromMin = m, fromHour = h, toMin = m2, toHour = h2 };

            File.WriteAllLines(
                Path.Combine(pathToOut, timesFileName),
                times.Select(t => $"<item>{t.fromHour:D2}:{t.fromMin:D2} - {t.toHour:D2}:{t.toMin:D2}</item>"));

            File.WriteAllLines(
                Path.Combine(pathToOut, timesValuesFileName),
                times.Select(t => $"<item>{t.fromHour:D2}:{t.fromMin:D2}</item>"));

            var service = new DSBLabsStationService(new Uri("http://traindata.dsb.dk/stationdeparture/opendataprotocol.svc"));

            var stationsList = service.Station.ToList();

            var stationsOrdered =
                stationsList
                    .OrderBy(s => s.Name, StringComparer.Create(new CultureInfo("da-DK"), true))
                    .ToList();

            File.WriteAllLines(Path.Combine(pathToOut, stationsIdfileName), stationsOrdered.Select(st => $"<item>{st.UIC}</item>"));
            File.WriteAllLines(Path.Combine(pathToOut, stationsNamesFileName), stationsOrdered.Select(st => $"<item>{st.Name}</item>"));
        }