Exemplo n.º 1
0
        public void ExecuteSimulation()
        {
            try
            {
                var timestamp = DateTimeOffset.Now;

                var dict = new Dictionary <string, object>();
                dict["Timestamp"] = timestamp;

                foreach (var field in MyModFieldStore.TheValues)
                {
                    double rndVal = TheCommonUtils.GetRandomDouble() * 1000.0;
                    if (field.SourceType == "double")
                    {
                        dict[field.PropertyName] = rndVal;
                    }
                    else if (field.SourceType == "float")
                    {
                        dict[field.PropertyName] = rndVal;
                    }
                }
                MyBaseThing.SetProperties(dict, timestamp);
            }
            catch (Exception)
            {
                // Console.WriteLine("Ignoring exception: " + ex.Message);
                //Console.WriteLine(ex.StackTrace);
            }
        }
Exemplo n.º 2
0
 public async void ReaderThread()
 {
     lock (readerLoopLock)
     {
         if (bReaderLoopRunning)
         {
             return;
         }
         bReaderLoopRunning = true;
     }
     try
     {
         bool bPreviousError = false;
         while (TheBaseAssets.MasterSwitch && IsConnected)
         {
             if (!MyBaseEngine.GetEngineState().IsSimulated)
             {
                 var error = OpenModBus();
                 if (!string.IsNullOrEmpty(error))
                 {
                     MyBaseThing.LastMessage = $"{DateTime.Now} - Modbus Device could not be opened: {error}";
                     if (bPreviousError)
                     {
                         TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l1_Error));
                         MyBaseThing.StatusLevel = 3;
                     }
                     else
                     {
                         TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l2_Warning));
                         MyBaseThing.StatusLevel = 2;
                     }
                     bPreviousError = true;
                 }
                 else
                 {
                     try
                     {
                         if (bPreviousError)
                         {
                             MyBaseThing.LastMessage = $"{DateTime.Now} - Modbus Device connected after previous error.";
                             TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l4_Message));
                             bPreviousError = false;
                         }
                         else
                         {
                             if (MyBaseThing.StatusLevel != 1)
                             {
                                 MyBaseThing.LastMessage = $"{DateTime.Now} - Modbus Device connected.";
                                 TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l4_Message));
                             }
                         }
                         MyBaseThing.StatusLevel = 1;
                         Dictionary <string, object> dict = ReadAll();
                         var timestamp = DateTimeOffset.Now;
                         TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(MyBaseThing.EngineName, String.Format("Setting properties for {0}", MyBaseThing.FriendlyName), eMsgLevel.l4_Message, String.Format("{0}: {1}", timestamp, dict.Aggregate("", (s, kv) => s + string.Format("{0}={1};", kv.Key, kv.Value)))));
                         MyBaseThing.SetProperties(dict, timestamp);
                         if (!KeepOpen)
                         {
                             CloseModBus();
                         }
                     }
                     catch (Exception e)
                     {
                         MyBaseThing.StatusLevel = 2;
                         MyBaseThing.LastMessage = $"{DateTime.Now} - Failure during read of modbus properties: {e.Message}";
                         TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l2_Warning, e.ToString()));
                         CloseModBus(); // Close the connection just in case there's an internal socket error that we didn't detect (yet)
                     }
                 }
             }
             else
             {
                 ExecuteSimulation();
             }
             await TheCommonUtils.TaskDelayOneEye((int)Interval, 100);
         }
     }
     catch (Exception e)
     {
         MyBaseThing.LastMessage = $"{DateTime.Now} - Error during read or processing of modbus properties: {e.Message}";
         TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l1_Error, e.ToString()));
     }
     finally
     {
         lock (readerLoopLock)
         {
             if (bReaderLoopRunning)
             {
                 bReaderLoopRunning = false;
             }
         }
         Disconnect(null);
     }
 }