The logic » History » Version 22

Janez Barbic, 11.01.2011 15:40

1 1 Aleksander Bešir
h1. The logic
2 1 Aleksander Bešir
3 2 Aleksander Bešir
{{toc}}
4 2 Aleksander Bešir
5 1 Aleksander Bešir
h2. Basic concept
6 3 David Božjak
7 3 David Božjak
System Eneratptor is designed to take predefined actions in response to events in the surrounding world. The logic system is designed to be easily calibrated and managed. In addition to its predefined logic it allows the human administrator (through HMI) to reverse any automated or scripted decision and to execute any of the predefined actions, thus allowing the operator to tweak the system according to her best judgement.
8 3 David Božjak
9 3 David Božjak
!Logika.jpg!
10 3 David Božjak
11 3 David Božjak
The logic of the system consists of the following concepts:
12 11 David Božjak
* Device maneger
13 4 David Božjak
* Mailbox
14 4 David Božjak
* Decision making software
15 4 David Božjak
* Predefined action list
16 4 David Božjak
* Action
17 7 Aleksander Bešir
* [[Human Machine Interface]]
18 1 Aleksander Bešir
19 11 David Božjak
The basic idea behind this design is modularity. Eneraptor supports any device and can execute any action as long as the driver follows our standard. In this scenario we [the eneraptor designers] expect the device manufacturer (ie air conditioning unit) at least two supply two _programs_ : the device manager and the action program. If there are several actions supported by the device, we expect each to be a standalone program or all actions to be in a single program targeted by different command line arguments.
20 6 David Božjak
21 3 David Božjak
h2. Concepts described in detail
22 1 Aleksander Bešir
23 11 David Božjak
h3. Device manager
24 4 David Božjak
25 11 David Božjak
Device manager is basically a program. The eneratptor system doesn't know anything about it, the only crucial part is the driver-mailbox communication through witch data is supplied. More about date in the _message specification_. The device manager has to know how to reach the mailbox and how to correctly format the message. The device manager does not receive any confirmation from the message recipient.
26 6 David Božjak
27 6 David Božjak
This driver is not necessarily the same software which is supplied by the manufacturer, it is only a program that communicates with the eneratpr Mailbox.
28 6 David Božjak
29 3 David Božjak
h3. Mailbox
30 6 David Božjak
31 11 David Božjak
Mailbox is a bridge between the device managers and the decision making software. It is a buffer of messages that the decision making software can read at its own pace.
32 1 Aleksander Bešir
33 12 David Božjak
*The design of the mailbox and message structure is still open to debate; all input is welcome!*
34 1 Aleksander Bešir
35 12 David Božjak
The mailbox should be event driven, meaning no periodic checking is necessary. The device manager will signal the control program which will in turn read the shared variable for data.
36 12 David Božjak
37 3 David Božjak
h3. Decision making software
38 1 Aleksander Bešir
39 8 David Božjak
Decision making software is the key mechanism of the eneraptor project. It brings all other components together to ensure all conditions set by (human) operators are met.
40 8 David Božjak
41 8 David Božjak
The decision making software continuously checks the mailbox for new messages. If any new info is read the software compares it with all the predefined conditions (see _predefined action list_). If a condition is met, the action is executed. If several conditions are met, all co-responding actions are executed.
42 8 David Božjak
43 8 David Božjak
This (very elegant) main loop is always executing, however it can be halted by the [[Human Machine Interface]]. When this happens the loop still executes, however all but the messages originating from HMI are ignored.
44 8 David Božjak
45 1 Aleksander Bešir
h3. Predefined action list
46 8 David Božjak
47 8 David Božjak
Predefined action list is a list of all the conditions and co-responding actions entered to the system. It is implemented as a non-ordered list of structures containing the information: 
48 1 Aleksander Bešir
49 9 David Božjak
# Input device ID
50 9 David Božjak
# Corner value
51 9 David Božjak
# Comparison operator
52 9 David Božjak
# Action to be executed
53 9 David Božjak
54 10 David Božjak
The list is saved in a csv/xml file. It is initialized with on the Decision Making Software program startup and then periodically refreshed, insuring no restart is required when additional rules are entered.
55 10 David Božjak
56 1 Aleksander Bešir
h3. Action
57 9 David Božjak
58 9 David Božjak
Action is a program path and program argument pair, thus allowing the _Decision making software_ to execute it.
59 9 David Božjak
60 9 David Božjak
There are several rules for the Action developers:
61 9 David Božjak
62 1 Aleksander Bešir
* The Action program must be executable through command line,
63 1 Aleksander Bešir
* The Action program must *not* expect human interaction,
64 1 Aleksander Bešir
* The Action program must terminate in a deterministic time,
65 1 Aleksander Bešir
66 1 Aleksander Bešir
Outside of this very general rules the Action developers have no limitations in writing the Action programs.
67 11 David Božjak
68 11 David Božjak
h2. Communication and message formats
69 11 David Božjak
70 11 David Božjak
The logic component uses three types of messages to connect to other parts of the Eneraptor system. They are the following:
71 11 David Božjak
72 11 David Božjak
* The message for communication with device manager -> XML
73 11 David Božjak
* The message for communication with HMI -> XML
74 11 David Božjak
* The message for logging (via syslog) -> formatted plain text
75 11 David Božjak
76 1 Aleksander Bešir
These messages are then transported over the network to their destination.
77 11 David Božjak
78 12 David Božjak
* The Logic -> device manager communication uses *IP sockets* to relay messages.
79 11 David Božjak
* The Logic <--> HMI communication uses *UDP* communication to relay messages.
80 22 Janez Barbic
* The Logic <--> Logging service uses system logger for logging messages. Relaying is done by syslog-ng.
81 11 David Božjak
82 1 Aleksander Bešir
h3. The logic <--> device manager message
83 1 Aleksander Bešir
84 13 David Božjak
The device manager logic is twofold: when the device manager signals new available data, it is read from a shared variable (encapsulated within a monitor). The IP socket is used for the reverse connection, when the HMI request a certain value which has to be read. When that happens a +request message+ is sent. The messaging thread blocks until an answer is received. The response is then transformed to Logic -> HMI message and sent to HMI.
85 12 David Božjak
86 12 David Božjak
The syntax of the request message is irrelevant since the there aren't several data points to choose from. The response however should be a single double value (defined with IEEE 754).
87 12 David Božjak
88 12 David Božjak
*Questions:*
89 16 David Božjak
* Will there ever be a device manager with several data points available for reading?
90 12 David Božjak
91 12 David Božjak
*Notes:*
92 12 David Božjak
* The named pipe option was considered however I prefer the IP socket protocol. The main reason behind this decision is that a named pipe can only be open for reading *or* writing thus complicating the programming somewhat.
93 12 David Božjak
94 11 David Božjak
h3. The logic <--> HMI message
95 11 David Božjak
96 14 Aleksander Bešir
> Alex B. proposal:
97 14 Aleksander Bešir
>
98 14 Aleksander Bešir
> There should exist three types of messages used for communication between the logic and HMI, depending on how should the logic react after receiving a message:
99 14 Aleksander Bešir
> * Type1: HMI requests for some information; logic replies.
100 14 Aleksander Bešir
> * Type2: HMI requests for a change in logic's configuration; logic reacts; logic sends a message confirming changes have been succesfully (or unsucsessfully) applied.
101 14 Aleksander Bešir
> * Type3: HMI wants to add or change a new acion
102 14 Aleksander Bešir
>
103 14 Aleksander Bešir
> The first message type from HMI could looke like this:
104 14 Aleksander Bešir
> <pre><code class="xml">
105 14 Aleksander Bešir
<hmi-request>
106 14 Aleksander Bešir
   <device-stat>
