public static async Task <ThingSpeakData> ReadThingspeak() { var client = new ThingSpeakClient(sslRequired: b_requiredSSL); ThingSpeakData feeds = await client.ReadAllFeedsAsync(s_readAPIKey, i_channelID); return(feeds); }
public async void HandleTimerCallback(object state) { //TimeSpan runTime = DateTime.UtcNow.Subtract(startTime); SingletonTSList.Instance.l_dataStoringList.Clear(); ThingSpeakData feeds = await DataStorage.ReadThingspeak(); if (CheckIfListExceeded(DataStorage.ThingspeakConverter(feeds), SingletonTSList.Instance.st_WatchdogStorage.i_FirstskBar, SingletonTSList.Instance.st_WatchdogStorage.i_SecondskBar)) { alarm_player.Start(); } }
public static List <Origin> ThingspeakConverter(ThingSpeakData feeds) { List <Origin> l_dataStoringList = new List <Origin>(); foreach (var element in feeds.Feeds) { l_dataStoringList = AddToList(l_dataStoringList, element.EntryId ?? 0, (element.Field5 == null) ? 0 : Double.Parse(element.Field5, CultureInfo.InvariantCulture), (element.Field1 == null) ? 0 : Double.Parse(element.Field1, CultureInfo.InvariantCulture), (element.Field2 == null) ? 0 : Double.Parse(element.Field2, CultureInfo.InvariantCulture), (element.Field3 == null) ? 0 : Double.Parse(element.Field3, CultureInfo.InvariantCulture)); } return(l_dataStoringList); }
static void Main(string[] args) { var thingSpeakConnection = new ThingSpeakClient(sslRequired: true); Int32 channelId = 108891; List <DateTime> last_30days = GenerateLast30DaysList(); // Unfortunately, ThingSpeak limits amount of data in one request to 8000, so we can't get all data in one huge request, // so we have to get data for each day in separate request (however, they can be parallelized) //TODO: parallelize calculation for different days for (var dt = last_30days.First(); dt < last_30days.Last(); dt = dt.AddDays(1)) { // Step 1: retrieve data // TODO include other fields when data are available for them ThingSpeakData allDataForDay = thingSpeakConnection.ReadFieldsAsync( String.Empty, channelId, fieldId: 1, start_date: dt, end_date: dt.AddDays(1)).Result; var dataForDay = allDataForDay.Feeds; Console.WriteLine("Retrieved {0} feeds for day {1}.", dataForDay.Count(), dt.ToString()); Double workingTimeInPercents = 0.0; if (dataForDay.Count() >= 5) { // We have enough data to calculate interval // Step 2: find average interval between measurements TimeSpan interval = GetManualIntervalForDay(dt); Console.WriteLine("Found interval for day {0}, it is {1} minutes", dt.ToString(), interval.TotalMinutes); Double intervalInSeconds = interval.TotalSeconds; workingTimeInPercents = (dataForDay.Count() * intervalInSeconds / TimeSpan.FromDays(1).TotalSeconds) * 100.0; } Int32 roundedWorkingTimeInPercents = (Int32)Math.Round(workingTimeInPercents); Console.WriteLine("Working time percentage for day {0} is {1} %", dt.ToString(), roundedWorkingTimeInPercents); } Console.WriteLine("================================ Application finished. Press any key to exit ============================="); Console.ReadKey(true); }
// MQTT end protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); btn_PopupMenu = FindViewById <Button>(Resource.Id.id_btnPopupMenu); btn_PopupMenu.Click += ShowPopupMenu; ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; ViewPager pager = FindViewById <ViewPager>(Resource.Id.id_pager); GenericFragmentPagerAdaptor adaptor = new GenericFragmentPagerAdaptor(SupportFragmentManager); mp_player = MediaPlayer.Create(this, Resource.Raw.beer_pour); SingletonOxy.Instance.i_Plotchoice = 0; // BASIC adaptor.AddFragmentView((i, v, b) => { var view = i.Inflate(Resource.Layout.Basic, v, false); TextView textSample = view.FindViewById <TextView>(Resource.Id.id_txtBasic); textSample.Text = AdditionalResource.IntroText(); ibtn_beerImage = view.FindViewById <ImageButton>(Resource.Id.id_imgBeer); ibtn_beerImage.Click += ShowCheers; return(view); }); // SHOW DATA adaptor.AddFragmentView((i, v, b) => { var view = i.Inflate(Resource.Layout.ShowData, v, false); btn_RetrieveFromTS = view.FindViewById <Button>(Resource.Id.id_btnRetrieveFromTS); btn_RetrieveFromTS.Click += async(s, arg) => { ListView viewList = view.FindViewById <ListView>(Resource.Id.id_vwListShowData); try { SingletonTSList.Instance.l_dataStoringList.Clear(); ThingSpeakData feeds = await DataStorage.ReadThingspeak(); SingletonTSList.Instance.l_dataStoringList = DataStorage.ThingspeakConverter(feeds); //DataStorage.ArtificialList(); Toast.MakeText(this, string.Format("DATA OBTAINED!"), ToastLength.Long).Show(); } catch /*(Exception ex)*/ { Toast.MakeText(this, string.Format("CANNOT OBTAIN DATA FROM THINGSPEAK!"), ToastLength.Long).Show(); //Toast.MakeText(this, string.Format(ex.ToString()), ToastLength.Long).Show(); } var adapter = new VwAdapter(this, SingletonTSList.Instance.l_dataStoringList, 5); viewList.Adapter = adapter; }; btn_TempShowData1 = view.FindViewById <Button>(Resource.Id.id_btnShowData1); btn_TempShowData1.Click += (s, arg) => { ListView viewList = view.FindViewById <ListView>(Resource.Id.id_vwListShowData); var adapter = new VwAdapter(this, SingletonTSList.Instance.l_dataStoringList, 1); viewList.Adapter = adapter; }; btn_TempShowData2 = view.FindViewById <Button>(Resource.Id.id_btnShowData2); btn_TempShowData2.Click += (s, arg) => { ListView viewList = view.FindViewById <ListView>(Resource.Id.id_vwListShowData); var adapter = new VwAdapter(this, SingletonTSList.Instance.l_dataStoringList, 2); viewList.Adapter = adapter; }; btn_TempShowData3 = view.FindViewById <Button>(Resource.Id.id_btnShowData3); btn_TempShowData3.Click += (s, arg) => { ListView viewList = view.FindViewById <ListView>(Resource.Id.id_vwListShowData); var adapter = new VwAdapter(this, SingletonTSList.Instance.l_dataStoringList, 3); viewList.Adapter = adapter; }; btn_TempShowData4 = view.FindViewById <Button>(Resource.Id.id_btnShowData4); btn_TempShowData4.Click += (s, arg) => { ListView viewList = view.FindViewById <ListView>(Resource.Id.id_vwListShowData); var adapter = new VwAdapter(this, SingletonTSList.Instance.l_dataStoringList, 4); viewList.Adapter = adapter; }; return(view); }); // SHOW CHARTS adaptor.AddFragmentView((i, v, b) => { var view = i.Inflate(Resource.Layout.ShowCharts, v, false); btn_RetrieveFromTS2 = view.FindViewById <Button>(Resource.Id.id_btnRetrieveFromTS2); btn_RetrieveFromTS2.Click += async(s, arg) => { try { SingletonTSList.Instance.l_dataStoringList.Clear(); ThingSpeakData feeds = await DataStorage.ReadThingspeak(); SingletonTSList.Instance.l_dataStoringList = DataStorage.ThingspeakConverter(feeds); Toast.MakeText(this, string.Format("DATA OBTAINED!"), ToastLength.Long).Show(); } catch { Toast.MakeText(this, string.Format("CANNOT OBTAIN DATA FROM THINGSPEAK!"), ToastLength.Long).Show(); } }; btn_PopupShowCharts = view.FindViewById <Button>(Resource.Id.id_btnChangeGraph); btn_PopupShowCharts.Click += ShowPopupCharts; btn_ShowCharts = view.FindViewById <Button>(Resource.Id.id_btnShowGraph); btn_ShowCharts.Click += (s, arg) => { SingletonOxy.Instance.viewPlot = view.FindViewById <PlotView>(Resource.Id.id_plotView); SingletonOxy.Instance.viewPlot.Model = PlotManager.CreatePlotModel(SingletonTSList.Instance.l_dataStoringList); }; return(view); }); // MODIFY PROCESSING adaptor.AddFragmentView((i, v, b) => { var view = i.Inflate(Resource.Layout.ModifyProcessing, v, false); skb_HigherTempBD = view.FindViewById <SeekBar>(Resource.Id.id_skbHigherBoundary); tvw_HigherTempBD = view.FindViewById <TextView>(Resource.Id.id_txtSeekBarHighB); tvw_HigherTempBD.Text = "0"; skb_LowerTempBD = view.FindViewById <SeekBar>(Resource.Id.id_skbLowerBoundary); tvw_LowerTempBD = view.FindViewById <TextView>(Resource.Id.id_txtSeekBarLowB); tvw_LowerTempBD.Text = "0"; skb_HigherTempBD.SetOnSeekBarChangeListener(this); skb_LowerTempBD.SetOnSeekBarChangeListener(this); btn_GetSBSettings = view.FindViewById <Button>(Resource.Id.id_btnGetSBSettings); btn_GetSBSettings.Click += (s, arg) => { double d_vwTxt1 = 0, d_vwTxt2 = 0; if (!Double.TryParse(tvw_HigherTempBD.Text, out d_vwTxt1)) { Toast.MakeText(this, string.Format("Problem with parsing text value."), ToastLength.Long).Show(); } if (!Double.TryParse(tvw_LowerTempBD.Text, out d_vwTxt2)) { Toast.MakeText(this, string.Format("Problem with parsing text value."), ToastLength.Long).Show(); } SingletonTSList.Instance.st_WatchdogStorage.i_FirstskBar = d_vwTxt1; SingletonTSList.Instance.st_WatchdogStorage.i_SecondskBar = d_vwTxt2; Toast.MakeText(this, "Settings obtained!", ToastLength.Long).Show(); }; btn_LaunchWatchdog = view.FindViewById <Button>(Resource.Id.id_btnLaunchWG); btn_StopWatchdog = view.FindViewById <Button>(Resource.Id.id_btnStopWG); btn_LaunchWatchdog.Click += StartStartedService; btn_StopWatchdog.Click += StopStartedService; return(view); }); // MODIFY VALVE adaptor.AddFragmentView((i, v, b) => { var view = i.Inflate(Resource.Layout.ModifyValve, v, false); edt_EdIP1 = view.FindViewById <EditText>(Resource.Id.id_txtEdIP1); edt_EdPort1 = view.FindViewById <EditText>(Resource.Id.id_txtEdPort1); edt_EdOutput1 = view.FindViewById <EditText>(Resource.Id.id_txtEdOutput1); edt_EdIP2 = view.FindViewById <EditText>(Resource.Id.id_txtEdIP2); edt_EdPort2 = view.FindViewById <EditText>(Resource.Id.id_txtEdPort2); edt_EdOutput2 = view.FindViewById <EditText>(Resource.Id.id_txtEdOutput2); worker.DoWork += (s, arg) => { while (client.Connected) { try { receive = STR.ReadLine(); //this.txtEdOutput1.Text = receive;//Dispatcher.Invoke(new Action(delegate () { txtEdOutput1.AppendText("You : " + receive + "\n"); })); receive = ""; } catch (Exception ex) { Toast.MakeText(this, ex.Message.ToString(), ToastLength.Long).Show(); } } }; worker2.DoWork += (s, arg) => { if (client.Connected) { STW.WriteLine(text_to_send); //this.txtEdOutput1.Text = text_to_send; //Dispatcher.Invoke(new Action(delegate () { TextBox2.AppendText("Me : " + text_to_send + "\n"); })); } else { Toast.MakeText(this, "Send failed!", ToastLength.Long).Show(); } worker2.CancelAsync(); }; btn_StartServer = view.FindViewById <Button>(Resource.Id.id_btnStartServer); btn_StartServer.Click += (s, arg) => { Toast.MakeText(this, "Non used in this case, just possibility!", ToastLength.Long).Show(); /*TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(edt_EdPort1.Text)); * listener.Start(); * client = listener.AcceptTcpClient(); * STR = new StreamReader(client.GetStream()); * STW = new StreamWriter(client.GetStream()); * STW.AutoFlush = true; * * worker.RunWorkerAsync(); // Start receiving data from background * worker2.WorkerSupportsCancellation = true; // Ability to cancel thread*/ }; btn_Connect = view.FindViewById <Button>(Resource.Id.id_btnConnect); btn_Connect.Click += (s, arg) => { try { client = new TcpClient(); IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(edt_EdIP2.Text), int.Parse(edt_EdPort2.Text, CultureInfo.InvariantCulture)); client.Connect(IP_End); if (client.Connected) { edt_EdOutput1.Append("Connected to server"); STW = new StreamWriter(client.GetStream()); STR = new StreamReader(client.GetStream()); STW.AutoFlush = true; worker.RunWorkerAsync(); // Start receiving data from background worker2.WorkerSupportsCancellation = true; // Ability to cancel thread } } catch (Exception ex) { Toast.MakeText(this, "Cannot connect to a server!", ToastLength.Long).Show(); Toast.MakeText(this, ex.Message.ToString(), ToastLength.Long).Show(); } }; btn_Send = view.FindViewById <Button>(Resource.Id.id_btnSend); btn_Send.Click += (s, arg) => { try { if (edt_EdOutput2.Text != "") { text_to_send = edt_EdOutput2.Text; worker2.RunWorkerAsync(); } edt_EdOutput2.Text = ""; } catch { Toast.MakeText(this, "Cannot send a message!", ToastLength.Long).Show(); } }; IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress address in localIP) { if (address.AddressFamily == AddressFamily.InterNetwork) { edt_EdIP1.Text = address.ToString(); edt_EdIP2.Text = "10.0.0.71"; edt_EdPort1.Text = "123"; edt_EdPort2.Text = "123"; } } return(view); }); pager.Adapter = adaptor; #pragma warning disable CS0618 // Type or member is obsolete pager.SetOnPageChangeListener(listener: new ViewPageListenerForActionBar(ActionBar)); #pragma warning restore CS0618 // Type or member is obsolete foreach (var element in s_arrangementTab) { ActionBar.AddTab(pager.GetViewPageTab(ActionBar, element)); } }