/**
 * Copyright © 2016-2026 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
syntax = "proto3";

option java_package = "org.thingsboard.server.gen.edge.v1";
option java_multiple_files = true;
option java_outer_classname = "EdgeProtos";

import "queue.proto";

package edge;

// Interface exported by the ThingsBoard Edge Transport.
service EdgeRpcService {

  rpc handleMsgs(stream RequestMsg) returns (stream ResponseMsg) {}

}

enum EdgeVersion {
  V_3_3_0 = 0;
  V_3_3_3 = 1;
  V_3_4_0 = 2;
  V_3_6_0 = 3;
  V_3_6_1 = 4;
  V_3_6_2 = 5;
  V_3_6_4 = 6;
  V_3_7_0 = 7;
  V_3_8_0 = 8;
  V_3_9_0 = 9;
  V_4_0_0 = 10;
  V_4_1_0 = 11;
  V_4_2_0 = 12;
  V_4_3_0 = 13;
  V_4_2_1_2 = 14;
  V_4_2_2 = 4220;
  V_4_2_2_1 = 4221;
  V_4_2_2_2 = 4222;
  V_4_2_2_3 = 4223;
  V_4_3_0_1 = 15;
  V_4_3_1 = 4310;
  V_4_3_1_1 = 4311;
  V_4_3_1_2 = 4312;
  V_4_3_1_3 = 4313;
  V_4_4_0 = 4400;

  V_LATEST = 99999;
}

/**
 * Data Structures;
 */
message RequestMsg {
  RequestMsgType msgType = 1;
  ConnectRequestMsg connectRequestMsg = 2;
  UplinkMsg uplinkMsg = 3;
  DownlinkResponseMsg downlinkResponseMsg = 4;
  SyncRequestMsg syncRequestMsg = 5;
}

message ResponseMsg {
  ConnectResponseMsg connectResponseMsg = 1;
  UplinkResponseMsg uplinkResponseMsg = 2;
  DownlinkMsg downlinkMsg = 3;
  EdgeUpdateMsg edgeUpdateMsg = 4;
}

enum RequestMsgType {
  CONNECT_RPC_MESSAGE = 0;
  UPLINK_RPC_MESSAGE = 1;
  SYNC_REQUEST_RPC_MESSAGE = 2;
}

message EdgeUpdateMsg {
  EdgeConfiguration configuration = 1;
}

message ConnectRequestMsg {
  string edgeRoutingKey = 1;
  string edgeSecret = 2;
  EdgeVersion edgeVersion = 3;
  optional int32 maxInboundMessageSize = 4;
}

enum ConnectResponseCode {
  ACCEPTED = 0;
  BAD_CREDENTIALS = 1;
  SERVER_UNAVAILABLE = 2;
}

message ConnectResponseMsg {
  ConnectResponseCode responseCode = 1;
  string errorMsg = 2;
  EdgeConfiguration configuration = 3;
  optional int32 maxInboundMessageSize = 4;
}

message SyncRequestMsg {
  optional bool fullSync = 2;
}

message SyncCompletedMsg {}

message EdgeConfiguration {
  int64 edgeIdMSB = 1;
  int64 edgeIdLSB = 2;
  int64 tenantIdMSB = 3;
  int64 tenantIdLSB = 4;
  int64 customerIdMSB = 5;
  int64 customerIdLSB = 6;
  string name = 7;
  string type = 8;
  string routingKey = 9;
  string secret = 10;
  string additionalInfo = 11;
  string cloudType = 12;
}

enum UpdateMsgType {
  ENTITY_CREATED_RPC_MESSAGE = 0;
  ENTITY_UPDATED_RPC_MESSAGE = 1;
  ENTITY_DELETED_RPC_MESSAGE = 2;
  ALARM_ACK_RPC_MESSAGE = 3;
  ALARM_CLEAR_RPC_MESSAGE = 4;
  // use 6 as a next number
}

message CalculatedFieldUpdateMsg{
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 4;
}

message AiModelUpdateMsg{
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 4;
}

message ApiKeyUpdateMsg{
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 4;
}

message EntityDataProto {
  int64 entityIdMSB = 1;
  int64 entityIdLSB = 2;
  string entityType = 3;
  transport.PostTelemetryMsg postTelemetryMsg = 4;
  transport.PostAttributeMsg postAttributesMsg = 5;
  transport.PostAttributeMsg attributesUpdatedMsg = 6;
  string postAttributeScope = 7;
  AttributeDeleteMsg attributeDeleteMsg = 8;
  optional int64 attributeTs = 9;
}

message AttributeDeleteMsg {
  string scope = 1;
  repeated string attributeNames = 2;
}

message RuleChainUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 10;
}

message RuleChainMetadataUpdateMsg {
  UpdateMsgType msgType = 1;
  string entity = 8;
}

message DashboardUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 12;
}

message DeviceUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 18;
}

message DeviceProfileUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 22;
}

message AssetProfileUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 13;
}

