Tuesday, November 26, 2013

some code refactor

Before:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
            var securityHeader = new PHSCredentialsHeader()
            {
                Username = _Username,
                Password = _Password
            };

            using (System.ServiceModel.OperationContextScope contextScope = new System.ServiceModel.OperationContextScope(svc.InnerChannel))
            {
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(securityHeader);

                                     ...

After using dll:

            PAS.UserAuths ret = new PAS.UserAuths();
            PAS.PASservicesSoapClient svc = new PAS.PASservicesSoapClient();
 
            PHSServiceAI.PHSServiceWrapper sw = new PHSServiceAI.PHSServiceWrapper(_Username, _Password, svc.InnerChannel);           
 
            using (sw.cScope)
            {
                                ...




DLL Code:

namespace PHSServiceAI
{
    /// 
    /// Security Header Wrapper for PHS AI Servcies
    /// 
    public class PHSServiceWrapper
    {
        public string UserId { getset; }
        public string Password { getset; }
        public System.ServiceModel.OperationContextScope cScope { getset; }
 
        public PHSServiceWrapper()
        {
 
        }
 
        public PHSServiceWrapper(string uName, string pw, System.ServiceModel.IContextChannel ic)
        {
 
            try
            {
 
                this.UserId = uName;
                this.Password = pw;
                PHSCredentialsHeader crdHdr;
 
                crdHdr = new PHSCredentialsHeader()
                {
                    Username = this.UserId,
                    Password = this.Password
                };
 
                cScope = new System.ServiceModel.OperationContextScope(ic);
                System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(crdHdr);
 
 
            }
            catch (Exception e)
            {
                //TODO log e.Message etc.
            }
 
 
        }
    }
}

Tuesday, August 20, 2013

92 F

Had to take the day off.  When the tough chore was completed, I road the local compressed woodlands of the city.  Total of Two main loops and 4 inner loops around sailors.  92 degrees.  I have not rode in that type of heat in a while.  I started off slow, but still sprinted up some of the hills and kept pedaling hard.

Tuesday, August 6, 2013

Conditional Breakpoint Exaple in Visual Studio 2010

I was looking for a few bad HL7 messages by control ID in this processor that we're coding.  Here's one way to get the debugger to stop on a break point on condition.  In this case I set up a bool that is set to True when any of the control IDs come into the main loop.  Add a breakpoint.  Right click on this breakpoint and select Condition.  Type in the name of the bool that you have set up and select Is True radio option.




Wednesday, June 19, 2013

Cleaner code with SQL Coalesce

ver 1.

if @runDate != null
  set @rd = CONVERT(VARCHAR(10),@runDate,101)
else
  set @rd = CONVERT(VARCHAR(10),getdate(),101)

set @rd = replace(@rd,'/','')



ver 2.

set @rd = replace(CONVERT(VARCHAR(10),COALESCE(@runDate,getdate()),101),'/','')


Coalesce takes in a list of values,  it will return the first non null value.

I also wrapped the replace call around the results of the convert statement.


More on MSDN: COALESCE (Transact-SQL)

Thursday, June 6, 2013

Sorting by last modified date of files







 private static void GetFileDates()
        {
            DirectoryInfo di = new DirectoryInfo(_path);
            FileSystemInfo[] files = di.GetFileSystemInfos();
            var orderedFiles = files.OrderBy(f => f.LastWriteTime);
            FileSystemInfo first = (FileSystemInfo)orderedFiles.First();
            FileSystemInfo last = (FileSystemInfo)orderedFiles.Last();

            _startDate = first.LastWriteTime.ToString();
            _stopDate = last.LastWriteTime.ToString();

           
        }

Friday, May 31, 2013

Bluetooth Chat Screens

For learning I put together a Bluetooth chat client.  I used sample code from the InTheHand examples to get this to work.  

I decided to store devices in a config file instead of trying to discover clients.  The discovery takes too much time on these devices.  Another advantage of storing Bluetooth addresses in a config file is these devices can remain in non discovery mode. 

I can send the source code to anyone interested.  

thanks.





Friday, May 17, 2013

Asynchronous write and read using InTheHand 32Feet Bluetooth serial communications

With the connection all ready opened:


public class MyAsyncInfo
        {
            public Byte[] ByteArray { get; set; }
            public Stream MyStream { get; set; }

            public MyAsyncInfo(Byte[] array, Stream stream)
            {
                ByteArray = array;
                MyStream = stream;
            }
        }

        private void CASMonitor_SendData()
        {
            byte[] data = new byte[4];
            data[0] = 0x58;
            data[1] = 0x30;
            data[2] = 0x39;
            data[3] = 0x0D;


            try
            {
                stream.BeginWrite(data, 0, data.Length, WriteComplete, null);
            }
            catch (Exception ee)
            {
                MessageBox.Show("Error: Sending, Verify Bluetooth Connection");
            }
        }

        private void WriteComplete(IAsyncResult ar)
        {
            try
            {
                stream.EndWrite(ar);

                byte[] DeviceBuffer = new byte[100];
                DeviceBuffer.Initialize();

                Stream DeviceStream = null;
                DeviceStream = btClient.GetStream();
                DeviceStream.Flush();

                DeviceStream.BeginRead(DeviceBuffer, 0, DeviceBuffer.Length, DeviceReadComplete, new MyAsyncInfo(DeviceBuffer, DeviceStream));

            }
            catch (Exception ex)
            {
                MessageBox.Show("write error");
            }
        }

        private void DeviceReadComplete(IAsyncResult r)
        {
            int numbytes = 0;
            MyAsyncInfo info = r.AsyncState as MyAsyncInfo;

            try
            {
                numbytes = info.MyStream.EndRead(r);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Readcomplete ex " + ex.Message);

                return;
            }

            string msg = System.Text.Encoding.Default.GetString(info.ByteArray, 0, numbytes);

            if ((info.ByteArray[0] == 'X') && (info.ByteArray[1] == '0') && (info.ByteArray[2] == '9') && (numbytes == 24))
            {
                ProcessDataProc(msg, numbytes);
            }
            else
            {
                if (_retryCnt < 3)
                {
                    CASMonitor_SendData();
                    return;
                }
            }

            return;
        }