Ejemplo n.º 1
0
        public void loadTracker(WebBrowser webBrowser, TrackerItem trackerItem, TrackerData trackerData, string displayMember) {
            if (webBrowser != null) {

                htmlDocument = (mshtml.IHTMLDocument3)webBrowser.Document;

                mshtml.IHTMLElementCollection collection = htmlDocument.getElementsByName("formTracker");


                foreach (mshtml.HTMLInputElement element in collection.item(name: "formTracker")) {

                    if (element.id == "trackerId") {
                        element.value = trackerData.Tracker.Id.ToString();
                    }
                    if (element.id == "trackerImei") {
                        element.value = trackerData.Tracker.TrackerImei;
                    }
                    if (element.id == "trackerLabel") {
                        if (displayMember == "VehicleRegistration") {
                            element.value = trackerData.Tracker.VehicleRegistration;
                        } else if (displayMember == "DriverName") {
                            element.value = trackerData.Tracker.DriverName;
                        } else if (displayMember == "VehicleModel") {
                            element.value = trackerData.Tracker.VehicleModel;
                        } else if (displayMember == "OwnerName") {
                            element.value = trackerData.Tracker.OwnerName;
                        } else if (displayMember == "TrackerImei") {
                            element.value = trackerData.Tracker.TrackerImei;
                        } else if (displayMember == "SimNumber") {
                            element.value = trackerData.Tracker.SimNumber;
                        }
                    }
                    if (trackerData.IsDataEmpty == false) {
                        if (element.id == "trackerLatitude") {
                            element.value = trackerData.Coordinate.latitude.ToString();
                        }
                        if (element.id == "trackerLongitude") {
                            element.value = trackerData.Coordinate.longitude.ToString();
                        }
                        if (element.id == "trackerDegrees") {
                            element.value = trackerData.Degrees.ToString();
                        }
                        if (element.id == "trackerIcon") {
                            element.value = trackerData.Tracker.ImageNumber.ToString();
                        }
                        if (element.id == "trackerIconStatus") {
                            if (trackerData.Speed > 0 && trackerData.ACC == true) {
                                element.value = "driver";
                            } else if (trackerData.Speed == 0 && trackerData.ACC == false) {
                                element.value = "stop";
                            } else if (trackerData.Speed == 0 && trackerData.ACC == true) {
                                element.value = "idle";
                            } else if (trackerData.SOS == true) {
                                element.value = "alarm";
                            } else {
                                element.value = "lost";
                            }
                        }
                        if (element.id == "trackerIconAlert") {
                            if (trackerData.Tracker.DateTimeExpired.CompareTo(DateTime.Now) <= 0 /*|| trackerItem.Tracker.VehicleRegistrationExpiry.CompareTo(DateTime.Now) <= 0*/) {
                                element.value = "alarmExpiry";
                            } else if (trackerData.DateTime.Subtract(DateTime.Now).Hours > 1) {
                                element.value = "alarmLostTracker";
                            } else if (trackerData.Speed > 0 && trackerData.ACC == false) {
                                element.value = "alarmBreakDown";
                            } else if (trackerData.OverSpeed == true) {
                                element.value = "alarmOverSpeed";
                            } else if (trackerData.GpsSatellites < 3) {
                                element.value = "alarmLostSignalGps";
                            } else if (trackerData.GsmSignal < 5) {
                                element.value = "alarmLostSignalGsm";
                                //} else if (trackerData.DateTime.Subtract(DateTime.Now) > (new TimeSpan(01, 00, 00))) {
                                //    element.value = "alarmLostTracker";
                            } else if ((trackerData.Mileage - trackerData.Tracker.MileageInitial) > trackerData.Tracker.MileageLimit) {
                                element.value = "alarmMileageLimit";
                            } else if (trackerData.Geofence != null) {
                                element.value = "alarmGeofence";
                            } else {
                                element.value = "alarmOk!";
                            }
                        }
                    }

                    if (element.id == "trackerIsEnabled") {
                        element.value = trackerItem.IsChecked ? "1" : "0";
                    }
                    if (element.id == "trackerSubmit") {
                        mshtml.HTMLInputElement buttonSubmit = element;
                        
                        buttonSubmit.click();
                    }
                }

            }
        }