message DeviceCredentialsUpdateMsg {
  string entity = 6;
}

message AssetUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 12;
}

message EntityViewUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 12;
}

message AlarmUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 18;
}

message AlarmCommentUpdateMsg {
  UpdateMsgType msgType = 1;
  string entity = 2;
}

message CustomerUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 14;
}

message RelationUpdateMsg {
  UpdateMsgType msgType = 1;
  string entity = 11;
}

message UserUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 11;
}

message WidgetsBundleUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  optional string widgets = 9;
  string entity = 11;
}

message WidgetTypeUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 14;
}

message AdminSettingsUpdateMsg {
  string entity = 4;
}

message UserCredentialsUpdateMsg {
  string entity = 5;
}

message TenantUpdateMsg {
  UpdateMsgType msgType = 1;
  string entity = 17;
}

message TenantProfileUpdateMsg {
  UpdateMsgType msgType = 1;
  string entity = 9;
}

message ResourceUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 11;
}

message OAuth2ClientUpdateMsg {
  UpdateMsgType msgType = 1;
  optional int64 idMSB = 2;
  optional int64 idLSB = 3;
  optional string entity = 4;
}

message OAuth2DomainUpdateMsg {
  UpdateMsgType msgType = 1;
  optional int64 idMSB = 2;
  optional int64 idLSB = 3;
  optional string entity = 4;
}

message NotificationRuleUpdateMsg {
  UpdateMsgType msgType = 1;
  optional int64 idMSB = 2;
  optional int64 idLSB = 3;
  optional string entity = 4;
}

message NotificationTargetUpdateMsg {
  UpdateMsgType msgType = 1;
  optional int64 idMSB = 2;
  optional int64 idLSB = 3;
  optional string entity = 4;
}

message NotificationTemplateUpdateMsg {
  UpdateMsgType msgType = 1;
  optional int64 idMSB = 2;
  optional int64 idLSB = 3;
  optional string entity = 4;
}

// DEPRECATED. FOR REMOVAL
message RuleChainMetadataRequestMsg {
  option deprecated = true;
  int64 ruleChainIdMSB = 1;
  int64 ruleChainIdLSB = 2;
}

message AttributesRequestMsg {
  int64 entityIdMSB = 1;
  int64 entityIdLSB = 2;
  string entityType = 3;
  string scope = 4;
}

message RelationRequestMsg {
  int64 entityIdMSB = 1;
  int64 entityIdLSB = 2;
  string entityType = 3;
}

message CalculatedFieldRequestMsg {
  int64 entityIdMSB = 1;
  int64 entityIdLSB = 2;
  string entityType = 3;
}

// DEPRECATED. FOR REMOVAL
message UserCredentialsRequestMsg {
  option deprecated = true;
  int64 userIdMSB = 1;
  int64 userIdLSB = 2;
}

// DEPRECATED. FOR REMOVAL
message DeviceCredentialsRequestMsg {
  option deprecated = true;
  int64 deviceIdMSB = 1;
  int64 deviceIdLSB = 2;
}

// DEPRECATED. FOR REMOVAL
message WidgetBundleTypesRequestMsg {
  option deprecated = true;
  int64 widgetBundleIdMSB = 1;
  int64 widgetBundleIdLSB = 2;
}

// DEPRECATED. FOR REMOVAL
message EntityViewsRequestMsg {
  option deprecated = true;
  int64 entityIdMSB = 1;
  int64 entityIdLSB = 2;
  string entityType = 3;
}

message DeviceRpcCallMsg {
  int64 deviceIdMSB = 1;
  int64 deviceIdLSB = 2;
  int64 requestUuidMSB = 3;
  int64 requestUuidLSB = 4;
  int32 requestId = 5;
  int64 expirationTime = 6;
  bool oneway = 7;
  RpcRequestMsg requestMsg = 8;
  RpcResponseMsg responseMsg = 9;
  optional bool persisted = 10;
  optional int32 retries = 11;
  optional string additionalInfo = 12;
  optional string serviceId = 13;
  optional string sessionId = 14;
}

message RpcRequestMsg {
  string method = 1;
  string params = 2;
}

message RpcResponseMsg {
  string response = 1;
  string error = 2;
}

message OtaPackageUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 18;
}

message QueueUpdateMsg {
  UpdateMsgType msgType = 1;
  int64 idMSB = 2;
  int64 idLSB = 3;
  string entity = 14;
}

/**
 * Main Messages;
 */

