예제 #1
0
 private void Changed(Perform p, SubRec sr)
 {
     if (Client == null || Client.status != MqClient.Status.Connected)
     {
         Disconnected();
         Log.Warning("{0}.Changed({1}) - Client OFFLINE", Owner.path, p.ToString());
         return;
     }
     if ((p.art == Perform.Art.subscribe || ((p.art == Perform.Art.changedState || p.art == Perform.Art.create) && p.prim != Owner)) && !p.src.CheckAttribute(Topic.Attribute.Internal))
     {
         var rp      = remotePrefix + p.src.path.Substring(Owner.path.Length);
         var payload = JsLib.Stringify(p.src.GetState() ?? JSC.JSValue.Null);
         if (!string.IsNullOrEmpty(rp) && payload != null)
         {
             Client.Send(new MqPublish(rp, payload)
             {
                 Retained = _retainedEn
             });
         }
     }
     else if (p.art == Perform.Art.subAck && _subEn)
     {
         Client.Subscribe(this);
     }
 }
예제 #2
0
        public void Start()
        {
            _owner   = Topic.root.Get("/$YS/AntSw");
            _verbose = _owner.Get("verbose");
            if (_verbose.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                _verbose.SetAttribute(Topic.Attribute.Required | Topic.Attribute.DB);
#if DEBUG
                _verbose.SetState(true);
#else
                _verbose.SetState(false);
#endif
            }
            _enableT = _owner.Get("remote");
            if (_enableT.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                _enableT.SetAttribute(Topic.Attribute.DB | Topic.Attribute.Required);
                _enableT.SetState(true);
            }

            var   rt = Topic.root.Get("/export/req", true, _owner);
            Topic con;
            for (int i = 1; i <= 8; i++)
            {
                con = rt.Get("con" + i.ToString(), true, _owner);
                con.Get("ptt").SetState(JSC.JSObject.Null);
                con.Get("rxcfg").SetState(0);
                con.Get("txcfg").SetState(0);
            }
            _di        = Topic.root.Get("/export/out", true, _owner);
            _reqSub    = rt.Subscribe(SubRec.SubMask.All | SubRec.SubMask.Value, Request);
            _transport = new Transport(this);
        }
예제 #3
0
 private void owner_changed(SubRec sb, Perform p) {
   for(int i=_cont.Count-1; i>=0; i--) {
     _cont[i].changed(sb, p);
   }
   if(p.art==Perform.Art.remove) {
     PLC.instance.DelVar(this);
   }
 }
예제 #4
0
        private void SubChanged(Perform p, SubRec sr)
        {
            var vj = JsLib.Stringify(p.src.GetState());

            Send(string.Concat("P\t", p.src.path, "\t", vj));
            if (WebUI_Pl.verbose)
            {
                X13.Log.Debug("ws.snd({0}, {1})", p.src.path, vj);
            }
        }
예제 #5
0
 public void Connected()
 {
     if (_pubEn)
     {
         _sr = Owner.Subscribe(_mask, Changed);
     }
     else if (_subEn)
     {
         Client.Subscribe(this);
     }
 }
예제 #6
0
 internal void Subscribe(SubRec sr)
 {
     if (this._subRecords == null)
     {
         this._subRecords = new List <SubRec>();
     }
     if (!this._subRecords.Exists(z => z.f == sr.f && z.mask == sr.mask))
     {
         this._subRecords.Add(sr);
     }
 }
