public string SendNotificationAsync(FcmViewModel fcmViewModel)
 {
     try
     {
         Fcm    fcmAuth        = new Fcm();
         var    str            = "-1";
         string applicationID  = fcmAuth.FCMAPIkey;
         string senderId       = fcmAuth.FCMSenderKey;
         string deviceId       = fcmViewModel.UserDeviceId;
         var    webAddr        = "https://fcm.googleapis.com/fcm/send";
         var    httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
         httpWebRequest.ContentType = "application/json";
         httpWebRequest.Method      = "POST";
         var data = new
         {
             to = fcmViewModel.UserDeviceId,
             content_available = fcmViewModel.ShowNotification,
             notification      = new
             {
                 body  = fcmViewModel.Body,
                 title = fcmViewModel.Title,
                 topic = fcmViewModel.Topic,
                 sound = "Enabled"
             }
         };
         var    json      = JsonConvert.SerializeObject(data);
         Byte[] byteArray = Encoding.UTF8.GetBytes(json);
         httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
         httpWebRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
         httpWebRequest.ContentLength = byteArray.Length;
         using (Stream dataStream = httpWebRequest.GetRequestStream())
         {
             dataStream.Write(byteArray, 0, byteArray.Length);
             using (WebResponse tResponse = httpWebRequest.GetResponse())
             {
                 using (Stream dataStreamResponse = tResponse.GetResponseStream())
                 {
                     using (StreamReader tReader = new StreamReader(dataStreamResponse))
                     {
                         String sResponseFromServer = tReader.ReadToEnd();
                         str = sResponseFromServer;
                     }
                 }
             }
             return(str);
         }
     }
     catch (Exception ex)
     {
         string str = ex.Message;
         return(str);
     }
 }
Ejemplo n.º 2
0
        public string SendByFCM(string[] deviceIds, NotificationModel notification)
        {
            var result = string.Empty;
            var fcm    = new Fcm();

            while (deviceIds.Any())
            {
                var part = deviceIds.Take(1000);
                result   += fcm.SendNotification(part.ToArray(), notification);
                deviceIds = deviceIds.Skip(1000).ToArray();
            }
            return(result);
        }
Ejemplo n.º 3
0
        private void btn_PSOFCM_Click(object sender, EventArgs e)
        {
            Pso ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, _data, _maxD);

            //File.WriteAllText("log", "");
            //File.WriteAllText("FitnessParticle", "");
            File.WriteAllText(DateSet + "//FitnessPSO", "");
            File.WriteAllText(DateSet + "//FitnessFCMPSO", "");
            //File.WriteAllText("datares", "");
            //File.WriteAllText("datafcm", "");
            var now = DateTime.Now;

            for (int i = 0; i < 200; i++)
            {
                ps.Calc();
                File.AppendAllText(DateSet + "//FitnessPSO", ps.GloablBestFitness + "\t\n");
                //File.AppendAllText("log", ps.GloablBestError + "\t;\t" + ps.Variance + "\t\n");
                if (ps.Variance < _rate)
                {
                    break;
                }
            }

            //for (int i = 0; i < ps.N; i++)
            //{
            //    for (int j = 0; j < ps.C; j++)
            //    {
            //        File.AppendAllText("datares", ps.U[i, j].ToString(CultureInfo.InvariantCulture) + ";");
            //    }
            //    File.AppendAllText("datares", "\t\n");
            //}
            Fcm fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, _data, ps.U);

            for (int a = 0; a < 100; a++)
            {
                fc.CalcCenter();
                fc.CalcU();
                fc.CalcFitness(_data, M);
                File.AppendAllText(DateSet + "//FitnessFCMPSO", fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t\n");
            }
            //for (int i = 0; i < ps.N; i++)
            //{
            //    for (int j = 0; j < ps.C; j++)
            //    {
            //        File.AppendAllText(DateSet + "//datafcm", fc.U[i, j].ToString(CultureInfo.InvariantCulture) + ";");
            //    }
            //    File.AppendAllText(DateSet + "//datafcm", "\t\n");
            //}
        }
