Example #1
0
        public IEnumerator<ITask> GetWindowHandler(GetWindow get)
        {
            if (!_state.Connected)
            {
                get.ResponsePort.Post(new Fault() { Reason = new ReasonText[] { new ReasonText() { Value = "Not connected" } } });
                yield break;
            }

            Fault fault = null;
            yield return Arbiter.Choice(
                _scribblerComPortPost(new ScribblerCommand(
                    ScribblerHelper.Commands.SET_WINDOW,
                    new byte[] {
                        get.Body.Window,
                        get.Body.XLow,
                        get.Body.YLow,
                        get.Body.XHigh,
                        get.Body.YHigh,
                        get.Body.XStep,
                        get.Body.YStep },
                        false, 0)),
                    delegate(ScribblerResponse r) { },
                    delegate(Fault f) { fault = f; });

            int width = ((int)get.Body.XHigh - (int)get.Body.XLow) / get.Body.XStep + 1,
                height = ((int)get.Body.YHigh - (int)get.Body.YLow) / get.Body.YStep + 1,
                size = width * height;
            if (fault == null)
                yield return Arbiter.Choice(
                    _scribblerComPortPost(new ScribblerCommand(
                        ScribblerHelper.Commands.GET_WINDOW,
                        new byte[] { get.Body.Window },
                        false,
                        size)),
                    delegate(ScribblerResponse r)
                    {
                        get.ResponsePort.Post(new ImageResponse()
                        {
                            Width = width,
                            Height = height,
                            Timestamp = DateTime.Now,
                            Data = r.Data
                        });
                    },
                    delegate(Fault f) { fault = f; });

            if (fault != null)
                get.ResponsePort.Post(fault);

            yield break;

        }
Example #2
0
 private void TimersTimer_Elapsed(object sender, ElapsedEventArgs e)
 {
     try
     {
         GetWindow g = new GetWindow("問診");
         // vb.NET里面的vbCr & vbLf 在c# 是\r\n, chr(13) chr(10)
         // 第二個是身分證字號
         //tempID = g.Key.Split('\n')[2];
         tempID = g.Key;
         //string[] ss = tempID.Split('\n');
     }
     catch
     {
         tempID = string.Empty;
     }
     if (StrID == string.Empty)
     {
         if (tempID == string.Empty)
         {
             // condition 1, strID = "" => ""                     do nothing
             return;
         }
         else
         {
             // 檢單查核, 如果分解後數目小於8, 應該就不是正確的
             // 20190930似乎有效
             if (tempID.Split(' ').Length < 8)
             {
                 //'MessageBox.Show("抓到了")
                 return;
             }
             // condition 2, strID = "" => something A            record A, starttime
             // 要做很多事情, 分解
             // 20190930 有些"問診畫面"的狀態,文字是不一樣的,這樣的話會有錯誤
             StrID = tempID;
             Com_clDataContext dc = new Com_clDataContext();
             string[]          s  = strID.Split(' '); //0 SDATE, 1 VIST, 2 RMNO, 4 Nr, 7 uid, 8 cname
             if (s[7].Substring(1, 10) == "A000000000")
             {
                 strID  = string.Empty;
                 StrUID = string.Empty;
                 return;
             }
             StrUID = s[7].Substring(1, 10);  // Propertychanged
             dc.sp_insert_access(DateTime.Parse(s[0]), s[1], byte.Parse(s[2]), byte.Parse(s[4]), strUID, s[8], true);
         }
     }
     else
     {
         if (StrID == tempID)
         {
             // condition 3, strID = something A => something A   do nothing
             return;
         }
         else if (tempID == string.Empty)
         {
             //' condition 4, strID = something A => ""            record endtime, write into database
             //' 做的事情也不少
             Com_clDataContext dc = new Com_clDataContext();
             string[]          s  = strID.Split(' '); //'0 SDATE, 1 VIST, 2 RMNO, 4 Nr, 7 uid, 8 cname
             dc.sp_insert_access(DateTime.Parse(s[0]), s[1], byte.Parse(s[2]), byte.Parse(s[4]), strUID, s[8], false);
             StrID  = tempID;
             StrUID = string.Empty;  // Propertychanged
         }
         else
         {
             //' condition 5, strID = something A => something B   I don't know if this is possible
             //' 有可能嗎? 我不知道
             //' 20191001 答案揭曉了,有可能,因為THESIS在畫form時會有A000000000臨時的資料,然後再讀資料庫蓋上,就會出現something A => something B的情況
             //' 我採用檢核若A000000000的情形就不要寫入的方式處理
             //' 檢單查核, 如果分解後數目小於8, 應該就不是正確的
             //'                MessageBox.Show("抓到了! " + vbCrLf + strID + "=>" + tempID)
             if (tempID.Split(' ').Length < 8)
             {
                 return;
             }
         }
     }
 }
