# GST.IoT.Library

## 종속성

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

## 주의 사항

* [**반드시 첨부 번호(attdatnum 필드)에 대한 개념을 이해하고 적용할 것**](https://youngtae.gitbook.io/gst_platform/file-attachment#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="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUqUmOUkvgPZZji2-Y9%2F-MUqVLR_rPNU6jtCj9mV%2Fimage.png?alt=media&#x26;token=a87b6071-8907-426c-af58-520a666eeab1" alt=""></div>

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

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

<div align="left"><img src="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUpxfDHizZBumsd9bcE%2F-MUq-QXBEYeqYuATGGvu%2Fimage.png?alt=media&#x26;token=f9a71217-6720-4106-8fa3-32cf9d614010" alt=""></div>

## Example 1 - 파일 다운로드

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

<div align="left"><img src="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUq0kU95S5LP0rPpgEF%2F-MUqHRjFOzkHRh6mXx6S%2Fimage.png?alt=media&#x26;token=36900884-6eb8-4217-8880-82c52b04118a" 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="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUq0kU95S5LP0rPpgEF%2F-MUqMBryIG2J1_hAq6Ns%2Fimage.png?alt=media&#x26;token=54101894-e342-49e2-b5dd-df94d8b7e8fe" alt=""></div>

<div align="left"><img src="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUq0kU95S5LP0rPpgEF%2F-MUqO-BFg_sd0LGrOUa2%2Fimage.png?alt=media&#x26;token=97a04e8e-714a-416c-ad02-00e6873115d0" alt=""></div>

<div align="left"><img src="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUq0kU95S5LP0rPpgEF%2F-MUqO7T7diERYsUp8jhV%2Fimage.png?alt=media&#x26;token=05884d10-a045-421d-98e6-2992b4df55ff" alt=""></div>

## 배포

<div align="left"><img src="https://3723764299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-IDnDQ8xPzEIgiFeXT%2F-MUqp6iU6A9q4srpKaqb%2F-MUqpk1BcTcTC5a3nNcP%2Fimage.png?alt=media&#x26;token=b2d65adf-f543-4035-8e35-398c56945715" alt=""></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://youngtae.gitbook.io/gst_platform/library/gst.iot.library.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