107 14 Aleksander Bešir
      <device id="Thermometer_01" />
108 14 Aleksander Bešir
      <device id="Moisture_meter" />
109 14 Aleksander Bešir
   </device-stat>
110 14 Aleksander Bešir
   <config-stat>
111 14 Aleksander Bešir
      <config id="startup_time" />
112 14 Aleksander Bešir
      <config id="logging-enabled" />
113 14 Aleksander Bešir
   </config-stat>
114 14 Aleksander Bešir
</hmi-request>
115 14 Aleksander Bešir
  </code></pre> 
116 14 Aleksander Bešir
>
117 14 Aleksander Bešir
> And the logic's response could look like this:
118 14 Aleksander Bešir
> <pre><code class="xml">
119 14 Aleksander Bešir
<logic-response>
120 14 Aleksander Bešir
   <device-stat>
121 14 Aleksander Bešir
      <device id="Thermometer_01">
122 14 Aleksander Bešir
         <value>24</value>
123 14 Aleksander Bešir
      </device>
124 14 Aleksander Bešir
      <device id="Moisture_meter">
125 14 Aleksander Bešir
         <error type="device_controller_returned_1" />
126 14 Aleksander Bešir
      </device>
127 14 Aleksander Bešir
   </device-stat>
128 14 Aleksander Bešir
   <config-stat>
