예제 #1
0
        private void ValueDispatchToRule(IDispatchable dispatchable, object o, Guid toRule, RuleInterfaceInstance toInterface)
        {
            lock (_lock)
            {
                Task.Run(async() =>
                {
                    await _ruleInstanceVisuNotifier.NotifyValueChanged(toInterface, o);
                }).ConfigureAwait(false);

                foreach (var rule in _logicInstancesStore.Dictionary())
                {
                    if (rule.Key.ObjId == toRule)
                    {
                        try
                        {
                            SystemLogger.Instance.LogDebug(
                                $"ValueDispatchToRule: {dispatchable.Name} write value {o} to {toInterface.This2RuleInterfaceTemplateNavigation.Name}");
                            var ruleResults = rule.Value.ValueChanged(toInterface, dispatchable, o);

                            foreach (var result in ruleResults)
                            {
                                Task.Run(async() =>
                                {
                                    await _dispatcher.DispatchValue(result.Instance, result.Value);
                                }).ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            SystemLogger.Instance.LogError(e, $"Error writing value ({o}) to {dispatchable.Name}");
                        }
                    }
                }
            }
        }
예제 #2
0
        protected override object ConvertFromBus(IDispatchable source, byte[] value)
        {
            var intValue = BitConverter.ToUInt64(value, 0);

            intValue = ((UInt64)(intValue / Factor) + (UInt64)Offset);
            return(intValue);
        }
예제 #3
0
        public sealed override object ConvertValueFromBus(IDispatchable source, ushort[] value)
        {
            var bytes = new List <byte>();

            foreach (var va in value)
            {
                bytes.AddRange(BitConverter.GetBytes(va));
            }

            switch (ByteOrder)
            {
            case ModBus4ByteOrder.AB_CD:
                break;

            case ModBus4ByteOrder.CD_AB:
                WordSwap(ref bytes);
                break;

            case ModBus4ByteOrder.BA_DC:
                ByteSwap(ref bytes);
                //
                break;

            case ModBus4ByteOrder.DC_BA:
                ByteSwap(ref bytes);
                WordSwap(ref bytes);
                break;
            }

            var correctValue = ConvertFromBus(source, bytes.ToArray());

            return(correctValue);
        }
예제 #4
0
        public override async Task WriteValue(IDispatchable source, object value)
        {
            if (bool.TryParse(value.ToString(), out bool bolValue))
            {
                if (bolValue)
                {
                    var dg = new RadioErp1Packet(Rorg.Rps, new ReadOnlyMemory <byte>(new byte[] { 0x10 }));

                    await Driver.SendTelegram(dg);

                    await Task.Delay(150);

                    dg = new RadioErp1Packet(Rorg.Rps, new ReadOnlyMemory <byte>(new byte[] { 0x00 }));
                    await Driver.SendTelegram(dg);
                }
                else
                {
                    var dg = new RadioErp1Packet(Rorg.Rps, new ReadOnlyMemory <byte>(new byte[] { 0x30 }));

                    await Driver.SendTelegram(dg);

                    await Task.Delay(150);

                    dg = new RadioErp1Packet(Rorg.Rps, new ReadOnlyMemory <byte>(new byte[] { 0x20 }));
                    await Driver.SendTelegram(dg);
                }
            }
        }
예제 #5
0
        public sealed override object ConvertValueFromBus(IDispatchable source, ushort[] value)
        {
            var bytes = new List <byte>();

            foreach (var va in value)
            {
                bytes.AddRange(BitConverter.GetBytes(va));
            }

            switch (ByteOrder)
            {
            case ModBus8ByteOrder.AB_CD_EF_GH:
                WordSwap(ref bytes);
                break;

            case ModBus8ByteOrder.GH_EF_CD_AB:
                break;

            case ModBus8ByteOrder.BA_DC_FE_HG:
                bytes.Reverse();
                break;

            case ModBus8ByteOrder.HF_FE_DC_BA:
                ByteSwap(ref bytes);
                break;
            }

            return(ConvertFromBus(source, bytes.ToArray()));
        }
예제 #6
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var shortValue = _attribute.ConvertValueToBus(source, value);

            DriverContext.Logger.LogInformation($"Get value ({value} - {String.Join("-", shortValue)}) from {source.Id} to {_parent.Name + $"(-{_parent.DeviceId}-)" +  Name} (Register: {_attribute.Register}, Lenght: {_attribute.RegisterLength}, Table: {_attribute.Table})");
            switch (_attribute.Table)
            {
            case ModBusTable.Coil:
                Driver.SetCoil(_parent.DeviceId, _attribute.Register, shortValue[0] == 1);
                return(Task.CompletedTask);

            case ModBusTable.DiscreteInput:
                Driver.SetDiscreteInput(_parent.DeviceId, _attribute.Register, shortValue[0] == 1);
                return(Task.CompletedTask);
            }

            for (int i = 0; i < _attribute.RegisterLength; i++)
            {
                var registerAddress = (ushort)(_attribute.Register + i);
                switch (_attribute.Table)
                {
                case ModBusTable.HoldingRegister:
                    Driver.SetHoldingRegister(_parent.DeviceId, registerAddress, shortValue[i]);
                    break;

                case ModBusTable.InputRegister:
                    Driver.SetInputRegister(_parent.DeviceId, registerAddress, shortValue[i]);
                    break;
                }
            }
            DispatchValue(value);
            return(base.WriteValue(source, value));
        }
