public void Write(BinaryState state, bool commit = true)
        {
            if (commit == false)
            {
                return;
            }

            lock (_syncRoot)
            {
                if (state == BinaryState.High)
                {
                    _sender.Send(_codeSequencePair.OnSequence);
                }
                else if (state == BinaryState.Low)
                {
                    _sender.Send(_codeSequencePair.OffSequence);
                }
                else
                {
                    throw new NotSupportedException();
                }

                _state = state;
            }
        }
        public BinaryOutputWithState(IBinaryOutput output, BinaryState state)
        {
            if (output == null) throw new ArgumentNullException(nameof(output));

            Output = output;
            State = state;
        }
Esempio n. 3
0
 public void Write(BinaryState state, bool commit = true)
 {
     lock (_syncRoot)
     {
         _state = CoerceState(state);
     }
 }
        public StateMachineState WithPort(IBinaryOutput output, BinaryState state)
        {
            if (output == null) throw new ArgumentNullException(nameof(output));

            _outputs.Add(new Tuple<IBinaryOutput, BinaryState>(output, state));
            return this;
        }
Esempio n. 5
0
 public void SetInternalState(BinaryState newState)
 {
     lock (_syncRoot)
     {
         _state = newState;
         StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(Read()));
     }
 }
Esempio n. 6
0
        private BinaryState CoerceState(BinaryState state)
        {
            if (!_stateIsInverted)
            {
                return state;
            }

            return state == BinaryState.High ? BinaryState.Low : BinaryState.High;
        }
        private BinaryState CoerceState(BinaryState state)
        {
            if (InvertValue)
            {
                return state == BinaryState.High ? BinaryState.Low : BinaryState.High;
            }

            return state;
        }
Esempio n. 8
0
 public void SetInternalState(BinaryState state)
 {
     lock (_syncRoot)
     {
         var oldState = _state;
         _state = state;
         StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, state));
     }
 }
        public void Write(BinaryState state, bool commit)
        {
            state = CoerceState(state);
            Board.SetPortState(Number, state);

            if (commit)
            {
                Board.CommitChanges();
            }
        }
Esempio n. 10
0
 public static string BinaryStateToString(BinaryState state)
 {
     switch (state)
     {
         case BinaryState.Off:
             return "0";
         case BinaryState.On:
             return "1";
         default:
             throw new ArgumentOutOfRangeException(nameof(state), state, null);
     }
 }
Esempio n. 11
0
        BinaryState IBinaryInput.Read()
        {
            lock (_syncRoot)
            {
                BinaryState currentState = CoerceState(Pin.Read() == GpioPinValue.High ? BinaryState.High : BinaryState.Low);
                if (currentState != _previousState)
                {
                    _previousState = currentState;
                    StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(currentState));
                }

                return currentState;
            }
        }
Esempio n. 12
0
        private BinaryState CoerceState(BinaryState state)
        {
            if (!_isStateInverted)
            {
                return state;
            }

            if (state == BinaryState.High)
            {
                return BinaryState.Low;
            }

            return BinaryState.High;
        }
Esempio n. 13
0
        BinaryState IBinaryInput.Read()
        {
            lock (_syncRoot)
            {
                BinaryState currentState = CoerceState(Pin.Read() == GpioPinValue.High ? BinaryState.High : BinaryState.Low);
                if (currentState != _previousState)
                {
                    _previousState = currentState;
                    StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(currentState));
                }

                return(currentState);
            }
        }
Esempio n. 14
0
        private void OnControllerStateChanged(object sender, IOBoardStateChangedEventArgs e)
        {
            bool oldState = e.OldState.GetBit(Number);
            bool newState = e.NewState.GetBit(Number);

            if (oldState == newState)
            {
                return;
            }

            BinaryState state = Read();

            StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(state));
        }
