public Result PostScriptVersion([FromBody] ScriptVersion scriptVersion)
        {
            var wId   = scriptVersion.WorkshopId;
            var names = new List <string> {
                scriptVersion.Name
            };

            if (ScriptVersionHelper.GetHaveSame(wId, names))
            {
                return(Result.GenError <Result>(Error.ScriptVersionIsExist));
            }

            var createUserId   = Request.GetIdentityInformation();
            var markedDateTime = DateTime.Now;

            scriptVersion.CreateUserId   = createUserId;
            scriptVersion.MarkedDateTime = markedDateTime;

            var valN = scriptVersion.ValueNumber == 0 ? 300 : scriptVersion.ValueNumber;
            var inN  = scriptVersion.InputNumber == 0 ? 255 : scriptVersion.InputNumber;
            var outN = scriptVersion.OutputNumber == 0 ? 255 : scriptVersion.OutputNumber;
            var msg  = new DeviceInfoMessagePacket(valN, inN, outN);

            scriptVersion.HeartPacket = msg.Serialize();
            scriptVersion.ScriptFile  = scriptVersion.ScriptFile ?? "";
            ScriptVersionHelper.Add(scriptVersion);

            RedisHelper.PublishToTable(ScriptVersionHelper.Instance.Table);
            return(Result.GenError <Result>(Error.Success));
        }
        public Result PostScriptVersion([FromBody] List <ScriptVersion> scriptVersions)
        {
            var names = scriptVersions.GroupBy(x => x.Name).Select(x => x.Key);

            if (names.Any())
            {
                var cnt =
                    ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `script_version` WHERE Name IN @Name AND MarkedDelete = 0;", new { Name = names }).FirstOrDefault();
                if (cnt > 0)
                {
                    return(Result.GenError <Result>(Error.ScriptVersionIsExist));
                }
            }

            foreach (var scriptVersion in scriptVersions)
            {
                scriptVersion.CreateUserId = Request.GetIdentityInformation();
                var valN = scriptVersion.ValueNumber == 0 ? 300 : scriptVersion.ValueNumber;
                var inN  = scriptVersion.InputNumber == 0 ? 255 : scriptVersion.InputNumber;
                var outN = scriptVersion.OutputNumber == 0 ? 255 : scriptVersion.OutputNumber;
                var msg  = new DeviceInfoMessagePacket(valN, inN, outN);
                scriptVersion.HeartPacket = msg.Serialize();
                scriptVersion.ScriptFile  = scriptVersion.ScriptFile ?? "";
            }
            ServerConfig.ApiDb.Execute(
                "INSERT INTO script_version (`CreateUserId`, `ModelId`, `Name`, `ValueNumber`, `InputNumber`, `OutputNumber`, `HeartPacket`, `ScriptFile`) " +
                "VALUES (@CreateUserId, @ModelId, @Name, @ValueNumber, @InputNumber, @OutputNumber, @HeartPacket, @ScriptFile);",
                scriptVersions);

            RedisHelper.PublishToTable(ScriptVersionHelper.Instance.Table);
            return(Result.GenError <Result>(Error.Success));
        }
        private void CheckScriptVersion(int scriptId)
        {
            var scriptVersion =
                ServerConfig.DeviceDb.Query <ScriptVersion>("SELECT * FROM `script_version` WHERE Id = @id AND `MarkedDelete` = 0;", new { id = scriptId }).FirstOrDefault();

            if (scriptVersion == null)
            {
                return;
            }
            var dataNameDictionaries =
                ServerConfig.DeviceDb.Query <DataNameDictionary>("SELECT * FROM `data_name_dictionary` WHERE ScriptId = @ScriptId AND `MarkedDelete` = 0;", new { ScriptId = scriptId });

            if (!dataNameDictionaries.Any())
            {
                return;
            }

            var group        = dataNameDictionaries.GroupBy(x => x.VariableTypeId).ToDictionary(x => x.Key, x => x.Count());
            var valueNumber  = group.ContainsKey(1) ? group[1] : 0;
            var inputNumber  = group.ContainsKey(2) ? group[2] : 0;
            var outputNumber = group.ContainsKey(3) ? group[3] : 0;

            scriptVersion.ValueNumber  = scriptVersion.ValueNumber < valueNumber ? valueNumber : scriptVersion.ValueNumber;
            scriptVersion.InputNumber  = scriptVersion.InputNumber < inputNumber ? inputNumber : scriptVersion.InputNumber;
            scriptVersion.OutputNumber = scriptVersion.OutputNumber < outputNumber ? outputNumber : scriptVersion.OutputNumber;

            var valN        = scriptVersion.ValueNumber < 300 ? 300 : scriptVersion.ValueNumber;
            var inN         = scriptVersion.InputNumber < 255 ? 255 : scriptVersion.InputNumber;
            var outN        = scriptVersion.OutputNumber < 255 ? 255 : scriptVersion.OutputNumber;
            var msg         = new DeviceInfoMessagePacket(valN, inN, outN);
            var heartPacket = msg.Serialize();
            var notify      = heartPacket != scriptVersion.HeartPacket;

            scriptVersion.HeartPacket = heartPacket;

            scriptVersion.CreateUserId   = Request.GetIdentityInformation();
            scriptVersion.MarkedDateTime = DateTime.Now;
            ServerConfig.DeviceDb.Execute(
                "UPDATE script_version SET `CreateUserId` = @CreateUserId, `MarkedDateTime` = @MarkedDateTime, `MarkedDelete` = @MarkedDelete, `ModifyId` = @ModifyId, `DeviceModelId` = @DeviceModelId, `ScriptName` = @ScriptName, " +
                "`ValueNumber` = @ValueNumber, `InputNumber` = @InputNumber, `OutputNumber` = @OutputNumber, `HeartPacket` = @HeartPacket WHERE `Id` = @Id;", scriptVersion);

            if (notify)
            {
                ServerConfig.RedisHelper.PublishToTable();
            }
        }
        public Result PostScriptVersion([FromBody] ScriptVersion scriptVersion)
        {
            scriptVersion.CreateUserId   = Request.GetIdentityInformation();
            scriptVersion.MarkedDateTime = DateTime.Now;
            var valN = scriptVersion.ValueNumber == 0 ? 300 : scriptVersion.ValueNumber;
            var inN  = scriptVersion.InputNumber == 0 ? 255 : scriptVersion.InputNumber;
            var outN = scriptVersion.OutputNumber == 0 ? 255 : scriptVersion.OutputNumber;
            var msg  = new DeviceInfoMessagePacket(valN, inN, outN);

            scriptVersion.HeartPacket = msg.Serialize();
            ServerConfig.DeviceDb.Execute(
                "INSERT INTO script_version (`CreateUserId`, `MarkedDateTime`, `MarkedDelete`, `ModifyId`, `DeviceModelId`, `ScriptName`, `ValueNumber`, `InputNumber`, `OutputNumber`, `HeartPacket`) " +
                "VALUES (@CreateUserId, @MarkedDateTime, @MarkedDelete, @ModifyId, @DeviceModelId, @ScriptName, @ValueNumber, @InputNumber, @OutputNumber, @HeartPacket);",
                scriptVersion);

            ServerConfig.RedisHelper.PublishToTable();
            return(Result.GenError <Result>(Error.Success));
        }
