|
 |
|
ช่วยดูโค้ตให้หน่อยค่ะ เกี่ยวกับส่งค่าพิกัดกลับค่ะ ถ้าต้องการให้โปรแกรมเมื่อรับ sms แล้วโปรแกรมจะส่งค่าพิกัดกลับทุก 5 นาที |
|
 |
|
|
 |
 |
|
ถ้าต้องการให้โปรแกรมเมื่อรับ sms แล้วโปรแกรมจะส่งค่าพิกัดกลับทุก 5 นาที ต้องแก้โค้ตตรงไหนบ้างค่ะ
โค้ตดังนี้ค่ะ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.WindowsMobile.Status;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.Samples.Location;
using CoreServices;
namespace Myproject
{
public partial class Form1 : Form, IDisposable
{
#region Delegates
delegate void VoidDelegate();
#endregion
#region Delegates
MessageInterceptorEventHandler _messageInterceptorEventHandler = null;
#endregion
#region private fields
MessageInterceptor _messageInterceptor;
Gps gps;
FindMeSettings _Settings = new FindMeSettings();
string _assemblyFolder;
String ruleName = "Myproject[InterceptRule]";
string responseTemplate = "http://maps.live.com/default.aspx?v=2&cp={0:0.000000}~{1:0.000000}&style=a&lvl=17&alt=-1000&sp=an.{0:0.000000}_{1:0.000000}____&encType=1";
string detailedResponseTemplate = "http://local.live.com/default.aspx?v=2&cp={0:0.000000}~{1:0.000000}&style=r&lvl=16&tilt=-90&dir=0&alt=-1000&sp=an.{0:0.000000}_{1:0.000000}_{3}_{4}__{2}&encType=1";
GpsPosition currentPosition = null;
object SyncLock = new Object();
#endregion
#region Constructors
public Form1()
{
InitializeComponent();
LoadSettings();
_messageInterceptorEventHandler = new MessageInterceptorEventHandler(this._messageInterceptor_MessageReceived);
}
#endregion
#region Properties
public string AssemblyFolder
{
get
{
if(_assemblyFolder==null)
{
string fullPath = this.GetType().Module.FullyQualifiedName;
string asmName = this.GetType().Module.Name;
_assemblyFolder = fullPath.Substring(0, fullPath.LastIndexOf(asmName));
}
return _assemblyFolder;
}
}
#endregion
#region event handlers
void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
if (args.Position.LatitudeValid && args.Position.LongitudeValid)
{
lock (SyncLock)
{
currentPosition = args.Position;
}
UpdatePosition();
}
}
private void menuItemExit_Click(object sender, EventArgs e)
{
ApplyChanges();
CloseGps();
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
BeginTrackingLocation();
if (MessageInterceptor.IsApplicationLauncherEnabled(ruleName))
{
_messageInterceptor = new MessageInterceptor(ruleName);
_messageInterceptor.MessageReceived += new MessageInterceptorEventHandler(_messageInterceptor_MessageReceived);
txtPin.Text = _messageInterceptor.MessageCondition.ComparisonValue;
this.chkFindMeEnabled.Checked = true;
}
else
{
this.chkFindMeEnabled.Checked = false;
}
PopulateContactList();
}
void _messageInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
if (this.currentPosition == null)
{
GpsPosition pos = gps.GetPosition(new TimeSpan(0, 0, 30));
if (pos != null)
{
lock (SyncLock)
{
currentPosition = pos;
}
}
}
if (currentPosition != null)
{
if (_Settings.IsAllowed(e.Message.From.Address)||_Settings.PermissionType==PermissionType.AllowAll)
{
string emailAddress = null;
if ((_Settings.OptionFlags & OptionChangedFlags.Email) == (OptionChangedFlags.Email))
emailAddress = _Settings.FindEmailAddress(e.Message.From.Address);
lock (SyncLock)
{
if (emailAddress != null)
this.EmailSendCoordinates(emailAddress, this.currentPosition);
else
SmsSendCoordinates(e.Message.From.Address, this.currentPosition);
}
if ((_Settings.OptionFlags & OptionChangedFlags.Alert) == OptionChangedFlags.Alert)
Sound.PlayFile(AssemblyFolder + "Alert.wav");
}
}
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
this.CloseGps();
}
private void rbAllowListed_CheckedChanged(object sender, EventArgs e)
{
EnablePermissionList();
}
private void rbAllowAnyone_CheckedChanged(object sender, EventArgs e)
{
DisablePermissionList();
}
private void menuItemApply_Click(object sender, EventArgs e)
{
ApplyChanges();
}
#endregion
#region methods
void ApplyChanges()
{
if (_messageInterceptor != null)
{
_messageInterceptor.DisableApplicationLauncher();
_messageInterceptor.MessageReceived -= _messageInterceptorEventHandler;
_messageInterceptor.Dispose();
_messageInterceptor = null;
}
if (this.chkFindMeEnabled.Checked)
{
try
{
if (_messageInterceptor == null)
{
_messageInterceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
}
_messageInterceptor.MessageCondition = new MessageCondition();
_messageInterceptor.MessageCondition.CaseSensitive = false;
_messageInterceptor.MessageCondition.ComparisonType = MessagePropertyComparisonType.Contains;
_messageInterceptor.MessageCondition.ComparisonValue = this.txtPin.Text;
_messageInterceptor.MessageCondition.Property = MessageProperty.Body;
_messageInterceptor.EnableApplicationLauncher(ruleName);
_messageInterceptor.MessageReceived += _messageInterceptorEventHandler;
}
catch (PocketOutlookException pEx)
{
MessageBox.Show(pEx.Message);
}
}
SaveSettings();
}
private void BeginTrackingLocation()
{
gps = new Gps();
gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
gps.Open();
}
void DisablePermissionList()
{
//this.lvContacts.Enabled = false;
}
void EnablePermissionList()
{
this._Settings.PermissionType = PermissionType.AllowListed;
}
private void LoadSettings()
{
FileInfo fi = new FileInfo(AssemblyFolder + "settings.config");
if (fi.Exists)
{
Stream s = fi.OpenRead();
this._Settings=FindMeSettings.Load(s);
if (_Settings.PermissionType == PermissionType.AllowListed)
this.rbAllowListed.Checked = true;
if (_Settings.PermissionType == PermissionType.AllowAll)
this.rbAllowAnyone.Checked = true;
else
this.rbAllowListed.Checked = true;
s.Close();
}
}
private void SaveSettings()
{
FileInfo fi = new FileInfo(AssemblyFolder+"settings.config");
Stream s;
_Settings.PermissionType = (this.rbAllowAnyone.Checked) ? PermissionType.AllowAll : PermissionType.AllowListed;
_Settings.Save(s=fi.Open(FileMode.Create));
s.Close();
}
void UpdatePosition()
{
if (this.InvokeRequired)
{
this.Invoke((new VoidDelegate(this.UpdatePosition)));
}
else
{
if (this.currentPosition != null)
{
this.txtLatitude.Text = String.Format("{0:0.000000}", this.currentPosition.Latitude);
this.txtLongitude.Text = String.Format("{0:0.000000}", this.currentPosition.Longitude);
}
else
{
this.txtLatitude.Text = this.txtLongitude.Text = string.Empty;
}
}
}
public void SmsSendCoordinates(string to,GpsPosition pos)
{
string message = String.Format(responseTemplate, pos.Latitude, pos.Longitude);
SmsMessage sms = new SmsMessage(to, message);
sms.Send();
this.eventLog.Add(DateTime.Now, to, pos);
}
public void EmailSendCoordinates(string emailAddress, GpsPosition pos)
{
string avatarUrl = string.Empty;
string displayName = string.Empty;
string customMessage=string.Empty;
if(((_Settings.OptionFlags&OptionChangedFlags.Avatar)==OptionChangedFlags.Avatar)&&(_Settings.Avatar.Length>0))
avatarUrl=_Settings.Avatar;
string message = String.Format(detailedResponseTemplate, pos.Latitude, pos.Longitude, Utility.UrlEncode(avatarUrl), Utility.UrlEncode(displayName), Utility.UrlEncode(customMessage));
EmailAccount account = (new OutlookSession()).EmailAccounts[_Settings.EmailAccount];
EmailMessage msg = new EmailMessage();
msg.To.Add(new Recipient(emailAddress));
msg.Subject = "My location";
msg.BodyText = message;
msg.Send(account);
this.eventLog.Add(DateTime.Now, emailAddress, pos);
}
#endregion
void CloseGps()
{
if (this.gps != null)
if (this.gps.Opened)
{
this.gps.Close();
}
gps = null;
}
void PopulateContactList()
{
OutlookSession os = new OutlookSession();
ContactCollection contactList = os.Contacts.Items;
for (int i = 0; i < contactList.Count; ++i)
{
Contact contact = contactList[i];
if ((contact.FirstName.Length > 0) || (contact.LastName.Length > 0))
{
ListViewItem lvi = new ListViewItem(new string[] { contact.FirstName, contact.LastName });
lvi.Tag = contact;
lvi.ImageIndex = (_Settings.IsAllowed(contact)?1:0);
lvi.Checked = _Settings.IsAllowed(contact);
}
}
}
#endregion
}
}
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2010-02-11 01:59:44 |
By :
KARAKED |
View :
1335 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
if (this.currentPosition == null)<<< Loop รอบแรก มันเข้า If นี้ แต่พอ Loop รอบที่ สอง มันไม่เข้าแล้ว เพราะมันได้ค่าจาก Loop แรกแล้ว มันเลยไม่เป็น Null อีก ทำไห้ไม่ได้พิกัดใหม่
{
GpsPosition pos = gps.GetPosition(new TimeSpan(0, 0, 30));
if (pos != null)
{
lock (SyncLock)
{
currentPosition = pos;
}
}
}
if (currentPosition != null) <<< พอมาตรงนี้มันเลยส่งแต่พิกัดเดิม
{
if (_Settings.IsAllowed(e.Message.From.Address) || _Settings.PermissionType == PermissionType.AllowAll)
{
string emailAddress = null;
if ((_Settings.OptionFlags & OptionChangedFlags.Email) == (OptionChangedFlags.Email))
emailAddress = _Settings.FindEmailAddress(e.Message.From.Address);
lock (SyncLock)
{
if (emailAddress != null)
this.EmailSendCoordinates(emailAddress, this.currentPosition);
else
SmsSendCoordinates(e.Message.From.Address, this.currentPosition);
}
if ((_Settings.OptionFlags & OptionChangedFlags.Alert) == OptionChangedFlags.Alert)
Sound.PlayFile(AssemblyFolder + "Alert.wav");
}
}
|
 |
 |
 |
 |
Date :
2010-02-12 10:34:25 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณค่ะ จะลองทำดู
|
 |
 |
 |
 |
Date :
2010-02-14 01:04:33 |
By :
KARAKED |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|