예제 #1
0
        public override void OnCreate()
        {
            base.OnCreate();

            userId = JwtHelper.GetAspNetUserId(UserSettings.Token);

            dbWriter = new SignalsDBWriter(this);
            timer    = new Timer
            {
                Interval = 30000,
                Enabled  = true
            };

            timer.Elapsed += (s, e) =>
            {
                Logger.Info("trying to sent telemetry...");

                var type      = MediaType.Parse("application/json; charset=utf-8");
                var telemetry = dbWriter.ReadTelemetry(userId, out ids);

                if (telemetry?.Count > 0)
                {
                    var          str     = Newtonsoft.Json.JsonConvert.SerializeObject(telemetry, new Trigger.Classes.TelemetryJsonConverter());
                    var          body    = RequestBody.Create(type, str);
                    OkHttpClient client  = new OkHttpClient();
                    Request      request = new Request.Builder()
                                           .Url(Url)
                                           .Header("scn-dev-msg", "")
                                           .Post(body)
                                           .Build();

                    Callback c = new Callback {
                        Writer = dbWriter, Ids = ids
                    };

                    var all_signals = telemetry.SelectMany(b => b.Select(i => i)).ToList();

                    Logger.Info("Send Telemetry: " + all_signals.Count.ToString());
                    client.NewCall(request).Enqueue(c);
                }
                else
                {
                    Logger.Info("No signals to send");
                }
            };
        }