Beispiel #1
0
        public DataResult Analysis()
        {
            var param  = Request.GetRequestParams();
            var insStr = param.GetValue("ins");
            var msgStr = param.GetValue("msg");
            //0xF3,0x02,0x2C,0x01,0xFF,0x00,0xFF,0x00,0x67,0x12
            var result = new DataResult();

            if (insStr.Length == 0)
            {
                return(result);
            }

            insStr = insStr.ToLower();
            if (insStr.Contains("0x"))
            {
                insStr = insStr.Replace("0x", "");
            }

            var insList = insStr.Split(",").Select(x => x.PadLeft(2, '0')).ToArray();
            var val     = Convert.ToInt32(insList[3] + insList[2], 16);
            var ins     = Convert.ToInt32(insList[5] + insList[4], 16);
            var outs    = Convert.ToInt32(insList[7] + insList[6], 16);

            var msg = new DeviceInfoMessagePacket(val, ins, outs);
            var t   = msg.Deserialize(msgStr);

            if (t != null)
            {
                result.datas.Add(t.vals);
                result.datas.Add(t.ins);
                result.datas.Add(t.outs);
            }
            return(result);
        }
Beispiel #2
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);
        }