Esempio n. 1
0
        public static int PushMessageToGroup(string pushTag, string pushTitle, string pushMessager)
        {
            JPushClient jpush_client   = new JPushClient(app_key, master_secret);
            var         registrationId = "12145125123151";
            var         devicePayload  = new DevicePayload()
            {
                Alias  = "alias1",
                Mobile = "12300000000",
                Tags   = new Dictionary <string, object>()
                {
                    { "add", new List <string>()
                      {
                          "tag1", "tag2"
                      } },
                    { "remove", new List <string>()
                      {
                          "tag3", "tag4"
                      } }
                }
            };
            var response = jpush_client.Device.UpdateDeviceInfo(registrationId, devicePayload);



            Console.WriteLine(response.Content);

            return(0);
        }
Esempio n. 2
0
        /// <summary>
        /// 更新设备信息(目前支持 tag, alias 和 mobile)。
        /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_2"/>
        /// </summary>
        /// <param name="registrationId">
        ///     客户端初始化 JPush 成功后,JPush 服务端会分配一个 Registration ID,作为此设备的标识(同一个手机不同 APP 的 Registration ID 是不同的)。
        /// </param>
        /// <param name="devicePayload">设备信息对象</param>
        public HttpResponse UpdateDeviceInfo(string registrationId, DevicePayload devicePayload)
        {
            Task <HttpResponse> task = Task.Run(() => UpdateDeviceInfoAsync(registrationId, devicePayload));

            task.Wait();
            return(task.Result);
        }
Esempio n. 3
0
        /// <summary>
        /// Get device personalities
        /// </summary>
        /// <param name="devicePayLoad"></param>
        /// <param name="personalities"></param>
        /// <returns></returns>
        private List <DbDevicePersonality> GetDevicePersonalities(DevicePayload devicePayLoad, List <DbDevicePersonality> personalities, DbDeviceType devicetype)
        {
            var  supportedPersonalities = GetDeviceSupportedPersonality(devicetype.DefaultValueJson);
            bool isCreateEvent          = personalities == null;  //this will be NULL for create case

            personalities = personalities ?? new List <DbDevicePersonality>();
            var populatedPersonalities = new List <DbDevicePersonality>();

            if (supportedPersonalities != null)
            {
                var personality = new DbDevicePersonality();
                foreach (var item in supportedPersonalities)
                {
                    string desc         = null;
                    var    propertyInfo = typeof(DevicePayload).GetProperty(item.PersonalityTypeName);
                    var    value        = propertyInfo != null?propertyInfo.GetValue(devicePayLoad, null) : null;

                    personality = personalities.FirstOrDefault(x => x.fk_PersonalityTypeID == item.DevicePersonalityTypeID);
                    if (personality != null)
                    {
                        personalities.Remove(personality);
                    }
                    string personalityValue = null;
                    if (value == null)
                    {
                        personalityValue = personality != null ? personality.PersonalityValue : null;
                    }
                    else                     // for create event Empty alone should be treated as NULL
                    {
                        if (isCreateEvent)
                        {
                            personalityValue = value.ToString() == string.Empty ? null : value.ToString();
                        }
                        else
                        {
                            personalityValue = value?.ToString();
                        }
                    }
                    if (personalityValue != null)
                    {
                        desc = !string.IsNullOrEmpty(value?.ToString()) && !string.IsNullOrEmpty(devicePayLoad.Description) ? devicePayLoad.Description : personality?.PersonalityDesc;

                        personalities.Add(
                            new DbDevicePersonality
                        {
                            DevicePersonalityUID = personality == null ? Guid.NewGuid() : personality.DevicePersonalityUID,
                            fk_DeviceUID         = devicePayLoad.DeviceUID,
                            fk_PersonalityTypeID = item.DevicePersonalityTypeID,
                            PersonalityDesc      = desc,
                            PersonalityValue     = value?.ToString() == string.Empty ? null : personalityValue,
                            RowUpdatedUTC        = DateTime.UtcNow
                        });
                    }
                }
            }
            return(personalities);
        }
        private async Task <DeviceResponse> HandleControllerRequest(DevicePayload payload)
        {
            _logger.LogInformation($"[Device] [{payload.Uuid}] Received control request: {payload.Type}");

            var device = await _deviceRepository.GetByIdAsync(payload.Uuid).ConfigureAwait(false);

            if (device == null)
            {
                await _deviceRepository.AddOrUpdateAsync(new Device
                {
                    Uuid     = payload.Uuid,
                    LastHost = Request.GetIPAddress(),
                }).ConfigureAwait(false);
            }
            switch (payload.Type.ToLower())
            {
            case "init":
                return(await HandleInitialize(device, payload.Uuid).ConfigureAwait(false));

            case "heartbeat":
                return(await HandleHeartbeat(payload.Uuid).ConfigureAwait(false));

            case "get_job":
                return(await HandleGetJob(device, payload.Username).ConfigureAwait(false));

            case "get_account":
                return(await HandleGetAccount(device).ConfigureAwait(false));

            case "account_banned":
            case "account_warning":
            case "account_invalid_credentials":
                return(await HandleAccountStatus(device, payload.Type).ConfigureAwait(false));

            case "tutorial_done":
                return(await HandleTutorialStatus(device).ConfigureAwait(false));

            case "logged_out":
                return(await HandleLogout(device).ConfigureAwait(false));

            case "job_failed":
                _logger.LogWarning($"[Device] [{device.Uuid}] Job failed");
                return(new DeviceResponse
                {
                    Status = "error",
                    Error = "Job failed",
                });

            default:
                _logger.LogWarning($"[Device] [{device?.Uuid}] Unhandled request type: {payload.Type}");
                return(new DeviceResponse
                {
                    Status = "error",
                    Error = $"Unhandled request type: {payload.Type}",
                });
            }
        }
