private VTTQ ReadVTTQ(DbDataReader reader) { Timestamp t = Timestamp.FromJavaTicks((long)reader["time"]); Timestamp tDB = t + Duration.FromSeconds((long)reader["diffDB"]); Quality q = (Quality)(int)(long)reader["quality"]; string data = (string)reader["data"]; return(new VTTQ(t, tDB, q, DataValue.FromJSON(data))); }
public static Timestamp GetNextNormalizedTimestamp(Duration cycle, Duration offset) { long cycleTicks = cycle.TotalMilliseconds; long offsetTicks = offset.TotalMilliseconds; long nowTicks = Timestamp.Now.JavaTicks - offsetTicks; long tLast = nowTicks - (nowTicks % cycleTicks); long tNext = tLast + cycleTicks + offsetTicks; return(Timestamp.FromJavaTicks(tNext)); }
public static List <VariableValue> Deserialize(BinaryReader reader) { int binaryVersion = reader.ReadByte(); if (binaryVersion == 0) { throw new IOException("Failed to deserialize VariableValue[]: Version byte is zero"); } if (binaryVersion > Common.CurrentBinaryVersion) { throw new IOException("Failed to deserialize VariableValue[]: Wrong version byte"); } if (reader.ReadByte() != Code) { throw new IOException("Failed to deserialize VariableValue[]: Wrong start byte"); } int N = reader.ReadInt32(); if (N > Common.MaxListLen) { throw new System.Exception($"VariableValue_Serializer: May not deserialize more than {Common.MaxListLen} items"); } var res = new List <VariableValue>(N); if (N == 0) { return(res); } long timeBase = reader.ReadInt64(); long diffBase = reader.ReadInt64(); string valBase = reader.ReadString(); string[] moduleIDs = new string[MaxModules]; string[] variableNames = new string[MaxVariables]; int countModulesIDs = reader.ReadByte(); for (int i = 0; i < countModulesIDs; ++i) { moduleIDs[i] = reader.ReadString(); } int countVariables = reader.ReadByte(); for (int i = 0; i < countVariables; ++i) { variableNames[i] = reader.ReadString(); } char[] buffer = new char[255]; char[] mapCode2Char = Common.mapCode2Char; for (int k = 0; k < N; ++k) { int control0 = reader.ReadByte(); int idxModuleID = control0 & 0x07; int idxVariable = (control0 & 0xF8) >> 3; bool implicitModuleID = idxModuleID < 0x07; bool implicitVarName = idxVariable < 0x1F; string moduleID; if (implicitModuleID) { moduleID = moduleIDs[idxModuleID]; } else { moduleID = reader.ReadString(); } string variable; if (implicitVarName) { variable = variableNames[idxVariable]; } else { variable = reader.ReadString(); } string objectID = reader.ReadString(); int control = reader.ReadByte(); Quality q = (Quality)(control & 0x03); long time = timeBase; long diff = diffBase; string val = valBase; if ((control & 0x10) == 0) { long diffFactor = ((control & 0x20) == 0) ? 1 : 1000; int codeByteCount = (control & 0xC0) >> 6; if (codeByteCount == 0) { diff = diffFactor * reader.ReadSByte(); } else if (codeByteCount == 1) { diff = diffFactor * reader.ReadInt16(); } else if (codeByteCount == 2) { diff = diffFactor * reader.ReadInt32(); } else if (codeByteCount == 3) { diff = diffFactor * reader.ReadInt64(); } } time += diff; if ((control & 0x04) == 0) { if ((control & 0x08) == 0) { int countBytes = reader.ReadByte(); int j = 0; for (int i = 0; i < countBytes; i++) { int b = reader.ReadByte(); int b0 = (0xF0 & b) >> 4; int b1 = (0x0F & b); buffer[j++] = mapCode2Char[b0]; if (b1 == 0x0F) { break; } buffer[j++] = mapCode2Char[b1]; } val = new string(buffer, 0, j); } else { val = reader.ReadString(); } } var vtq = VTQ.Make(DataValue.FromJSON(val), Timestamp.FromJavaTicks(time), q); res.Add(VariableValue.Make(moduleID, objectID, variable, vtq)); timeBase = time; diffBase = diff; valBase = val; } return(res); }
public static List <VTQ> Deserialize(BinaryReader reader) { int binaryVersion = reader.ReadByte(); if (binaryVersion == 0) { throw new IOException("Failed to deserialize VTQ[]: Version byte is zero"); } if (binaryVersion > Common.CurrentBinaryVersion) { throw new IOException("Failed to deserialize VTQ[]: Wrong version byte"); } if (reader.ReadByte() != Code) { throw new IOException("Failed to deserialize VTQ[]: Wrong start byte"); } int N = reader.ReadInt32(); if (N > Common.MaxListLen) { throw new System.Exception($"VTQ_Serializer: May not deserialize more than {Common.MaxListLen} items"); } var res = new List <VTQ>(N); if (N == 0) { return(res); } long timeBase = reader.ReadInt64(); long diffBase = reader.ReadInt64(); string valBase = reader.ReadString(); char[] buffer = new char[255]; char[] mapCode2Char = Common.mapCode2Char; for (int k = 0; k < N; ++k) { int control = reader.ReadByte(); Quality q = (Quality)(control & 0x03); long time = timeBase; long diff = diffBase; string val = valBase; if ((control & 0x10) == 0) { long diffFactor = ((control & 0x20) == 0) ? 1 : 1000; int codeByteCount = (control & 0xC0) >> 6; if (codeByteCount == 0) { diff = diffFactor * reader.ReadSByte(); } else if (codeByteCount == 1) { diff = diffFactor * reader.ReadInt16(); } else if (codeByteCount == 2) { diff = diffFactor * reader.ReadInt32(); } else if (codeByteCount == 3) { diff = diffFactor * reader.ReadInt64(); } } time += diff; if ((control & 0x04) == 0) { if ((control & 0x08) == 0) { int countBytes = reader.ReadByte(); int j = 0; for (int i = 0; i < countBytes; i++) { int b = reader.ReadByte(); int b0 = (0xF0 & b) >> 4; int b1 = (0x0F & b); buffer[j++] = mapCode2Char[b0]; if (b1 == 0x0F) { break; } buffer[j++] = mapCode2Char[b1]; } val = new string(buffer, 0, j); } else { val = reader.ReadString(); } } res.Add(VTQ.Make(DataValue.FromJSON(val), Timestamp.FromJavaTicks(time), q)); timeBase = time; diffBase = diff; valBase = val; } return(res); }
public override Task <Result <DataValue> > OnMethodCall(Origin origin, string methodName, NamedValue[] parameters) { if (methodName == "GetActiveAlarms") { var data = DataValue.FromObject(aggregatedWarningsAndAlarms); var res = Result <DataValue> .OK(data); return(Task.FromResult(res)); } if (methodName == "Ack" || methodName == "Reset") { try { string comment = parameters.FirstOrDefault(n => n.Name == "Comment").Value; string strKeys = parameters.FirstOrDefault(n => n.Name == "Keys").Value; Timestamp[] keys = strKeys.Split(';').Select(s => long.Parse(s)).Select(n => Timestamp.FromJavaTicks(n)).ToArray(); bool ack = methodName == "Ack"; if (ack) { DoACK(keys, origin, comment); } else { DoReset(keys, origin, comment); } var res = Result <DataValue> .OK(DataValue.Empty); return(Task.FromResult(res)); } catch (Exception exp) { return(Task.FromResult(Result <DataValue> .Failure(exp.Message))); } } return(base.OnMethodCall(origin, methodName, parameters)); }
private async Task AdapterScheduledReadTask(AdapterState adapter) { var dict = new Dictionary <Timestamp, List <string> >(); adapter.Instance.StartRunning(); adapter.State = State.Running; while (adapter.State == State.Running) { dict.Clear(); Timestamp Now = Timestamp.Now; foreach (ItemSchedule it in adapter.ScheduledDataItems) { long intervalMS = it.Interval.TotalMilliseconds; long offMS = it.Offset.TotalMilliseconds; long intervals = 1 + (Now.JavaTicks - offMS) / intervalMS; Timestamp tStart = Timestamp.FromJavaTicks(intervals * intervalMS + offMS); if (!dict.ContainsKey(tStart)) { dict[tStart] = new List <string>(); } dict[tStart].Add(it.DataItemID); } if (dict.Count == 0) { await Task.Delay(TimeSpan.FromSeconds(1)); } else { KeyValuePair <Timestamp, List <string> > x = dict.OrderBy(kv => kv.Key).First(); Timestamp timestamp = x.Key; List <string> itemsToRead = x.Value; Duration waitTime = timestamp - Timestamp.Now; if (waitTime > Duration.FromSeconds(5)) { await Task.Delay(TimeSpan.FromSeconds(1)); } else { while (waitTime.TotalMilliseconds > 0) { await Task.Delay((int)waitTime.TotalMilliseconds); waitTime = timestamp - Timestamp.Now; } if (adapter.State == State.Running && adapter.Instance != null) { ReadRequest[] requests = itemsToRead .Where(it => !adapter.SetOfPendingReadItems.Contains(it)) .Select(s => { VTQ value = dataItemsState[s].LastReadValue; return(new ReadRequest(s, value)); }).ToArray(); if (requests.Length == 0) { await Task.Delay(500); } else { foreach (ReadRequest rr in requests) { adapter.SetOfPendingReadItems.Add(rr.ID); } IList <ReadTask> readTasks = adapter.ReadItems(requests, null); foreach (ReadTask rt in readTasks) { Task tx = rt.Task.ContinueWith(completedReadTask => { adapter.SetOfPendingReadItems.ExceptWith(rt.IDs); if (adapter.State == State.Running) { if (completedReadTask.IsFaulted) { Exception exp = completedReadTask.Exception.GetBaseException() ?? completedReadTask.Exception; Task ignored = RestartAdapterOrCrash(adapter, "Scheduled read exception: " + exp.Message); } DataItemValue[] result = completedReadTask.Result; var values = new List <VariableValue>(result.Length); var badItems = new List <ItemState>(); var goodItems = new List <ItemState>(); foreach (DataItemValue val in result) { if (dataItemsState.ContainsKey(val.ID)) { VTQ vtq = val.Value; bool strict = !adapter.NonStrictScheduledDataItems.Contains(val.ID); if (strict) { vtq.T = timestamp; } ItemState istate = dataItemsState[val.ID]; if (vtq.Q == Quality.Bad && istate.LastReadValue.Q != Quality.Bad) { badItems.Add(istate); } else if (vtq.Q == Quality.Good && istate.LastReadValue.Q != Quality.Good) { goodItems.Add(istate); } vtq = RoundFloat(istate, vtq); istate.LastReadValue = vtq; values.Add(VariableValue.Make(moduleID, val.ID, VariableName, vtq)); } } if (values.Count > 0) { notifier.Notify_VariableValuesChanged(values); } NotifyQualityChange(badItems, goodItems); } }); } } } } } } // while running }