Exemple #5
0
        public DataResult GetDeviceLibraryState([FromRoute] int id)
        {
            var device =
                ServerConfig.DeviceDb.Query <DeviceLibrary>("SELECT `Id`, ScriptId FROM `device_library` WHERE Id = @id AND `MarkedDelete` = 0;", new { id }).FirstOrDefault();

            if (device == null)
            {
                return(Result.GenError <DataResult>(Error.DeviceNotExist));
            }

            var scriptVersion =
                ServerConfig.DeviceDb.Query <ScriptVersion>("SELECT * FROM `script_version` WHERE Id = @id AND `MarkedDelete` = 0;", new { id = device.ScriptId }).FirstOrDefault();

            if (scriptVersion == null)
            {
                return(Result.GenError <DataResult>(Error.ScriptVersionNotExist));
            }

            var usuallyDictionaries =
                ServerConfig.DeviceDb.Query <UsuallyDictionary>("SELECT * FROM `usually_dictionary` WHERE ScriptId = @ScriptId AND MarkedDelete = 0;", new { device.ScriptId });

            if (!usuallyDictionaries.Any())
            {
                return(Result.GenError <DataResult>(Error.UsuallyDictionaryNotExist));
            }

            var usuallyDictionaryTypes = ServerConfig.DeviceDb.Query <UsuallyDictionaryType>("SELECT `Id` FROM `usually_dictionary_type` WHERE MarkedDelete = 0 AND IsDetail = 1;");

            if (!usuallyDictionaryTypes.Any())
            {
                return(Result.GenError <DataResult>(Error.UsuallyDictionaryTypeNotExist));
            }

            var result = new DataResult();
            var url    = ServerConfig.GateUrl + UrlMappings.Urls["sendBackGate"];
            var msg    = new DeviceInfoMessagePacket(scriptVersion.ValueNumber, scriptVersion.InputNumber,
                                                     scriptVersion.OutputNumber);
            //向GateProxyLink请求数据
            var resp = HttpServer.Post(url, new Dictionary <string, string> {
                { "deviceInfo", new DeviceInfo
                  {
                      DeviceId    = id,
                      Instruction = msg.Serialize()
                  }.ToJSON() }
            });

            if (resp != "fail")
            {
                try
                {
                    var dataResult = JsonConvert.DeserializeObject <MessageResult>(resp);
                    if (dataResult.errno == Error.Success)
                    {
                        if (dataResult.messages.Any())
                        {
                            var data = dataResult.messages.First().Item2;
                            var res  = msg.Deserialize(data);
                            if (res != null)
                            {
                                foreach (var usuallyDictionaryType in usuallyDictionaryTypes)
                                {
                                    var usuallyDictionary =
                                        usuallyDictionaries.FirstOrDefault(
                                            x => x.VariableNameId == usuallyDictionaryType.Id);
                                    if (usuallyDictionary != null)
                                    {
                                        var v   = string.Empty;
                                        var dId = usuallyDictionary.DictionaryId;
                                        switch (usuallyDictionary.VariableTypeId)
                                        {
                                        case 1:
                                            if (((List <int>)res.vals).Count >= usuallyDictionary.DictionaryId)
                                            {
                                                v = res.vals[dId].ToString();
                                                if (usuallyDictionary.Id == 6)
                                                {
                                                    var flowCard = ServerConfig.FlowCardDb.Query <dynamic>("SELECT FlowCardName, ProductionProcessId FROM `flowcard_library` WHERE Id = @id AND MarkedDelete = 0;",
                                                                                                           new { id = v }).FirstOrDefault();
                                                    if (flowCard != null)
                                                    {
                                                        v = flowCard.FlowCardName;
                                                        var processNumber = ServerConfig.ProcessDb.Query <dynamic>(
                                                            "SELECT Id, ProcessNumber FROM `process_management` WHERE FIND_IN_SET(@DeviceId, DeviceIds) AND FIND_IN_SET(@ProductModel, ProductModels) AND MarkedDelete = 0;", new
                                                        {
                                                            DeviceId     = id,
                                                            ProductModel = flowCard.ProductionProcessId
                                                        }).FirstOrDefault();
                                                        if (processNumber != null)
                                                        {
                                                            result.datas.Add(new Tuple <int, string>(-1, processNumber.ProcessNumber));
                                                        }
                                                    }
                                                }
                                            }
                                            break;

                                        case 2:
                                            if (((List <int>)res.ins).Count >= usuallyDictionary.DictionaryId)
                                            {
                                                v = res.ins[dId].ToString();
                                            }
                                            break;

                                        case 3:
                                            if (((List <int>)res.outs).Count >= usuallyDictionary.DictionaryId)
                                            {
                                                v = res.outs[dId].ToString();
                                            }
                                            break;
                                        }
                                        result.datas.Add(new Tuple <int, string>(usuallyDictionaryType.Id, v));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.ErrorFormat($"{UrlMappings.Urls["sendBackGate"]} 返回:{resp},信息:{e.Message}");
                }
            }

            return(result);
        }