Blame view

app/gui/oven_control/haccp.cpp 13.2 KB
af27778be   김태훈   HACCP 구현
1
2
3
4
5
6
7
8
9
10
11
  #include "haccp.h"
  
  #include <QDateTime>
  #include <QDir>
  #include <QTextStream>
  #include <QTimer>
  
  #include "unistd.h"
  
  #include "stringer.h"
  #include "oven.h"
21aff1b81   김태훈   HACCP에 시스템 정보 포함
12
  #include "config.h"
39fec1254   김태훈   요청 사항 반영
13
  #include "udphandler.h"
af27778be   김태훈   HACCP 구현
14
15
16
17
18
19
20
21
22
  
  namespace
  {
  
  enum Type { Invalid, Manual, Auto, Multi, Wash, Engineering };
  
  struct Stamp
  {
      QDateTime time;
39fec1254   김태훈   요청 사항 반영
23
24
25
26
      int remainingTime;
  
      int targetTemp;
      int curTemp;
af27778be   김태훈   HACCP 구현
27
28
      int targetCoreTemp;
      int curCoreTemp;
39fec1254   김태훈   요청 사항 반영
29
30
31
32
33
  
      int targetHumidity;
  
      int waterLevel;
      int heater;
af27778be   김태훈   HACCP 구현
34
      QString caption;
39fec1254   김태훈   요청 사항 반영
35
36
37
  
      bool door;
      bool ssv;
af27778be   김태훈   HACCP 구현
38
  };
21aff1b81   김태훈   HACCP에 시스템 정보 포함
39
  struct AutoCookConfig
af27778be   김태훈   HACCP 구현
40
41
42
43
44
45
46
47
48
49
  {
      Define::CookConfigType type;
      int level;
  };
  
  struct Data
  {
      QDateTime startedAt;
      Type type;
      QString autoRoot;
21aff1b81   김태훈   HACCP에 시스템 정보 포함
50
      QList<AutoCookConfig> autoConfig;
af27778be   김태훈   HACCP 구현
51
52
53
      int washType;
      QList<Stamp> records;
  } data;
af27778be   김태훈   HACCP 구현
54
55
56
57
58
59
  int processCount;
  QTimer checkTimer;
  QTime lastStampedTime;
  
  Define::Mode lastMode;
  int lastFan;
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
60
  int lastWaterLevel;
af27778be   김태훈   HACCP 구현
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  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 구현
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
      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 구현
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      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 구현
129
130
131
132
133
134
135
136
137
138
139
      if (data.type == Invalid)
          return;
  
      stampEnd();
      saveData();
  }
  
  void error(QString code)
  {
      if (data.type == Invalid)
          return;
ec6c602ce   김태훈   엔지니어링 모드 실행 중엔 오류...
140
141
142
143
144
145
146
      if (data.type == Engineering)
          stampError(code);
      else
      {
          stampError(code);
          saveData();
      }
af27778be   김태훈   HACCP 구현
147
148
149
150
  }
  
  void setConfig(Define::CookConfigType type, int level)
  {
21aff1b81   김태훈   HACCP에 시스템 정보 포함
151
      data.autoConfig.append(AutoCookConfig{type, level});
af27778be   김태훈   HACCP 구현
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  }
  
  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   김태훈   전원 켜짐 기록 기능 제거
373
      data.startedAt = QDateTime::currentDateTime();
