Go to the documentation of this file.00001 """Copyright 2012, System Insights, Inc.
00002
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006
00007 http://www.apache.org/licenses/LICENSE-2.0
00008
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License."""
00014
00015 from collections import OrderedDict
00016
00017 class DataItem:
00018 """A Representation of an MTConnect Data Item"""
00019
00020 def __init__(self, name):
00021 self._name = name
00022 self._changed = False
00023 self._value = None
00024 self._separate_line = False
00025
00026 def value(self):
00027 return self._value
00028
00029 def set_value(self, value):
00030 if self._value != value:
00031 self._value = value
00032 self._changed = True
00033
00034 def changed(self):
00035 return self._changed
00036
00037 def separate_line(self):
00038 return self._separate_line
00039
00040 def name(self):
00041 return self._name
00042
00043 def begin(self):
00044 return
00045
00046 def complete(self):
00047 return
00048
00049 def sweep(self):
00050 self._changed = False
00051
00052 def unavailable(self):
00053 if self._value:
00054 self._value = False
00055 self._changed = True
00056
00057 def values(self, all = False):
00058 if self._value:
00059 v = self._value
00060 else:
00061 v = "UNAVAILABLE"
00062 return ["|" + self._name + "|" + str(v)]
00063
00064
00065 class Event(DataItem):
00066 """An Event"""
00067 pass
00068
00069 class Sample(DataItem):
00070 """A Sample"""
00071 pass
00072
00073 class ThreeDSample(DataItem):
00074 def values(self, all = False):
00075 if self._value:
00076 v = ' '.join([str(i) for i in self._value])
00077 else:
00078 v = "UNAVAILABLE"
00079 return ["|" + self._name + "|" + str(v)]
00080
00081
00082 class ConditionActivation:
00083 def __init__(self, level, text, code, qualifier, severity):
00084 self._level = level
00085 self._text = text
00086 self._code = code
00087 self._qualifier = qualifier
00088 self._severity = severity
00089 self._changed = True
00090 self._marked = True
00091
00092 def mark(self):
00093 self._marked = True
00094
00095 def clear(self):
00096 self._marked = False
00097 self._changed = False
00098
00099 def clear_changed(self):
00100 self._changed = False
00101
00102 def marked(self):
00103 return self._marked
00104
00105 def changed(self):
00106 return self._changed
00107
00108 def code(self):
00109 return self._code
00110
00111 def __eq__(self, other):
00112 return self._code == other.code
00113
00114 def __str__(self):
00115 return self._level + "|" + self._code + "|" + self._severity + "|" + self._qualifier + "|" + self._text
00116
00117
00118 class Condition(DataItem):
00119 def __init__(self, name):
00120 DataItem.__init__(self, name)
00121 self._active = OrderedDict()
00122 self._separate_line = True
00123
00124 def add(self, level, text, code, qualifier = "", severity = ""):
00125 self._value = True
00126 if code in self._active:
00127 cond = self._active[code]
00128 cond.mark()
00129 else:
00130 cond = ConditionActivation(level,text, code, qualifier, severity)
00131 self._active[code] = cond
00132 self._changed = True
00133
00134 return cond
00135
00136 def normal(self):
00137 if not self._value:
00138 self._value = True
00139 self._changed = True
00140
00141 def begin(self):
00142 for code, cond in self._active.items():
00143 cond.clear()
00144
00145 def complete(self):
00146 marked = len([cond for code, cond in self._active.items() if not cond.marked()])
00147 if marked > 0:
00148 self._changed = True
00149
00150 def sweep(self):
00151 DataItem.sweep(self)
00152 for code in self._active.keys():
00153 if not self._active[code].marked():
00154 del self._active[code]
00155
00156
00157 def values(self, all = False):
00158 if self._value:
00159 active = [cond for code, cond in self._active.items() if cond.marked()]
00160 if len(active) == 0:
00161 return ["|" + self._name + "|normal||||"]
00162 elif all:
00163 return ["|" + self._name + "|" + str(cond) for cond in active]
00164 else:
00165 cleared = [cond for code, cond in self._active.items() if not cond.marked()]
00166 return ["|" + self._name + "|normal|" + cond.code() + "|||" for cond in cleared] + \
00167 ["|" + self._name + "|" + str(cond) for cond in active if cond.changed()]
00168 else:
00169 return ["|" + self._name + "|UNAVAILABLE||||"]
00170
00171
00172 class SimpleCondition(Condition):
00173 def begin(self):
00174 for code, cond in self._active.items():
00175 cond.clear_changed()
00176
00177 def remove(self, code):
00178 if code in self._active:
00179 cond = self._active[code]
00180 cond.clear()