Beispiel #1
0
 void ITopicOwned.SetOwner(Topic owner) {
   if (_owner != owner) {
     if (_owner != null) {
       _owner.Unsubscribe("+", _owner_changed);
       Change_A(null);
       Change_B(null);
       _parent = null;
     }
     _owner = owner as DVar<PiWire>;
     if (_owner != null) {
       _owner.saved = true;
       var dc = _owner.Get<string>("_declarer");
       dc.saved = true;
       dc.value = "Wire";
       if (_owner.parent != null && _owner.parent.valueType == typeof(PiLogram)) {
         _parent = (_owner.parent as DVar<PiLogram>).value;
       }
       _owner.Subscribe("+", _owner_changed);
       if (exec) {
         Change_A(_a);
         Change_B(_b);
       }
     }
   }
 }
Beispiel #2
0
    public LogView() {
      _instance=this;
      AddLogEntryDelegate=new Action<LogEntry>(AddLogEntry);

      InitializeComponent();
      this.DataContext = this;
      if(!Directory.Exists("../log")) {
        Directory.CreateDirectory("../log");
      }

      var now=DateTime.Now;
      try {
        foreach(string f in Directory.GetFiles("../log/", "*_cc.log", SearchOption.TopDirectoryOnly)) {
          if(File.GetLastWriteTime(f).AddDays(3)<now)
            File.Delete(f);
        }
      }
      catch(System.IO.IOException) {
      }
      _lfPath="../log/"+now.ToString("yyMMdd")+"_cc.log";

      _log=new X13.WOUM.BlockingQueue<LogEntry>(ProcessLog);
      Log.Write+=new Action<LogLevel, DateTime, string>(Log_Write);

      LogCollection=new ObservableCollection<LogEntry>();
      lvLog.ItemsSource=CollectionViewSource.GetDefaultView(LogCollection);
      this._showDebug=Settings.LogShowDebug;
      tbShowDebug.IsChecked=_showDebug;
      if(_showDebug) {
        (lvLog.ItemsSource as System.ComponentModel.ICollectionView).Filter=(o) => true;
      } else {
        (lvLog.ItemsSource as System.ComponentModel.ICollectionView).Filter=(o) => (o as LogEntry).ll!=LogLevel.Debug;
      }
      _lHead=Topic.root.Get<long>("/var/log");
      _oldHead=(_lHead.value+1)%100;
      _lHead.changed+=_lHead_changed;
      _lDebug=_lHead.Get<string>("A0");
      _lDebug.changed+=_lDebug_changed;
    }
Beispiel #3
0
 public void Attach(DVar<PiLogram> model) {
   if(this.model!=model) {
     this.model=model;
     var w=model.Get<long>("_width");
     if(w.value==0) {
       w.saved=true;
       w.value=24;
     }
     this.Width=w.value*LogramView.CellSize;
     var h=model.Get<long>("_height");
     if(h.value<=0) {
       h.saved=true;
       h.value=24;
     }
     this.Height=h.value*LogramView.CellSize;
     _map.Clear();
     DrawingVisual cur;
     foreach(var p in model.children.Where(z => z.valueType==typeof(Topic)).Cast<DVar<Topic>>()) {
       try {
         cur=new uiAlias(p, this);
       }
       catch(Exception ex) {
         Log.Error("create uiAlias({0}) - {1}", p.path, ex.ToString());
       }
     }
     foreach(var p in model.children.Where(z => z.valueType==typeof(PiStatement)).Cast<DVar<PiStatement>>()) {
       try {
       cur=new uiStatement(p, this);
       }
       catch(Exception ex) {
         Log.Error("create uiStatement({0}) - {1}", p.path, ex.ToString());
       }
     }
     foreach(var p in model.children.Where(z => z.valueType==typeof(PiWire)).Cast<DVar<PiWire>>()) {
       try {
       cur=new uiWire(p, this);
       }
       catch(Exception ex) {
         Log.Error("create uiWire({0}) - {1}", p.path, ex.ToString());
       }
     }
     foreach(var p in model.children.Where(z => z.valueType==typeof(PiTracer)).Cast<DVar<PiTracer>>()) {
       try {
       cur=new uiTracer(p, this);
       }
       catch(Exception ex) {
         Log.Error("create uiTracer({0}) - {1}", p.path, ex.ToString());
       }
     }
     model.Subscribe("+", ModelChanged);
   }
 }
Beispiel #4
0
 public LM75(Topic owner) {
   _T=owner as DVar<double>;
   if(_T==null) {
     throw new ArgumentException();
   }
   _present=_T.Get<bool>("present");
   _present.saved=false;
   _present.value=false;
   addr=(byte)(0x48+int.Parse(_T.name.Substring(6)));
   Reset();
 }