Esempio n. 15
0
        public void TestProjectableness()
        {
            var textState   = "test-state";
            var state       = new BinaryState("123", typeof(string), 1, Encoding.UTF8.GetBytes(textState), 1, Metadata.With("value", "op"));
            var projectable = new BinaryProjectable(state, Enumerable.Empty <IEntry>(), "p123");

            Assert.Equal("op", projectable.BecauseOf()[0]);
            Assert.Equal(Encoding.UTF8.GetBytes(textState), projectable.DataAsBytes());
            Assert.Equal("123", projectable.DataId);
            Assert.Equal(1, projectable.DataVersion());
            Assert.Equal("value", projectable.Metadata);
            Assert.Equal("p123", projectable.ProjectionId);
            Assert.Equal(typeof(String).AssemblyQualifiedName, projectable.Type);
            Assert.Equal(1, projectable.TypeVersion);
        }
Esempio n. 16
0
        public void TestBasicBinaryState()
        {
            var state      = "test-state";
            var bytes      = Encoding.UTF8.GetBytes(state);
            var basicState = new BinaryState("123", typeof(string), 1, bytes, 1);

            Assert.True(basicState.IsBinary);
            Assert.False(basicState.IsText);
            Assert.False(basicState.IsEmpty);
            Assert.Equal("123", basicState.Id);
            Assert.Equal(typeof(string).AssemblyQualifiedName, basicState.Type);
            Assert.Equal(1, basicState.TypeVersion);
            Assert.Equal(state, Encoding.UTF8.GetString(basicState.Data));
            Assert.False(basicState.HasMetadata);
        }
Esempio n. 17
0
        public void Write(BinaryState state, bool commit = true)
        {
            lock (_syncRoot)
            {
                state = CoerceState(state);

                if (state == _previousState)
                {
                    return;
                }

                Pin.Write(state == BinaryState.High ? GpioPinValue.High : GpioPinValue.Low);
                _previousState = state;
                StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(state));
            }
        }
Esempio n. 18
0
        public void Write(BinaryState state, bool commit = true)
        {
            lock (_syncRoot)
            {
                state = CoerceState(state);

                if (state == _previousState)
                {
                    return;
                }

                Pin.Write(state == BinaryState.High ? GpioPinValue.High : GpioPinValue.Low);
                _previousState = state;
                StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(state));
            }
        }
Esempio n. 19
0
        public void Write(BinaryState state, WriteBinaryStateMode mode = WriteBinaryStateMode.Commit)
        {
            lock (_syncRoot)
            {
                state = CoerceState(state);

                if (state == _previousState)
                {
                    return;
                }

                Pin.Write(state == BinaryState.High ? GpioPinValue.High : GpioPinValue.Low);

                var oldState = _previousState;
                _previousState = state;
                StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, state));
            }
        }
Esempio n. 20
0
        public void TestBinaryStateWithMetadataValue()
        {
            var state = "test-state";
            var bytes = Encoding.UTF8.GetBytes(state);
            var metadataValueState = new BinaryState("123", typeof(string), 1, bytes, 1, Metadata.WithValue("value"));

            Assert.True(metadataValueState.IsBinary);
            Assert.False(metadataValueState.IsText);
            Assert.False(metadataValueState.IsEmpty);
            Assert.Equal("123", metadataValueState.Id);
            Assert.Equal(typeof(string).AssemblyQualifiedName, metadataValueState.Type);
            Assert.Equal(1, metadataValueState.TypeVersion);
            Assert.Equal(state, Encoding.UTF8.GetString(metadataValueState.Data));
            Assert.True(metadataValueState.HasMetadata);
            Assert.True(metadataValueState.Metadata.HasValue);
            Assert.Equal("value", metadataValueState.Metadata.Value);
            Assert.False(metadataValueState.Metadata.HasOperation);
        }
Esempio n. 21
0
        public void Write(BinaryState state, bool commit = true)
        {
            state = CoerceState(state);

            foreach (var output in _outputs)
            {
                output.Write(state, false);
            }

            if (commit)
            {
                foreach (var output in _outputs)
                {
                    output.Write(state);
                }
            }

            _state = state;
        }