Esempio n. 5
0
        public static DevicePayload GetDevice(long referenceId)
        {
            var device = Device.AllDevices.Find(x => x.ReferenceId == referenceId);

            if (device == null)
            {
                return(null);
            }
            return(DevicePayload.FromDevice(device));
        }
Esempio n. 6
0
        public static IList <DevicePayload> GetDevices()
        {
            // Devices can have duplicates in this list.
            var set = new HashSet <Device>();

            foreach (var device in Device.AllDevices)
            {
                set.Add(device);
            }
            return(set.Select(x => DevicePayload.FromDevice(x)).ToList());
        }
Esempio n. 7
0
        public static DevicePayload UpdateDevice(long referenceId, DevicePayload updates)
        {
            var device = Device.AllDevices.Find(x => x.ReferenceId == referenceId);

            if (device == null)
            {
                return(null);
            }

            ThingsModel.WriteThingProperties(device, updates);
            return(DevicePayload.FromDevice(device));
        }
        public async Task <DeviceResponse> PostAsync(DevicePayload payload)
        {
            var response = await HandleControllerRequest(payload).ConfigureAwait(false);

            if (response == null)
            {
                _logger.LogError($"[Device] [{payload.Uuid}] null data response!");
            }
            Response.Headers["Accept"]       = "application/json";
            Response.Headers["Content-Type"] = "application/json";
            return(response);
        }
        public async Task PostDevice(IHttpContext context, long referenceId, DevicePayload body)
        {
            Authenticator.VerifyAuth(context);

            var response = await Dispatcher.RunOnMainThread(() => DevicesModel.UpdateDevice(referenceId, body));

            if (response == null)
            {
                throw new NotFoundException("Device Not Found.");
            }

            await context.SendResponse(HttpStatusCode.OK, response);
        }
Esempio n. 10
0
        /// <summary>
        /// <see cref="UpdateDeviceInfo(string, DevicePayload)"/>
        /// 更新设备信息(目前支持 tag, alias 和 mobile)。
        /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_2"/>
        /// </summary>
        /// <param name="registrationId">
        ///     客户端初始化 JPush 成功后,JPush 服务端会分配一个 Registration ID,作为此设备的标识(同一个手机不同 APP 的 Registration ID 是不同的)。
        /// </param>
        /// <param name="devicePayload">设备信息对象</param>
        public HttpResponse UpdateDeviceInfo(string registrationId, DevicePayload devicePayload)
        {
            if (string.IsNullOrEmpty(registrationId))
            {
                throw new ArgumentNullException(nameof(registrationId));
            }

            if (devicePayload == null)
            {
                throw new ArgumentNullException(nameof(devicePayload));
            }

            var json = devicePayload.ToString();

            return(UpdateDeviceInfo(registrationId, json));
        }