message UplinkMsg {
  int32 uplinkMsgId = 1;
  repeated EntityDataProto entityData = 2;
  repeated DeviceUpdateMsg deviceUpdateMsg = 3;
  repeated DeviceCredentialsUpdateMsg deviceCredentialsUpdateMsg = 4;
  repeated AlarmUpdateMsg alarmUpdateMsg = 5;
  repeated RelationUpdateMsg relationUpdateMsg = 6;
  repeated RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg = 7 [deprecated = true];
  repeated AttributesRequestMsg attributesRequestMsg = 8;
  repeated RelationRequestMsg relationRequestMsg = 9;
  repeated UserCredentialsRequestMsg userCredentialsRequestMsg = 10 [deprecated = true];
  repeated DeviceCredentialsRequestMsg deviceCredentialsRequestMsg = 11 [deprecated = true];
  repeated DeviceRpcCallMsg deviceRpcCallMsg = 12;
  repeated WidgetBundleTypesRequestMsg widgetBundleTypesRequestMsg = 14 [deprecated = true];
  repeated EntityViewsRequestMsg entityViewsRequestMsg = 15 [deprecated = true];
  repeated AssetUpdateMsg assetUpdateMsg = 16;
  repeated DashboardUpdateMsg dashboardUpdateMsg = 17;
  repeated EntityViewUpdateMsg entityViewUpdateMsg = 18;
  repeated AssetProfileUpdateMsg assetProfileUpdateMsg = 19;
  repeated DeviceProfileUpdateMsg deviceProfileUpdateMsg = 20;
  repeated ResourceUpdateMsg resourceUpdateMsg = 21;
  repeated AlarmCommentUpdateMsg alarmCommentUpdateMsg = 22;
  repeated RuleChainUpdateMsg ruleChainUpdateMsg = 23;
  repeated RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = 24;
  repeated CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = 25;
  repeated CalculatedFieldRequestMsg calculatedFieldRequestMsg = 26;
  repeated AiModelUpdateMsg aiModelUpdateMsg = 27;
  repeated UserUpdateMsg userUpdateMsg = 28;
  repeated UserCredentialsUpdateMsg userCredentialsUpdateMsg = 29;
  repeated ApiKeyUpdateMsg apiKeyUpdateMsg = 30;
}

message UplinkResponseMsg {
  bool success = 1;
  string errorMsg = 2;
  int32 uplinkMsgId = 3;
}

message DownlinkResponseMsg {
  bool success = 1;
  string errorMsg = 2;
  int32 downlinkMsgId = 3;
}

message DownlinkMsg {
  int32 downlinkMsgId = 1;
  SyncCompletedMsg syncCompletedMsg = 2;
  repeated EntityDataProto entityData = 3;
  repeated DeviceCredentialsRequestMsg deviceCredentialsRequestMsg = 4 [deprecated = true];
  repeated DeviceUpdateMsg deviceUpdateMsg = 5;
  repeated DeviceProfileUpdateMsg deviceProfileUpdateMsg = 6;
  repeated DeviceCredentialsUpdateMsg deviceCredentialsUpdateMsg = 7;
  repeated RuleChainUpdateMsg ruleChainUpdateMsg = 8;
  repeated RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = 9;
  repeated DashboardUpdateMsg dashboardUpdateMsg = 10;
  repeated AssetUpdateMsg assetUpdateMsg = 11;
  repeated EntityViewUpdateMsg entityViewUpdateMsg = 12;
  repeated AlarmUpdateMsg alarmUpdateMsg = 13;
  repeated UserUpdateMsg userUpdateMsg = 14;
  repeated UserCredentialsUpdateMsg userCredentialsUpdateMsg = 15;
  repeated CustomerUpdateMsg customerUpdateMsg = 16;
  repeated RelationUpdateMsg relationUpdateMsg = 17;
  repeated WidgetsBundleUpdateMsg widgetsBundleUpdateMsg = 18;
  repeated WidgetTypeUpdateMsg widgetTypeUpdateMsg = 19;
  repeated AdminSettingsUpdateMsg adminSettingsUpdateMsg = 20;
  repeated DeviceRpcCallMsg deviceRpcCallMsg = 21;
  repeated OtaPackageUpdateMsg otaPackageUpdateMsg = 22;
  repeated QueueUpdateMsg queueUpdateMsg = 23;
  repeated AssetProfileUpdateMsg assetProfileUpdateMsg = 24;
  EdgeConfiguration edgeConfiguration = 25;
  repeated TenantUpdateMsg tenantUpdateMsg = 26;
  repeated TenantProfileUpdateMsg tenantProfileUpdateMsg = 27;
  repeated ResourceUpdateMsg resourceUpdateMsg = 28;
  repeated AlarmCommentUpdateMsg alarmCommentUpdateMsg = 29;
  repeated OAuth2ClientUpdateMsg oAuth2ClientUpdateMsg = 30;
  repeated NotificationRuleUpdateMsg notificationRuleUpdateMsg = 31;
  repeated NotificationTargetUpdateMsg notificationTargetUpdateMsg = 32;
  repeated NotificationTemplateUpdateMsg notificationTemplateUpdateMsg = 33;
  repeated OAuth2DomainUpdateMsg oAuth2DomainUpdateMsg = 34;
  repeated CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = 35;
  repeated AiModelUpdateMsg aiModelUpdateMsg = 36;
  repeated ApiKeyUpdateMsg apiKeyUpdateMsg = 37;
}
