Пример #1
0
 private static async void OnOffSensor_04_digitalOnOffSensorSend(OnOffDigitalSensorMgr sender, OnOffDigitalSensorMgr.OnOffSensorEventArgs e)
 {
     await WriteOnOffEntityToCloud(e);
 }
Пример #2
0
        static async Task WriteOnOffEntityToCloud(OnOffDigitalSensorMgr.OnOffSensorEventArgs e)
        {
            //Console.WriteLine("WriteOnOff to Cloud Common-method Thread-Id: " + e.DestinationTable + " "+ Thread.CurrentThread.ManagedThreadId.ToString());

            bool validStorageAccount           = false;
            CloudStorageAccount storageAccount = null;
            Exception           CreateStorageAccountException = null;

            try
            {
                storageAccount      = Common.CreateStorageAccountFromConnectionString(connectionString);
                validStorageAccount = true;
            }
            catch (Exception ex0)
            {
                CreateStorageAccountException = ex0;
            }
            if (!validStorageAccount)
            {
                return;
            }
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            DateTime timeZoneCorrectedDateTime = DateTime.Now.AddMinutes(timeZoneOffset);

            // actDateTime is corrected for timeZoneOffset and DayLightSavingTime
            DateTime actDateTime = timeZoneCorrectedDateTime.AddMinutes(GetDlstOffset(timeZoneCorrectedDateTime));


            CloudTable cloudTable = tableClient.GetTableReference(e.DestinationTable + actDateTime.Year);

            if (!existingOnOffTables.Contains(cloudTable.Name))
            {
                try
                {
                    await cloudTable.CreateIfNotExistsAsync();

                    existingOnOffTables.Add(cloudTable.Name);
                }
                catch (Exception exc)
                {
                    Console.WriteLine("Could not create On/Off Table with name: \r\n" + cloudTable.Name + "\r\nCheck your Internet Connection.\r\nAction aborted.");
                    return;
                }
            }

            // formatting the PartitionKey this way to have the tables sorted with last added row upmost
            string partitionKey = onOffTablePartPrefix + actDateTime.Year + "-" + (12 - actDateTime.Month).ToString("D2");

            // formatting the RowKey this way to have the tables sorted with last added row upmost
            string rowKey = (10000 - actDateTime.Year).ToString("D4") + (12 - actDateTime.Month).ToString("D2") + (31 - actDateTime.Day).ToString("D2")
                            + (23 - actDateTime.Hour).ToString("D2") + (59 - actDateTime.Minute).ToString("D2") + (59 - actDateTime.Second).ToString("D2");


            //string sampleTime = actDateTime.Month.ToString("D2") + "/" + actDateTime.Day.ToString("D2") + "/" + actDateTime.Year + " " + actDateTime.Hour.ToString("D2") + ":" + actDateTime.Minute.ToString("D2") + ":" + actDateTime.Second.ToString("D2");
            string   sampleTime = actDateTime.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            TimeSpan tflSend    = e.TimeFromLastSend;
            string   timeFromLastSendAsString = tflSend.Days.ToString("D3") + "-" + tflSend.Hours.ToString("D2") + ":" + tflSend.Minutes.ToString("D2") + ":" + tflSend.Seconds.ToString("D2");

            string onTimeDayAsString = e.OnTimeDay.ToString(@"ddd\-hh\:mm\:ss", CultureInfo.InvariantCulture);

            Dictionary <string, EntityProperty> entityDictionary = new Dictionary <string, EntityProperty>();

            entityDictionary.Add("SampleTime", EntityProperty.GeneratePropertyForString(sampleTime));
            entityDictionary.Add("ActStatus", EntityProperty.GeneratePropertyForString(e.ActState ? "Off" : "On"));
            entityDictionary.Add("LastStatus", EntityProperty.GeneratePropertyForString(e.OldState ? "Off" : "On"));
            entityDictionary.Add("TimeFromLast", EntityProperty.GeneratePropertyForString(timeFromLastSendAsString));
            entityDictionary.Add("OnTimeDay", EntityProperty.GeneratePropertyForString(onTimeDayAsString));

            DynamicTableEntity dynamicTableEntity = await Common.InsertOrMergeEntityAsync(cloudTable, new DynamicTableEntity(partitionKey, rowKey, null, entityDictionary));
        }
Пример #3
0
 private static async void OnOffSensor_01_digitalOnOffSensorSend(OnOffDigitalSensorMgr sender, OnOffDigitalSensorMgr.OnOffSensorEventArgs e)
 {
     // Console.WriteLine("On/Off-Sensor event happened. ActState = " + e.ActState.ToString() + " , oldState = " + e.OldState.ToString());
     await WriteOnOffEntityToCloud(e);
 }