Ejemplo n.º 2
0
        private void asyncTrackerUpdate (object state) {
            Task.Run(() => {
                try {
                    lock (threadProperties) {
                        threadProperties.CurrentThreadCount++;
                    }
                    TrackerItem trackerItem = (TrackerItem)state;
                    Query query = new Query(database);
                    TrackerData trackerData;

                    if (trackerItem.IsChecked) {
                        //trackerData = query.getTrackerLatestData(company, trackerItem.getTracker());
                        trackerData = query.getTrackerLatestData(company, trackerItem.getTracker(), server);
                    } else {
                        trackerData = new TrackerData();
                        trackerData.Tracker = trackerItem.getTracker();
                        trackerData.IsDataEmpty = true;

                        Dispatcher.Invoke(new Action(() => {
                            for (int index = 0; listViewTrackersData.Items.Count > index; index++) {
                                TrackerData trackerDataItem = (TrackerData)listViewTrackersData.Items[index];
                                if (trackerDataItem.Tracker.Id == trackerData.Tracker.Id) {
                                    listViewTrackersData.Items.RemoveAt(index);
                                }
                            }
                        }));
                    }

                    Dispatcher.Invoke(new Action(() => {
                        if (!webBrowserMap.IsLoaded) {
                            lock (threadProperties) {
                                threadProperties.CurrentThreadCount--;
                            }
                            return;
                        }

                        //lock (map) {
                        try {
                            map.loadTracker(webBrowserMap, trackerItem, trackerData, (string)ribbonGalleryComboBoxDisplayMember.SelectedValue);
                        } catch (Exception exception) {
                            TextLog.Write(exception);
                        }
                        //}

                        if (trackerItem.IsChecked) {
                            bool isExisting = false;

                            for (int index = 0; listViewTrackersData.Items.Count > index; index++) {
                                TrackerData trackerDataItem = (TrackerData)listViewTrackersData.Items[index];
                                if (trackerDataItem.Tracker.Id == trackerData.Tracker.Id) {
                                    listViewTrackersData.Items.RemoveAt(index);
                                    listViewTrackersData.Items.Insert(index, trackerData);
                                    isExisting = true;
                                    break;
                                }
                            }

                            if (isExisting == false) {
                                listViewTrackersData.Items.Add(trackerData);
                            }
                        }
                    }));

                    lock (threadProperties) {
                        threadProperties.CurrentThreadCount--;
                    }
                } catch (Exception exception) {
                    Debug.Print(exception.Message);
                }
            });
        }