Esempio n. 22
0
        public void Write(BinaryState state, WriteBinaryStateMode mode = WriteBinaryStateMode.Commit)
        {
            if (_state == state)
            {
                return;
            }

            var payload = "off";

            if (state == BinaryState.High)
            {
                payload = "on";
            }

            _messageBroker.Publish(_topic, Encoding.UTF8.GetBytes(payload), MqttQosLevel.AtMostOnce, true);
            StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(_state, state));

            _state = state;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        protected override void OnPropertyChanged(string name)
        {
            base.OnPropertyChanged(name);
            if (name == "State")
            {
                this.RefreshTemplate();

                if (StateChanged != null)
                {
                    StateChanged.Invoke(this);
                }
            }
            else if (name == "SourceNode")
            {
                // Subscribing StateChanged for the input connection
                if (cacheSourceNode != null)
                {
                    cacheSourceNode.StateChanged -= OnInputStateChanged;
                    cacheSourceNode = null;
                }
                if (this.SourceNode is BaseGateViewModel inputNode)
                {
                    cacheSourceNode = inputNode;
                    this.State      = cacheSourceNode.State;
                    cacheSourceNode.StateChanged += OnInputStateChanged;
                }
            }
            else if (name == "TargetNode")
            {
                // Subscribing StateChanged for the output connection
                if (cacheTargetNode != null)
                {
                    cacheTargetNode.DisposeEvents(this);
                    cacheTargetNode = null;
                }
                if (this.TargetNode is GateViewModel outputNode)
                {
                    cacheTargetNode = outputNode;
                    cacheTargetNode.InitEvents(this);
                }
            }
        }
Esempio n. 24
0
        public void Write(BinaryState state, bool commit = true)
        {
            state = CoerceState(state);

            // TODO: Implement animations here.
            foreach (var output in _outputs)
            {
                output.Write(state, false);
            }

            if (commit)
            {
                foreach (var output in _outputs)
                {
                    output.Write(state);
                }
            }

            _state = state;
        }
Esempio n. 25
0
        public void Write(BinaryState state, WriteBinaryStateMode mode)
        {
            state = CoerceState(state);

            // TODO: Implement animations here.
            foreach (var output in _outputs)
            {
                output.Write(state, WriteBinaryStateMode.NoCommit);
            }

            if (mode == WriteBinaryStateMode.Commit)
            {
                foreach (var output in _outputs)
                {
                    output.Write(state, WriteBinaryStateMode.Commit);
                }
            }

            _state = state;
        }
        public void Write(BinaryState state, bool commit = true)
        {
            state = CoerceState(state);

            // TODO: Implement animations here.
            foreach (var output in _outputs)
            {
                output.Write(state, false);
            }

            if (commit)
            {
                foreach (var output in _outputs)
                {
                    output.Write(state);
                }
            }

            _state = state;
        }
        private CNCState.CNCState CommitTool(int tool_id,
                                             ActionProgram.ActionProgram program,
                                             CNCState.CNCState state)
        {
            IAction action = null;
            IDriver driver = tool_drivers[tool_id];

            if (driver is N700E_driver n700e)
            {
                SpindleState ss = state.ToolStates[tool_id] as SpindleState;
                action = n700e.CreateAction(ss.RotationState, ss.SpindleSpeed);
            }
            else if (driver is GPIO_driver gpio)
            {
                BinaryState bs = state.ToolStates[tool_id] as BinaryState;
                action = gpio.CreateAction(bs.Enabled);
            }
            else if (driver is RawModbus_driver modbus)
            {
                BinaryState bs = state.ToolStates[tool_id] as BinaryState;
                action = modbus.CreateAction(bs.Enabled);
            }
            else if (driver is Dummy_driver dummy)
            {
                action = dummy.CreateAction();
            }
            else
            {
                throw new InvalidOperationException("invalid driver: " + driver);
            }

            if (action != null)
            {
                program.AddAction(action, state, state);
                return(state);
            }
            else
            {
                return(state);
            }
        }