예제 #7
0
 protected virtual Task DispatchValueInternal(IDispatchable self, object value, Action <IDispatchable, object> dis)
 {
     return(Task.Run(() =>
     {
         dis(self, value);
     }));
 }
예제 #8
0
        protected override byte[] ConvertToBus(IDispatchable source, object value)
        {
            try
            {
                var intValue = Convert.ToUInt64(value);
                intValue = ((UInt64)(intValue * Factor) - (UInt64)Offset);

                var firstQ  = (ushort)(intValue >> 48);
                var secondQ = (ushort)(intValue >> 32);
                var thirdQ  = (ushort)(intValue >> 16);
                var fourthQ = (ushort)(intValue & 0xffff);

                var bytes = new List <byte>();
                bytes.AddRange(BitConverter.GetBytes(firstQ));
                bytes.AddRange(BitConverter.GetBytes(secondQ));
                bytes.AddRange(BitConverter.GetBytes(thirdQ));
                bytes.AddRange(BitConverter.GetBytes(fourthQ));

                return(bytes.ToArray());
            }
            catch (Exception e)
            {
                DriverContext.Logger.LogError(e, $"Could not convert {value} to int64");
                throw new InvalidInputValueException(e);
            }
        }
예제 #9
0
        private Task ExecuteDispatch(IDispatchable self, object value, IList <Action <IDispatchable, object> > dispatch, Action <IDispatchable, object, Action <IDispatchable, object> > dispatchAction)
        {
            foreach (var dis in dispatch)
            {
                if (!_hopCounts[self.Id].ContainsKey(dis))
                {
                    _hopCounts[self.Id].Add(dis, 0);
                }

                try
                {
                    if (_hopCounts[self.Id][dis] > 10)
                    {
                        _hopCounts.Clear();
                        throw new DispatchLoopDetectedException(self);
                    }

                    IncrementHopCount(self, dis);
                    dispatchAction.Invoke(self, value, dis);

                    ResetHopCount(self, dis);
                }
                catch (DispatchLoopDetectedException dlde)
                {
                    _logger.LogError(dlde, $"Detected a dispatch loop while dispatching {dlde.Dispatchable.Id}-{dlde.Dispatchable.Name}");
                    throw;
                }
                catch (Exception e)
                {
                    _logger.LogError($"Error while dispatching {self.Id}-{self.Name}. {e}");
                }
            }

            return(Task.CompletedTask);
        }
예제 #10
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            WriteReceived = true;

            DispatchValue(value);

            return(Task.CompletedTask);
        }
예제 #11
0
        protected override object ConvertFromBus(IDispatchable source, byte[] value)
        {
            var dblValue = BitConverter.ToDouble(value, 0);

            dblValue = dblValue / Factor + Offset;

            return(dblValue);
        }