Ejemplo n.º 4
0
        private void btn_FCM_Click(object sender, EventArgs e)
        {
            File.WriteAllText(DateSet + "//FitnessFCM", "");
            //File.WriteAllText(DateSet + "datafcm", "");
            var now = DateTime.Now;

            double[,] U = new double[_n, C];
            for (int i = 0; i < _n; i++)
            {
                double[] tem = new double[C];
                double   sum = 0;
                for (int j = 0; j < C; j++)
                {
                    tem[j] = GeneralCom.GetRandom() + 0.001;
                    sum   += tem[j];
                }
                for (int j = 0; j < C; j++)
                {
                    U[i, j] = tem[j] / sum;
                }
            }
            Fcm fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, _data, U);

            for (int a = 0; a < 100; a++)
            {
                fc.CalcCenter();
                fc.CalcU();
                fc.CalcFitness(_data, M);
                File.AppendAllText(DateSet + "//FitnessFCM", fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t\n");
            }
            //for (int i = 0; i < _n; i++)
            //{
            //    for (int j = 0; j < C; j++)
            //    {
            //        File.AppendAllText(DateSet + "//datafcm", fc.U[i, j].ToString(CultureInfo.InvariantCulture) + ";");
            //    }
            //    File.AppendAllText(DateSet + "//datafcm", "\t\n");
            //}
        }
Ejemplo n.º 5
0
 public FcmMc(double[,] obs)
 {
     _fcm = new Fcm(obs);
     _mc = new MarkovChain();
 }
Ejemplo n.º 6
0
        private void btn_PSOFCMRR_Click(object sender, EventArgs e)
        {
            _data = _data.OrderBy(x => Guid.NewGuid()).ToList();
            int step     = 100;
            var tempData = _data.Skip(0).Take(step).ToList();
            var now      = DateTime.Now;

            _n = tempData.Count;
            Pso ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, tempData, _maxD);

            for (int i = 0; i < 200; i++)
            {
                ps.Calc();
                if (ps.Variance < _rate)
                {
                    break;
                }
            }
            Fcm fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, tempData, ps.U);

            for (int a = 0; a < 100; a++)
            {
                fc.CalcCenter();
                fc.CalcU();
                fc.CalcFitness(_data, M);
                //File.AppendAllText(DateSet + "//FitnessFCMPSO", fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t\n");
            }
            var aCenters = fc.Centers.OrderBy(p => p.Val[0]).ThenBy(p => p.Val[1]).ThenBy(p => p.Val[2]).ThenBy(p => p.Val[3]).ThenBy(p => p.Val[4]).ToArray();

            for (int it = 0; it < 1000; it++)
            {
                tempData.AddRange(_data.Skip(it * step + step).Take(step).ToList());
                _n = tempData.Count;
                ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, tempData, _maxD, aCenters);
                for (int i = 0; i < 200; i++)
                {
                    ps.Calc();
                    if (ps.Variance < _rate)
                    {
                        break;
                    }
                }
                fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, tempData, ps.U);
                for (int a = 0; a < 100; a++)
                {
                    fc.CalcCenter();
                    fc.CalcU();
                    fc.CalcFitness(_data, M);
                }
                double delta    = 0;
                var    bCenters = fc.Centers.OrderBy(p => p.Val[0]).ThenBy(p => p.Val[1]).ThenBy(p => p.Val[2]).ThenBy(p => p.Val[3]).ThenBy(p => p.Val[4]).ToArray();
                for (int d = 0; d < aCenters.Length; d++)
                {
                    delta += GeneralCom.Euclideandistance(aCenters[d], bCenters[d]);
                }
                delta /= aCenters.Length;
                File.AppendAllText(DateSet + "//RRR100", delta + "\t;\t" + fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t;\t" + aCenters.Aggregate("", (current, d) => current + (d + "&")) + "\t;\t" + bCenters.Aggregate("", (current, d) => current + (d + "&")) + "\t\n");
                aCenters = bCenters;
                //MessageBox.Show(delta.ToString());
            }
        }