Beispiel #1
0
        async void MessageReceived(string msg)
        {
            try
            {
                log.WriteLogToFile(msg, "WebSocketLog");

                string type = "";
                Dictionary <string, Object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(msg.ToLower());
                if (dic["content"] != null && dic["senderid"] != null)
                {
                    Dictionary <string, Object> Commands = JsonConvert.DeserializeObject <Dictionary <string, Object> >(dic["content"].ToString());

                    if (!string.IsNullOrEmpty(Commands["type"].ToString()))
                    {
                        type = Commands["type"].ToString();
                    }

                    switch (type.Trim())
                    {
                    case "dataupdate":
                        log.WriteLogToFile("云端数据更新", "WebSocketLog");
                        if (!string.IsNullOrEmpty(Commands["modulename"].ToString()))
                        {
                            Pull pull = new Pull();
                            switch (Commands["modulename"].ToString().Trim())
                            {
                            case "app":
                                await pull.PullAppData();

                                break;

                            case "dev":
                                await pull.PullDevData();

                                break;

                            case "file":
                                await pull.PullFileData();

                                break;

                            case "init":
                                await pull.PullInitData();

                                break;

                            case "prog":
                                await pull.PullProgramData();

                                break;

                            case "review":
                                await pull.PullReviewData();

                                break;

                            case "shopinfo":
                                await pull.PullShopInfoData();

                                break;

                            case "system":
                                await pull.PullSystemData();

                                break;

                            default:
                                break;
                            }
                        }
                        break;

                    case "devicecommand":
                        log.WriteLogToFile("设备命令", "WebSocketLog");
                        if (!string.IsNullOrEmpty(Commands["devicecode"].ToString()) && string.IsNullOrEmpty(Commands["cmdstr"].ToString()) && string.IsNullOrEmpty(Commands["msgtype"].ToString()))
                        {
                            var         devicecode  = Commands["devicecode"].ToString();
                            var         cmdstr      = Commands["cmdstr"].ToString();
                            var         msgtype     = Commands["msgtype"].ToString();
                            MsgTemplate msgTemplate = new MsgTemplate();
                            msgTemplate.SenderID    = Method.ServerAddr;
                            msgTemplate.MessageType = msgtype;
                            msgTemplate.Content     = cmdstr;
                            msgTemplate.ReceiverID  = devicecode;
                            await Method.SendMsgAsync(msgTemplate);
                        }
                        break;

                    default:
                        break;
                    }
                }
                if (WebSocketReceiveEvent != null)
                {
                    WebSocketReceiveEvent(type, "");
                }
            }
            catch (Exception ex)
            {
                log.WriteLogToFile(ex.ToString(), "WebSocketLog");
            }
        }