Blame view

app/gui/oven_control/haccp.cpp 11.6 KB
af27778be   김태훈   HACCP 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include "haccp.h"
  
  #include <QDateTime>
  #include <QDir>
  #include <QTextStream>
  #include <QTimer>
  
  #include "unistd.h"
  
  #include "stringer.h"
  #include "oven.h"
  
  namespace
  {
  
  enum Type { Invalid, Manual, Auto, Multi, Wash, Engineering };
  
  struct Stamp
  {
      QDateTime time;
      int temp;
      int targetCoreTemp;
      int curCoreTemp;
      bool door;
      QString caption;
  };
  
  struct Config
  {
      Define::CookConfigType type;
      int level;
  };
  
  struct Data
  {
      QDateTime startedAt;
      Type type;
      QString autoRoot;
      QList<Config> autoConfig;
      int washType;
      QList<Stamp> records;
  } data;
af27778be   김태훈   HACCP 구현
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  int processCount;
  QTimer checkTimer;
  QTime lastStampedTime;
  
  Define::Mode lastMode;
  int lastFan;
  bool lastDoor;
  bool lastDamper;
  bool lastSideNozzle;
  bool lastCoreTempEnabled;
  
  int figureProcessCount();
  void initData(Type type);
  void start();
  void stamp(QString caption = "");
  void stampStart();
  void stampMode(Define::Mode mode);
  void stampDamper();
  void stampSideNozzle();
  void stampEnd();
  void stampError(QString error);
  void saveData();
  void check();
  }
  
  namespace HACCP
  {
  
  void init()
  {
      // make the directory if it doesn't exist
      QDir().mkpath("/prime/haccp");
af27778be   김태훈   HACCP 구현
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
      processCount = figureProcessCount() + 1;
  
      checkTimer.setInterval(100);
      QObject::connect(&checkTimer, &QTimer::timeout, check);
  
      Oven *oven = Oven::getInstance();
      QObject::connect(oven, &Oven::changed, check);
  }
  
  void autoStart(const QString &path)
  {
      initData(Auto);
      data.autoRoot = path;
  
      start();
  }
  
  void manualStart()
  {
af27778be   김태훈   HACCP 구현
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
      initData(Manual);
      start();
  }
  
  void multiStart()
  {
      initData(Multi);
      start();
  }
  
  void washStart(int type)
  {
      initData(Wash);
      data.washType = type;
  }
  
  void engineeringStart()
  {
      initData(Engineering);
  }
  
  void done()
  {
af27778be   김태훈   HACCP 구현
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
      if (data.type == Invalid)
          return;
  
      stampEnd();
      saveData();
  }
  
  void error(QString code)
  {
      if (data.type == Invalid)
          return;
  
      stampError(code);
      saveData();
  }
  
  void setConfig(Define::CookConfigType type, int level)
  {
      data.autoConfig.append(Config{type, level});
  }
  
  void setStep(Define::StepType type)
  {
      switch (type)
      {
      case Define::Preheat:
          stamp("preheat");
          break;
      case Define::Load:
          stamp("load");
          break;
      case Define::PutThermometer:
          stamp("therometer");
          break;
      case Define::Cut:
          stamp("cut");
          break;
      case Define::Pour:
          stamp("pour");
          break;
      case Define::Bake:
          stamp("bake");
          break;
      case Define::Dry:
          stamp("dry");
          break;
      case Define::Ferment:
          stamp("ferment");
          break;
      case Define::BlowSteam:
          stamp("steaming");
          break;
      case Define::CoolDown:
          stamp("cooldown");
          break;
      case Define::Steam:
          stamp("steam");
          break;
      case Define::Roast:
          stamp("roasting");
          break;
      case Define::Boil:
          stamp("boil");
          break;
      case Define::Thicken:
          stamp("thicken");
          break;
      case Define::WarmUp:
          stamp("warmup");
          break;
      case Define::MakeCrispy:
          stamp("crispy2");
          break;
      case Define::Finish:
          stamp("finish");
          break;
      case Define::Damp:
          stamp("damp");
          break;
      case Define::Defer:
          stamp("defer");
          break;
      case Define::Grill:
          stamp("grill");
          break;
      case Define::End:
          stamp("end");
          break;
      case Define::Burn:
          stamp("burn");
          break;
      case Define::Fry:
          stamp("fry");
          break;
      case Define::HeatUp:
          stamp("heatup");
          break;
      case Define::Ripen:
          stamp("ripen");
          break;
      case Define::RipenKeep:
          stamp("ripenkeep");
          break;
      case Define::BoilSteadily:
          stamp("boilsteadily");
          break;
      case Define::CookGratin:
          stamp("cookgratin");
          break;
      case Define::Brown:
          stamp("brown");
          break;
      case Define::Simmer:
          stamp("simmer");
          break;
      case Define::Moisten:
          stamp("moisten2");
          break;
      }
  }
  
  void setProcess(Define::Process type)
  {
      switch (type)
      {
      case Define::CookAgain:
          stamp("again");
          break;
      case Define::MakeCrisper:
          stamp("crispy3");
          break;
      case Define::KeepWarm:
          stamp("heatreserve");
          break;
      }
  }
  
  }
  
  
  namespace
  {
  
  QString path()
  {
      return "/prime/haccp";
  }
  
  QString path(int year)
  {
      return QString("/prime/haccp/%1").arg(year, 4, 10, QLatin1Char('0'));
  }
  
  QString path(int year, int month)
  {
      return QString("/prime/haccp/%1/%2")
              .arg(year, 4, 10, QLatin1Char('0'))
              .arg(month, 2, 10, QLatin1Char('0'));
  }
  
  QString path(int year, int month, int day)
  {
      return QString("/prime/haccp/%1/%2/%3")
              .arg(year, 4, 10, QLatin1Char('0'))
              .arg(month, 2, 10, QLatin1Char('0'))
              .arg(day, 2, 10, QLatin1Char('0'));
  }
  
  QString path(int year, int month, int day, int proc)
  {
      return QString("/prime/haccp/%1/%2/%3/%4")
              .arg(year, 4, 10, QLatin1Char('0'))
              .arg(month, 2, 10, QLatin1Char('0'))
              .arg(day, 2, 10, QLatin1Char('0'))
              .arg(proc);
  }
  
  int figureLatest(const QString &path, QDir::Filter filter)
  {
      QDir root(path);
      if (!root.exists())
          return -1;
  
      QStringList list = root.entryList(filter);
      if (list.empty())
          return -1;
  
      int latest = -1;
      foreach (QString e, list)
      {
          bool ok;
          int num = e.toInt(&ok);
          if (!ok)
              continue;
  
          if (num > latest)
              latest = num;
      }
  
      return latest;
  }
  
  int figureLatestDir(const QString &path)
  {
      return figureLatest(path, QDir::AllDirs);
  }
  
  int figureLatestFile(const QString &path)
  {
      return figureLatest(path, QDir::Files);
  }
  
  int figureProcessCount()
  {
      int year = figureLatestDir(path());
      if (year < 0)
          return 0;
  
      int month = figureLatestDir(path(year));
      if (month < 0)
          return 0;
  
      int day = figureLatestDir(path(year, month));
      if (day < 0)
          return 0;
  
      int proc = figureLatestFile(path(year, month, day));
      if (proc < 0)
          return 0;
  
      return proc;
  }
  
  void initData(Type type)
  {
      data.type = type;
      data.autoRoot = QString();
      data.autoConfig.clear();
      data.washType = 0;
      data.records.clear();
c25b83f42   김태훈   전원 켜짐 기록 기능 제거
357
      data.startedAt = QDateTime::currentDateTime();
af27778be   김태훈   HACCP 구현
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  }
  
  void start()
  {
      qDebug() << __FUNCTION__;
  
      lastMode = Define::InvalidMode;
      lastFan = -1;
      lastDoor = false;
      lastDamper = false;
      lastSideNozzle = false;
      lastCoreTempEnabled = false;
  
      Oven *oven = Oven::getInstance();
      stampMode(oven->mode());
  
      checkTimer.start();
  }
  
  void stamp(QString caption)
  {
      qDebug() << __FUNCTION__;
  
      if (data.type == Invalid)
          return;
  
      Oven *oven = Oven::getInstance();
  
      Stamp s;
      s.time = QDateTime::currentDateTime();
      s.temp = oven->currentTemp();
  
      if (oven->interTempEnabled())
      {
          s.targetCoreTemp = oven->interTemp();
          s.curCoreTemp = oven->currentInterTemp();
      }
      else
      {
          s.targetCoreTemp = 0;
          s.curCoreTemp = 0;
      }
  
      s.door = oven->door();
      s.caption = caption;
  
      data.records.append(s);
  
      lastDoor = oven->door();
      lastStampedTime.start();
  }
  
  void stampStart()
  {
af27778be   김태훈   HACCP 구현
412
413
414
415
416
      stamp("START (POWER ON)");
  }
  
  void stampMode(Define::Mode mode)
  {
af27778be   김태훈   HACCP 구현
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
      switch (mode)
      {
      case Define::SteamMode:
          stamp("Mode STEAM");
          break;
      case Define::CombiMode:
          stamp("Mode COMBI");
          break;
      case Define::DryMode:
          stamp("Mode DRY");
          break;
      }
  
      lastMode = mode;
  }
  
  void stampFan(int fan)
  {
af27778be   김태훈   HACCP 구현
435
436
437
438
439
440
      lastFan = fan;
      stamp(QString("FAN = %1").arg(fan));
  }
  
  void stampDamper()
  {
af27778be   김태훈   HACCP 구현
441
442
443
444
445
      stamp("DAMPER");
  }
  
  void stampSideNozzle()
  {
af27778be   김태훈   HACCP 구현
446
447
448
449
450
      stamp("SIDE NOZZLE");
  }
  
  void stampEnd()
  {
af27778be   김태훈   HACCP 구현
451
452
453
454
455
456
457
458
459
460
      stamp("END");
  }
  
  void stampError(QString error)
  {
      stamp(QString("END (%1)").arg(error));
  }
  
  void saveData()
  {
af27778be   김태훈   HACCP 구현
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
      if (data.records.isEmpty())
      {
          data.type = Invalid;
          return;
      }
  
      QDate date = data.startedAt.date();
  
      QDir().mkpath(path(date.year(), date.month(), date.day()));
  
      QString p = path(date.year(), date.month(), date.day(), processCount);
  
      QFile file(p);
      if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly))
      {
          qDebug() << "Failed to open";
          return;
      }
  
      QTextStream stream(&file);
      stream.setCodec("UTF-8");
  
      // TODO: fill them
      stream << "No.," << processCount << "
  ";
      stream << "Typ," << "
  ";
      stream << "Serial No.," << "
  ";
      stream << "Version," << "
  ";
      stream << "Time," << data.startedAt.toString("yyyy.MM.dd HH:mm:ss") << "
  ";
  
      stream << "Progr,";
      switch (data.type)
      {
      case Manual:
          stream << 1;
          break;
      case Auto:
          stream << data.autoRoot;
          break;
      case Multi:
          stream << 4;
          break;
      case Wash:
          stream << 6 << "-" << data.washType;
          break;
      case Engineering:
          stream << 999;
          break;
      }
      stream << "
  ";
  
      stream << "
  "
             << "#1,Time
  "
             << "#2,Inner Tank Temp
  "
             << "#3,Food Temp (Target)
  "
             << "#4,Food Temp
  "
             << "#5,Door Open / Closed
  ";
  
      stream << "
  ";
  
      stream << "#1,#2,#3,#4,#5
  ";
      for (int i = 0; i < data.records.size(); i++)
      {
          const Stamp &s = data.records.at(i);
          if (!s.caption.isEmpty() && i+1 < data.records.size())
          {
              Stamp &n = data.records[i+1];
  
              int secs = data.startedAt.secsTo(s.time);
              int nextSecs = data.startedAt.secsTo(n.time);
              if (secs == nextSecs
                      && s.temp == n.temp
                      && s.targetCoreTemp == n.targetCoreTemp
                      && s.curCoreTemp == n.curCoreTemp
                      && s.door == n.door)
              {
                  if (n.caption.isEmpty())
                      n.caption = s.caption;
                  else
                      n.caption = QString("%1
  %2").arg(s.caption).arg(n.caption);
  
                  continue;
              }
          }
  
          if (!s.caption.isEmpty())
              stream << s.caption << "
  ";
  
          stream << Stringer::timeSecs(data.startedAt.secsTo(s.time)) << ","
                 << s.temp << ","
                 << s.targetCoreTemp << ","
                 << s.curCoreTemp << ","
                 << (s.door ? "O
  " : "C
  ");
      }
  
      if (!data.autoConfig.isEmpty())
      {
          foreach (Config c, data.autoConfig)
              stream << Define::name(c.type)
                     << " = "
                     << c.level << "
  ";
      }
  
      stream.flush();
      file.close();
      sync();
af27778be   김태훈   HACCP 구현
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
      processCount++;
  
      data.type = Invalid;
  
      checkTimer.stop();
  }
  
  void check()
  {
      switch (data.type)
      {
      case Invalid:
      case Wash:
      case Engineering:
          return;
      default:
          break;
      }
  
      Oven *oven = Oven::getInstance();
  
      if (oven->mode() != lastMode)
          stampMode(oven->mode());
  
      if (oven->fan() != lastFan)
          stampFan(oven->fan());
  
      if (oven->damper() != lastDamper)
      {
          lastDamper = oven->damper();
          if (lastDamper)
              stampDamper();
      }
  
      if (oven->humidification() != lastSideNozzle)
      {
          lastSideNozzle = oven->humidification();
          if (lastSideNozzle)
              stampSideNozzle();
      }
  
      if (oven->interTempEnabled() != lastCoreTempEnabled)
      {
          lastCoreTempEnabled = oven->interTempEnabled();
          stamp();
      }
  
      if (oven->door() != lastDoor)
          stamp();
  
      if (lastStampedTime.elapsed() > 60 * 1000 - 100)
          stamp();
  }
  
  }