129 14 Aleksander Bešir
      <config id="startup_time">08:25:22 22/12/2010</config>
130 14 Aleksander Bešir
      <config id="logging-enabled">true</config>
131 14 Aleksander Bešir
   </config-stat>
132 14 Aleksander Bešir
</logic-response>
133 14 Aleksander Bešir
  </code></pre>
134 14 Aleksander Bešir
>
135 14 Aleksander Bešir
> The second type of HMI's request could look like this:
136 14 Aleksander Bešir
> <pre><code class="xml">
137 14 Aleksander Bešir
<hmi-request>
138 14 Aleksander Bešir
   <config>
139 14 Aleksander Bešir
      <config id="logging-enabled">false</config>
140 14 Aleksander Bešir
   </config>
141 14 Aleksander Bešir
</hmi-request>
142 14 Aleksander Bešir
  </code></pre> 
143 14 Aleksander Bešir
>
144 14 Aleksander Bešir
> And the response could look like this:
145 14 Aleksander Bešir
> <pre><code class="xml">
146 14 Aleksander Bešir
<logic-response>
147 14 Aleksander Bešir
   <config>
148 14 Aleksander Bešir
      <config id="logging-enabled">success</config>
149 14 Aleksander Bešir
   </config>
150 14 Aleksander Bešir
</logic-response>
151 14 Aleksander Bešir
  </code></pre>
152 14 Aleksander Bešir
>
153 14 Aleksander Bešir
> The third message type from HMI could look like this:
154 14 Aleksander Bešir
> <pre><code class="xml">
155 14 Aleksander Bešir
<hmi-request>
156 14 Aleksander Bešir
   
157 14 Aleksander Bešir
  <todo>
158 14 Aleksander Bešir
159 14 Aleksander Bešir
</hmi-request>
160 14 Aleksander Bešir
  </code></pre> 
161 14 Aleksander Bešir
>
162 14 Aleksander Bešir
> And the response could look like this:
163 14 Aleksander Bešir
> <pre><code class="xml">
164 14 Aleksander Bešir
<logic-response>
165 14 Aleksander Bešir
   
166 14 Aleksander Bešir
  <todo>
167 14 Aleksander Bešir
168 14 Aleksander Bešir
</logic-response>
169 14 Aleksander Bešir
  </code></pre> 
