private async Task <bool> OpTypeTira(ProductionOrder productionOrder)
        {
            var trigger = _configuration["TagIdTrigger"];
            // Finaliza Ordem anterior
            var triggerPlc = _interleverDb.Write("2", trigger, "Linha");


            List <Thing> thingsGetList = new List <Thing>();
            var          trigger3      = _configuration["TagIdTrigger"];
            await _interleverDb.Write("2", trigger, "Linha");

            var phases = productionOrder.recipe.phases;

            /*Console.WriteLine("");
             * Console.WriteLine("");
             * Console.WriteLine("");
             * Console.WriteLine("Phase: ------------------------------------------------------------ ");
             * Console.WriteLine("Phase-> " +JsonConvert.SerializeObject(phases).ToString() );
             * Console.WriteLine("");
             * Console.WriteLine("");
             * Console.WriteLine("");
             * Console.WriteLine("Loop de phases");
             * Console.WriteLine(""); */
            foreach (var phase in phases)
            {
                foreach (var phaseParameter in phase.phaseParameters)
                {
                    /*Console.WriteLine("");
                    *  Console.WriteLine("");
                    *  Console.WriteLine("");
                    *  Console.WriteLine("phaseParameter: ------------------------------------------------------------ ");
                    *  Console.WriteLine("phaseParameter-> " +JsonConvert.SerializeObject(phaseParameter).ToString() );
                    *  Console.WriteLine("");
                    *  Console.WriteLine("");
                    *  Console.WriteLine("");*/
                    string value = phaseParameter.setupValue;

                    var tag = await _otherApi.GetTag(phaseParameter.tag.tagId);

                    //var tag = phaseParameter.tag;
                    //Console.WriteLine("tag: ------------------------------------------------------------ ");
                    //Console.WriteLine("tag-> " +JsonConvert.SerializeObject(tag).ToString() );
                    var thingGroup = await _otherApi.GetThingGroup(tag.thingGroupId);

                    //Console.WriteLine("Loop thingGroup: ------------------------------------------------------------ ");

                    foreach (var thingId in thingGroup.thingsIds)
                    {
                        Console.WriteLine("Foi no Foreach");
                        Console.WriteLine("");
                        Thing thing = null;

                        thing = thingsGetList.Where(x => x.thingId == thingId).FirstOrDefault();
                        Console.WriteLine("thingId" + thingId.ToString());
                        if (thing == null)
                        {
                            Console.WriteLine("get na thing");
                            thing = await _otherApi.GetThing(thingId);

                            //Console.WriteLine(JsonConvert.SerializeObject(await _otherApi.GetThing(thingId)).ToString() );
                            if (thing != null)
                            {
                                thingsGetList.Add(thing);
                            }
                        }
                        //Console.WriteLine(JsonConvert.SerializeObject(thing).ToString() );
                        if (string.IsNullOrEmpty(tag.physicalTag))
                        {
                            Console.WriteLine("");
                            Console.WriteLine("");
                            Console.WriteLine("Deu ruim");
                            Console.WriteLine("");
                            Console.WriteLine("");
                            continue;
                        }
                        Console.Write("Physical da tag :");
                        Console.WriteLine(tag.physicalTag);
                        Console.Write("Valor da tag :");
                        Console.WriteLine(value);
                        Console.Write("phisical connection : ");
                        Console.WriteLine(thing.physicalConnection);
                        Console.WriteLine("");
                        var e = _interleverDb.Write(value, tag.physicalTag, thing.physicalConnection);
                        Console.WriteLine("Fim");
                    }
                }
            }

            // envia o trigger para o PLC
            var triggerPlc2 = await _interleverDb.Write("5", trigger, "Linha");

            return(true);
        }
