Exemple #1
0
        private void onEvent(Event evt)
        {
            switch (evt.Code)
            {
            case AdapterMsg.ID_Event_AlarmOrEvent:
                var alarm = StdJson.ObjectFromUtf8Stream <AdapterAlarmOrEvent>(evt.Payload);
                if (alarm != null)
                {
                    callback?.Notify_AlarmOrEvent(alarm);
                }
                break;

            case AdapterMsg.ID_Event_DataItemsChanged:
                var items = StdJson.ObjectFromUtf8Stream <DataItemValue[]>(evt.Payload);
                if (items != null)
                {
                    callback?.Notify_DataItemsChanged(items);
                }
                break;

            default:
                Console.Error.WriteLine("Unknown event code: " + evt.Code);
                break;
            }
        }
 private async Task <T> SendRequest <T>(AdapterMsg requestMsg)
 {
     using (Response res = await connection.SendRequest(requestMsg.GetMessageCode(), stream => StdJson.ObjectToStream(requestMsg, stream))) {
         if (res.Success)
         {
             return(StdJson.ObjectFromUtf8Stream <T>(res.SuccessPayload));
         }
         else
         {
             throw new Exception(res.ErrorMsg);
         }
     }
 }
        private static async Task Loop(TcpConnectorSlave connector, AdapterBase module)
        {
            Process?parentProcess = null;

            using (Request request = await connector.ReceiveRequest(5000)) {
                if (request.Code != AdapterMsg.ID_ParentInfo)
                {
                    throw new Exception("Missing ParentInfo request");
                }
                ParentInfoMsg?info = StdJson.ObjectFromUtf8Stream <ParentInfoMsg>(request.Payload);
                if (info == null)
                {
                    throw new Exception("ParentInfoMsg is null");
                }
                parentProcess = Process.GetProcessById(info.PID);
                connector.SendResponseSuccess(request.RequestID, s => { });
            }

            Thread t = new Thread(() => { ParentAliveChecker(parentProcess); });

            t.IsBackground = true;
            t.Start();

            var  helper = new AdapterHelper(module, connector);
            bool run    = true;

            while (run)
            {
                using (Request request = await connector.ReceiveRequest()) {
                    helper.ExecuteAdapterRequestAsync(request);
                    bool shutdown = request.Code == AdapterMsg.ID_Shutdown;
                    run = !shutdown;
                }
            }

            // Wait until parent process kills us (after Shutdown method completed)
            while (true)
            {
                await Task.Delay(1000);
            }
        }
 private static T Deserialize <T>(MemoryStream stream) => StdJson.ObjectFromUtf8Stream <T>(stream);
 private static T Deserialize <T>(MemoryStream stream) => StdJson.ObjectFromUtf8Stream <T>(stream) ?? throw new Exception("Unexpected null value");