Example #3
0
        public IEnumerator<ITask> GetImageHandler(GetImage get)
        {
            if (!_state.Connected)
            {
                get.ResponsePort.Post(new Fault() { Reason = new ReasonText[] { new ReasonText() { Value = "Not connected" } } });
                yield break;
            }

            MyroImageType imageType = null;
            byte[] responseData = null;
            Fault fault = null;
            // Retrieve image data based on image type
            if (get.Body.ImageType.Equals(MyroImageType.Color.Guid))
            {
                imageType = MyroImageType.Color;
                yield return Arbiter.Choice(_scribblerComPortPost(
                    new ScribblerCommand(ScribblerHelper.Commands.GET_IMAGE)),
                    delegate(ScribblerResponse r) { responseData = r.Data; },
                    delegate(Fault f) { fault = f; });
            }
            else if (get.Body.ImageType.Equals(MyroImageType.Gray.Guid))
            {
                imageType = MyroImageType.Gray;
                var gw = new GetWindow(new GetWindowBody()
                {
                    Window = 0,
                    XLow = 1,
                    YLow = 0,
                    XHigh = 255,
                    YHigh = 191,
                    XStep = 2,
                    YStep = 1
                });
                _mainPort.PostUnknownType(gw);
                yield return Arbiter.Choice(gw.ResponsePort,
                    delegate(ImageResponse r)
                    {
                        if (r.Data.Length != 128 * 192)
                            fault = RSUtils.FaultOfException(new Exception("Invalid grayscale image from GetWindow"));
                        else
                        {
                            // Scale image up and copy bits
                            Bitmap bmp = new Bitmap(128, 192, PixelFormat.Format8bppIndexed);
                            bmp.Palette = grayscalePallette;
                            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, 128, 192), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                            System.Runtime.InteropServices.Marshal.Copy(r.Data, 0, bmpData.Scan0, 128 * 192);
                            bmp.UnlockBits(bmpData);
                            Bitmap bmp2 = new Bitmap(256, 192, PixelFormat.Format24bppRgb);
                            Graphics g = Graphics.FromImage(bmp2);
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                            g.DrawImage(bmp, 0, 0, 256, 192);
                            BitmapData bmpData2 = bmp2.LockBits(new Rectangle(0, 0, 256, 192), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                            responseData = new byte[256 * 192 * 3];
                            System.Runtime.InteropServices.Marshal.Copy(bmpData2.Scan0, responseData, 0, 256 * 192 * 3);
                            bmp2.UnlockBits(bmpData2);
                            bmp.Dispose();
                            bmp2.Dispose();
                            g.Dispose();
                        }
                    },
                    delegate(Fault f) { fault = f; });
            }
            else if (get.Body.ImageType.Equals(MyroImageType.JpegColor.Guid) ||
                get.Body.ImageType.Equals(MyroImageType.JpegColorFast.Guid))
            {
                imageType = MyroImageType.JpegColor;

                // Get Header
                if (cachedJpegHeaderColor == null)
                {
                    // NOTE: Hack here, in ScribblerComm.SendCommand, if the command is to
                    // get a JPEG header, it will get the header length from the first two
                    // bytes from the serial port.
                    yield return Arbiter.Choice(_scribblerComPortPost(new ScribblerCommand(
                        ScribblerHelper.Commands.GET_JPEG_COLOR_HEADER)),
                        delegate(ScribblerResponse r) { cachedJpegHeaderColor = r.Data; },
                        delegate(Fault f) { fault = f; });
                    //if (fault == null)
                    //    Console.WriteLine("JPEG: header is " + cachedJpegHeaderColor.Length + " bytes");
                }

                // Get scans
                byte[] scans = null;
                if (fault == null)
                {
                    byte reliable = get.Body.ImageType.Equals(MyroImageType.JpegColor.Guid) ? (byte)1 : (byte)0;
                    yield return Arbiter.Choice(_scribblerComPortPost(new ScribblerCommand(
                        ScribblerHelper.Commands.GET_JPEG_COLOR_SCAN, reliable) { EndMarker1 = 0xff, EndMarker2 = 0xd9 }),
                        delegate(ScribblerResponse r) { scans = r.Data; },
                        delegate(Fault f) { fault = f; });
                }
                //if (fault == null)
                //    Console.WriteLine("JPEG: scans is " + scans.Length + " bytes");

                // Decode JPEG
                if (fault == null)
                {
                    if (cachedJpegHeaderColor == null || scans == null)
                        fault = RSUtils.FaultOfException(new ScribblerDataException("Had null header or scans in GetImageHandler for jpeg"));
                    else
                    {
                        try
                        {
                            byte[] jpeg = new byte[cachedJpegHeaderColor.Length - 2 + scans.Length];
                            Array.Copy(cachedJpegHeaderColor, 2, jpeg, 0, cachedJpegHeaderColor.Length - 2);
                            Array.Copy(scans, 0, jpeg, cachedJpegHeaderColor.Length - 2, scans.Length);

                            // Create Bitmap from jpeg and scale (jpegs from Fluke are half-width)
                            Bitmap obmp = new Bitmap(new MemoryStream(jpeg));
                            Bitmap bmp = new Bitmap(imageType.Width, imageType.Height, PixelFormat.Format24bppRgb);
                            Graphics g = Graphics.FromImage(bmp);
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                            g.DrawImage(obmp, 0, 0, imageType.Width, imageType.Height);
                            obmp.Dispose();
                            g.Dispose();

                            if (bmp.Width != imageType.Width || bmp.Height != imageType.Height ||
                                    bmp.PixelFormat != PixelFormat.Format24bppRgb)
                                throw new ScribblerDataException("Bitmap from JPEG had wrong size or format");

                            // Copy frame
                            //Console.WriteLine("Copying frame...");
                            BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                            responseData = new byte[imageType.Width * imageType.Height * 3];
                            System.Runtime.InteropServices.Marshal.Copy(
                                bitmapData.Scan0, responseData, 0, imageType.Width * imageType.Height * 3);
                            bmp.UnlockBits(bitmapData);
                            //Console.WriteLine("JPEG done");
                        }
                        catch (Exception e)
                        {
                            fault = RSUtils.FaultOfException(e);
                        }

                    }
                }
            }
            else if (get.Body.ImageType.Equals(MyroImageType.JpegGray.Guid) ||
                get.Body.ImageType.Equals(MyroImageType.JpegGrayFast.Guid))
            {
                imageType = MyroImageType.JpegGray;

                // Get Header
                if (cachedJpegHeaderGray == null)
                {
                    // NOTE: Hack here, in ScribblerComm.SendCommand, if the command is to
                    // get a JPEG header, it will get the header length from the first two
                    // bytes from the serial port.
                    yield return Arbiter.Choice(_scribblerComPortPost(new ScribblerCommand(
                        ScribblerHelper.Commands.GET_JPEG_GRAY_HEADER)),
                        delegate(ScribblerResponse r) { cachedJpegHeaderGray = r.Data; },
                        delegate(Fault f) { fault = f; });
                    //if (fault == null)
                    //    Console.WriteLine("JPEG: header is " + cachedJpegHeaderGray.Length + " bytes");
                }

                // Get scans
                byte[] scans = null;
                if (fault == null)
                {
                    byte reliable = get.Body.ImageType.Equals(MyroImageType.JpegGray.Guid) ? (byte)1 : (byte)0;
                    yield return Arbiter.Choice(_scribblerComPortPost(new ScribblerCommand(
                        ScribblerHelper.Commands.GET_JPEG_GRAY_SCAN, reliable) { EndMarker1 = 0xff, EndMarker2 = 0xd9 }),
                        delegate(ScribblerResponse r) { scans = r.Data; },
                        delegate(Fault f) { fault = f; });
                }
                //if (fault == null)
                //    Console.WriteLine("JPEG: scans is " + scans.Length + " bytes");

                // Decode JPEG
                if (fault == null)
                {
                    if (cachedJpegHeaderGray == null || scans == null)
                        fault = RSUtils.FaultOfException(new ScribblerDataException("Had null header or scans in GetImageHandler for jpeg"));
                    else
                    {
                        try
                        {
                            byte[] jpeg = new byte[cachedJpegHeaderGray.Length - 2 + scans.Length];
                            Array.Copy(cachedJpegHeaderGray, 2, jpeg, 0, cachedJpegHeaderGray.Length - 2);
                            Array.Copy(scans, 0, jpeg, cachedJpegHeaderGray.Length - 2, scans.Length);

                            // Create Bitmap from jpeg and scale (jpegs from Fluke are half-width)
                            Bitmap obmp = new Bitmap(new MemoryStream(jpeg));
                            Bitmap bmp = new Bitmap(imageType.Width, imageType.Height, PixelFormat.Format24bppRgb);
                            Graphics g = Graphics.FromImage(bmp);
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                            g.DrawImage(obmp, 0, 0, imageType.Width, imageType.Height);
                            obmp.Dispose();
                            g.Dispose();

                            if (bmp.Width != imageType.Width || bmp.Height != imageType.Height ||
                                    bmp.PixelFormat != PixelFormat.Format24bppRgb)
                                throw new ScribblerDataException("Bitmap from JPEG had wrong size or format");

                            // Copy frame
                            BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                            responseData = new byte[imageType.Width * imageType.Height * 3];
                            System.Runtime.InteropServices.Marshal.Copy(
                                bitmapData.Scan0, responseData, 0, imageType.Width * imageType.Height * 3);
                            bmp.UnlockBits(bitmapData);
                        }
                        catch (Exception e)
                        {
                            fault = RSUtils.FaultOfException(e);
                        }

                    }
                }
            }
            else
            {
                fault = RSUtils.FaultOfException(
                        new ArgumentException("Invalid image type: " + get.Body.ImageType, "ImageType"));
            }

            // Send the image response
            if (fault == null)
            {
                if (responseData != null && imageType != null)
                    get.ResponsePort.Post(new ImageResponse()
                        {
                            Width = imageType.Width,
                            Height = imageType.Height,
                            Timestamp = DateTime.Now,
                            Data = responseData
                        });
                else
                    get.ResponsePort.Post(RSUtils.FaultOfException(new Exception("Internal error: in ScribblerBase.GetImageHandler")));
            }
            else
                get.ResponsePort.Post(fault);

            yield break;
        }