af27778be   김태훈   HACCP 구현
374
375
376
377
  }
  
  void start()
  {
af27778be   김태훈   HACCP 구현
378
379
      lastMode = Define::InvalidMode;
      lastFan = -1;
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
380
      lastWaterLevel = -1;
af27778be   김태훈   HACCP 구현
381
382
383
384
385
386
387
388
389
390
391
392
393
      lastDoor = false;
      lastDamper = false;
      lastSideNozzle = false;
      lastCoreTempEnabled = false;
  
      Oven *oven = Oven::getInstance();
      stampMode(oven->mode());
  
      checkTimer.start();
  }
  
  void stamp(QString caption)
  {
af27778be   김태훈   HACCP 구현
394
395
396
397
      if (data.type == Invalid)
          return;
  
      Oven *oven = Oven::getInstance();
39fec1254   김태훈   요청 사항 반영
398
      UdpHandler *udp = UdpHandler::getInstance();
af27778be   김태훈   HACCP 구현
399
400
401
  
      Stamp s;
      s.time = QDateTime::currentDateTime();
39fec1254   김태훈   요청 사항 반영
402
403
404
405
      s.remainingTime = oven->time();
  
      s.targetTemp = oven->temp();
      s.curTemp = oven->currentTemp();
af27778be   김태훈   HACCP 구현
406
407
408
409
410
411
412
413
414
415
416
  
      if (oven->interTempEnabled())
      {
          s.targetCoreTemp = oven->interTemp();
          s.curCoreTemp = oven->currentInterTemp();
      }
      else
      {
          s.targetCoreTemp = 0;
          s.curCoreTemp = 0;
      }
39fec1254   김태훈   요청 사항 반영
417
      s.targetHumidity = oven->humidity();
af27778be   김태훈   HACCP 구현
418
419
      s.door = oven->door();
      s.caption = caption;
39fec1254   김태훈   요청 사항 반영
420
421
422
      s.waterLevel = udp->waterLevel();
      s.ssv = udp->ssv();
      s.heater = udp->heater();
af27778be   김태훈   HACCP 구현
423
424
425
      data.records.append(s);
  
      lastDoor = oven->door();
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
426
      lastWaterLevel = udp->waterLevel();
af27778be   김태훈   HACCP 구현
427
428
429
430
431
      lastStampedTime.start();
  }
  
  void stampStart()
  {
af27778be   김태훈   HACCP 구현
432
433
434
435
436
      stamp("START (POWER ON)");
  }
  
  void stampMode(Define::Mode mode)
  {
af27778be   김태훈   HACCP 구현
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
      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 구현
455
456
457
458
459
460
      lastFan = fan;
      stamp(QString("FAN = %1").arg(fan));
  }
  
  void stampDamper()
  {
af27778be   김태훈   HACCP 구현
461
462
463
464
465
      stamp("DAMPER");
  }
  
  void stampSideNozzle()
  {
af27778be   김태훈   HACCP 구현
466
467
468
469
470
      stamp("SIDE NOZZLE");
  }
  
  void stampEnd()
  {
af27778be   김태훈   HACCP 구현
471
472
473
474
475
      stamp("END");
  }
  
  void stampError(QString error)
  {
659f92ed3   김태훈   HACCP 오류 출력 변경
476
477
478
479
      if (data.type == Engineering)
          stamp(QString("ERROR (%1)").arg(error));
      else
          stamp(QString("END (%1)").arg(error));
af27778be   김태훈   HACCP 구현
480
481
482
483
  }
  
  void saveData()
  {
af27778be   김태훈   HACCP 구현
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
      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");
af27778be   김태훈   HACCP 구현
505
506
      stream << "No.," << processCount << "
  ";
21aff1b81   김태훈   HACCP에 시스템 정보 포함
507
508
509
510
511
512
      stream << "Typ," << Config::getInstance()->getProductModelName() << "
  ";
      stream << "Serial No.," << Config::getInstance()->getProductSerial() << "
  ";
      stream << "Version," << Config::getInstance()->getProductSWVersion() << "
  ";
af27778be   김태훈   HACCP 구현
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
      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
  "
39fec1254   김태훈   요청 사항 반영
548
549
550
551
552
553
554
555
556
557
558
559
560
561
             << "#5,Door Open / Closed
  "
             << "#6,Humidity (Target)
  "
             << "#7,Temp (Target)
  "
             << "#8,Time (Target)
  "
             << "#9,Water Level
  "
             << "#10,Valve
  "
             << "#11,Heater
  ";
af27778be   김태훈   HACCP 구현
562
563
564
  
      stream << "
  ";
39fec1254   김태훈   요청 사항 반영
565
566
      stream << "#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11
  ";
af27778be   김태훈   HACCP 구현
567
568
569
570
571
572
573
574
575
576
      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
39fec1254   김태훈   요청 사항 반영
577
                      && s.curTemp == n.curTemp
af27778be   김태훈   HACCP 구현
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
                      && 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)) << ","
39fec1254   김태훈   요청 사항 반영
597
                 << s.curTemp << ","
af27778be   김태훈   HACCP 구현
598
599
                 << s.targetCoreTemp << ","
                 << s.curCoreTemp << ","
39fec1254   김태훈   요청 사항 반영
600
601
602
603
604
605
606
607
608
609
610
611
612
                 << (s.door ? "O," : "C,")
                 << s.targetHumidity << ","
                 << s.targetTemp << ","
                 << Stringer::timeSecs(s.remainingTime) << ","
                 << (s.waterLevel == 3 ? "H," :
                     s.waterLevel == 1 ? "C," :
                     s.waterLevel == 0 ? "L," : "E,")
                 << (s.ssv ? "O," : "C,")
                 << (s.heater & 0x01 ? "1" : "0")
                 << (s.heater & 0x02 ? "1" : "0")
                 << (s.heater & 0x04 ? "1" : "0")
                 << (s.heater & 0x08 ? "1" : "0") << "
  ";
af27778be   김태훈   HACCP 구현
613
614
615
616
      }
  
      if (!data.autoConfig.isEmpty())
      {
21aff1b81   김태훈   HACCP에 시스템 정보 포함
617
          foreach (AutoCookConfig c, data.autoConfig)
af27778be   김태훈   HACCP 구현
618
619
620
621
622
623
624
625
626
              stream << Define::name(c.type)
                     << " = "
                     << c.level << "
  ";
      }
  
      stream.flush();
      file.close();
      sync();
af27778be   김태훈   HACCP 구현
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
      processCount++;
  
      data.type = Invalid;
  
      checkTimer.stop();
  }
  
  void check()
  {
      switch (data.type)
      {
      case Invalid:
      case Wash:
      case Engineering:
          return;
      default:
          break;
      }
  
      Oven *oven = Oven::getInstance();
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
647
      UdpHandler *udp = UdpHandler::getInstance();
af27778be   김태훈   HACCP 구현
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  
      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();
b67520704   고영탁   재세척 생략 버전 2.0.05 릴리즈
677
  #if WATER_LEVEL_HACCP_STAMP
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
678
679
      if (udp->waterLevel() != lastWaterLevel)
          stamp();
b67520704   고영탁   재세척 생략 버전 2.0.05 릴리즈
680
  #endif
2c746c43f   김태훈   HACCP 기록 조건에 수위 변...
681
39fec1254   김태훈   요청 사항 반영
682
      if (lastStampedTime.elapsed() > 30 * 1000 - 100)
af27778be   김태훈   HACCP 구현
683
684
685
686
          stamp();
  }
  
  }