> For the complete documentation index, see [llms.txt](https://youngtae.gitbook.io/gst_platform/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://youngtae.gitbook.io/gst_platform/library/gst.iot.library.md).

# GST.IoT.Library

## 종속성

* .NET Framework 4.6.1 이상
* GST.Platform.Client.Library.dll
* GST.Platform.ServiceInterface.dll

## 주의 사항

* [**반드시 첨부 번호(attdatnum 필드)에 대한 개념을 이해하고 적용할 것**](/gst_platform/library/file-attachment.md#class-attachmenthelper-ftpattachmenthelper)

## ServiceInterface class

### \[ Constructors ]

```csharp
public ServiceInterface(Uri serviceUri, string internalIP);
```

### \[ Properties ]

### ServiceUri

* 생성자를 통해 할당된 웹 서비스 URI

```csharp
public Uri ServiceUri { get; }
```

### InternalIP

* 생성자를 통해 할당된 웹 서비스 URI의 내부 IP

```csharp
public string InternalIP { get; }
```

### \[ Methods ]

### FileExists

* **서버의 파일 첨부 루트 디렉터리로부터** 경로(string filename)에 파일 존재 여부 반환
* Parameters
  * `string filename` : 파일 경로 (서버 기준)
* Returns
  * `bool` : 존재하면 `true`, 아니면 `false`

```csharp
public bool FileExists(string filename);
```

### FileUpload

* **서버의 파일 첨부 루트 디렉터리로부터** 경로(string filename)에 파일을 업로드
* Parameters
  * `string filename` : 파일 경로 (서버 기준)
  * `byte[] fileBytes` : 업로드할 파일의 이진 데이터
  * `bool isOverWrite` : 덮어쓰기 여부 (덮어쓰기를 하지 않을때 중복된 이름의 파일이 존재하면 이름을 다르게 생성)
* Returns
  * `string[]`&#x20;
    * \[0] : 업로드 성공 여부 ("OK" or Exception.Message)
    * \[1] : 업로드 파일명 (업로드 실패시 Exception.ToString() 값)

```csharp
public string[] FileUpload(string filename, byte[] fileBytes, bool isOverWrite);
```

### FileDownload

* **서버의 파일 첨부 루트 디렉터리로부터** 경로(string filename)의 파일을 다운로드
* Parameters
  * `string filename` : 파일 경로 (서버 기준)
* Returns
  * `byte[]` : 다운로드 받은 파일의 이진 데이터, 파일이 존재하지 않으면 `null`

```csharp
public byte[] FileDownload(string filename);
```

### FileDelete

* **서버의 파일 첨부 루트 디렉터리로부터** 경로(string filename)의 파일을 제
* Parameters
  * `string filename` : 파일 경로 (서버 기준)
* Returns
  * `string[]`&#x20;
    * \[0] : 삭제 성공 여부("OK" or "NO" or Exception.Message)
    * \[1] : 삭제된 파일명

```csharp
public string[] FileDelete(string filename);
```

### GetData

* 파일 첨부 번호에 대한 첨부 정보를 반환
* Parameters
  * `string attdatnum` : 파일 첨부 번호
* Returns
  * `DataTable` : 전달 받은 첨부 번호(attdatnum)에 대한 조회 결과

```csharp
public DataTable GetData(string attdatnum);
```

### SaveAndUpload

* 클라이언트 파일 경로를 읽고, **첨부 번호에 대한 데이터 처리 후 서버에 파일 업로드**
* Parameters
  * `string[] filePaths` : 업로드할 파일의 경로 (클라이언트 기준의 파일 FullName)
  * `ref string attdatnum` : 파일 첨부 번호 (이미 생성된 첨부 번호에 파일을 추가하려면 유효한 값을 전달하고, 새로운 첨부 번호를 생성하려면 빈 값을 전달)
  * `out string[] savedFileNames` : 서버에 업로드된 파일명 (savenm 필드의 값, 전달한 클라이언트 파일 경로 개수만큼 반환)
* Returns
  * `bool` : 정상 처리 여부

```csharp
public bool SaveAndUpload(string[] filePaths, ref string attdatnum, out string[] savedFileNames);
```

### DeleteAttachments

* 파일 첨부 번호로 등록된 첨부 정보와 서버에서 파일을 제거
* Parameters
  * `string attdatnum` : 파일 첨부 번호
* Returns
  * `bool` : 정상 처리 여부

```csharp
public bool DeleteAttachments(string attdatnum);
```

### GetServerDateTime

* WAS(Web Application Server)의 일시(`DateTime`)를 반환
* Returns
  * `DateTime` : 현재 일시

```csharp
public DateTime GetServerDateTime();
```

## Setting

### 어셈블리 참조

* **GST.IoT.Library.dll**  : 따로 다운로드 필요

```bash
# 외부 IP
git clone http://gst@222.96.157.69/git_fa_team/ReferenceAssemblies.git

# 내부 IP
git clone http://gst@192.168.10.69/git_fa_team/ReferenceAssemblies.git
```

* **GST.Platform.Client.Library.dll**  : GSTBrowser가 설치된 폴더 내 위치 (기본 경로: C:\GSTBrowser)
* **GST.Platform.ServiceInterface.dll**  : GSTBrowser가 설치된 폴더 내 위치 (기본 경로: C:\GSTBrowser)

<div align="left"><img src="/files/-MUqVLR_rPNU6jtCj9mV" alt=""></div>

### **ServiceInterface 타입의 객체를 필드 멤버로 정의**

* 웹 서비스 URI과 내부 IP정보는 주영태 대리한테 문의
* [ServiceInterface 생성자](/gst_platform/library/gst.iot.library.md#constructors)

<div align="left"><img src="/files/-MUq-QXBEYeqYuATGGvu" alt=""></div>

## Example 1 - 파일 다운로드

* 다음과 같이 저장된 첨부 정보를 예시로 작성된 코드이며, 리터널로 처리된 부분은 프로그램 컨셉에 따라 적절하게 응용 필요
* [Util 정적 클래스](/gst_platform/library/util.md)

<div align="left"><img src="/files/-MUqHRjFOzkHRh6mXx6S" alt=""></div>

```csharp
private void OnClick(object sender, EventArgs e)
{
    string path = Path.Combine(Util.GetTempDirectory(), "캡처.PNG");

    DownloadFile("20210208000020001", path);
}

private void DownloadFile(string savenm, string path)
{
    string serverPath = Path.Combine(AttachmentHelper.DefaultUploadDirectory, savenm);
        
    byte[] fileBytes = _ServiceInterface.FileDownload(serverPath);

    File.WriteAllBytes(path, fileBytes);
}
```

## Example 2 - 파일 업로드

* 하나 이상의 파일을 선택하여 DB에 첨부 정보를 저장함과 동시에 서버에 파일을 업로드하고, 그 결과를 MessageBox를 통해 출력하는 예시
* **SaveAndUpload 메서드의 결과(첨부번호:attdatnum, 저장파일명:savedFileNames)를 필요에 의해 응용하여 프로그램 개발할 것**

```csharp
private void OnClick(object sender, EventArgs e)
{
    if (sender.Equals(btnDownload))
    {
        string path = Path.Combine(Util.GetTempDirectory(), "캡처.PNG");

        DownloadFile("20210208000020001", path);
    }
    else if (sender.Equals(btnUpload))
    {
        UploadFiles();
    }
}

private void UploadFiles()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = true;

    if (ofd.ShowDialog() != DialogResult.OK)
        return;

    string attdatnum = string.Empty;
    string[] savedFileNames = null;

    if (_ServiceInterface?.SaveAndUpload(ofd.FileNames, ref attdatnum, out savedFileNames) ?? false)
    {
        // 저장 성공시 파일 정보 출력 // 
        if ((savedFileNames?.Length ?? 0) != 0)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < savedFileNames.Length; i++)
            {
                if (i > 0)
                    sb.AppendLine();
                sb.Append($"savenm : {savedFileNames[i] ?? string.Empty}");
            }

            MessageBox.Show(sb.ToString(), $"attdatnum : {attdatnum}");
        }
    }
}

```

### 결과

<div align="left"><img src="/files/-MUqMBryIG2J1_hAq6Ns" alt=""></div>

<div align="left"><img src="/files/-MUqO-BFg_sd0LGrOUa2" alt=""></div>

<div align="left"><img src="/files/-MUqO7T7diERYsUp8jhV" alt=""></div>

## 배포

<div align="left"><img src="/files/-MUqpk1BcTcTC5a3nNcP" alt=""></div>
