aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operation/StorageManager.cs
blob: f6b7e8d281385007702230b3a26d96d2b276c2ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Logging;
using Tango.PMR.IO;

namespace Tango.Integration.Operation
{
    public class StorageManager : ExtendedObject
    {
        private IMachineOperator _operator;

        public StorageManager(IMachineOperator machineOperator)
        {
            _operator = machineOperator;
        }

        private void EnsureOperator()
        {
            if (_operator.State != Transport.TransportComponentState.Connected)
            {
                throw new InvalidOperationException("Error executing storage command. Machine operator is not connected.");
            }
        }

        #region Private Methods

        /// <summary>
        /// Logs the request sent.
        /// </summary>
        /// <param name="message">The message.</param>
        protected void LogRequestSent(IMessage message)
        {
            LogManager.Log(String.Format("Sending request '{0}'...{1}{2}", message.GetType().Name, Environment.NewLine, message.ToJsonString()));
        }

        /// <summary>
        /// Logs the request failed.
        /// </summary>
        /// <param name="message">The message.</param>
        protected void LogRequestFailed(IMessage message, Exception ex)
        {
            LogManager.Log(String.Format("Request failed '{0}'...{1}{2}{1}{3}", message.GetType().Name, Environment.NewLine, message.ToJsonString(), ex.ToString()), LogCategory.Error);
        }

        /// <summary>
        /// Logs the response received.
        /// </summary>
        /// <param name="message">The message.</param>
        protected void LogResponseReceived(IMessage message)
        {
            LogManager.Log(String.Format("Response received '{0}'...{1}{2}", message.GetType().Name, Environment.NewLine, message.ToJsonString()));
        }

        #endregion

        public async Task Create(FileAttribute attr, String path, String name)
        {
            EnsureOperator();

            CreateResponse response = null;
            CreateRequest request = new CreateRequest()
            {
                Attribute = attr,
                Path = path + "/" + name,
            };

            try
            {
                response = await _operator.SendRequest<CreateRequest, CreateResponse>(request);
            }
            catch (Exception ex)
            {
                LogRequestFailed(request, ex);
                throw ex;
            }
        }

        public async Task Delete(String path)
        {
            EnsureOperator();

            DeleteResponse response = null;
            DeleteRequest request = new DeleteRequest()
            {
                Path = path,
            };

            try
            {
                response = await _operator.SendRequest<DeleteRequest, DeleteResponse>(request);
            }
            catch (Exception ex)
            {
                LogRequestFailed(request, ex);
                throw ex;
            }
        }

        public async Task<GetStorageInfoResponse> GetStorageInfo()
        {
            EnsureOperator();

            GetStorageInfoResponse response = null;
            GetStorageInfoRequest request = new GetStorageInfoRequest();

            try
            {
                response = await _operator.SendRequest<GetStorageInfoRequest, GetStorageInfoResponse>(request);
            }
            catch (Exception ex)
            {
                LogRequestFailed(request, ex);
                throw ex;
            }

            return response;
        }

        public async Task<List<FileInfo>> GetFiles(String path)
        {
            EnsureOperator();

            GetFilesResponse response = null;
            GetFilesRequest request = new GetFilesRequest();

            try
            {
                response = await _operator.SendRequest<GetFilesRequest, GetFilesResponse>(request);
            }
            catch (Exception ex)
            {
                LogRequestFailed(request, ex);
                throw ex;
            }

            return response.Items.ToList();
        }
    }
}