예제 #7
0
        private void SubFunc(Perform p, SubRec sr)
        {
            if (p.art == Perform.Art.create)
            {
                return;
            }
            MqSite   ms = _sites.FirstOrDefault(z => z.Owner == p.src);
            MqClient client;

            if (ms != null)
            {
                ms.Dispose();
                _sites.Remove(ms);
            }
            if (p.art == Perform.Art.changedField || p.art == Perform.Art.subscribe)
            {
                var uri = p.src.GetField("MQTT.uri").Value as string;
                if (string.IsNullOrEmpty(uri))
                {
                    return;
                }
                Uri uUri;
                try {
                    uUri = new Uri(uri, UriKind.Absolute);
                }
                catch (Exception ex) {
                    Log.Warning("{0}.MQTT.uri = {1} - {2}", p.src.path, uri, ex.Message);
                    return;
                }
                string uName, uPass;
                if (!string.IsNullOrEmpty(uUri.UserInfo))
                {
                    var uia = uUri.UserInfo.Split(':');
                    uName = uia[0];
                    uPass = uia.Length > 1 ? uia[1] : null;
                }
                else
                {
                    uName = null;
                    uPass = null;
                }
                var cid = "MQTT://" + (uName == null ? string.Empty : (uName + "@")) + uUri.DnsSafeHost + (uUri.IsDefaultPort ? string.Empty : (":" + uUri.Port.ToString()));
                client = _clients.FirstOrDefault(z => z.Signature == cid);
                if (client == null)
                {
                    client = new MqClient(this, uUri.DnsSafeHost, uUri.IsDefaultPort?1883:uUri.Port, uName, uPass);
                    _clients.Add(client);
                }
                _sites.Add(new MqSite(this, client, p.src, uUri));
            }
        }
예제 #8
0
        private void DeviceChanged(Perform p, SubRec sr)
        {
            var d = _devs.FirstOrDefault(z => z.owner == p.src);

            if (d != null)
            {
                _devs.Remove(d);
                d.Dispose();
            }
            JSC.JSValue jType;
            if ((p.art == Perform.Art.create || p.art == Perform.Art.changedField || p.art == Perform.Art.subscribe) && (jType = p.src.GetField("type")).ValueType == JSC.JSValueType.String &&
                jType.Value != null && (jType.Value as string).StartsWith("TWI"))
            {
                _devs.Add(new TwiDevice(p.src, this));
            }
        }
예제 #9
0
        public void Start()
        {
            _owner = Topic.root.Get("/$YS/MQTT");
            var verboseT = _owner.Get("verbose");

            if (verboseT.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                verboseT.SetAttribute(Topic.Attribute.Required | Topic.Attribute.DB);
//#if DEBUG
//        verboseT.SetState(true);
//#else
                verboseT.SetState(false);
//#endif
            }
            _verboserSR = verboseT.Subscribe(SubRec.SubMask.Once | SubRec.SubMask.Value, (p, s) => verbose = (_verboserSR.setTopic != null && _verboserSR.setTopic.GetState().As <bool>()));
            _subMq      = Topic.root.Subscribe(SubRec.SubMask.Field | SubRec.SubMask.All, "MQTT.uri", SubFunc);
        }
예제 #10
0
        private void Request(Perform p, SubRec sr)
        {
            byte con;
            int  tmp;
            var  en = _enableT.GetState();

            if ((en.ValueType == JSC.JSValueType.Boolean && !((bool)en)) || p.prim == _owner || p.src.path.Length < 17 || !p.src.path.StartsWith("/export/req/con") || !byte.TryParse(p.src.path.Substring(15, 1), out con) || con == 0 || con > 8)
            {
                return;
            }
            switch (p.src.name)
            {
            case "ptt":
                if (p.src.GetState().ValueType == JSC.JSValueType.Boolean)
                {
                    _transport.Write(new Command(CommandCode.Event, (byte)(32 + con), (byte)(((bool)p.src.GetState())?4:3)));
                }
                p.src.SetState(JSC.JSObject.Null, _owner);
                break;

            case "band":
                if (p.src.GetState().IsNumber&& (tmp = (int)p.src.GetState()) > 0 && tmp <= 8)
                {
                    _transport.Write(new Command(CommandCode.Event, (byte)(32 + con), (byte)((tmp - 1) * 2 + 64)));
                }
                p.src.SetState(0, _owner);
                break;

            case "rxcfg":
                if (p.src.GetState().IsNumber&& (tmp = (int)p.src.GetState()) > 0 && tmp <= 8)
                {
                    _transport.Write(new Command(CommandCode.Event, (byte)(32 + con), (byte)((tmp - 1) * 2 + 96)));
                }
                p.src.SetState(0, _owner);
                break;

            case "txcfg":
                if (p.src.GetState().IsNumber&& (tmp = (int)p.src.GetState()) > 0 && tmp <= 8)
                {
                    _transport.Write(new Command(CommandCode.Event, (byte)(32 + con), (byte)((tmp - 1) * 2 + 97)));
                }
                p.src.SetState(0, _owner);
                break;
            }
        }
