示例#1
0
        private static async Task <tResult> WriteStream(tRevertCommand oRevertCommand, ILambdaContext context)
        {
            const string myStreamName = "CloudEcoPlus";
            string       strInput;
            string       strEncoded;
            tResult      oResult = new tResult();

            try
            {
                context.Logger.LogLine("Putting records in stream : " + myStreamName);

                // Write 10 UTF-8 encoded records to the stream.

                PutRecordRequest requestRecord = new PutRecordRequest();

                requestRecord.StreamName = myStreamName;

                strInput           = JsonSerializer.Serialize(oRevertCommand);
                requestRecord.Data = new MemoryStream(Encoding.UTF8.GetBytes(strInput));
                context.Logger.LogLine("Putting records in stream 1 : " + myStreamName);

                strEncoded = Base64Encode(strInput);  // just for debug base64 encoding

                //
                context.Logger.LogLine("Putting records in stream 2 : " + myStreamName);

                requestRecord.PartitionKey = "partitionKey";

                context.Logger.LogLine("Putting records in stream 3 : " + myStreamName);

                PutRecordResponse PutRecordResult = await kinesisClient.PutRecordAsync(requestRecord);

                context.Logger.LogLine("PutRecordResult ok" + PutRecordResult.HttpStatusCode);
            }

            catch (Exception ex)
            {
                oResult.Ok   = false;
                oResult.Info = ex.Message;
                context.Logger.LogLine("Error WriteStream ");
            }

            context.Logger.LogLine("Putting records in stream 4");
            return(oResult);
        }
示例#2
0
        public async Task <tResult> FunctionHandler(tInput oInput, ILambdaContext context)
        {
            SqlConnection  oSqlConnection = null;
            string         strIMEI;
            tCommand       oCommandSend;
            tResult        oResult = new tResult();
            diags          oCurrentDiag;
            diags          oNewDiag       = new diags();
            tRevertCommand oRevertCommand = new tRevertCommand();
            DateTime       datRevertAfter;

            /*
             *  Get the IMEI
             *  Get the current diag value
             *  Reconfigure to supplied values
             *  If the current is > 45 seconds, if less than 45 seconds the unit is already in a high frequency mode and can be safely left in it
             *      Queue a message on Kinesis, which will be ignored until the time is past. When time is passed the values are returned to the original values
             *
             *
             */

            try
            {
                oSqlConnection = new SqlConnection(ecoCommon.GetSecret("CloudEcoPlus", context)); oSqlConnection.Open();
                context.Logger.LogLine("FunctionHandler 2");
            }
            catch (Exception ex)
            {
                context.Logger.LogLine("WriteRecord Ex  1" + ex.Message);
            }

            try
            {
                oSqlConnection.Open();
            }
            catch (Exception)
            {
            }


            strIMEI = ecoCommon.GetDeviceIMEINumber(oInput.SerialNumber, context, ref oSqlConnection);  // The aws iot api uses the IMEI, not the unit serial number, this is captured during manufacturing


            // --Get Current values (diags)  -----------------------------------------------------------------


            context.Logger.LogLine("FunctionHandler Get Current values ");

            oCommandSend = new tCommand();

            oCommandSend.CommandName = "diags";
            oCommandSend.UrlPath     = "diags/{devid}";
            oCommandSend.CommandJson = "";
            oCommandSend.ToEco       = false;

            tCommand oControlreportGetReply = new tCommand();

            oControlreportGetReply = PostApi(oCommandSend, strIMEI);
            if (oControlreportGetReply.PostStatus != "ok")
            {
                oResult.Ok   = false;
                oResult.Info = oControlreportGetReply.PostStatus;
                return(oResult);
            }
            ;

            oCurrentDiag = JsonSerializer.Deserialize <diags>(oControlreportGetReply.ReplyJson);

            context.Logger.LogLine("FunctionHandler Get Current values End " + oCurrentDiag.sensorInterval.ToString());

            // -- End Command -----------------------------------------------------------------



            // -- Queue the revert ------------------------------------------------------------------


            if (oCurrentDiag.sensorInterval > 45)  // Only revert back if > 45 seconds, less then is considered as already being in test mode
            {
                // Write a record to kinesis, carrying 1) When to revert 2) The previously set value to revert to

                datRevertAfter = DateTime.Now.AddMinutes(oInput.RevertWindowMinutes);
                oRevertCommand.SensorInterval = oCurrentDiag.sensorInterval;

                oRevertCommand.RevertAfter = ecoCommon.JsonDate(datRevertAfter);
                oRevertCommand.IMEI        = strIMEI;


                context.Logger.LogLine("FunctionHandler Set Revert " + oRevertCommand.RevertAfter + " From " + datRevertAfter.ToString());


                oResult = await WriteStream(oRevertCommand, context);  // Write to Kinesis



                // --Send New values diags -----------------------------------------------------------------

                oCommandSend = new tCommand();

                oNewDiag.controlInterval = oInput.SensorInterval;
                oNewDiag.sensorInterval  = oInput.SensorInterval;

                oCommandSend.CommandName = "diags";
                oCommandSend.UrlPath     = "diags/{devid}";
                oCommandSend.CommandJson = JsonSerializer.Serialize(oNewDiag);
                oCommandSend.ToEco       = true;

                tCommand oControlreportSetReply = new tCommand();

                context.Logger.LogLine("FunctionHandler Set diags ------------> " + oNewDiag.controlInterval.ToString());

                oControlreportSetReply = PostApi(oCommandSend, strIMEI);
                if (oControlreportSetReply.PostStatus != "ok")
                {
                    oResult.Ok   = false;
                    oResult.Info = oControlreportSetReply.PostStatus;
                    return(oResult);
                }
                ;
            }

            // -- End Command -----------------------------------------------------------------



            return(oResult);
        }