aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/PMR/ThreadLoading/StartThreadLoadingResponse.pb-c.c
blob: dbe90df25880b4c6f32ffd93cf93e56c78b0c0d3 (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
/* Generated by the protocol buffer compiler.  DO NOT EDIT! */
/* Generated from: StartThreadLoadingResponse.proto */

/* Do not generate deprecated warnings for self */
#ifndef PROTOBUF_C__NO_DEPRECATED
#define PROTOBUF_C__NO_DEPRECATED
#endif

#include "StartThreadLoadingResponse.pb-c.h"
void   start_thread_loading_response__init
                     (StartThreadLoadingResponse         *message)
{
  static const StartThreadLoadingResponse init_value = START_THREAD_LOADING_RESPONSE__INIT;
  *message = init_value;
}
size_t start_thread_loading_response__get_packed_size
                     (const StartThreadLoadingResponse *message)
{
  assert(message->base.descriptor == &start_thread_loading_response__descriptor);
  return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t start_thread_loading_response__pack
                     (const StartThreadLoadingResponse *message,
                      uint8_t       *out)
{
  assert(message->base.descriptor == &start_thread_loading_response__descriptor);
  return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t start_thread_loading_response__pack_to_buffer
                     (const StartThreadLoadingResponse *message,
                      ProtobufCBuffer *buffer)
{
  assert(message->base.descriptor == &start_thread_loading_response__descriptor);
  return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
StartThreadLoadingResponse *
       start_thread_loading_response__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data)
{
  return (StartThreadLoadingResponse *)
     protobuf_c_message_unpack (&start_thread_loading_response__descriptor,
                                allocator, len, data);
}
void   start_thread_loading_response__free_unpacked
                     (StartThreadLoadingResponse *message,
                      ProtobufCAllocator *allocator)
{
  if(!message)
    return;
  assert(message->base.descriptor == &start_thread_loading_response__descriptor);
  protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
static const ProtobufCFieldDescriptor start_thread_loading_response__field_descriptors[4] =
{
  {
    "",
    1,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_ENUM,
    offsetof(StartThreadLoadingResponse, has_state),
    offsetof(StartThreadLoadingResponse, state),
    &thread_loading_state__descriptor,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
  {
    "",
    2,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_STRING,
    0,   /* quantifier_offset */
    offsetof(StartThreadLoadingResponse, errorreason),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
  {
    "",
    3,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_DOUBLE,
    offsetof(StartThreadLoadingResponse, has_progresspercentage),
    offsetof(StartThreadLoadingResponse, progresspercentage),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
  {
    "",
    4,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_STRING,
    0,   /* quantifier_offset */
    offsetof(StartThreadLoadingResponse, message),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
};
static const unsigned start_thread_loading_response__field_indices_by_name[] = {
  1,   /* field[1] = ErrorReason */
  3,   /* field[3] = Message */
  2,   /* field[2] = ProgressPercentage */
  0,   /* field[0] = State */
};
static const ProtobufCIntRange start_thread_loading_response__number_ranges[1 + 1] =
{
  { 1, 0 },
  { 0, 4 }
};
const ProtobufCMessageDescriptor start_thread_loading_response__descriptor =
{
  PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
  "",
  "",
  "",
  "",
  sizeof(StartThreadLoadingResponse),
  4,
  start_thread_loading_response__field_descriptors,
  start_thread_loading_response__field_indices_by_name,
  1,  start_thread_loading_response__number_ranges,
  (ProtobufCMessageInit) start_thread_loading_response__init,
  NULL,NULL,NULL    /* reserved[123] */
};
class="p">.UtcNow.AddDays(1)); return new LoginResponse() { Token = token.AccessToken, ExpirationUTC = token.Expiration.Value, }; } } [JwtWebApiTokenFilter] public List<DataStoreWebItem> Get(String sn = null, String collection = null, String key = null) { try { if (!RequestToken.Object.Permissions.Contains(Permissions.DataStoreRead)) { throw CreateHttpException(new AuthenticationException("The current user was not authorized to read from the data store."), HttpStatusCode.Unauthorized); } if (key != null && collection == null) { throw CreateHttpException(new ArgumentException(), HttpStatusCode.BadRequest, "When specifying a key, collection must be specified."); } ValidateCollectionAndKey(collection, key); using (var db = ObservablesContextHelper.CreateContext()) { if (sn != null) { var machineGuid = db.Machines.Where(x => x.SerialNumber == sn).Select(x => x.Guid).FirstOrDefault(); if (machineGuid == null) { throw CreateHttpException(new KeyNotFoundException(), HttpStatusCode.NotFound, "The specified machine serial number could not be found."); } var localItems = db.DataStoreItems.Where(x => !x.IsDeleted).Where(x => x.MachineGuid == machineGuid && (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList(); var globalItems = db.GlobalDataStoreItems.Where(x => (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList(); if (localItems.Count == 0 && globalItems.Count == 0 && key != null) { throw CreateHttpException(new KeyNotFoundException(), HttpStatusCode.NotFound, "The specified key was not found on the data store."); } List<DataStoreWebItem> finalList = new List<DataStoreWebItem>(); foreach (var localItem in localItems) { var globalItem = globalItems.FirstOrDefault(x => x.CollectionName == localItem.CollectionName && x.Key == localItem.Key); finalList.Add(localItem.ToWebItem(globalItem)); globalItems.Remove(globalItem); } finalList.AddRange(globalItems.Select(x => x.ToWebItem())); return finalList; } else { var globalItems = db.GlobalDataStoreItems.Where(x => (collection == null || x.CollectionName == collection) && (key == null || x.Key == key)).ToList(); var finalList = globalItems.Select(x => x.ToWebItem()).ToList(); return finalList; } } } catch (HttpResponseException ex) { throw ex; } catch (Exception ex) { throw CreateHttpException(ex, HttpStatusCode.InternalServerError); } } [JwtWebApiTokenFilter] public void Put([FromBody]DataStoreWebPutItem item) { try { if (!RequestToken.Object.Permissions.Contains(Permissions.DataStoreWrite)) { throw CreateHttpException(new AuthenticationException("The current user was not authorized to write to the data store."), HttpStatusCode.Unauthorized); } if (item.Collection == null || item.Key == null) { throw CreateHttpException(new AuthenticationException("Collection and key must be specified."), HttpStatusCode.BadRequest); } ValidateCollectionAndKey(item.Collection, item.Key); using (var db = ObservablesContextHelper.CreateContext()) { if (item.MachineSerialNumber != null) { var machineGuid = db.Machines.Where(x => x.SerialNumber == item.MachineSerialNumber).Select(x => x.Guid).FirstOrDefault(); if (machineGuid == null) { throw CreateHttpException(new KeyNotFoundException("The specified machine serial number could not be found."), HttpStatusCode.NotFound); } DataStoreItem dbItem = db.DataStoreItems.FirstOrDefault(x => x.CollectionName == item.Collection && x.Key == item.Key); if (dbItem == null) { if (!RequestToken.Object.Permissions.Contains(Permissions.DataStoreCreate)) { throw CreateHttpException(new AuthenticationException("The current user was not authorized to create new items on the data store."), HttpStatusCode.Unauthorized); } dbItem = new DataStoreItem(); dbItem.Key = item.Key; dbItem.CollectionName = item.Collection; dbItem.MachineGuid = machineGuid; db.DataStoreItems.Add(dbItem); } dbItem.DataType = (int)item.DataType; dbItem.IsDeleted = false; dbItem.IsSynchronized = false; dbItem.LastUpdated = DateTime.UtcNow; dbItem.Value = EFDataStoreHelper.CreateBytes(item.DataType, DataStoreHelper.ParseDataStoreValue(item.DataType, item.Value.ToStringSafe(), item.ProtoMessageType)); } else { if (!RequestToken.Object.Permissions.Contains(Permissions.DataStoreCreateWriteGlobal)) { throw CreateHttpException(new AuthenticationException("The current user was not authorized to write to the global data store."), HttpStatusCode.Unauthorized); } GlobalDataStoreItem dbItem = db.GlobalDataStoreItems.FirstOrDefault(x => x.CollectionName == item.Collection && x.Key == item.Key); if (dbItem == null) { if (!RequestToken.Object.Permissions.Contains(Permissions.DataStoreCreate)) { throw CreateHttpException(new AuthenticationException("The current user was not authorized to create new items on the data store."), HttpStatusCode.Unauthorized); } dbItem = new GlobalDataStoreItem(); dbItem.Key = item.Key; dbItem.CollectionName = item.Collection; db.GlobalDataStoreItems.Add(dbItem); } dbItem.DataType = (int)item.DataType; dbItem.LastUpdated = DateTime.UtcNow; dbItem.Value = EFDataStoreHelper.CreateBytes(item.DataType, DataStoreHelper.ParseDataStoreValue(item.DataType, item.Value.ToStringSafe(), item.ProtoMessageType)); } db.SaveChanges(); } } catch (HttpResponseException ex) { throw ex; } catch (Exception ex) { throw CreateHttpException(ex, HttpStatusCode.InternalServerError); } } private HttpResponseException CreateHttpException(Exception ex, HttpStatusCode code, String message = null) { return new HttpResponseException(new HttpResponseMessage(code) { Content = new StringContent(message != null ? message : ex.Message), ReasonPhrase = ex.FlattenMessage() }); } private void ValidateCollectionAndKey(String collection = null, String key = null) { if (collection != null) { if (!DataStoreHelper.ValidateCollectionOrKeyName(collection)) { throw new ArgumentException("Collection name contains invalid characters."); } } if (key != null) { if (!DataStoreHelper.ValidateCollectionOrKeyName(key)) { throw new ArgumentException("Item key contains invalid characters."); } } } } #region Extension Methods public static class IDataStoreExtensions { public static DataStoreWebItem ToWebItem(this DataStoreItem item, GlobalDataStoreItem globalItem = null) { IDataStoreItem dsItem = item.ToDataStoreItem(); DataStoreWebItem webItem = new DataStoreWebItem(); webItem.Collection = item.CollectionName; webItem.Type = globalItem != null ? DataStoreWebItemType.Overrides : DataStoreWebItemType.Local; webItem.DataType = dsItem.Type; webItem.Date = dsItem.Date; webItem.Key = dsItem.Key; webItem.LocalValue = dsItem.Value; if (webItem.LocalValue is DataStoreProtoObject protoObject) { webItem.LocalValue = protoObject.Message; webItem.ProtoMessageType = protoObject.MessageType; } if (globalItem != null) { var dsGlobalItem = globalItem.ToDataStoreItem(); webItem.GlobalValue = dsGlobalItem.Value; if (webItem.GlobalValue is DataStoreProtoObject protoObjectGlobal) { webItem.GlobalValue = protoObjectGlobal.Message; webItem.ProtoMessageType = protoObjectGlobal.MessageType; } } return webItem; } public static DataStoreWebItem ToWebItem(this GlobalDataStoreItem item) { IDataStoreItem dsItem = item.ToDataStoreItem(); DataStoreWebItem webItem = new DataStoreWebItem(); webItem.Collection = item.CollectionName; webItem.Type = DataStoreWebItemType.Global; webItem.DataType = dsItem.Type; webItem.Date = dsItem.Date; webItem.Key = dsItem.Key; webItem.GlobalValue = dsItem.Value; if (webItem.GlobalValue is DataStoreProtoObject protoObject) { webItem.GlobalValue = protoObject.Message; webItem.ProtoMessageType = protoObject.MessageType; } return webItem; } } #endregion }