Example #1
0
        public PointResponse Cancel(int blockId, int pointId)
        {
            // This method is better because it doesn't need NHibernate

            var result = new PointResponse()
            {
                BlockID   = blockId,
                PointID   = pointId,
                Error     = false,
                Message   = string.Empty,
                StartTime = DateTime.Now
            };

            if (FutureActionManager.Current.TryRemove(pointId, out PointTimer pt))
            {
                // Do it immediately
                pt.Cancel(true);

                // Cancelled timers will still execute the handler but by the time they do the corresponding
                // PointTimer object will already have been removed from the timers ConcurrentDictionary, or
                // Cancel will be true so when the handler is finally called at that time nothing will happen.
            }
            else
            {
                result.Error   = true;
                result.Message = "No pending action found to cancel";
            }

            return(result);
        }
Example #2
0
        public PointResponse SendSetPointStateCommand(int pointId, bool state)
        {
            Point point = DA.Current.Single <Point>(pointId);

            if (point == null)
            {
                throw new InvalidOperationException($"Point not found with PointID = {pointId}");
            }

            using (var sender = Connect(point.Block))
            {
                byte[] buffer = CreateSetPointStateBuffer(point, state);

                Log.Write(point.Block.BlockID, $"WagoConnection: Sending SetPointState message to block: BlockID = {point.Block.BlockID}, PointID = {point.PointID}, Data = {WagoUtility.GetDataString(buffer)}");

                byte[] recvBuffer = new byte[65];

                int bytesRecv = SendMessageToBlock(sender, buffer, recvBuffer, false);

                // the recvBuffer is not used for SetPointState because there is no response

                PointResponse result = point.CreatePointResponse();

                return(result);
            }
        }
Example #3
0
        public PointResponse SetPointState(int blockId, int pointId, bool state, uint duration = 0)
        {
            // This method is better because it doesn't need NHibernate

            // Compose Action object
            PointAction action = new PointAction(pointId, state);

            // SetPointState messages do not have a return value because we do not wait for any data back from Wago like we do in GetBlockState.

            // Get RequestQueue, and push data into queue
            IRequestQueue queue = QueueCollection.Current.Item(blockId);

            if (queue == null)
            {
                throw new InvalidOperationException($"Could not find queue with BlockID {blockId}");
            }

            // Send request for block state
            queue.Push(action);

            // Wait a bit for the the response to appear
            PointResponse result = (PointResponse)queue.GetResponse(action.MessageID);

            // When duration is zero only send one message otherwise send the first message and then send another using a Timer.
            if (!result.Error && duration > 0)
            {
                PointAction futureAction = new PointAction(pointId, !action.State);
                HandleFutureAction(queue, action, futureAction, duration);
            }

            return(result);
        }
Example #4
0
        public static PointResponse CreatePointResponse(this Point point)
        {
            PointResponse result = new PointResponse
            {
                Error     = false,
                Message   = string.Empty,
                PointID   = 0,
                BlockID   = 0,
                StartTime = DateTime.Now
            };

            if (point != null)
            {
                result.BlockID = point.Block.BlockID;
                result.PointID = point.PointID;
            }

            return(result);
        }