예제 #11
0
        public static void Init(MQTT_SNPl pl)
        {
            _pl = pl;
            ThreadPool.RegisterWaitForSingleObject(_startScan, ScanSerialPorts, null, 289012, false);
            _portsTopic = Topic.root.Get("/$YS/MQTT-SN/ports");
            if (!_portsTopic.CheckAttribute(Topic.Attribute.Required) || _portsTopic.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                var act = new JSL.Array(1);
                var r_a = JSC.JSObject.CreateObject();
                r_a["name"] = "MQTT_SN.RefreshPorts";
                r_a["text"] = "Refresh";
                act[0]      = r_a;
                _portsTopic.SetField("Action", act);
                _portsTopic.SetState(true);
                _portsTopic.SetAttribute(Topic.Attribute.Required | Topic.Attribute.Config);
                _scanBusy = 1;
            }
            _portValuesSR = _portsTopic.Subscribe(SubRec.SubMask.Chldren | SubRec.SubMask.Value, PortValuesChanged);

            _startScan.Set();
        }
예제 #12
0
        public TWI(Topic owner, Action <byte[]> pub)
        {
            this._owner   = owner;
            this._pub     = pub;
            this._devs    = new List <TwiDevice>();
            this._reqs    = new Queue <Tuple <byte[], TaskCompletionSource <JSC.JSValue> > >();
            this._verbose = Topic.root.Get("/$YS/TWI/verbose");
            if (_verbose.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                _verbose.SetAttribute(Topic.Attribute.Required | Topic.Attribute.DB);
#if DEBUG
                _verbose.SetState(true);
#else
                _verbose.SetState(false);
#endif
            }

            _flag             = 1;
            _deviceChangedsSR = this._owner.Subscribe(SubRec.SubMask.Chldren | SubRec.SubMask.Field, "type", DeviceChanged);
            if (verbose)
            {
                Log.Debug("{0}.Created", _owner.path);
            }
        }
예제 #13
0
        public void Start()
        {
            _owner = Topic.root.Get("/$YS/MQTT-SN");
            var verboseT = _owner.Get("verbose");

            if (verboseT.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                verboseT.SetAttribute(Topic.Attribute.Required | Topic.Attribute.Config);
#if DEBUG
                verboseT.SetState(true);
#else
                verboseT.SetState(false);
#endif
            }
            _verboserSR = verboseT.Subscribe(SubRec.SubMask.Once | SubRec.SubMask.Value, (p, s) => verbose = (_verboserSR.setTopic != null && _verboserSR.setTopic.GetState().As <bool>()));
            _stat       = _owner.Get("statistic");
            if (_stat.GetState().ValueType != JSC.JSValueType.Boolean)
            {
                _stat.SetAttribute(Topic.Attribute.Required | Topic.Attribute.Config);
                _stat.SetState(false);
            }
            _gates.Add(new MsGUdp(this));
            MsGSerial.Init(this);
        }
예제 #14
0
 public void changed(SubRec sr, Perform p) {
   if(_owner == null) {
     return;
   }
   if(p.art == Perform.Art.remove) {
     _owner.Remove(p.prim);
   } else if(p.art == Perform.Art.changed || p.art==Perform.Art.subscribe) {
     if(p.src.vType == typeof(PiAlias)) {
       PiAlias al = p.src.As<PiAlias>();
       if(p.src == _ipTopic) {
         input.DelCont(this);
         input = al.origin;
         al.AddLink(this);
         input.AddCont(this);
       } else if(p.src == _opTopic) {
         if(al.origin.ip) {
           throw new ArgumentException(string.Format("{0} already hat source", _opTopic.path));
         }
         output.DelCont(this);
         output = al.origin;
         al.AddLink(this);
         output.AddCont(this);
       } else {
         return;
       }
     } else if(p.src != input.owner) {
       return;
     }
     if(input.layer!=0 || input.owner._value.Defined) {
       output.owner._value=input.owner._value;
       if(p.src==input.owner) {
         var c=Perform.Create(output.owner, Perform.Art.changed, this.owner);
         c.o=input.owner._value;
         PLC.instance.DoCmd(c, true);
       }
     }
   }
 }