예제 #12
0
        public sealed override ushort[] ConvertValueToBus(IDispatchable source, object value)
        {
            var bytes = ConvertToBus(source, value);

            switch (ByteOrder)
            {
            case ModBus8ByteOrder.AB_CD_EF_GH:
            case ModBus8ByteOrder.GH_EF_CD_AB:
                break;

            case ModBus8ByteOrder.BA_DC_FE_HG:
            case ModBus8ByteOrder.HF_FE_DC_BA:
            {
                var b1 = bytes[0];
                var b2 = bytes[1];

                var b3 = bytes[2];
                var b4 = bytes[3];

                var b5 = bytes[4];
                var b6 = bytes[5];

                var b7 = bytes[6];
                var b8 = bytes[7];

                bytes[0] = b2;
                bytes[1] = b1;

                bytes[3] = b3;
                bytes[2] = b4;

                bytes[5] = b5;
                bytes[4] = b6;

                bytes[7] = b7;
                bytes[6] = b8;
            }
            break;
            }

            var v1 = BitConverter.ToUInt16(new[] { bytes[0], bytes[1] }, 0);
            var v2 = BitConverter.ToUInt16(new[] { bytes[2], bytes[3] }, 0);
            var v3 = BitConverter.ToUInt16(new[] { bytes[4], bytes[5] }, 0);
            var v4 = BitConverter.ToUInt16(new[] { bytes[6], bytes[7] }, 0);

            switch (ByteOrder)
            {
            case ModBus8ByteOrder.AB_CD_EF_GH:
            case ModBus8ByteOrder.BA_DC_FE_HG:
                return(new[] { v1, v2, v3, v4 });

            case ModBus8ByteOrder.GH_EF_CD_AB:
            case ModBus8ByteOrder.HF_FE_DC_BA:
                return(new[] { v4, v3, v2, v1 });
            }
            throw new InvalidInputValueException();
        }
예제 #13
0
        public override object ConvertValueFromBus(IDispatchable source, ushort[] value)
        {
            if (value.Length == 1)
            {
                return(value[0] == 1);
            }

            throw new ArgumentException(nameof(value));
        }
예제 #14
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var writeValue = ConvertValue(value);

            Characteristic.Value = writeValue;
            Driver.WriteCharacteristic(Characteristic);

            return(Task.FromResult(true));
        }
예제 #15
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var strValue = Convert.ToString(value);

            _value = strValue;

            Container.Gateway.Driver.SetColor(Container.DeviceId, strValue);
            return(base.WriteValue(source, value));
        }
예제 #16
0
 public override Task WriteValue(IDispatchable source, object value)
 {
     if (value is System.DateTime)
     {
         var solarTimes = new SolarTimes((System.DateTime)value, Latitude, Longitude);
         DispatchValue(_getValueFunc(solarTimes, (System.DateTime)value));
     }
     return(base.WriteValue(source, value));
 }
        public override Task WriteValue(IDispatchable source, object value)
        {
            var intValue = Convert.ToInt32(value);

            _value = intValue;

            Container.Gateway.Driver.SetDimmer(Container.DeviceId, intValue);
            return(base.WriteValue(source, intValue));
        }
예제 #18
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var parent    = (HueOnOffNode)Parent;
            var boolValue = Convert.ToBoolean(value);

            Driver.SetLightState(parent.LightNumber, new HueBridge.Data.HueSwitchLightData(boolValue, boolValue ? 100 : 0));
            DispatchValue(value);
            return(base.WriteValue(source, value));
        }
예제 #19
0
        protected override object ConvertFromBus(IDispatchable source, byte[] value)
        {
            var bytes = new[] { value[2], value[3], value[0], value[1] };

            var intValue = BitConverter.ToUInt32(bytes, 0);

            intValue = (uint)(intValue / Factor + Offset);

            return(intValue);
        }
예제 #20
0
 private void DataCallback(IDispatchable dispatchable, object value)
 {
     if (_recorders.ContainsKey(dispatchable.Id))
     {
         foreach (var rec in _recorders[dispatchable.Id])
         {
             rec.ValueChanged(value, dispatchable.Name);
         }
     }
 }
        protected override IList <IRuleOutputChanged> InputValueChanged(RuleInterfaceInstance instance,
                                                                        IDispatchable source, object value)
        {
            if (instance == _input)
            {
                return(ValueChanged(_output, value));
            }

            return(new List <IRuleOutputChanged>());
        }
예제 #22
0
    internal void _handleInternal(IDispatchable eventToHandle)
    {
        HandlerMethodInfo handlerMethodInfo = null;

        _handlerMethods.TryGetValue(eventToHandle.GetType(), out handlerMethodInfo);
        if (handlerMethodInfo != null && eventToHandle != null)
        {
            handlerMethodInfo.Method.Invoke(handlerMethodInfo.Target, new object[] { eventToHandle });
        }
    }
