Ejemplo n.º 1
0
        //------------------------------------------------------------------------------------------------------------------------
        private void OnRxPythonMsg(SharpPy msg)
        {
#if DEBUG
            DebugEx.TraceLog("OnRxPython " + msg.payload + ", syncid: " + msg.syncid + ", watcherid " + msg.watcherid);
            if (PendingRequests.Count != 0)
            {
                DebugEx.TraceLogInlineBegin("PENDING syncids: ");
                foreach (var item in PendingRequests.Keys)
                {
                    DebugEx.TraceLogInline(item.ToString());
                }
                DebugEx.TraceLogInlineEnd("");
            }
            DebugEx.TraceLog("This watcherid:" + this.WatcherId);
#endif
            if (msg.watcherid == this.WatcherId)
            {
                int syncId = msg.syncid;
                if (syncId == 0)
                {
                    return;
                }
                Waiter w = null;
                w = PendingRequests.TryGetOrDefault(syncId);
                //set result and wake
                if (w != null)
                {
                    lock (w)
                    {
                        w.response = msg.payload;
                        Monitor.Pulse(w);
                    }
                }
                else
                {
                    DebugEx.TraceLog("w is null");
                }
            }
        }
Ejemplo n.º 2
0
        //------------------------------------------------------------------------------------------------------------------------
        public object GetSingleValue <Req>(Req data)
        {
            var syncid = GetNewSyncId();
            var w      = new Waiter();

            lock (PendingRequests)
                PendingRequests.Add(syncid, w);
            //construct sharppy msg
            SharpPy msg = new SharpPy()
            {
                isRequest = true,
                watcherid = WatcherId,
                syncid    = syncid,
                pin       = this.sensor.Pin.ToString(),
                operation = (data as SharpPy).operation,
                payload   = (data as SharpPy).payload,
            };

            lock (w)
            {
                this.sharppyiface.Send2python(msg);
                Monitor.Wait(w);
            }
            lock (PendingRequests)
                PendingRequests.Remove(syncid);

            if (w.response != null)
            {
                if (w.response != prevData && w.response != "255" && w.response != "65535")
                {
                    prevData = w.response;
                    DebugEx.TraceLog("CONT: wId:" + WatcherId + " sId:" + syncid + " response:" + w.response);
                    return(this.sensor.DeserializePayload(w.response));
                }
                //else
                //    DebugEx.TraceLog("DROP: wId:" + WatcherId + " sId:" + syncid + " response:" + w.response);
            }
            return(null);
        }