Beispiel #5
0
#pragma warning restore 649

    public void Init(bool defState) {
      string path=Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      string exeName=Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
      var root=Topic.root;
      Log.Info("Starting {0}, version {1}", exeName, Assembly.GetExecutingAssembly().GetName().Version.ToString(4));

      var myId=Topic.root.Get<string>("/local/cfg/id");
      if(string.IsNullOrWhiteSpace(myId.value)) {
        myId.saved=true;
        if(defState) {
          myId.value=string.Format("{0}", Environment.MachineName);
        } else {
          myId.value=string.Format("{0}_{2}@{1}", Environment.UserName, Environment.MachineName, exeName);
        }
      }

      var catalog = new AggregateCatalog();
      catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
      catalog.Catalogs.Add(new DirectoryCatalog(path));
      CompositionContainer _container = new CompositionContainer(catalog);
      try {
        _container.ComposeParts(this);
      }
      catch(CompositionException ex) {
        Log.Error("Load plugins - {0}", ex.ToString());
        throw;
      }
      {
        _now=Topic.root.Get<DateTime>("/var/now");
        _now.saved=false;
        _nowOffset=Topic.root.Get<long>("/local/cfg/Client/TimeOffset");
        _nowOffset.saved=false;
        DateTime nowDT=DateTime.Now;
        _now.value=nowDT;
        _now.Get<long>("second").saved=false;
        _now.Get<long>("second").value=nowDT.Second;
        _now.Get<long>("minute").saved=false;
        _now.Get<long>("minute").value=nowDT.Minute;
        _now.Get<long>("hour").saved=false;
        _now.Get<long>("hour").value=nowDT.Hour;
        _now.Get<long>("wDay").saved=false;
        _now.Get<long>("wDay").value=(long)nowDT.DayOfWeek;
        _now.Get<long>("day").saved=false;
        _now.Get<long>("day").value=nowDT.Day;
        _now.Get<long>("month").saved=false;
        _now.Get<long>("month").value=nowDT.Month;
        _now.Get<long>("year").saved=false;
        _now.Get<long>("year").value=nowDT.Year;
      }

      foreach(var i in _modules.OrderBy(z => z.Metadata.priority)) {
        try {
          if(!string.IsNullOrWhiteSpace(i.Metadata.name)) {
            string plPath="/local/cfg/"+i.Metadata.name+"/enable";
            Topic enT;
            DVar<bool> enD;
            if(Topic.root.Exist(plPath, out enT)) {
              enD=enT as DVar<bool>;
              if(enD!=null && !enD.value) {
                continue;                     // plugin disabled
              }
            } else {
              enD=Topic.root.Get<bool>(plPath);
              enD.saved=true;
              enD.value=defState;
              if(!enD.value) {
                continue;
              }
            }
          }
          i.Value.Init();
          Log.Debug("plugin {0} Loaded", i.Metadata.name??i.Value.GetType().FullName);
        }
        catch(Exception ex) {
          Log.Error("Load plugin {0} failure - {1}", i.Metadata.name??i.Value.GetType().FullName, ex.ToString());
        }
      }

    }
Beispiel #6
0
    public bool StartUp(string cfgPath=null) {
      string path=Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      Directory.SetCurrentDirectory(path);
      if(!string.IsNullOrWhiteSpace(cfgPath)) {
        _cfgPath=cfgPath;
      } else {
        _cfgPath="../data/Engine.xst";
      }

      string siName=string.Format("Global\\X13.engine@{0}", Path.GetFullPath(_cfgPath).Replace('\\', '$'));
      _singleInstance=new Mutex(true, siName);

      _log=new BlockingQueue<LogEntry>(ProcessLog);
      if(!Directory.Exists("../log")) {
        Directory.CreateDirectory("../log");
      }
      if(!Directory.Exists("../data")) {
        Directory.CreateDirectory("../data");
      }
      AppDomain.CurrentDomain.UnhandledException+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

      var root=Topic.root;
      Topic.Import(_cfgPath, "/local/cfg");

      _lHead=root.Get<long>("/var/log");
      _lHead.saved=false;
      _lThreshold=root.Get<LogLevel>("/etc/log/threshold");

      Log.Write+=new Action<LogLevel, DateTime, string>(Log_Write);

      if(!_singleInstance.WaitOne(TimeSpan.Zero, true)) {
        Log.Error("only one instance at a time");
        _singleInstance=null;
        return false;
      }

      Topic.brokerMode=true;

      _plugins=new Plugins();
      _plugins.Init(true);

      _debug=Topic.root.Get<bool>("/local/cfg/repository/_verbose");
      if(_debug.value) {
        root.Subscribe("/#", MQTT_Main_changed);
      }

      string dbVersion=Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
      var dbVer=Topic.root.Get<string>("/etc/system/version");
      if(dbVer.value==null || dbVer.value!=dbVersion) {
        dbVer.value=dbVersion;
        _lHead.Get<string>("A0").saved=false;
#if DEBUG
        _lThreshold.value=LogLevel.Debug;
#else
        _lThreshold.value=LogLevel.Info;
#endif
        using(var sr=new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("X13.PLC.types.xst"))) {
          Topic.Import(sr, null);
        }
      }

      _plugins.Start();
      _startDT=DateTime.Now;
      _statTimer=new Timer(o => {
        SendStat(2);
      }, null, 7200000, 7200000);
      ThreadPool.QueueUserWorkItem(o => {
        SendStat(1);
        Topic.Export(_cfgPath, Topic.root.Get("/local/cfg"));
      });
      return true;
    }