예제 #23
0
        protected override IList <IRuleOutputChanged> InputValueChanged(RuleInterfaceInstance instance,
                                                                        IDispatchable source, object value)
        {
            WriteReceived = true;

            return(new List <IRuleOutputChanged>()
            {
                new RuleOutputChanged(Output, true)
            });
        }
예제 #24
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var newValue = Convert.ToDouble(value);

            DispatchValue(newValue);
            _value = newValue;

            Driver.Write(GroupAddress, ConvertToBus(newValue));

            return(Task.CompletedTask);
        }
예제 #25
0
 public async Task DispatchValue(IDispatchable dispatchable, object value)
 {
     var topic = $"{RemoteTopicConstants.DISPATCHER_TOPIC}/{dispatchable.Type}/{dispatchable.Id}";
     await _mqttServer.PublishAsync(new MqttApplicationMessage()
     {
         Topic = topic,
         QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
         Payload = BinarySerializer.Serialize(value),
         Retain  = true
     });
 }
예제 #26
0
 private void ValueDispatched(IDispatchable dispatchable, object o, Guid to)
 {
     foreach (var node in _coreServer.DriverNodes)
     {
         if (node.Id == to)
         {
             SystemLogger.Instance.LogDebug($"ValueDispatched: {dispatchable.Name} write value {o} to {node.Name}-{node.Id}");
             node.WriteValue(dispatchable, o);
         }
     }
 }
예제 #27
0
        private async Task Dispatch(IDispatchable self, object value, Action <IDispatchable, object, Action <IDispatchable, object> > dispatchAction)
        {
            StoreValue(self, value);

            if (!_hopCounts.ContainsKey(self.Id))
            {
                _hopCounts.Add(self.Id, new ConcurrentDictionary <Action <IDispatchable, object>, int>());
            }

            _logger.LogInformation($"Driver {self.Id}-{self.Name} dispatched value {value}");
            if (_registrationMap.ContainsKey(self.Type) && _registrationMap[self.Type].ContainsKey(self.Id))
            {
                var dispatch = _registrationMap[self.Type][self.Id];

                foreach (var dis in dispatch)
                {
                    if (!_hopCounts[self.Id].ContainsKey(dis))
                    {
                        _hopCounts[self.Id].Add(dis, 0);
                    }

                    try
                    {
                        if (_hopCounts[self.Id][dis] > 10)
                        {
                            _hopCounts.Clear();
                            throw new DispatchLoopDetectedException(self);
                        }

                        IncrementHopCount(self, dis);
                        dispatchAction.Invoke(self, value, dis);

                        ResetHopCount(self, dis);
                    }
                    catch (DispatchLoopDetectedException dlde)
                    {
                        _logger.LogError(dlde, $"Detected a dispatch loop while dispatching {dlde.Dispatchable.Id}-{dlde.Dispatchable.Name}");
                        throw;
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"Error while dispatching {self.Id}-{self.Name}. {e}");
                    }
                }
            }
            else if (self.Source != DispatchableSource.Remote)
            {
                _logger.LogInformation($"Dispatch value via mqtt dispatcher");

                await _remoteSender.DispatchValue(self, value);
            }

            await _dataBroadcast.DispatchValue(self.Type, self.Id, value);
        }
예제 #28
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            if (_value == value)
            {
                return(Task.CompletedTask);
            }
            _value = value;
            DriverContext.Logger.LogDebug($"WriteValue {value}");

            DispatchValue(value);
            return(Task.CompletedTask);
        }
예제 #29
0
 protected override void ParamterValueChanged(RuleInterfaceInstance instance, IDispatchable source, object value)
 {
     if (instance.This2RuleInterfaceTemplate == DelayedOffRuleFactory.RuleParamDelay)
     {
         _delay = Convert.ToInt64(value);
         if (_timerRunning)
         {
             StartStopTimer();
         }
     }
     base.ParamterValueChanged(instance, source, value);
 }
예제 #30
0
        public override Task WriteValue(IDispatchable source, object value)
        {
            var boolValue = Convert.ToBoolean(value);

            if (boolValue != Value)
            {
                _parent.WriteToBus();
            }

            Value = boolValue;

            return(Task.CompletedTask);
        }