170 14 Aleksander Bešir
>
171 14 Aleksander Bešir
> Of course all three types of messages could be joined in a single XML file _if_ this would be useful (?).
172 14 Aleksander Bešir
173 11 David Božjak
h3. The logic -> logging
174 1 Aleksander Bešir
175 13 David Božjak
The logic uses syslog to save a plain text message to the database. However the message has to be formatted, so the information can be extracted by the Statistical Data Analysis Software ([[Archive and statistics]]. 
176 13 David Božjak
177 13 David Božjak
The archiving can be initiated for the following reasons:
178 13 David Božjak
* Type1: New data was sent by the device manager,
179 13 David Božjak
* Type2: Action was taken due to the read changes in the observed world,
180 13 David Božjak
* Type3: Action was initiated by the system administrator via HMI.
181 13 David Božjak
182 15 David Božjak
The reason for the archiving has to be visible in the message. Thus the first byte of the message is reserved for the type of the message. The rest of the message is conditioned by the type. The first byte is followed by:
183 13 David Božjak
184 13 David Božjak
h4. Type1
185 1 Aleksander Bešir
186 15 David Božjak
* The new data received, saved in a single double value (IEEE 754).
187 15 David Božjak
188 13 David Božjak
h4. Type2
189 1 Aleksander Bešir
190 15 David Božjak
* The action taken (id of the action saved in a single short int value)
191 15 David Božjak
192 1 Aleksander Bešir
h4. Type3
193 13 David Božjak
194 15 David Božjak
* The action taken (id) followed by the system administrator id.
195 13 David Božjak
196 17 David Božjak
*Questions:*
197 17 David Božjak
* Should the files be human readable or should they be just a data stream?
198 17 David Božjak
* Is the datetime part of the log written automatically by the syslog? (The above data structures assume so).
199 17 David Božjak
* Did I forget any vital information? 
200 17 David Božjak
201 1 Aleksander Bešir
h2. Configuration
202 12 David Božjak
203 13 David Božjak
The configuration consists of a XML file, which is read on startup. The XML file should look like this:
204 1 Aleksander Bešir
205 13 David Božjak
<todo>
206 13 David Božjak
207 13 David Božjak
The configuration can also be modified by the HMI. When that happens the XML configuration file has to be modified to reflect the new changes.
208 13 David Božjak
209 1 Aleksander Bešir
h2. The interfaces
210 13 David Božjak
211 13 David Božjak
The following interfaces have to be defined by the extern components for them to be seen as a valid Eneraptor component:
212 13 David Božjak
213 13 David Božjak
<todo>
214 18 David Božjak
215 18 David Božjak
h2. Logic separation to parts
216 18 David Božjak
217 18 David Božjak
The logic can be separated into several key components:
218 18 David Božjak
219 18 David Božjak
* Communication with the device managers,
220 18 David Božjak
* Communication with the HMI,
221 20 David Božjak
* The startup method which reads the configuration and starts all the necessary threads,
222 20 David Božjak
* The device manager.
223 18 David Božjak
224 18 David Božjak
h3. The startup method
225 18 David Božjak
226 18 David Božjak
The startup method is run when the logic program first starts and is responsible for setting up all the threads according to the configuration XML file. It starts a "Device manager <--> The Logic" thread for each measuring device found in the configuration file. Then it should start the "HMI <--> The Logic" thread. 
227 18 David Božjak
228 18 David Božjak
After the initialization is complete this function waits for termination signal (via HMI). When the termination signal is received it should stop all the "Device manager <--> The Logic" threads, leaving only one communication channel open: The "HMI <--> The Logic" thread. The function then waits for the resume signal (from HMI). When the resume signal is received, the configuration file is used to restart the "Device manager <--> The Logic" communication.
229 18 David Božjak
230 18 David Božjak
It should also be noted that the configuration file may change (due to human interaction via HMI). When that happens the "HMI <--> The Logic" thread signals The startup method which in response rechecks the file for changes and acts accordingly.
231 19 David Božjak
232 19 David Božjak
h3. Communication with the HMI
233 19 David Božjak
234 19 David Božjak
Communication with the HMI runs in its own thread (see *The startup method*). It consists of a blocking socket server that waits for incoming message from the HMI. When the connection is established a new thread is started to deal with the request and the main thread starts waiting for another connection.
235 19 David Božjak
236 19 David Božjak
This part of the Logic interacts with The startup method for signaling it the termination/resume signal. It is also interacting with the "Device manager <--> The Logic" threads for generating responses to incoming requests from HMI.
237 19 David Božjak
238 1 Aleksander Bešir
h3. Communication with the device managers
239 1 Aleksander Bešir
240 20 David Božjak
Communication with the device managers runs in its own threads, one for each measuring device (see *The startup method*). The communication with the device manager is twofold (see *The logic <--> device manager message*). This thread waits for the device manager to signal a change in its status variable. The change is then logged (via Syslog). The new value is evaluated and any necessary action program is run. This continues until the termination signal (from "The startup method") is received.
241 20 David Božjak
242 20 David Božjak
The other assignment of this thread is to create a response for the HMI. When the request is received the "HMI <--> The Logic" reads the last value read from the thread and returns it to HMI.
243 20 David Božjak
244 21 David Božjak
*Note:* It was previously discussed that a new value should be requested from the device manager for the response to the HMI. This is still an option, however the logic software is becoming rather complex. If the device managers check their value often enough and signal for every change then the value should be up to date thus making another communication channel redundant.
245 20 David Božjak
246 1 Aleksander Bešir
h3. Device manager
247 21 David Božjak
248 21 David Božjak
The device manager is a program that can read from a measuring device. It reads the value periodically and signals The Logic on every change.