Esempio n. 28
0
        public void Write(BinaryState state, WriteBinaryStateMode mode)
        {
            // TODO: Implement animations here.
            foreach (var output in _outputs)
            {
                output.Write(state, WriteBinaryStateMode.NoCommit);
            }

            if (mode == WriteBinaryStateMode.Commit)
            {
                foreach (var output in _outputs)
                {
                    output.Write(state);
                }
            }

            var oldState = _state;

            _state = state;

            StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, _state));
        }
Esempio n. 29
0
        private BinaryState Update(BinaryState newState)
        {
            var oldState = _latestState;

            if (oldState == newState)
            {
                return(oldState);
            }

            _latestState = newState;

            try
            {
                StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, newState));
            }
            catch (Exception exception)
            {
                Log.Default.Error(exception, $"Error while reading input state of GPIO{_pin.PinNumber}.");
            }

            return(newState);
        }
Esempio n. 30
0
        private void MqttCallback(DeviceMessage deviceMessage)
        {
            var oldState = _state;

            var payload = Encoding.UTF8.GetString(deviceMessage.Payload);

            if (payload == "pressed")
            {
                _state = BinaryState.High;
            }
            else
            {
                _state = BinaryState.Low;
            }

            if (_state == oldState)
            {
                return;
            }

            StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, _state));
        }
Esempio n. 31
0
        public void Write(BinaryState state, WriteBinaryStateMode mode)
        {
            if (mode != WriteBinaryStateMode.Commit)
            {
                return;
            }

            BinaryState oldState;

            lock (_syncRoot)
            {
                if (state == _latestState)
                {
                    return;
                }

                _pin.Write(state == BinaryState.High ? GpioPinValue.High : GpioPinValue.Low);

                oldState     = _latestState;
                _latestState = state;
            }

            StateChanged?.Invoke(this, new BinaryStateChangedEventArgs(oldState, state));
        }
Esempio n. 32
0
 public string SetBinaryState(BinaryState state)
 {
     var response = ExecuteSoapAction("SetBinaryState", $"<BinaryState>{BinaryStateHelp.BinaryStateToString(state)}</BinaryState>").SetBinaryStateResponse;
     var results = (IDictionary<string, object>) response;
     return results.ContainsKey("CountdownEndTime") ? response.CountdownEndTime : response.BinaryState;
 }
 /// <summary>
 /// Raises when the input value to the wire is changed
 /// </summary>
 /// <param name="inputNode"></param>
 private void OnInputStateChanged(IGroupable inputNode)
 {
     this.State = (inputNode as BaseGateViewModel).State;
 }