예제 #15
0
 private void children_changed(SubRec sb, Perform p) {
   if(_decl==null) {
     return;
   }
   if(p.art == Perform.Art.remove) {
     PiVar v;
     if(_pins.TryGetValue(p.src.name, out v)) {
       _pins.Remove(p.src.name);
       v.DelCont(this);
     }
   } else if(p.art != Perform.Art.unsubscribe) {
     PinDeclarer pd = AddPin(p.src);
     if(pd!=null && ((p.art == Perform.Art.changed && pd.ip) || p.art == Perform.Art.subscribe)) {
       _decl.Calc(this);
     }
   }
 }
예제 #16
0
        private void TopicChanged(Perform p, SubRec sb)
        {
            switch (p.art) //-V3002
            {
            case Perform.Art.create:
                if (sb.setTopic != p.src)
                {
                    base.SendArr(new JSL.Array {
                        4, p.src.path
                    });
                }
                break;

            case Perform.Art.subscribe:
                if (sb.setTopic == p.src)
                {
                    var m = p.src.GetField(null);
                    if (m == null || m.ValueType != JSC.JSValueType.Object || m.Value == null)
                    {
                        m = EmptyManifest;
                    }
                    var s = p.src.GetState();
                    if (s == null)
                    {
                        s = JSC.JSValue.Undefined;
                    }
                    base.SendArr(new JSL.Array {
                        4, p.src.path, s, m
                    });
                }
                else
                {
                    base.SendArr(new JSL.Array {
                        4, p.src.path
                    });
                }
                break;

            case Perform.Art.subAck:
                var sr = p.o as SubRec;
                if (sr != null)
                {
                    lock (_subscriptions) {
                        foreach (var msg in _subscriptions.Where(z => z.Item1 == sr && z.Item2 != null).Select(z => z.Item2))
                        {
                            msg.Response(3, msg[1], true, true);
                        }
                    }
                }
                break;

            case Perform.Art.changedState:
                if (!p.src.disposed && p.prim != _owner && sb.setTopic == p.src)
                {
                    base.SendArr(new JSL.Array {
                        6, p.src.path, p.src.GetState()
                    });
                }
                break;

            case Perform.Art.changedField:
                if (sb.setTopic == p.src)
                {
                    base.SendArr(new JSL.Array {
                        14, p.src.path, p.src.GetField(null)
                    });
                }
                break;

            case Perform.Art.move:
                if (sb.setTopic != p.src)
                {
                    base.SendArr(new JSL.Array {
                        10, p.o as string, p.src.parent.path, p.src.name
                    });
                }
                break;

            case Perform.Art.remove:
                if (sb.setTopic == p.src.parent)
                {
                    base.SendArr(new JSL.Array {
                        12, p.src.path
                    });
                    lock (_subscriptions) {
                        _subscriptions.RemoveAll(z => z.Item1.setTopic == p.src);
                    }
                }
                break;
            }
        }
예제 #17
0
    public void changed(SubRec sb, Perform p) {

    }
예제 #18
0
 private void _owner_changed(SubRec sb, Perform p) {
   if(p.art==Perform.Art.remove) {
     for(int i=links.Count-1; i>=0; i--) {
       links[i].owner.Remove(p.prim);
     }
   }
 }
예제 #19
0
 private static void PortValuesChanged(Perform p, SubRec sr)
 {
     _startScan.Set();
 }
예제 #20
0
 public void changed(SubRec sb, Perform p) {
   if(p.src==origin.owner && p.art==Perform.Art.remove && _owner!=null) {
     _owner.Remove(p.prim);
   }
 }