Ejemplo n.º 3
0
        //public DataTable getAllCompanies() {
        //    DataTable dataTable = new DataTable();
        //    try {
        //        mysqlConnection.Open();

        //        string sql =
        //        "SELECT * " +
        //        "FROM dbt_tracking_master.cmps;";

        //        MySqlCommand mySqlCommand = new MySqlCommand(sql, mysqlConnection);

        //        MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();

        //        if (!mySqlDataReader.HasRows) {
        //            throw new QueryException(1, "Companies's Collection is empty.");
        //        } else {
        //            dataTable.Load(mySqlDataReader);
        //        }

        //    } catch (QueryException queryException) {
        //        throw queryException;
        //    } catch (MySqlException mySqlException) {
        //        throw new QueryException(1, mySqlException.Message);
        //    } catch (Exception exception) {
        //        throw new QueryException(1, exception.Message);
        //    } finally {
        //        mysqlConnection.Close();
        //    }
        //    return dataTable;
        //}

        //public TrackerData getTrackerLatestData (Company company, Tracker tracker) {
        //    TrackerData trackerData = new TrackerData();
        //    trackerData.Tracker = tracker;
        //    try {
        //        mysqlConnection.Open();

        //        string sql =
        //             "SELECT * " +
        //             "FROM trk_" + tracker.DatabaseName + ".gps_" + tracker.DatabaseName + " " +
        //             "ORDER BY " +
        //             "trk_" + tracker.DatabaseName + ".gps_" + tracker.DatabaseName + ".gm_time " +
        //             "DESC limit 1;";
        //        //"SELECT * " +
        //        // "FROM trk_" + tracker.DatabaseName + ".gps_" + tracker.DatabaseName + " " +
        //        // "ORDER BY " +
        //        // "trk_" + tracker.DatabaseName + ".gps_" + tracker.DatabaseName + ".gm_id " +
        //        // "DESC limit 1;";

        //        MySqlCommand mySqlCommand = new MySqlCommand(sql, mysqlConnection);
        //        MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();

        //        if (!mySqlDataReader.HasRows) {
        //            trackerData.IsDataEmpty = true;
        //            return trackerData;
        //        } else {
        //            mySqlDataReader.Read();
        //            trackerData.IsDataEmpty = false;
        //            trackerData.Id = mySqlDataReader.GetInt32("gm_id");
        //            double dateTime = double.Parse(mySqlDataReader.GetString("gm_time"));
        //            trackerData.DateTime = Parser.UnixTime.toDateTime(dateTime);
        //            double latitude = double.Parse(mySqlDataReader.GetString("gm_lat"));
        //            double longitude = double.Parse(mySqlDataReader.GetString("gm_lng"));
        //            trackerData.Coordinate = new Coordinate(latitude, longitude);

        //            trackerData.Speed = int.Parse(mySqlDataReader.GetString("gm_speed"));
        //            trackerData.Degrees = int.Parse(mySqlDataReader.GetString("gm_ori"));
        //            trackerData.Direction = Direction.degreesToCardinalDetailed(double.Parse(mySqlDataReader.GetString("gm_ori")));
        //            //trackerData.Mileage = double.Parse(mySqlDataReader.GetString("gm_mileage"));

        //            double deviceMileage = double.Parse(mySqlDataReader.GetString("gm_mileage"));
        //            double carMileage = (tracker.MileageInitial);
        //            trackerData.Mileage = deviceMileage + carMileage;

        //            //1,			            //                                                          (0)
        //            //35,			            //Event code(Decimal)
        //            //11,			            //Number of satellites(Decimal)
        //            //26,			            //GSM signal status(Decimal)
        //            //17160691, 		        //Mileage(Decimal)unit: meter
        //            //0.7, 			            //hpos accuracy(Decimal)
        //            //18, 			            //Altitude(Decimal)unit: meter
        //            //18661240, 		        //Run time(Decimal)unit: second
        //            //427|2|0078|283F, 	        //Base station information(binary|binary|hex|hex)           (8)
        //            //==============================================0200
        //            //0,0,0,0,0,0,0,0,          //Io port lowbyte (low bit start from left)                 (9)
        //            //0,1,0,0,0,0,0,0,          //Io port lowbyte (low bit start from left)                 (17)
        //            //==============================================
        //            //000B,0000,0000,0A6E,0434, //Analog input value                                        (25)
        //            //00000001 		            //System mark

        //            string gmData = (string)mySqlDataReader["gm_data"];
        //            string[] data = gmData.Split(',');
        //            trackerData.EventCode = (EventCode)int.Parse(data[1]);

        //            trackerData.GpsSatellites = int.Parse(data[2]);
        //            trackerData.GsmSignal = int.Parse(data[3]);
        //            trackerData.Altitude = int.Parse(data[6]);

        //            trackerData.ACC = (int.Parse(data[18]) == 1) ? true : false;
        //            trackerData.SOS = (int.Parse(data[17]) == 1) ? true : false;
        //            trackerData.OverSpeed = ((int)trackerData.Speed > tracker.SpeedLimit) ? true : false;
        //            //Geofence
        //            Coordinate coordinate = new Coordinate(latitude, longitude);

        //            foreach (Geofence geofence in company.Geofences) {
        //                if (Geofence.isPointInPolygon(geofence, coordinate)) {
        //                    trackerData.Geofence = geofence;
        //                }
        //            };

        //            double batteryStrength = (double)int.Parse(data[28], System.Globalization.NumberStyles.AllowHexSpecifier);
        //            batteryStrength = ((batteryStrength - 2114f) * (100f / 492f));//*100.0;
        //            batteryStrength = Math.Round(batteryStrength, 2);
        //            if (batteryStrength > 100) {
        //                batteryStrength = 100f;
        //            } else if (batteryStrength < 0) {
        //                batteryStrength = 0;
        //            }

        //            double batteryVoltage = (double)int.Parse(data[28], System.Globalization.NumberStyles.AllowHexSpecifier);
        //            batteryVoltage = (batteryVoltage * 3 * 2) / 1024;
        //            batteryVoltage = Math.Round(batteryVoltage, 2);
        //            double externalVoltage = (double)int.Parse(data[29], System.Globalization.NumberStyles.AllowHexSpecifier);
        //            externalVoltage = (externalVoltage * 3 * 16) / 1024;
        //            externalVoltage = Math.Round(externalVoltage, 2);

        //            trackerData.Battery = batteryStrength;
        //            trackerData.BatteryVoltage = batteryVoltage;
        //            trackerData.ExternalVoltage = externalVoltage;

        //            return trackerData;
        //        }

        //        //} catch (QueryException queryException) {
        //        //throw queryException;
        //        //} catch (MySqlException mySqlException) {
        //        //throw new QueryException(1, mySqlException.Message);
        //    } catch (Exception exception) {
        //        Debug.Write(exception);
        //        //throw new QueryException(1, exception.Message);
        //        return trackerData;
        //    } finally {
        //        mysqlConnection.Close();
        //    }

        //}

        public TrackerData getTrackerLatestData (Company company, Tracker tracker, Server server) {
            TrackerData trackerData = new TrackerData();
            trackerData.Tracker = tracker;

            try {
                WebRequest request = WebRequest.Create("http://" + server.Ip + "/connect/get_realtime.php?url=http://" + server.Ip + ":" + server.PortHttp + "/?id=" + tracker.TrackerImei);

                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                //Console.WriteLine(responseFromServer);
                // Clean up the streams and the response.
                reader.Close();
                response.Close();
                //===========================================

                //===========================================
                responseFromServer = responseFromServer.Trim();
                if (responseFromServer == "1") {
                    trackerData.IsDataEmpty = true;
                    return trackerData;
                } else {
                    dynamic jsonData = (dynamic)JsonConvert.DeserializeObject<dynamic>(responseFromServer);

                    trackerData.IsDataEmpty = false;
                    trackerData.Tracker.TrackerImei = (string)jsonData.gm_unit;

                    double dateTime = double.Parse((string)jsonData.gm_time);

                    trackerData.DateTime = Parser.UnixTime.toDateTime(dateTime);
                    //DateTime dateTime1 = Parser.UnixTime.toDateTime(dateTime);
                    //TimeSpan timeSpan = dateTime1.Subtract(DateTime.Now);

                    //if (timeSpan.Hours > 6) {
                    //    trackerData.DateTime = dateTime1;
                    //} else {
                    //    trackerData.DateTime = DateTime.Now;
                    //}


                    double latitude = 0;
                    double.TryParse((string)jsonData.gm_lat, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out latitude);
                    double longitude = 0;
                    double.TryParse((string)jsonData.gm_lng, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out longitude);

                    trackerData.Coordinate = new Coordinate(latitude, longitude);

                    trackerData.Speed = int.Parse((string)jsonData.gm_speed);
                    trackerData.Degrees = int.Parse((string)jsonData.gm_ori);
                    trackerData.Direction = Direction.degreesToCardinalDetailed(double.Parse((string)jsonData.gm_ori));
                    //trackerData.Mileage = double.Parse((string)jsonData.gm_mileage);

                    double deviceMileage = 0;
                    double.TryParse((string)jsonData.gm_mileage, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out deviceMileage);

                    double carMileage = (tracker.MileageInitial);
                    trackerData.Mileage = deviceMileage + carMileage;

                    //1,			            //(0)
                    //35,			            //Event code(Decimal)
                    //11,			            //Number of satellites(Decimal)
                    //26,			            //GSM signal status(Decimal)
                    //17160691, 		        //Mileage(Decimal)unit: meter
                    //0.7, 			            //hpos accuracy(Decimal)
                    //18, 			            //Altitude(Decimal)unit: meter
                    //18661240, 		        //Run time(Decimal)unit: second
                    //427|2|0078|283F, 	        //Base station information(binary|binary|hex|hex)           (8)
                    //==============================================0200
                    //0,0,0,0,0,0,0,0,          //Io port lowbyte (low bit start from left)                 (9)
                    //0,1,0,0,0,0,0,0,          //Io port lowbyte (low bit start from left)                 (17)
                    //==============================================
                    //000B,0000,0000,0A6E,0434, //Analog input value                                        (25)
                    //00000001 		            //System mark

                    string gmData = (string)jsonData.gm_data;
                    string[] data = gmData.Split(',');
                    trackerData.EventCode = (TqatProModel.Devices.Meitrack.EventCode)int.Parse(data[1]);

                    trackerData.GpsSatellites = int.Parse(data[2]);
                    trackerData.GsmSignal = int.Parse(data[3]);
                    trackerData.Altitude = int.Parse(data[6]);

                    trackerData.ACC = (int.Parse(data[18]) == 1) ? true : false;
                    trackerData.SOS = (int.Parse(data[17]) == 1) ? true : false;
                    trackerData.OverSpeed = ((int)trackerData.Speed > tracker.SpeedLimit) ? true : false;
                    //Geofence
                    Coordinate coordinate = new Coordinate(latitude, longitude);

                    if (company.Geofences != null) {
                        foreach (Geofence geofence in company.Geofences) {
                            if (Geofence.isPointInPolygon(geofence, coordinate)) {
                                trackerData.Geofence = geofence;
                            }
                        };
                    }


                    double batteryVoltage = (double)int.Parse(data[28], System.Globalization.NumberStyles.AllowHexSpecifier);
                    batteryVoltage = (batteryVoltage * 3.3 * 2) / 4096;

                    double externalVoltage = (double)int.Parse(data[29], System.Globalization.NumberStyles.AllowHexSpecifier);
                    externalVoltage = (externalVoltage * 3.3 * 16) / 4096;

                    double batteryStrength = batteryVoltage - 3.5;
                    batteryStrength = (batteryStrength / 0.8) * 100;

                    if (batteryStrength < 0) {
                        batteryStrength = 0;
                    } else if (batteryStrength > 100) {
                        batteryStrength = 100;
                    }

                    trackerData.EPC = (externalVoltage > 3) ? false : true;

                    trackerData.Battery = Math.Round(batteryStrength, 2);
                    trackerData.BatteryVoltage = Math.Round(batteryVoltage, 2);
                    trackerData.ExternalVoltage = Math.Round(externalVoltage, 2);

                    return trackerData;
                }

            } catch (QueryException queryException) {
                throw queryException;
            } catch (MySqlException mySqlException) {
                throw new QueryException(1, mySqlException.Message);
            } catch (Exception exception) {
                Debug.Write(exception);
                mysqlConnection.Close();
                throw new QueryException(1, exception.Message);
            } finally {
                mysqlConnection.Close();
            }

        }