Пример #1
0
 public GeoFenceObserver(Guid vehicleId, Location location, double radiusInKm)
     : this()
 {
     SubjectId = vehicleId;
     Location = location;
     Radius = radiusInKm;
 }
        private async void observeVehicleButton_Click(object sender, EventArgs e)
        {
            string vehicleGuidString = vehicleIdTextBox.Text;
            Guid vehicleId;
            if (Guid.TryParse(vehicleGuidString, out vehicleId) && !_isDefaultTextObserverName)
            {
                bool isUniqueObserver = true;
                foreach (var keyValue in _observerDictionary)
                {
                    var observer = keyValue.Value;
                    if (String.CompareOrdinal(observer.Name, observerNameTextBox.Text) == 0)
                    {
                        isUniqueObserver = false;
                    }
                }

                if (!isUniqueObserver)
                {
                    MessageBox.Show(this, @"Please choose a unique observer name", @"Error: Observer Creation",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                try
                {
                    var code = new StringBuilder();
                    var console = new StringBuilder();
                    code.AppendLine("// GENERIC OBSERVER");
                    code.AppendLine("//var newObserverResponse = await Client.CreateAsync(new Observer(ObserverType.Generic)");
                    code.AppendLine("//                             {");
                    code.AppendLine("//                                 Name = observerNameTextBox.Text,");
                    code.AppendLine("//                                 AppId = ApplicationId,");
                    code.AppendLine("//                                 Subject = \"Vehicle\",");
                    code.AppendLine("//                                 SubjectId = vehicleId,");
                    code.AppendLine("//                                 Transports = Transport.SignalR");
                    code.AppendLine("//                             });");
                    code.AppendLine();
                    code.AppendLine("// EVENT OBSERVER - IgnitionOn & Ignition Off");
                    code.AppendLine("//var newObserverResponse = await Client.CreateAsync(new EventObserver(vehicleId, new EventType[] { EventType.IgnitionOn, EventType.IgnitionOff }, ObserverTiming.leading)");
                    code.AppendLine("//                             {");
                    code.AppendLine("//                                 Name = observerNameTextBox.Text,");
                    code.AppendLine("//                                 AppId = ApplicationId,");
                    code.AppendLine("//                                 Transports = Transport.SignalR");
                    code.AppendLine("//                             });");
                    code.AppendLine();
                    code.AppendLine("// GEO FENCE OBSERVER");
                    code.AppendLine("var location = new Location()");
                    code.AppendLine("{");
                    code.AppendLine("   Lat = 49.25784,");
                    code.AppendLine("   Lng = -123.17184");
                    code.AppendLine("};");
                    code.AppendLine("var newObserverResponse = await Client.CreateAsync(new GeoFenceObserver(vehicleId, location, 1)");
                    code.AppendLine("{");
                    code.AppendLine("   Name = observerNameTextBox.Text,");
                    code.AppendLine("   AppId = ApplicationId,");
                    code.AppendLine("   Subject = \"Vehicle\",");
                    code.AppendLine("   Transports = Transport.SignalR");
                    code.AppendLine("});");
                    code.AppendLine("var observer = newObserverResponse.Data;");
                    code.AppendLine("var status = newObserverResponse.StatusCode;");
                    code.AppendLine("var errorMessage = newObserverResponse.ErrorMessage;");
                    code.AppendLine("if (status == HttpStatusCode.OK && observer.Id != Guid.Empty)");
                    code.AppendLine("{");
                    code.AppendLine("   _observerDictionary.Add(observer.Id, observer);");
                    code.AppendLine("   await Client.Observe(observer.Id);");
                    code.AppendLine("   Client.ObserveHandler += delegate(GuidEntity entity)");
                    code.AppendLine("   {");
                    code.AppendLine("       // HANDLE CALL BACK CODE");
                    code.AppendLine("   };");
                    code.AppendLine("}");

                    // GEO FENCE OBSERVER
                    var location = new Location()
                    {
                        Lat = 49.25784,
                        Lng = -123.17184
                    };
                    var newObserverResponse = await Client.CreateAsync(new GeoFenceObserver(vehicleId, location, 1)
                    {
                        Name = observerNameTextBox.Text,
                        AppId = ApplicationId,
                        Subject = "Vehicle",
                        Transports = Transport.SignalR
                    });

                    var observer = newObserverResponse.Data;
                    var status = newObserverResponse.StatusCode;
                    var errorMessage = newObserverResponse.ErrorMessage;

                    if (status == HttpStatusCode.OK && observer.Id != Guid.Empty)
                    {
                        _observerDictionary.Add(observer.Id, observer);
                        await Client.Observe(observer.Id);

                        _codeWindow.DisplayNewCode(code.ToString());
                        _consoleWindow.DisplayModifiedObservers(@"The following observer has been created!",observer);

                        Client.ObserveHandler += delegate(GuidEntity entity)
                        {
                            if (entity.GetType() == typeof(Vehicle))
                            {
                                this.Invoke(new Action(() =>
                                {
                                    MessageBox.Show(this, entity.IdToString, @"Observer Call Back",
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }));
                            }
                            else if (entity.GetType() == typeof (Event))
                            {
                                this.Invoke(new Action(() =>
                                {
                                    MessageBox.Show(this, entity.IdToString, @"Observer Call Back",
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }));
                            }
                            else
                            {
                                this.Invoke(new Action(() =>
                                {
                                    MessageBox.Show(this, @"Observer Call Back Occured", @"Observer Call Back");
                                }));
                            }
                        };
                    }
                    else
                    {
                        console.AppendLine("Failed to create a new observer!");
                        console.AppendLine(errorMessage);
                        _consoleWindow.DisplayErrorMessage(console.ToString());
                    }
                }
                catch (Exception ex)
                {
                    _consoleWindow.DisplayErrorMessage("Failed to setup an observer for User");
                    MessageBox.Show(this, @"An exception was thrown by Mojio Client while setting up an Observer\nException Message: " + ex.Message, @"Exception Thrown");
                }
            }
            else
            {
                MessageBox.Show(this, @"Invalid Vehicle Id or Observer Name", @"Error: Observer Creation",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }