Files @ 65c134a3d619
Branch filter:

Location: seniordesign-ui/Demo.WindowsMobile/Forms/MainForm.cs

mkanning@CL-ENS241-10.cedarville.edu
Initial import of mapping source (huge commit)
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.GPS;
using GMap.NET.Internals;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using Microsoft.Win32;
using Microsoft.WindowsCE.Forms;
using GMap.NET.MapProviders;

namespace Demo.WindowsMobile
{
   public partial class MainForm : Form
   {
      PointLatLng start = new PointLatLng(54.6961334816182, 25.2985095977783);
      PointLatLng destinationPoint = new PointLatLng(54.6961334816182, 25.2985095977783);

      // marker
      GMarkerCross gpsPos;

      // layers
      GMapOverlay top;
      internal GMapOverlay objects;

      #region -- variables --
      string LogDb;
      SQLiteConnection cn;
      DbCommand cmd;
      DateTime LastFlush = DateTime.Now;
      TimeSpan FlushDelay = TimeSpan.FromSeconds(60);

      readonly Gps gps = new Gps();
      GpsDeviceState device = null;

      int count = 0;
      int countReal = 0;
      double Total = 0;

      EventHandler updateDataHandler;
      TimeSpan delay = TimeSpan.FromSeconds(1);
      DateTime? TimeUTC;
      double? Lat = 0;
      double? Lng = 0;
      double Delta = 0;
      internal readonly List<Satellite> Satellites = new List<Satellite>();

      IntPtr gpsPowerHandle = IntPtr.Zero;

      GPS pageGps;
      Transport pageTransport;
      Search pageSearch;

      readonly HookKeys hook = new HookKeys();
      #endregion

      public MainForm()
      {
         InitializeComponent();

         pageGps = new GPS(this);
         pageTransport = new Transport(this);
         pageSearch = new Search(this);

#if DEBUG
         MainMap.Manager.Mode = AccessMode.ServerAndCache;
         menuItemServerAndCache.Checked = true;
         menuItemEnableGrid.Checked = true;
         menuItemGPSenabled.Checked = false;
         MainMap.ShowTileGridLines = true;
#else
         menuItemGPSenabled.Checked = false;
         MainMap.Manager.Mode = AccessMode.CacheOnly;
         menuItemCacheOnly.Checked = true;
#endif
         MainMap.MapProvider = GMapProviders.LithuaniaMap;
         MainMap.MaxZoom = 11;
         MainMap.MinZoom = 1;
         MainMap.Zoom = MainMap.MinZoom + 1;
         MainMap.Position = start;

         MainMap.OnMapTypeChanged += new MapTypeChanged(MainMap_OnMapTypeChanged);
         MainMap.OnMapZoomChanged += new MapZoomChanged(MainMap_OnMapZoomChanged);
         MainMap.OnPositionChanged += new PositionChanged(MainMap_OnPositionChanged);

         // add custom layers  
         {
            objects = new GMapOverlay("objects");
            MainMap.Overlays.Add(objects);

            top = new GMapOverlay("top");
            MainMap.Overlays.Add(top);
         }

         // gps pos
         gpsPos = new GMarkerCross(MainMap.Position);
         gpsPos.IsVisible = false;
         top.Markers.Add(gpsPos);

#if DEBUG
         // transparent marker test
         GMapMarkerTransparentGoogleGreen goo = new GMapMarkerTransparentGoogleGreen(MainMap.Position);
         goo.ToolTipMode = MarkerTooltipMode.Always;
         goo.ToolTipText = "Welcome to Lithuania! ;}";
         objects.Markers.Add(goo);
#endif

         // hook for volume up/down zooming
         hook.HookEvent += new HookKeys.HookEventHandler(hook_HookEvent);

         // test performance
         if(PerfTestEnabled)
         {
            timer.Interval = 111;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
         }
      }

      void MainMap_OnPositionChanged(PointLatLng point)
      {
         if(menuItemshowDestination.Checked && !menuItemGPSenabled.Checked)
         {
            UpdateDestinationRoute(true, menuItemSnapToGps.Checked ? MainMap.Position : gpsPos.Position); 
         }
      }

      readonly IntPtr volumeUp = new IntPtr(257);

      bool hook_HookEvent(HookEventArgs e, KeyBoardInfo keyBoardInfo)
      {
         if(keyBoardInfo.vkCode == 117)
         {
            if(e.wParam == volumeUp)
            {
               MainMap.Zoom = (int) (MainMap.Zoom) + 1;
            }
         }
         else if(keyBoardInfo.vkCode == 118)
         {
            if(e.wParam == volumeUp)
            {
               MainMap.Zoom = (int) (MainMap.Zoom) - 1;
            }
         }
         return true;
      }

      #region -- performance test--

      bool PerfTestEnabled = false;

      double NextDouble(Random rng, double min, double max)
      {
         return min + (rng.NextDouble() * (max - min));
      }

      Random r = new Random();

      int tt = 0;
      void timer_Tick(object sender, EventArgs e)
      {
         var pos = new PointLatLng(NextDouble(r, MainMap.ViewArea.Top, MainMap.ViewArea.Bottom), NextDouble(r, MainMap.ViewArea.Left, MainMap.ViewArea.Right));
         GMapMarker m = new GMapMarkerGoogleGreen(pos);
         {
            m.ToolTipText = (tt++).ToString();
            m.ToolTipMode = MarkerTooltipMode.Always;
            m.Offset = new System.Drawing.Point(-m.Size.Width, -m.Size.Height);
         }

         objects.Markers.Add(m);

         if(tt >= 44)
         {
            timer.Enabled = false;
            tt = 0;
         }
      }

      Timer timer = new Timer();
      #endregion

      public void ZoomToFitMarkers()
      {
         if(objects.Markers.Count > 0)
         {
            RectLatLng? m = MainMap.GetRectOfAllMarkers(null);
            if(m.HasValue)
            {
               MainMap.SetZoomToFitRect(m.Value);
            }
         }
      }

      void MainMap_OnMapZoomChanged()
      {
         this.Text = "GMap.NET: " + (int) MainMap.Zoom;
      }

      void MainMap_OnMapTypeChanged(GMapProvider type)
      {
         //if(routes.Routes.Count > 0)
         //{
         //   MainMap.ZoomAndCenterRoutes(null);
         //}
      }

      private void menuItem3_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      // zoom in
      private void menuItem4_Click(object sender, EventArgs e)
      {
         MainMap.Zoom = (int) (MainMap.Zoom) + 1;
      }

      // zoom out
      private void menuItem5_Click(object sender, EventArgs e)
      {
         MainMap.Zoom = (int) (MainMap.Zoom) - 1;
      }

      private void menuItem15_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.LithuaniaOrtoFotoMap;
      }

      private void menuItem10_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.GoogleHybridMap;
      }

      private void menuItem12_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.OpenStreetMap;
      }

      private void menuItem22_Click(object sender, EventArgs e)
      {
         //MainMap.MapProvider = GMapProviders.OpenStreetMapSurfer;
      }

      private void menuItem23_Click(object sender, EventArgs e)
      {
         //MainMap.MapProvider = GMapProviders.OpenStreetOsm;
      }

      private void menuItem9_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.GoogleMap;
      }

      private void menuItem16_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.BingMap;
      }

      private void menuItem18_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.BingHybridMap;
      }

      private void menuItem20_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.YahooMap;
      }

      private void menuItem21_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.YahooHybridMap;
      }

      private void menuItem14_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.LithuaniaMap;
      }

      private void menuItem25_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.ArcGIS_World_Topo_Map;
      }

      private void menuItem26_Click(object sender, EventArgs e)
      {
         MainMap.MapProvider = GMapProviders.ArcGIS_World_Physical_Map;
      }

      private void menuItem27_Click(object sender, EventArgs e)
      {
         MainMap.ReloadMap();
      }

      private void menuItemCacheOnly_Click(object sender, EventArgs e)
      {
         MainMap.Manager.Mode = AccessMode.CacheOnly;
         menuItemCacheOnly.Checked = true;
         menuItemServerAndCache.Checked = false;
         menuItemServerOnly.Checked = false;
      }

      private void menuItemServerAndCache_Click(object sender, EventArgs e)
      {
         MainMap.Manager.Mode = AccessMode.ServerAndCache;
         menuItemServerAndCache.Checked = true;
         menuItemCacheOnly.Checked = false;
         menuItemServerOnly.Checked = false;
      }

      private void menuItemServerOnly_Click(object sender, EventArgs e)
      {
         MainMap.Manager.Mode = AccessMode.ServerOnly;
         menuItemServerOnly.Checked = true;
         menuItemServerAndCache.Checked = false;
         menuItemCacheOnly.Checked = false;
      }

      private void menuItemGPSenabled_Click(object sender, EventArgs e)
      {
         menuItemGPSenabled.Checked = !menuItemGPSenabled.Checked;

         if(!menuItemGPSenabled.Checked)
         {
            if(gps.Opened)
            {
               gps.Close();
               SetOffGPSPower();
            }

            count = 0;
            countReal = 0;
            Total = 0;
            {
               TimeUTC = null;
               Lat = null;
               Lng = null;
               Delta = 0;
            }
            lock(Satellites)
            {
               Satellites.Clear();
               Satellites.TrimExcess();
            }

            if(Controls.Contains(pageGps))
            {
               pageGps.panelSignals.Invalidate();
            }

            TryCommitData();

            gpsPos.Pen.Color = Color.Blue;
         }
         else // start tracking
         {
            gpsPos.Pen.Color = Color.Red;
            gpsPos.IsVisible = true;

            if(!gps.Opened)
            {
               gps.Open();
               SetOnGPSPower();
            }
         }
      }

      public new void Hide()
      {
         Native.ShowWindow(this.Handle, Native.SW_MINIMIZED);
         timerKeeperOfLife.Enabled = false;
         IsVisible = false;

         hook.Stop();
      }

      object visibleLock = new object();
      bool visible = true;
      public bool IsVisible
      {
         get
         {
            lock(visibleLock)
            {
               return visible;
            }
         }
         set
         {
            lock(visibleLock)
            {
               visible = value;
            }
         }
      }

      public bool AddToLogCurrentInfo(GpsPosition data)
      {
         if(string.IsNullOrEmpty(LogDb))
         {
            return false;
         }

         bool ret = true;
         try
         {
            {
               if(cmd.Transaction == null)
               {
                  cmd.Transaction = cn.BeginTransaction(IsolationLevel.Serializable);
                  Debug.WriteLine("BeginTransaction: " + DateTime.Now.ToLongTimeString());
               }

               cmd.Parameters["@p1"].Value = data.Time.Value;
               cmd.Parameters["@p2"].Value = countReal++;
               cmd.Parameters["@p3"].Value = Delta;
               cmd.Parameters["@p4"].Value = data.Speed;
               cmd.Parameters["@p5"].Value = data.SeaLevelAltitude;
               cmd.Parameters["@p6"].Value = data.EllipsoidAltitude;
               cmd.Parameters["@p7"].Value = (short?) data.SatellitesInViewCount;
               cmd.Parameters["@p8"].Value = (short?) data.SatelliteCount;
               cmd.Parameters["@p9"].Value = data.Latitude.Value;
               cmd.Parameters["@p10"].Value = data.Longitude.Value;
               cmd.Parameters["@p11"].Value = data.PositionDilutionOfPrecision;
               cmd.Parameters["@p12"].Value = data.HorizontalDilutionOfPrecision;
               cmd.Parameters["@p13"].Value = data.VerticalDilutionOfPrecision;
               cmd.Parameters["@p14"].Value = (byte) data.FixQuality;
               cmd.Parameters["@p15"].Value = (byte) data.FixType;
               cmd.Parameters["@p16"].Value = (byte) data.FixSelection;

               cmd.ExecuteNonQuery();
            }

            if(DateTime.Now - LastFlush >= FlushDelay)
            {
               TryCommitData();
            }
         }
         catch(Exception ex)
         {
            Debug.WriteLine("AddToLog: " + ex.ToString());
            ret = false;
         }

         return ret;
      }

      void TryCommitData()
      {
         try
         {
            if(cmd.Transaction != null)
            {
               using(cmd.Transaction)
               {
                  cmd.Transaction.Commit();
                  LastFlush = DateTime.Now;
               }
               cmd.Transaction = null;
               Debug.WriteLine("TryCommitData: OK " + LastFlush.ToLongTimeString());
            }
         }
         catch(Exception ex)
         {
            Debug.WriteLine("TryCommitData: " + ex.ToString());
         }
      }

      public bool CheckLogDb(string file)
      {
         bool ret = true;

         try
         {
            string dir = Path.GetDirectoryName(file);
            if(!Directory.Exists(dir))
            {
               Directory.CreateDirectory(dir);
            }

            cn = new SQLiteConnection();
            {
               cn.ConnectionString = string.Format("Data Source=\"{0}\";FailIfMissing=False;", file);
               cn.Open();
               {
                  using(DbTransaction tr = cn.BeginTransaction())
                  {
                     try
                     {
                        using(DbCommand cmd = cn.CreateCommand())
                        {
                           cmd.Transaction = tr;
                           cmd.CommandText = @"CREATE TABLE IF NOT EXISTS GPS (id INTEGER NOT NULL PRIMARY KEY,
                                                TimeUTC DATETIME NOT NULL,
                                                SessionCounter INTEGER NOT NULL,
                                                Delta DOUBLE,
                                                Speed DOUBLE,
                                                SeaLevelAltitude DOUBLE,
                                                EllipsoidAltitude DOUBLE,
                                                SatellitesInView TINYINT,
                                                SatelliteCount TINYINT,
                                                Lat DOUBLE NOT NULL,
                                                Lng DOUBLE NOT NULL,
                                                PositionDilutionOfPrecision DOUBLE,
                                                HorizontalDilutionOfPrecision DOUBLE,
                                                VerticalDilutionOfPrecision DOUBLE,
                                                FixQuality TINYINT NOT NULL,
                                                FixType TINYINT NOT NULL,
                                                FixSelection TINYINT NOT NULL); 
                                               CREATE INDEX IF NOT EXISTS IndexOfGPS ON GPS (TimeUTC, PositionDilutionOfPrecision);";
                           cmd.ExecuteNonQuery();
                        }

                        this.cmd = cn.CreateCommand();
                        {
                           cmd.CommandText = @"INSERT INTO GPS
                                         (TimeUTC,
                                          SessionCounter,
                                          Delta,
                                          Speed,
                                          SeaLevelAltitude,
                                          EllipsoidAltitude,
                                          SatellitesInView,
                                          SatelliteCount,
                                          Lat,
                                          Lng,
                                          PositionDilutionOfPrecision,
                                          HorizontalDilutionOfPrecision,
                                          VerticalDilutionOfPrecision,
                                          FixQuality,
                                          FixType,
                                          FixSelection) VALUES(@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16);";

                           cmd.Parameters.Add(new SQLiteParameter("@p1"));
                           cmd.Parameters.Add(new SQLiteParameter("@p2"));
                           cmd.Parameters.Add(new SQLiteParameter("@p3"));
                           cmd.Parameters.Add(new SQLiteParameter("@p4"));
                           cmd.Parameters.Add(new SQLiteParameter("@p5"));
                           cmd.Parameters.Add(new SQLiteParameter("@p6"));
                           cmd.Parameters.Add(new SQLiteParameter("@p7"));
                           cmd.Parameters.Add(new SQLiteParameter("@p8"));
                           cmd.Parameters.Add(new SQLiteParameter("@p9"));
                           cmd.Parameters.Add(new SQLiteParameter("@p10"));
                           cmd.Parameters.Add(new SQLiteParameter("@p11"));
                           cmd.Parameters.Add(new SQLiteParameter("@p12"));
                           cmd.Parameters.Add(new SQLiteParameter("@p13"));
                           cmd.Parameters.Add(new SQLiteParameter("@p14"));
                           cmd.Parameters.Add(new SQLiteParameter("@p15"));
                           cmd.Parameters.Add(new SQLiteParameter("@p16"));
                           cmd.Prepare();
                        }

                        tr.Commit();
                     }
                     catch
                     {
                        tr.Rollback();
                        ret = false;
                     }
                  }
               }
            }
         }
         catch(Exception ex)
         {
            if(cn != null)
            {
               cn.Dispose();
               cn = null;
            }
            if(cmd != null)
            {
               cmd.Dispose();
               cmd = null;
            }
            Debug.WriteLine("CreateEmptyDB: " + ex.ToString());
            ret = false;
         }

         if(ret)
         {
            LogDb = file;
         }
         else
         {
            LogDb = null;
         }

         return ret;
      }

      private void MainForm_Load(object sender, EventArgs e)
      {
         updateDataHandler = new EventHandler(UpdateData);

         gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
         gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);

         string sd = Native.GetRemovableStorageDirectory();
         if(!string.IsNullOrEmpty(sd))
         {
            var fileName = sd + Path.DirectorySeparatorChar + "GMap.NET" + Path.DirectorySeparatorChar + "log.gpsd";
            {
               CheckLogDb(fileName);
            }
         }

         timerKeeperOfLife.Interval = ShortestTimeoutInterval() * 1000;
         timerKeeperOfLife.Enabled = true;

         if(menuItemGPSenabled.Checked)
         {
            if(!gps.Opened)
            {
               gps.Open();
               gpsPos.Pen.Color = Color.Red;
               gpsPos.IsVisible = true;

               SetOnGPSPower();
            }
         }
         else
         {
            gpsPos.Pen.Color = Color.Blue;
            gpsPos.IsVisible = false;
         }

         try
         {
            // Config AutoRotate
            {
               using(RegistryKey Config = Registry.CurrentUser.OpenSubKey(@"Software\HTC\HTCSENSOR\GSensor\ModuleName", true))
               {
                  if(Config != null)
                  {
                     string gmapnet = Config.GetValue("GMapNET", null) as string;
                     if(string.IsNullOrEmpty(gmapnet))
                     {
                        Config.SetValue("GMapNET", @"\Program files\gmap.net\GMap.NET.exe");
                     }
                  }
               }

               using(RegistryKey Config = Registry.CurrentUser.OpenSubKey(@"Software\HTC\HTCSENSOR\GSensor\WhiteList", true))
               {
                  if(Config != null)
                  {
                     string gmapnet = Config.GetValue("GMapNET", null) as string;
                     if(string.IsNullOrEmpty(gmapnet))
                     {
                        Config.SetValue("GMapNET", "#NETCF_AGL_BASE_");
                     }
                  }
               }
            }
         }
         catch(Exception ex)
         {
            Debug.WriteLine(ex.ToString());
         }
      }

      void gps_LocationChanged(object sender, GpsPosition position)
      {
         try
         {
            if(position != null)
            {
               count++;

               //Debug.WriteLine("LocationChanged: " + DateTime.Now.ToLongTimeString() + " -> " + count);

               if(position.Time.HasValue && position.Latitude.HasValue && position.Longitude.HasValue)
               {
                  //Debug.WriteLine("Location: " + position.Latitude.Value + "|" + position.Longitude.Value);

                  // first time
                  if(!TimeUTC.HasValue)
                  {
                     TimeUTC = position.Time;
                     Lat = position.Latitude;
                     Lng = position.Longitude;
                  }

                  if(TimeUTC.HasValue && position.Time - TimeUTC.Value >= delay)
                  {
                     Delta = gps.GetDistance(position.Latitude.Value, position.Longitude.Value, Lat.Value, Lng.Value);
                     Total += Delta;
                     Lat = position.Latitude;
                     Lng = position.Longitude;
                     TimeUTC = position.Time;

                     AddToLogCurrentInfo(position);
                  }
               }
               else
               {
                  Lat = position.Latitude;
                  Lng = position.Longitude;
                  TimeUTC = position.Time;
               }

               // do not update if user is idling
               if(IsVisible)
               {
                  lock(Satellites)
                  {
                     Satellites.Clear();
                     Satellites.AddRange(position.GetSatellitesInView());
                     Satellites.TrimExcess();
                  }

                  Invoke(updateDataHandler, position);
               }
            }
         }
         catch(Exception ex)
         {
            Debug.WriteLine("gps_LocationChanged: " + ex);
         }
      }

      void gps_DeviceStateChanged(object sender, GpsDeviceState args)
      {
         device = args;

         if(IsVisible)
         {
            Invoke(updateDataHandler);
         }
      }

      private void MainForm_Closed(object sender, EventArgs e)
      {
         hook.Stop();

         if(gps.Opened)
         {
            gps.Close();
         }

         SetOffGPSPower();

         if(cn != null)
         {
            TryCommitData();

            if(cn.State == ConnectionState.Open)
            {
               cn.Close();
            }
            cn.Dispose();
            cn = null;
         }
         if(cmd != null)
         {
            cmd.Dispose();
            cmd = null;
         }
      }

      void SetOnGPSPower()
      {
         // Keep the GPS and device alive
         bool power = Native.PowerPolicyNotify(Native.PPN_UNATTENDEDMODE, true);
         if(!power)
         {
            Debug.WriteLine("PowerPolicyNotify failed for PPN_UNATTENDEDMODE");
         }
         else
         {
            if(gpsPowerHandle == IntPtr.Zero)
            {
               gpsPowerHandle = Native.SetPowerRequirement("gps0:", Native.CedevicePowerStateState.D0, Native.POWER_NAME | Native.POWER_FORCE, null, 0);
               if(gpsPowerHandle == IntPtr.Zero)
               {
                  Debug.WriteLine("SetPowerRequirement failed for GPS");
               }
            }
         }
      }

      void SetOffGPSPower()
      {
         if(gpsPowerHandle != IntPtr.Zero)
         {
            Native.ReleasePowerRequirement(gpsPowerHandle);
            gpsPowerHandle = IntPtr.Zero;
         }
         Native.PowerPolicyNotify(Native.PPN_UNATTENDEDMODE, false);
      }

      ~MainForm()
      {
         MainForm_Closed(null, null);
      }

      void UpdateData(object sender, System.EventArgs args)
      {
         try
         {
            var lastData = sender as GpsPosition;

            // update signals
            if(Controls.Contains(pageGps))
            {
               pageGps.panelSignals.Invalidate();

               string str = Environment.NewLine;

               str += "GPS: " + device.DeviceState + ", Driver: " + device.ServiceState + ", " + count + " | " + countReal + "\n";

               if(lastData != null)
               {
                  if(lastData.Time.HasValue && lastData.Longitude.HasValue && lastData.Longitude.HasValue)
                  {
                     int deltaClock = ((int) (DateTime.UtcNow - lastData.Time.Value).TotalSeconds);

                     str += "Time(UTC): " + lastData.Time.Value.ToLongTimeString() + ", delay: " + deltaClock + "s \n";
                     str += "Delta: " + string.Format("{0:0.00}m, total: {1:0.00km}\n", Delta * 1000.0, Total);
                     str += "Latitude: " + lastData.Latitude.Value + "\n";
                     str += "Longitude: " + lastData.Longitude.Value + "\n\n";

                     if(Math.Abs(deltaClock) > 5) // 5s
                     {
                        UpdateTime(lastData.Time.Value);
                     }
                  }
                  else
                  {
                     str += "Time(UTC): -" + "\n";
                     str += "Delta: - \n";
                     str += "Latitude: -" + "\n";
                     str += "Longitude: -" + "\n\n";
                  }

                  if(lastData.Speed.HasValue)
                  {
                     str += "Speed: " + string.Format("{0:0.0}km/h | {1:0.0}m/s, head: {2}\n", lastData.Speed, lastData.Speed / 3.6, (int) (lastData.Heading.HasValue ? lastData.Heading.Value : 0));
                  }
                  else
                  {
                     str += "Speed: -\n";
                  }

                  if(lastData.SeaLevelAltitude.HasValue)
                  {
                     str += "SeaLevelAltitude: " + string.Format("{0:0.00}m\n", lastData.SeaLevelAltitude);
                  }
                  else
                  {
                     str += "SeaLevelAltitude: -\n";
                  }

                  if(lastData.PositionDilutionOfPrecision.HasValue)
                  {
                     str += "PositionDilutionOfPrecision: " + string.Format("{0:0.00}\n", lastData.PositionDilutionOfPrecision);
                  }
                  else
                  {
                     str += "PositionDilutionOfPrecision: -\n";
                  }

                  if(lastData.HorizontalDilutionOfPrecision.HasValue)
                  {
                     str += "HorizontalDilutionOfPrecision: " + string.Format("{0:0.00}\n", lastData.HorizontalDilutionOfPrecision);
                  }
                  else
                  {
                     str += "HorizontalDilutionOfPrecision: -\n";
                  }

                  if(lastData.VerticalDilutionOfPrecision.HasValue)
                  {
                     str += "VerticalDilutionOfPrecision: " + string.Format("{0:0.00}\n", lastData.VerticalDilutionOfPrecision);
                  }
                  else
                  {
                     str += "VerticalDilutionOfPrecision: -\n";
                  }

                  if(lastData.SatellitesInViewCount.HasValue)
                  {
                     str += "SatellitesInView: " + lastData.SatellitesInViewCount + "\n";
                  }
                  else
                  {
                     str += "SatellitesInView: -" + "\n";
                  }

                  if(lastData.SatelliteCount.HasValue)
                  {
                     str += "SatelliteCount: " + lastData.SatelliteCount + "\n";
                  }
                  else
                  {
                     str += "SatelliteCount: -" + "\n";
                  }
               }
               pageGps.status.Text = str;
            }
            else if(Controls.Contains(MainMap))
            {
               if(lastData != null)
               {
                  if(lastData.Time.HasValue && lastData.Longitude.HasValue && lastData.Longitude.HasValue)
                  {
                     // center map
                     if(menuItemGPSenabled.Checked)
                     {
                        if(menuItemSnapToGps.Checked && !MainMap.IsDragging)
                        {
                           var newPos = new PointLatLng(lastData.Latitude.Value, lastData.Longitude.Value);
                           
                           if(menuItemshowDestination.Checked)
                           {
                              UpdateDestinationRoute(false, newPos);
                           }

                           MainMap.Position = newPos;
                        }
                        else
                        {
                           gpsPos.Position = new PointLatLng(lastData.Latitude.Value, lastData.Longitude.Value);

                           if(menuItemshowDestination.Checked)
                           {
                              UpdateDestinationRoute(true, gpsPos.Position);
                           }
                        }
                     }                     
                  }
               }
            }
         }
         catch(Exception ex)
         {
            pageGps.status.Text = "\n" + ex.ToString();
         }
      }

      int ShortestTimeoutInterval()
      {
         int retVal = 1000;

         try
         {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"\SYSTEM\CurrentControlSet\Control\Power");
            object oBatteryTimeout = key.GetValue("BattPowerOff");
            object oACTimeOut = key.GetValue("ExtPowerOff");
            object oScreenPowerOff = key.GetValue("ScreenPowerOff");
            key.Close();

            if(oBatteryTimeout is int)
            {
               int v = (int) oBatteryTimeout;
               if(v > 0)
                  retVal = Math.Min(retVal, v);
            }
            if(oACTimeOut is int)
            {
               int v = (int) oACTimeOut;
               if(v > 0)
                  retVal = Math.Min(retVal, v);
            }
            if(oScreenPowerOff is int)
            {
               int v = (int) oScreenPowerOff;
               if(v > 0)
                  retVal = Math.Min(retVal, v);
            }
         }
         catch(Exception ex)
         {
            Debug.WriteLine("ShortestTimeoutInterval: " + ex.ToString());
         }

         return retVal * 9 / 10;
      }

      private void menuItemEnableGrid_Click(object sender, EventArgs e)
      {
         menuItemEnableGrid.Checked = !menuItemEnableGrid.Checked;
         MainMap.ShowTileGridLines = menuItemEnableGrid.Checked;
      }

      private void timerKeeperOfLife_Tick(object sender, EventArgs e)
      {
         Native.SystemIdleTimerReset();
      }

      private void menuItemDisableAutoSleep_Click(object sender, EventArgs e)
      {
         menuItemDisableAutoSleep.Checked = !menuItemDisableAutoSleep.Checked;
         timerKeeperOfLife.Enabled = menuItemDisableAutoSleep.Checked;
      }

      private void MainForm_Activated(object sender, EventArgs e)
      {
         timerKeeperOfLife.Enabled = menuItemDisableAutoSleep.Checked;
         IsVisible = true;
         hook.Start();
      }

      private void menuItem32_Click(object sender, EventArgs e)
      {
         this.Hide();
      }

      internal void menuItemGotoMap_Click(object sender, EventArgs e)
      {
         menuItemGotoMap.Checked = true;
         menuItemGotoGps.Checked = false;
         menuItemGotoTransport.Checked = false;
         menuItemSearch.Checked = false;

         this.SuspendLayout();
         this.Controls.Clear();
         this.Controls.Add(this.MainMap);
         this.ResumeLayout(false);
      }

      private void menuItemGotoGps_Click(object sender, EventArgs e)
      {
         menuItemGotoGps.Checked = true;
         menuItemGotoTransport.Checked = false;
         menuItemGotoMap.Checked = false;
         menuItemSearch.Checked = false;

         this.SuspendLayout();
         this.Controls.Clear();
         this.pageGps.Dock = DockStyle.Fill;
         this.Controls.Add(pageGps);
         this.ResumeLayout(false);

         pageGps.panelSignals.Invalidate();
      }

      internal void menuItemGotoTransport_Click(object sender, EventArgs e)
      {
         menuItemGotoTransport.Checked = true;
         menuItemGotoMap.Checked = false;
         menuItemGotoGps.Checked = false;
         menuItemSearch.Checked = false;

         this.SuspendLayout();
         this.Controls.Clear();
         this.pageTransport.Dock = DockStyle.Fill;
         this.Controls.Add(pageTransport);
         this.ResumeLayout(false);
      }

      private void menuItemSearch_Click(object sender, EventArgs e)
      {
         menuItemSearch.Checked = true;
         menuItemGotoTransport.Checked = false;
         menuItemGotoMap.Checked = false;
         menuItemGotoGps.Checked = false;

         this.SuspendLayout();
         this.Controls.Clear();
         this.pageSearch.Dock = DockStyle.Fill;
         this.Controls.Add(pageSearch);
         this.ResumeLayout(false);
      }

      private void menuItem35_Click(object sender, EventArgs e)
      {
         ZoomToFitMarkers();
      }

      private void menuItem31_Click(object sender, EventArgs e)
      {
         MainMap.Zoom = MainMap.MinZoom;
      }

      private void menuItem33_Click(object sender, EventArgs e)
      {
         MainMap.Zoom = MainMap.MaxZoom;
      }

      // clear markers
      private void menuItem37_Click(object sender, EventArgs e)
      {
         objects.Markers.Clear();
      }

      private struct SYSTEMTIME
      {
         public short Year;
         public short Month;
         public short DayOfWeek;
         public short Day;
         public short Hour;
         public short Minute;
         public short Second;
         public short Milliseconds;
      }

      [DllImport("coredll.dll")]
      private static extern bool SetSystemTime(ref SYSTEMTIME time);

      private void UpdateTime(DateTime gpsTime)
      {
         SYSTEMTIME s = new SYSTEMTIME();
         s.Year = (short) gpsTime.Year;
         s.Month = (short) gpsTime.Month;
         s.DayOfWeek = (short) gpsTime.DayOfWeek;
         s.Day = (short) gpsTime.Day;
         s.Hour = (short) gpsTime.Hour;
         s.Minute = (short) gpsTime.Minute;
         s.Second = (short) gpsTime.Second;
         s.Milliseconds = (short) gpsTime.Millisecond;

         bool t = SetSystemTime(ref s);
         Debug.WriteLine("SetSystemTime: " + t);
      }

      private void menuItemSnapToGps_Click(object sender, EventArgs e)
      {
         menuItemSnapToGps.Checked = !menuItemSnapToGps.Checked;
         if(!menuItemSnapToGps.Checked)
         {
            gpsPos.Position = MainMap.Position;
         }
      }

      private void menuItemSetDestination_Click(object sender, EventArgs e)
      {
         {
            destinationPoint = MainMap.Position;

            if(menuItemshowDestination.Checked)
            {
               UpdateDestinationRoute(true, menuItemSnapToGps.Checked ? MainMap.Position : gpsPos.Position);
            }
         }           
      }

      internal GMapRoute destinationRoute;

      private void menuItemshowDestination_Click(object sender, EventArgs e)
      {
         menuItemshowDestination.Checked = !menuItemshowDestination.Checked;

         if(menuItemshowDestination.Checked)
         {
            if(MainMap.panel == null)
            {
               MainMap.panel = this;
            }
            UpdateDestinationRoute(true, menuItemSnapToGps.Checked ? MainMap.Position : gpsPos.Position);
         }

         if(destinationRoute != null)
         {
            destinationRoute.IsVisible = menuItemshowDestination.Checked;
         }
      }

      void UpdateDestinationRoute(bool forceUpdate, PointLatLng startPoint)
      {
         if(destinationRoute == null)
         {
            destinationRoute = new GMapRoute("destination route");

            destinationRoute.Stroke.Color = Color.Red;
            destinationRoute.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            destinationRoute.Stroke.Width = 1.0f;
 
            objects.Routes.Add(destinationRoute);
         }
         else
         {
            destinationRoute.Points.Clear();
         }

         destinationRoute.Points.Add(startPoint);
         destinationRoute.Points.Add(destinationPoint);
         if(forceUpdate)
         {
            MainMap.UpdateRouteLocalPosition(destinationRoute);
            MainMap.Invalidate();
         }
      }
   }

   public class Map : GMapControl
   {
      readonly Brush screenBrush = new SolidBrush(Color.Navy);
      readonly Font screenFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular);

      public MainForm panel;

      protected override void OnPaintOverlays(Graphics g)
      {
         base.OnPaintOverlays(g);

         if(panel != null && panel.menuItemshowDestination.Checked)
         {
            g.DrawString(string.Format("destination: {0:0.00} km", panel.destinationRoute.Distance), screenFont, screenBrush, screenFont.Size, screenFont.Size);
         }
      }
   }
}