Exemplo n.º 2
0
        public async Task <(bool, string)> ReadTags()
        {
            string   numeroRolo     = "";
            string   numeroOP       = "";
            string   numeroCodTira  = "";
            DateTime dateMonitoring = DateTime.Now;
            // faz get nas tags de input
            var tagsInput = await _otherApi.GetTagList("input");

            //Faz get dos alarm
            var thingAlarms = await _otherApi.GetAlarm();

            //Faz get da tag com o número do rolo
            var tagRolo = await _otherApi.GetTag(Convert.ToInt32(_configuration["IdTagRolo"]));

            if (tagRolo != null)
            {
                numeroRolo = await _interlevelDb.Read(tagRolo.physicalTag);
            }
            //Faz get da tag com o número da OP
            var tagOP = await _otherApi.GetTag(Convert.ToInt32(_configuration["IdTagOP"]));

            if (tagOP != null)
            {
                numeroOP = await _interlevelDb.Read(tagOP.physicalTag);
            }
            //Faz get da tag com o código da Tira
            var tagCodTira = await _otherApi.GetTag(Convert.ToInt32(_configuration["IdTagCodTira"]));

            if (tagCodTira != null)
            {
                numeroCodTira = await _interlevelDb.Read(tagCodTira.physicalTag);
            }



            foreach (var tag in tagsInput)
            {
                foreach (var thingId in tag.thingGroup.thingsIds)
                {
                    DateTime dt = DateTime.Now;
                    // Verifica se a thing existe na lista..... se não existir faz get e adiciona na lista
                    //Console.WriteLine("antes thing" + new TimeSpan((DateTime.Now - dt).Ticks).TotalMilliseconds.ToString());
                    var thing = ReturnThing(thingId);
                    if (thing == null)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("");
                        Console.WriteLine("");
                        Console.WriteLine("Retornou não encontrado thing");
                        Console.WriteLine("");
                        Console.WriteLine("");
                        Console.WriteLine("");
                        return(false, "Thing não encontrada. Thing Id = " + thingId.ToString());
                    }
                    //Console.WriteLine("depois thing" + new TimeSpan((DateTime.Now - dt).Ticks).TotalMilliseconds.ToString());
                    var valueTag = await _interlevelDb.Read(tag.physicalTag);

                    //Console.WriteLine("depois select" + new TimeSpan((DateTime.Now - dt).Ticks).TotalMilliseconds.ToString());
                    var tagHistorian = new {
                        thingId = thingId,
                        tag     = tag.tagName,
                        value   = valueTag,
                        group   = tag.tagGroup,
                        date    = dateMonitoring.Ticks
                    };
                    //Console.WriteLine("antes post" + new TimeSpan((DateTime.Now - dt).Ticks).TotalMilliseconds.ToString());
                    var post = await _otherApi.PostHistorian(tagHistorian);

                    Console.WriteLine("depois post" + new TimeSpan((DateTime.Now - dt).Ticks).TotalMilliseconds.ToString());
                    var tagHistorianRolo = new {
                        thingId = thingId,
                        tag     = "rolo",
                        value   = numeroRolo,
                        group   = "Linha",
                        date    = dateMonitoring.Ticks
                    };

                    var postNumRolo = await _otherApi.PostHistorian(tagHistorianRolo);

                    var tagHistorianOP = new {
                        thingId = thingId,
                        tag     = "ordem",
                        value   = numeroOP,
                        group   = "Linha",
                        date    = dateMonitoring.Ticks
                    };

                    var postNumOp = await _otherApi.PostHistorian(tagHistorianOP);

                    var tagHistorianCodTira = new {
                        thingId = thingId,
                        tag     = "codTira",
                        value   = numeroCodTira,
                        group   = "Linha",
                        date    = dateMonitoring.Ticks
                    };

                    var postCodTira = await _otherApi.PostHistorian(tagHistorianCodTira);
                }
            }



            foreach (var thingAlarm in thingAlarms)
            {
                foreach (var alarm in thingAlarm.alarms)
                {
                    var tagHistorian = new {
                        thingId = alarm.thingId,
                        tag     = alarm.alarmName,
                        value   = alarm.alarmDescription,
                        group   = "Alarme",
                        date    = dateMonitoring.Ticks
                    };

                    var post = _otherApi.PostHistorian(tagHistorian);
                }
            }


            return(true, string.Empty);
        }