Esempio n. 11
0
 private DevicePayloadV2 ToDevicePayloadV2(DevicePayload deviceEvent, List <DbDevicePersonality> devicePersonality)
 {
     return(new DevicePayloadV2
     {
         DeviceUID = deviceEvent.DeviceUID,
         DeviceType = deviceEvent.DeviceType,
         DeviceState = deviceEvent.DeviceState.ToString(),
         DeviceSerialNumber = deviceEvent.DeviceSerialNumber,
         ModuleType = deviceEvent.ModuleType,
         DeregisteredUTC = deviceEvent.DeregisteredUTC,
         DataLinkType = deviceEvent.DataLinkType,
         Personalities = devicePersonality?.Select(personality => new DevicePersonalityPayload
         {
             Name = ((DevicePersonalityTypeEnum)personality.fk_PersonalityTypeID).ToString(),
             Description = personality.PersonalityDesc,
             Value = personality.PersonalityValue
         }).ToList(),
         ActionUTC = deviceEvent.ActionUTC,
         ReceivedUTC = deviceEvent.ReceivedUTC
     });
 }
Esempio n. 12
0
        private static void ExecuteDeviceEample()
        {
            var registrationId = "12145125123151";
            var devicePayload  = new DevicePayload
            {
                Alias  = "alias1",
                Mobile = "12300000000",
                Tags   = new Dictionary <string, object>
                {
                    { "add", new List <string>()
                      {
                          "tag1", "tag2"
                      } },
                    { "remove", new List <string>()
                      {
                          "tag3", "tag4"
                      } }
                }
            };
            var response = client.Device.UpdateDeviceInfo(registrationId, devicePayload);

            Console.WriteLine(response.Content);
        }
Esempio n. 13
0
        public IActionResult ExecuteDeviceEample()
        {
            var registrationId = "12145125123151";
            var devicePayload  = new DevicePayload
            {
                Alias  = "alias1",
                Mobile = "12300000000",
                Tags   = new Dictionary <string, object>
                {
                    { "add", new List <string>()
                      {
                          "tag1", "tag2"
                      } },
                    { "remove", new List <string>()
                      {
                          "tag3", "tag4"
                      } }
                }
            };
            var response = _jPushClient.Device.UpdateDeviceInfo(registrationId, devicePayload);

            return(Content(response.Content));
        }
Esempio n. 14
0
        /// <summary>
        /// 更新设备信息(目前支持 tag, alias 和 mobile)。
        /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/#_2"/>
        /// </summary>
        /// <param name="registrationId">
        ///     客户端初始化 JPush 成功后,JPush 服务端会分配一个 Registration ID,作为此设备的标识(同一个手机不同 APP 的 Registration ID 是不同的)。
        /// </param>
        /// <param name="devicePayload">设备信息对象</param>
        public HttpResponse UpdateDeviceInfo(string registrationId, DevicePayload devicePayload)
        {
            HttpResponse response = null;
            var          index    = 1;

            do
            {
                try
                {
                    var task = Task.Run(() => UpdateDeviceInfoAsync(registrationId, devicePayload));
                    task.Wait();
                    response = task.Result;
                    break;
                }
                catch (Exception ex)
                {
                    index++;
                    _log.Error(ex);
                    _log.ErrorFormat("Update device info error, {0} - {1}", registrationId, JsonConvert.SerializeObject(devicePayload));
                }
            } while (index <= 3);

            return(response);
        }
Esempio n. 15
0
 /// <summary>
 /// Updates the device information.
 /// </summary>
 /// <param name="registrationId">The registration identifier.</param>
 /// <param name="devicePayload">The device payload.</param>
 /// <returns></returns>
 public string UpdateDeviceInfo(string registrationId, DevicePayload devicePayload)
 {
     return(client.Device.UpdateDeviceInfo(registrationId, devicePayload).Content);
 }