Dec 27 2008

OpenCellID for .Net

Category: Windows Mobile | WindowsJoel Ivory Johnson @ 13:17

I made mention of OpenCellID.org in a post not long ago.  In that post I had stated that I probably would not use the service since it was Java oriented.  I've got to retract that statement as I've just made a simple C# class for accessing the functionality.  It will be in an upcoming article on codeproject.com.  If you wanted to see the OpenCellID portion of the code look below.

 

 

using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.XPath;


namespace J2i.Net.OpenCellID
{

    
    public class OpenCellClient : IDisposable
    {
        string _baseUrl = "http://www.opencellid.org/cell";
        string _appID = String.Empty;

        public OpenCellClient()
        {

        }
        public OpenCellClient(string appID):this()
        {
            _appID = appID;
        }

        public CellTowerLocation GetCellTowerLocation (uint mobileCountryCode, uint  mobileNetworkCode, uint  localeAreaCode, uint   cellID)
        {
            CellTowerLocation retVal = null;
            string requestAddress = String.Format("{0}/get?mnc={1}&mcc={2}&lac={3}&cellid={4}",
                _baseUrl,
                mobileNetworkCode, mobileCountryCode, localeAreaCode, cellID);
            WebRequest webRequest = HttpWebRequest.Create(requestAddress);
            WebResponse response = webRequest.GetResponse();
            using (Stream xmlSource = response.GetResponseStream())
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(xmlSource );

                if(xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                {
                    XmlNode cellNode = xd.GetElementsByTagName("cell")[0];
                    retVal = new CellTowerLocation();
                    retVal.cellid = cellID;
                    retVal.mcc = mobileCountryCode;
                    retVal.mnc = mobileNetworkCode;

                    retVal.lac = localeAreaCode;
                    retVal.lat = Double.Parse(cellNode.Attributes["lat"].Value);
                    retVal.lon = Double.Parse(cellNode.Attributes["lon"].Value);
                    retVal.nbSamples = Int32.Parse(cellNode.Attributes["nbSamples"].Value);
                    retVal.range = Int32.Parse(cellNode.Attributes["range"].Value);
                }
            }
            return retVal;

        }

        public CellTowerLocation GetCellTowerLocation()
        {

            RILCELLTOWERINFO towerInfo = RIL.GetCellTowerInfo();
            return GetCellTowerLocation(towerInfo.dwMobileCountryCode, towerInfo.dwMobileNetworkCode, towerInfo.dwLocationAreaCode, towerInfo.dwCellID);

        }

        public int AddMeasure(uint mobileCountryCode, uint mobileNetworkCode, uint localeAreaCode, uint cellID, double latitude, double longitude)
        {
            TestAppID();
            int retVal = 0;
            string requestAddress = String.Empty;

            requestAddress = string.Format("{0}/measure/add?key={1}?&mnc={2}&mcc={3}&lac={4}&cellid={5}&lat={6}&long={7}",
                _baseUrl,
                _appID, mobileNetworkCode,
                mobileNetworkCode,
                mobileCountryCode,
                localeAreaCode,
                cellID,
                latitude,
                longitude);
            WebRequest webRequest = HttpWebRequest.Create(requestAddress);
            WebResponse response = webRequest.GetResponse();
            using (Stream xmlSource = response.GetResponseStream())
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(xmlSource);

                if (xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                {
                    retVal = int.Parse(xd.DocumentElement.Attributes["id"].Value);
                }
            }
            return retVal;
        }

        public bool DeleteMeasure(int id)
        {
            TestAppID();
            bool retVal = false;
            string requestAddress = String.Format("{0}/measure/delete/{1}?key={2}", _baseUrl, id, _appID);
            WebRequest webRequest = HttpWebRequest.Create(requestAddress);
            WebResponse response = webRequest.GetResponse();
            using (Stream xmlSource = response.GetResponseStream())
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(xmlSource);

                if (xd.GetElementsByTagName("rsp")[0].Attributes["stat"].Value.Equals("ok"))
                {
                    retVal = true;
                }
            }
            return retVal;
        }


        private void TestAppID()
        {
            if ((_appID == null) || (_appID.Equals(String.Empty)))
                throw new ArgumentException("An Application ID has not been set on the OpenCellClient object.  A key can be requested at http://opencellid.org/users/signup", "AppID");
        }

        #region IDisposable Members

        public void Dispose()
        {

        }

        #endregion
    }
}

Tags: