2014年10月23日 星期四

診間報到系統-3(FileSystemWatcher)

由於診間報到系統,護士瑞及電視牆兩隻程式需要交換資料去觸發畫面更新事件。過去,常用timer設定long loop的方式來取得特定目錄的檔案更新,現在則打算用FileSystemWatcher的事件觸發來執行。一方面提昇反應時間,另一方面也是自我提昇。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ' 1.引用類別
 Dim watcher As New FileSystemWatcher()
 ' 2.設定監聽路徑
 watcher.Path = "C:\Temp"
 ' 3.過濾監聽條件
 watcher.Filter = "*.txt"
 ' 4.設定監看類型
 watcher.NotifyFilter = NotifyFilters.FileName
 ' 5.設定監聽項目
 AddHandler watcher.Created, AddressOf watcher_Created
 ' 6.開啟監聽事件
 watcher.EnableRaisingEvents = True
End Sub

Private Shared locker As Object = New [Object]()
Private Sub watcher_Created(ByVal sender As Object, ByVal e As FileSystemEventArgs)
 SyncLock locker
  Dim file As New FileInfo(e.FullPath)
            Dim intCounter As Integer = 0
            Do While file.IsLocked()
                System.Threading.Thread.Sleep(100)
                intCounter += 1
                If intCounter >= 100 Then Exit Sub
            Loop
   
   string readText = file.ReadAllText();
 End SyncLock
End Sub

Module clsExtension

    <Extension()> _
    Public Function IsLocked(ByVal file As FileInfo) As Boolean
        Dim stream As FileStream = Nothing

        Try
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
        Catch ex_IsLocked As IOException
            'the file is unavailable because it is:
            'still being written to
            'or being processed by another thread
            'or does not exist (has already been processed)
            Return True
        Finally
            If stream IsNot Nothing Then
                stream.Close()
            End If
        End Try

        'file is not locked
        Return False
    End Function

    <Extension()> _
    Public Function ReadAllText(ByVal file As FileInfo) As String
        Try
            Using reader As StreamReader = file.OpenText()
                Return reader.ReadToEnd()
            End Using
        Catch ex_ReadAllText As Exception
            Return Nothing
        End Try
    End Function

End Module

參考資料
[1] [VB.NET][C#.NET] 使用 FileSystemWatcher 監看資料夾 是否被變更
[2] Wait Until File Is Completely Written

2014年10月21日 星期二

健保e化抽審-1(PDF Merge & Bookmark)

private void button17_Click(object sender, EventArgs e)
{
  Dictionary<PrintDocument, byte[]> myFiles = new Dictionary<PrintDocument, byte[]>();

  myFiles.Add(new PrintDocument { Title = "title1", ChapterName = "chapterName1", ChapterNumber = 1 }, File.ReadAllBytes(@"C:\temp\title1.pdf"));
  myFiles.Add(new PrintDocument { Title = "title2", ChapterName = "chapterName1", ChapterNumber = 1 }, File.ReadAllBytes(@"C:\temp\title2.pdf"));
  myFiles.Add(new PrintDocument { Title = "title3", ChapterName = "chapterName1", ChapterNumber = 1 }, File.ReadAllBytes(@"C:\temp\title3.pdf"));

  File.WriteAllBytes(@"C:\temp\test.pdf", MergeFilesAndAddBookmarks(myFiles));
}

public static byte[] MergeFilesAndAddBookmarks(Dictionary<PrintDocument, byte[]> sourceFiles)
{
  using (var ms = new MemoryStream())
  {
    using (var document = new iTextSharp.text.Document())
    {
      using (var copy = new iTextSharp.text.pdf.PdfCopy(document, ms))
      {
        //Order the files by chapternumber
        var files = sourceFiles.GroupBy(f => f.Key.ChapterNumber);

        document.Open();
        
        var outlines = new List<Dictionary<string, object>>();

        var pageIndex = 1;

        foreach (var chapterGroup in files)
        {
          var map = new Dictionary<string, object>();
          outlines.Add(map);
          map.Add("Title", chapterGroup.First().Key.ChapterName);
          var kids = new List<Dictionary<string, object>>();
          map.Add("Kids", kids);

          foreach (var sourceFile in chapterGroup)
          {
            using (var reader = new iTextSharp.text.pdf.PdfReader(sourceFile.Value))
            {
              //Add the pages
              var n = reader.NumberOfPages;

              for (var page = 0; page < n;)
              {
                if (page == 0)
                {
                  var kid = new Dictionary<string, object>();
                  kids.Add(kid);
                  kid["Title"] = sourceFile.Key.Title;
                  kid["Action"] = "GoTo";
                  kid["Page"] = String.Format("{0} Fit", pageIndex);
                }
                copy.AddPage(copy.GetImportedPage(reader, ++page));
              }

              pageIndex += n;
              reader.Close();
            }
          }
        }

        copy.Outlines = outlines;
        document.Close();
        copy.Close();
        ms.Close();
      }
    }
    return ms.ToArray();
  }
}

public class PrintDocument
{
  public string Title { get; set; }
  public string ChapterName { get; set; }
  public int ChapterNumber { get; set; }
}
參考資料
[1] 使用ASP .NET (C#) 產生PDF檔的好幫手—iTextSharp library (上)
[2] 使用ASP .NET (C#) 產生PDF檔的好幫手—iTextSharp library (下)

2014年10月19日 星期日

[MobileWeb]網路掛號Prototype-2(Listview Autodivider搭配JSON)

HTML更新如下
回上頁

查詢時間

JavaScript
$(document).ready(function () {
    var jsonData = [
        {"醫師": "柯X哲", "科別": "外科", "時段": "上午(08:30~11:30)"},
        {"醫師": "邱X逹", "科別": "外科", "時段": "下午(13:30~17:00)"},
        {"醫師": "沈X雄", "科別": "內科", "時段": "上午(08:30~11:30)"},
        {"醫師": "賴X德", "科別": "內科", "時段": "上午(08:30~11:30)"},
        {"醫師": "柯X銘", "科別": "牙科", "時段": "上午(08:30~11:30)"},
        {"醫師": "涂X哲", "科別": "內科", "時段": "夜診(08:30~11:30)"}
    ];
    
    var targetList = $("#查詢時間List");
    
    if (jsonData) {
        var dataLength = jsonData.length;
        for (var i = 0; i < dataLength; i++) {
            targetList.append("
  • " + jsonData[i].醫師 + "

    " + jsonData[i].科別 + "

  • "); } } targetList.listview({ autodividers: true, autodividersSelector: function ( li ) { return li.data("時段"); } }).listview("refresh"); });

    ※ 原始碼及執行預覽(jsFiddle): http://jsfiddle.net/ez0brabf/11/

    2014年10月15日 星期三

    [MobileWeb]網路掛號Prototype-1(jQuery Mobile 框架)

    我們運用jQuery Mobile,開發以行動裝置所使用的MobileWeb,不但可以簡化及加速UI的設計與開發,專注於程式功能及流程上的開發及整合,也可以減少技能重新學習的成本。

    基本的原型的框架如下~
    HTML
    
    
    
    回上頁

    查詢科別

    回上頁

    查詢時間

    Page content goes here.

    Page Footer

    回上頁

    查詣 / 取消掛號

    Page content goes here.

    Page Footer

    回上頁

    語音掛號

    Page content goes here.

    Page Footer


    CSS
    .noshadow * {
      -webkit-box-shadow: none !important;
      -moz-box-shadow: none !important;
      box-shadow: none !important;
    }
    

    ※ 原始碼及執行預覽(jsFiddle): http://jsfiddle.net/ez0brabf/8/

    2014年10月10日 星期五

    設定SyntaxHighlighter讓Blogger可以貼上程式碼

    最近開始在Blogger上貼程式碼,但又希望它呈現的方式跟一般IDE介面一樣,Google了許久,最後選定了SyntaxHighlighter做為解決方案。參考官網上的安裝方式,開啟Blogger的範本,按下編輯HTML~

    將以下程式碼加入在"</head>"之前
    將以下程式碼加入在"</body>"之前
    
    
    

    建立新文章時以HTML頁面鍵入"<pre></pre>",中間貼入置你的程式碼,以下是我常用的類型~
    類型1:
    <pre class="brush: csharp">
    ...your code
    </pre>

    類型2:
    <pre class="brush: vbnet">
    ...your code
    </pre>

    類型3:
    <script type="syntaxhighlighter" class="brush: javascript; html-script: true"><![CDATA[
    ...your code
    ]]></script>

    類型4:
    <pre class="brush: javascript; html-script: true">
    ...your code
    </pre>

    2014年10月9日 星期四

    [Security Alert: Apache Cordova vulnerabilities in your Google Play app]在PhoneGap找不到cordova-3.5.1.jar

    我的第一個Android APP上架後就再沒有更新它,沒想到Google寄了因為安全問題必須升級Apache Cordova的一封信給我。快二年沒有研究它了,沒想到已經不太一樣!只是需要3.5.1的Apache Cordova就搞了好幾天,還是記錄一下免得忘光光。

    一、作業系統環境需求
    1.1. JDK 6(設定 Java 環境)
    1.1.1. 安裝jdk-6u45-windows-i586.exe
    1.1.2. 設定Path(加入C:\Program Files\Java\jdk1.6.0_45\bin)




     1.1.3. 設定JAVA_HOME(C:\Program Files\Java\jdk1.6.0_45)


    1.2 Apache Ant 1.8 or later
    (Eclipse ADT Bundle已內建,待會將路徑加入Path)

    1.3 解壓Eclipse ADT Bundle
    (D:\My Apps\adt-bundle-windows-x86-20140702)

    1.4. 設定Path,加入以下路徑
    D:\My Apps\adt-bundle-windows-x86-20140702\sdk\tools
    D:\My Apps\adt-bundle-windows-x86-20140702\sdk\platform-tools
    D:\My Apps\adt-bundle-windows-x86-20140702\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\bin


    1.5. 設定Android SDK Manager


    1.6 設定Android Virtual Device (AVD) Manager


    二、安裝Apache Cordova
    2.1. 安裝Node.js(http://nodejs.org/)

    2.2. 安裝Git(http://git-scm.com/)


    2.3. 用npm(Node.js Package Manager)下載Apache Cordova
    npm install -g cordova

    三、制作cordova.jar及更新cordova.js
    3.1. 建立新專案
    cd D:\workspace
    cordova create YGHApp tw.com.ygh.yghapp YGHApp

    3.2 加入作業系統平台
    cd D:\workspace\YGHApp
    cordova platform add android
    cordova platform ls

    3.3制作jar檔
    cd C:\Users\HPC\.cordova\lib\android\cordova\3.5.1\framework
    android update project -p .
    ant jar

    四、參考資料
    [1] Upgrading Cordova Android
    [2] Upgrading from 2.9 to 3.x