Esempio n. 34
0
        private void HandleBinaryStates(byte[] data)
        {
            switch (currentBinaryState)
            {
            case BinaryState.First_Put_Response:
                // first response for put
                if (CheckResponseStatus(data) == 0)
                {
                    // send file data in chunks
                    for (var offset = 0; offset < fileDataToBeSent.Count; offset += 1024)
                    {
                        SendData(fileDataToBeSent.ToArray().Slice(offset, offset + 1024));
                    }
                    currentBinaryState = BinaryState.Final_Put_Response;
                }
                break;

            case BinaryState.Final_Put_Response:
                // final response for put
                if (CheckResponseStatus(data) == 0)
                {
                    Console.WriteLine(String.Format("Sent {0}, {1} bytes", fileNameOfFileToBeSent, fileDataToBeSent.Count));
                    OnFileSent.DynamicInvoke(this, new FileSentEventArgs(FileOperationResult.Success, fileNameOfFileToBeSent, fileDataToBeSent.Count));
                }
                else
                {
                    Console.WriteLine(String.Format("Failed sending {0}", fileNameOfFileToBeSent));
                    OnFileSent.DynamicInvoke(this, new FileSentEventArgs(FileOperationResult.Failure, fileNameOfFileToBeSent, -1));
                }
                currentBinaryState = BinaryState.None;
                break;

            case BinaryState.First_Get_Response:
                // first response for get
                if (CheckResponseStatus(data) == 0)
                {
                    currentBinaryState = BinaryState.File_Data;
                    byte[] dataPacket = new byte[] { 0x00 };
                    SendData(dataPacket);
                }
                break;

            case BinaryState.File_Data:
                // file data
                int sz = data[0] | (data[1] << 8);
                if (data.Length == 2 + sz)
                {
                    // we assume that the data comes in single chunks
                    if (sz == 0)
                    {
                        // end of file
                        currentBinaryState = BinaryState.Final_Response;
                    }
                    else
                    {
                        // accumulate incoming data to get_file_data
                        fileDataReceived.AddRange(data.Slice(2));
                        Console.WriteLine(String.Format("Getting {0}, {1} bytes", fileNameOfFileReceived, fileDataReceived.Count));
                        OnFileDataReceived.Invoke(this, new FileDataReceivedEventArgs(fileNameOfFileReceived, fileDataReceived.Count));
                        byte[] dataPacket = new byte[] { 0x00 };
                        SendData(dataPacket);
                    }
                }
                else
                {
                    currentBinaryState = BinaryState.None;
                }
                break;

            case BinaryState.Final_Response:
                // final response
                if (CheckResponseStatus(data) == 0)
                {
                    Console.WriteLine(String.Format("Got {0}, {1} bytes", fileNameOfFileReceived, fileDataReceived.Count));
                    OnFileReceived.DynamicInvoke(this, new FileReceivedEventArgs(FileOperationResult.Success, fileNameOfFileReceived, fileDataReceived.ToArray()));
                }
                else
                {
                    Console.WriteLine(String.Format("Failed getting {0}", fileNameOfFileReceived));
                    OnFileReceived.DynamicInvoke(this, new FileReceivedEventArgs(FileOperationResult.Failure, fileNameOfFileReceived, null));
                }
                currentBinaryState = BinaryState.None;
                break;

            case BinaryState.Get_Version_Response:
                // first (and last) response for GET_VER
                OnVersionReceived.DynamicInvoke(this, new VersionEventArgs(data));
                currentBinaryState = BinaryState.None;
                break;
            }
        }
Esempio n. 35
0
 protected static BinaryState CoerceState(BinaryState state)
 {
     return(state == BinaryState.High ? BinaryState.Low : BinaryState.High);
 }
Esempio n. 36
0
 public void Write(BinaryState state, WriteBinaryStateMode mode = WriteBinaryStateMode.Commit)
 {
     _binaryOutput.Write(CoerceState(state), mode);
 }
 public BinaryStateChangedEventArgs(BinaryState oldState, BinaryState newState)
 {
     OldState = oldState;
     NewState = newState;
 }
 internal void SetPortState(int pinNumber, BinaryState state)
 {
     lock (_syncRoot)
     {
         _state.SetBit(pinNumber, state == BinaryState.High);
     }
 }
Esempio n. 39
0
 public BinaryStateChangedEventArgs(BinaryState oldState, BinaryState newState)
 {
     OldState = oldState;
     NewState = newState;
 }
 public BinaryStateChangedEventArgs(BinaryState newState)
 {
     NewState = newState;
 }
 public BinaryStateChangedEventArgs(BinaryState newState)
 {
     NewState = newState;
 }
Esempio n. 42
0
 public TestPort(BinaryState initialState)
 {
     _state = initialState;
 }
Esempio n. 43
0
 public void Write(BinaryState state, WriteBinaryStateMode mode)
 {
     _board.SetPortState(_id, state, mode == WriteBinaryStateMode.Commit);
 }