Esempio n. 1
0
        public void FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            tRevertCommand oRevertCommand;
            DateTime       datRevert;
            diags          oNewDiag = new diags();
            tCommand       oCommandSend;

            context.Logger.LogLine("FunctionHandler 1 1145");

            foreach (var record in kinesisEvent.Records)
            {
                string recordData = GetRecordContents(record.Kinesis);

                oRevertCommand = JsonSerializer.Deserialize <tRevertCommand>(recordData);

                if (oRevertCommand.MessageName.ToLower() != "diagintervalrevert")
                {
                    continue;
                }

                context.Logger.LogLine("FunctionHandler rd  >" + recordData);

                datRevert = ecoCommon.DateJson(oRevertCommand.RevertAfter); // encoded as dd/mm/yyyy hh:mm:ss
                if (DateTime.Compare(DateTime.Now, datRevert) < 0)
                {
                    context.Logger.LogLine("FunctionHandler Not my time " + datRevert.ToString() + " " + DateTime.Now.ToString());

                    context.Logger.LogLine(oRevertCommand.MakeItFail.ToString()); // Property does not exists, deliberately let it fail (cludge) and leave the message on the queue

                    continue;                                                     // Not my time, let the stream represent until ready
                }


                // --Send New values diags to the Eco+ -----------------------------------------------------------------



                oCommandSend = new tCommand();

                oNewDiag.controlInterval = oRevertCommand.SensorInterval;
                oNewDiag.sensorInterval  = oRevertCommand.SensorInterval;

                context.Logger.LogLine("FunctionHandler This is my time, Reverting " + datRevert.ToString() + " " + DateTime.Now.ToString() + " To Interval " + oRevertCommand.SensorInterval.ToString());

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

                tCommand oControlreportSetReply = new tCommand();

                oControlreportSetReply = PostApi(oCommandSend, oRevertCommand.IMEI);

                // -- End Command -----------------------------------------------------------------
            }
        }
Esempio n. 2
0
        private tCommand PostApi(tCommand oCommandSend, string strIMEI)
        {
            HttpWebRequest  request;
            HttpWebResponse response = null /* TODO Change to default(_) if this is not a reference type */;
            StreamReader    reader;
            Uri             address;
            string          appId;
            string          strResponse = "";


            Stream postStream = null;
            string strUrl;

            byte[]        byteData;
            StringBuilder data;
            tCommand      oCommandReply = new tCommand();

            oCommandReply.PostStatus = "offline";

            strUrl = strApiUrl;

            strUrl = strUrl + oCommandSend.UrlPath;
            strUrl = strUrl.Replace("{devid}", strIMEI);
            strUrl = strUrl.Replace("{pathelement}", oCommandSend.PathElement);

            address = new Uri(strUrl);

            try
            {
                // The reply is mainly the same as the send, for easier processing by the client, ie send and reply together

                oCommandReply.CommandJson   = oCommandSend.CommandJson;
                oCommandReply.CommandName   = oCommandSend.CommandName;
                oCommandReply.CompanionJson = oCommandSend.CompanionJson;
                oCommandReply.CompanionName = oCommandSend.CompanionName;
                oCommandReply.EndTime       = oCommandSend.EndTime;
                oCommandReply.PathElement   = oCommandSend.PathElement;
                oCommandReply.StartTime     = oCommandSend.StartTime;
                oCommandReply.ToEco         = oCommandSend.ToEco;

                request = (HttpWebRequest)WebRequest.Create(address);
                if (oCommandSend.ToEco == true)
                {
                    request.Method = "PATCH";
                }
                else
                {
                    request.Method = "GET";
                }

                request.ContentType = "application/x-www-form-urlencoded";

                request.Headers.Add("x-api-key", strApiKey);

                appId = "ss-form";
                if (oCommandSend.ToEco == true)
                {
                    data = new StringBuilder();

                    data.Append(oCommandSend.CommandJson);

                    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

                    // Set the content length in the request headers
                    request.ContentLength = byteData.Length;

                    // Write data
                    try
                    {
                        postStream = request.GetRequestStream();
                        postStream.Write(byteData, 0, byteData.Length);
                    }
                    finally
                    {
                        if (postStream != null)
                        {
                            postStream.Close();
                        }
                    }
                }
                else
                {
                    if (postStream != null)
                    {
                        postStream.Close();
                    }
                }
                try
                {
                    // Get response
                    response = (HttpWebResponse)request.GetResponse();

                    // Get the response stream into a reader
                    reader = new StreamReader(response.GetResponseStream());

                    strResponse = reader.ReadToEnd();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        oCommandReply.PostStatus = "ok";
                        oCommandReply.ReplyJson  = strResponse;

                        return(oCommandReply);
                    }
                    else
                    {
                    }
                }
                finally
                {
                    if (response != null)
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            oCommandReply.PostStatus = "ok";
                        }

                        if (response.StatusCode.ToString().IndexOf("504") != -1)
                        {
                            oCommandReply.PostStatus = "timeout";
                        }
                        response.Close();
                    }
                    else
                    {
                        oCommandReply.PostStatus = "offline";
                    }
                }
            }
            catch (Exception ex)
            {
            }


            return(oCommandReply);
        }