| ofs | hex dump | ascii |
|---|
| 0000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 f4 00 00 01 98 08 06 00 00 00 b4 e0 10 | .PNG........IHDR................ |
| 0020 | ab 00 00 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 00 00 09 70 48 59 73 00 00 19 f6 00 00 19 | .....gAMA......a.....pHYs....... |
| 0040 | f6 01 8a f9 eb 79 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 70 61 69 6e 74 2e 6e 65 74 | .....y....tEXtSoftware.paint.net |
| 0060 | 20 34 2e 30 2e 32 31 f1 20 69 95 00 00 a8 88 49 44 41 54 78 5e ed 9d 09 78 94 e5 b9 fe bb f7 74 | .4.0.21..i.....IDATx^...x......t |
| 0080 | 3d 3d ad 5d 4e 4f 21 40 04 22 9c 40 14 8b 15 ad b6 3d ed 5f ed be b8 75 39 3d 6d 6d 55 d0 6a 51 | ==.]NO!@.".@.....=._...u9=mmU.jQ |
| 00a0 | 51 8a 0b 02 22 45 41 ad 52 0a b6 a0 08 8a 58 44 10 51 90 2c 6c 01 12 c2 4e 58 93 00 21 7b 66 86 | Q..."EA.R.....XD.Q.,l...NX..!{f. |
| 00c0 | ec db 24 99 ff f3 fb f2 7e e3 47 c8 32 93 4c 26 33 c9 73 5f d7 7d 41 be d9 de 6f bd df e7 79 9f | ..$.....~.G.2.L&3.s_.}A...o...y. |
| 00e0 | e5 7d 0a 85 a2 4f e2 03 c2 4f 1a 7e 56 f8 25 c3 c1 c2 58 e1 85 c2 78 e1 68 c3 6b 84 d7 19 de 2c | .}...O...O.~V.%...X...x.h.k...., |
| 0100 | bc c5 70 bc f0 61 07 e7 08 ff ee e0 52 e1 1a 07 df 14 6e 17 ee 6a c5 83 c2 fc 0e 58 22 6c 16 fa | ..p..a......R.....n..j.....X"l.. |
| 0120 | 42 c0 4a 61 5b bf 01 73 85 ad c7 b6 49 e8 dc 07 f6 c9 de bf b9 42 7b df ef 11 da c7 e5 fb 42 8e | B.Ja[..s....I........B{.......B. |
| 0140 | d5 15 42 8e df 7f 0b 39 ae 1c 5f 8e f3 e7 85 1c fb 8f 0a 15 0a 85 42 d1 0f 81 00 20 08 88 03 42 | ..B....9.._...........B........B |
| 0160 | 71 a5 10 b1 fd b1 f0 d7 42 c4 e4 11 e1 54 e1 42 e1 0b c2 e5 42 44 69 b3 f0 84 f0 94 f0 b4 b0 50 | q.......B....T.B....BDi........P |
| 0180 | 58 24 74 09 cf 1a 56 0b 6b 0d 43 25 a0 ca f7 bd af 49 c8 31 ad 11 72 9c 3d 42 8e 7d 9e 90 f3 b1 | X$t...V.k.C%.....I.1..r.=B.}.... |
| 01a0 | 5b 98 2c 7c 49 38 5f c8 39 bc 5d f8 73 21 e7 77 ac f0 22 e1 17 84 4c c6 14 0a 85 42 d1 8b f8 37 | [.,|I8_.9.].s!.w.."...L....B...7 |
| 01c0 | e1 a7 85 58 67 5f 11 62 05 5f 26 e4 81 cd 83 fb 4f c2 47 85 4f 0b 11 e1 75 42 ac d9 6c 61 81 10 | ...Xg_.b._&.....O.G.O...uB..la.. |
| 01e0 | 51 40 64 6d b6 25 1c 91 46 c6 8c 88 39 89 a0 b9 5b 91 49 05 e2 76 a6 2d c6 c6 c6 16 dc 7f ff fd | Q@dm.%..F...9...[.I..v.-........ |
| 0200 | 25 a1 e0 85 17 5e d8 ee ef 08 39 ce ad c7 56 21 74 8e 9f 7d 6a 6b 5f 7b 9a ce 73 5f 26 3c 2e dc | %....^....9...V!t..}jk_{..s_&<.. |
| 0220 | 26 fc 97 f0 59 21 93 b8 3b 84 d7 0b af 16 0e 15 0e 10 72 bd 7d 42 f8 21 a1 42 a1 50 28 3a 00 16 | &...Y!..;.........r.}B.!.B.P(:.. |
| 0240 | d3 05 c2 11 42 5c ac 3f 14 de 2a 9c 26 7c 5e f8 8e f0 90 10 61 46 4c 10 09 2c e1 de 14 65 7e bb | ....B\.?..*.&|^.....aFL..,...e~. |
| 0260 | 54 88 2b 9b 31 65 1a a6 0b 37 c5 c7 c7 a7 cd 9b 37 ef c8 82 05 0b 32 e1 f2 e5 cb f7 ac 5a b5 6a | T.+.1e...7......7.....2......Z.j |
| 0280 | 17 7c f7 dd 77 b7 a7 a6 a6 6e 85 db b7 6f df 72 fc f8 f1 64 9b a7 4f 9f 4e 72 b9 5c 7e ba dd ee | .|..w....n...o.r...d..O.Nr.\~... |
| 02a0 | c4 f2 f2 f2 73 58 5d 5d 9d 58 53 53 93 d4 9a 0d 0d 0d c9 ed b1 a9 a9 a9 d4 17 3a b8 da fa 0d 58 | ....sX]].XSS..............:....X |
| 02c0 | 5f 5f 9f dc 7a 5c 55 55 55 49 ce f1 b3 4f f6 fe e5 e6 e6 26 d9 fb be 6b d7 ae cd 1c 93 2d 5b b6 | __..z\UUUI...O.....&...k.....-[. |
| 02e0 | 6c b5 8f d5 0b 2f bc b0 9f e3 f7 ec b3 cf 1e e5 98 ca b1 dd 69 8e 73 8e 90 63 5f 27 6c eb fc 84 | l..../..............i.s..c_'l... |
| 0300 | 8a 4c 40 98 2c e1 91 39 2a c4 4b 83 17 60 86 10 6f ce 77 85 97 08 63 84 ea fe 57 28 14 7d 0e ef | .L@.,..9*.K..`..o.w...c...W(.}.. |
| 0320 | 17 3a ad e9 41 42 84 fa 06 e1 dd c2 d9 42 2c 68 ac 23 dc a5 5e 61 a3 d0 b6 a0 db 7a b0 06 cb 06 | .:..AB.......B,h.#..^a.....z.... |
| 0340 | 21 2e 5a ac 43 dc b3 a5 62 a5 16 dd 7b ef bd ae 69 d3 a6 e5 3e fe f8 e3 27 9f 7b ee b9 23 0b 17 | !.Z.C...b...{...i...>...'.{..#.. |
| 0360 | 2e 3c f4 f2 cb 2f ef 59 b9 72 65 06 42 0b 33 32 32 36 ed de bd 7b 93 2d b0 79 79 79 7e 41 42 b0 | .<.../.Y.re.B.3226...{.-.yyy~AB. |
| 0380 | 44 1c 53 da a3 88 5d b2 a1 07 e5 53 84 14 1c d3 36 8f 7f 45 45 45 92 c7 e3 49 e2 7c 1d 3d 7a 34 | D.S...]....S....6..EEE...I.|.=z4 |
| 03a0 | 99 f3 c7 b9 5c bb 76 ed 4e ce ef fc f9 f3 0f 3f f1 c4 13 d9 8f 3c f2 48 de c4 89 13 cf 0e 1e 3c | ....\.v.N......?.....<.H.......< |
| 03c0 | d8 5e 26 21 1e a0 5e d8 d6 35 14 28 b9 6e b9 7e b9 8e 89 23 48 15 2e 11 4e 17 fe 5e 88 07 69 a4 | .^&!..^..5.(.n.~...#H...N..^..i. |
| 03e0 | f0 8b 42 ee 09 15 7e 85 42 11 51 f8 b0 f0 3f 85 a3 84 df 16 fe 46 38 53 f8 8a 90 07 da 61 21 0f | ..B...~.B.Q...?......F8S.....a!. |
| 0400 | 37 1e 98 6d 3d 04 bb 42 c4 99 75 eb a3 23 46 8c d8 37 77 ee dc e3 08 f2 6b af bd 96 b1 71 e3 c6 | 7..m=..B..u..#F..7w.....k....q.. |
| 0420 | 54 2c 3c 1e e6 08 70 49 49 89 65 dd f2 a0 17 2b d1 b2 1c 8d 20 28 14 3e 99 04 94 e0 3d 90 eb c3 | T,<...pII.e....+.....(.>....=... |
| 0440 | f2 16 e4 e7 e7 27 1e 3e 7c 38 19 8f c9 ba 75 eb 76 be f8 e2 8b fb 9e 7a ea a9 e3 5c 67 72 bd 11 | .....'.>|8....u.v......z...\gr.. |
| 0460 | 37 81 37 a6 bb 93 4d 26 98 dc 13 78 9b 52 84 ff 10 4e 12 fe 48 f8 55 21 ee 7d 26 c2 0a 85 42 11 | 7.7...M&...x.R...N..H.U!.}&...B. |
| 0480 | 12 60 5d 7f 44 c8 9a e1 e7 84 04 8e f1 c0 c1 b2 66 9d f1 6d 21 6b 8f 3c 9c b0 6c b0 4c ba f2 a0 | .`].D...........f..m!k.<..l.L... |
| 04a0 | c3 9a e1 3b 2a 07 0d 1a 54 f6 c0 03 0f 14 cf 9a 35 2b c7 b8 a7 77 db 96 72 4e 4e 8e 25 cc 08 72 | ...;*...T.......5+...w..rNN.%..r |
| 04c0 | 63 63 23 b4 2d 61 15 67 45 8f 83 6b 4d ae 51 26 af 58 d5 1f 13 12 65 ff 2d e1 ff 09 89 d7 40 94 | cc#.-a.gE..kM.Q&.X....e.-.....@. |
| 04e0 | 59 12 22 cb 00 ab 9f 09 2c d7 35 f7 45 5b d7 7d 7b e4 fd 78 95 70 f1 1f 11 be 2e 7c 5c c8 ef 7c | Y.".....,.5.E[.}{..x.p.....|\..| |
| 0500 | 5d f8 5f 42 a2 fa b9 37 b9 47 15 0a 85 c2 8f 8f 0b 71 85 5f 2a fc 81 10 4b 81 a8 ee 77 85 fb 85 | ]._B...7.G.......q._*...K...w... |
| 0520 | 44 10 07 fb 50 72 92 07 5b 6e 5c 5c 5c e6 ec d9 b3 73 16 2f 5e bc 1f cb 67 c7 8e 1d 5b b2 b2 b2 | D...Pr..[n\\\....s./^...g...[... |
| 0540 | 92 b1 88 ca ca ca ac 35 56 79 6e aa 38 47 00 b0 5a 65 e2 94 04 cd ba b7 45 39 4f 89 25 25 25 7e | .......5Vyn.8G..Ze......E9O.%%%~ |
| 0560 | b2 ee 8d 6b 1b 9e 3c 79 32 69 df be 7d 9b f6 ee dd eb 27 56 ae bd 7c 61 73 c3 86 0d db 71 7d b7 | ...k..<y2i..}.....'V..|as....q}. |
| 0580 | c5 a5 4b 97 ee 7d e6 99 67 8e 85 9b f3 e6 cd 3b dc d6 78 a0 bd 04 f3 83 1f fc e0 af 72 1d 4f 31 | ..K..}..g......;..x.........r.O1 |
| 05a0 | fc b3 f0 7f 5b 91 d4 3a 02 e8 6c 7e 47 78 a3 f0 4e 21 82 3f 4f 88 e0 1f 13 62 e9 17 0b 11 fc d6 | ....[..:..l~Gx..N!.?O....b...... |
| 05c0 | f7 4b 67 64 d2 cc 84 e1 80 90 7b 94 71 11 c5 ff 0d 21 d1 fb a4 4e 2a 14 8a 3e 08 66 f0 1f 14 62 | .Kgd......{.q....!...N*..>.f...b |
| 05e0 | 59 90 a2 45 6a 16 33 7d 66 fc ab 85 3c 5c ca 85 c1 5a 12 3c 54 78 bf 77 e0 c0 81 75 44 3c b3 2e | Y..Ej.3}f...<\...Z.<Tx.w...uD<.. |
| 0600 | 69 8b 35 0f 76 dc dd d5 d5 d5 88 02 d6 34 62 ed 6e 91 0b 45 17 e0 c1 42 74 90 f5 63 cb 4b 81 b7 | i.5.v........4b.n..E...Bt..c.K.. |
| 0620 | c2 16 5d 44 96 e3 9e 93 93 93 9c 99 99 99 72 e0 c0 81 94 6d db b6 6d 85 9c 97 d7 5f 7f 7d 17 cb | ..]D..........r....m..m...._.}.. |
| 0640 | 12 ac 23 23 62 73 e6 cc 39 c1 5a b2 bd 9e 3c 7e fc f8 6a 18 13 13 53 25 e7 96 89 18 c4 52 e4 fa | ..##bs..9.Z...<~..j...S%.....R.. |
| 0660 | 80 04 9e e1 51 b1 19 aa 38 87 be 48 eb fe 30 b4 e3 38 6c 72 4c f1 6a 85 32 ca 9f 73 c1 77 5a 13 | ....Q...8..H..0..8lrL.j.2..s.wZ. |
| 0680 | 68 e1 3a e1 63 42 b2 3e c6 08 59 b3 e7 39 c0 f3 40 d3 f5 14 8a 08 06 37 29 d1 e1 c3 85 df 14 32 | h.:.cB.>..Y..9..@......7)......2 |
| 06a0 | 63 27 65 6b 95 90 b5 3f 52 73 da 7a 08 74 44 1e 10 04 91 9d bc e8 a2 8b 32 09 1c c3 5a d9 bc 79 | c'ek...?Rs.z.tD.........2...Z..y |
| 06c0 | f3 56 c4 a2 b0 b0 30 11 21 11 b1 51 a1 0e 00 22 c0 a5 4c 6e 38 66 26 70 0b 2b 37 89 e3 98 9d 9d | .V....0.!..Q..."..Ln8f&p.+7..... |
| 06e0 | 9d c4 ba 2d 5e 0b c4 77 cd 9a 35 69 6f bc f1 46 3a 13 a4 45 8b 16 1d 4c 48 48 d8 2a e7 21 49 c8 | ...-^..w..5io..F:..E...LHH.*.!I. |
| 0700 | b9 c4 a5 4b fc 40 28 0b c4 28 fb 3e b9 56 78 0e 90 a7 8f 65 ff aa 90 65 02 bc 07 bf 13 f2 dc c0 | ...K.@(..(.>.Vx....e...e........ |
| 0720 | c2 e7 39 a2 ae 7c 85 22 4c e0 86 1b 27 a4 f8 09 d5 c4 36 0a 49 97 22 52 37 a8 d4 9d 01 03 06 f8 | ..9..|."L...'.....6.I."R7....... |
| 0740 | 6e b9 e5 16 ef 94 29 53 8a 08 fc 61 cd 1a 41 c1 c2 43 74 ea ea ea ac 14 28 d1 23 75 85 9f 0b 0f | n.....)S...a..A..Ct.....(.#u.... |
| 0760 | 4b 04 88 32 82 4c 14 3c 5e 09 04 99 68 ea 65 cb 96 ed 21 48 8f 08 fa 87 1f 7e 38 ff ee bb ef 2e | K..2.L.<^...h.e...!H.....~8..... |
| 0780 | 1b 3c 78 b0 33 17 1b 0b d8 8e 3b 68 f3 dc 84 9b 5c 0b ff f7 7f ff 67 71 c2 84 09 d5 8c 19 12 d3 | .<x.3.....;h....\.....gq........ |
| 07a0 | f0 e8 a3 8f 9e 81 8f 3d f6 d8 29 d2 c6 6c 32 e1 c0 4d 6e 13 2f 80 d3 bd ce e4 8f e3 d2 9a 78 11 | .......=..)..l2..Mn./.........x. |
| 07c0 | da 62 7e 7e be e5 e2 27 1d 2f 9c 64 c9 a7 ad f1 40 fb dc 3a 99 91 91 b1 d9 b9 9f 70 c5 8a 15 bb | .b~~...'./.d....@..:.......p.... |
| 07e0 | 9d c7 c2 79 9c 9e 7c f2 c9 2c fb 18 da d7 03 bc eb ae bb ca 7f f3 9b df 34 73 cc 39 fe 6d 9d 97 | ...y..|..,..............4s.9.m.. |
| 0800 | 30 93 6b 92 e7 08 a9 80 2f 0a 59 4a 18 22 54 28 14 5d 04 d1 ad 44 8d 93 de 42 1e 36 6b da ff 14 | 0.k...../.YJ."T(.]...D...B.6k... |
| 0820 | 12 31 8e c5 dc d6 8d d8 1e 11 8c b3 14 00 e1 61 b2 60 c1 82 43 58 7f 44 81 b3 e6 e9 f5 7a 0b 8c | .1.............a.`..CX.D.....z.. |
| 0840 | 40 f5 47 78 98 ac d8 11 ce 88 73 41 41 81 f5 00 27 08 2f 31 31 31 15 2b 99 87 33 de 89 19 33 66 | @.Gx......sAA...'./111.+..3...3f |
| 0860 | 9c 66 69 41 c4 ae 2a cc 0f 5f 2c 2b 72 ed 99 04 60 95 b3 f6 4a 8a df c9 e1 c3 87 9f 78 f0 c1 07 | .fiA..*.._,+r...`...J.......x... |
| 0880 | 0b 99 38 e0 3e c7 95 4e be b6 8c 79 df 9b 6f be 99 06 45 54 b7 91 d3 cd 39 c7 0b 40 f4 3f c2 c9 | ..8.>..N...y..o...ET....9..@.?.. |
| 08a0 | be e2 a2 47 40 61 53 53 53 b1 39 2e 8a de 81 db 3e 17 4c 30 38 3f 90 38 13 ce 1b 5e 31 bb 66 01 | ...G@aSSS.9.....>.L08?.8...^1.f. |
| 08c0 | 99 1c 9cusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Integration.Operation;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Connection;
using Tango.Transport.Adapters;
using Tango.Settings;
using Tango.Logging;
using Tango.BL.Builders;
using Tango.Core.DI;
using Tango.PPC.Common.Messages;
using Tango.Emulations.Emulators;
using Tango.Transport.Transporters;
using Tango.Integration;
using Tango.Transport;
using System.Threading;
using Tango.Core.ExtensionMethods;
namespace Tango.PPC.Common.Connection
{
/// <summary>
/// Represents the default <see cref="IMachineProvider"/>.
/// </summary>
/// <seealso cref="Tango.Core.ExtendedObject" />
/// <seealso cref="Tango.PPC.Common.Connection.IMachineProvider" />
public class DefaultMachineProvider : ExtendedObject, IMachineProvider
{
private bool _isInitialized;
private Thread _connection_thread;
private ObservablesContext _context;
private bool disableConnectionFileLogging;
/// <summary>
/// Occurs when the machine has connected.
/// </summary>
public event EventHandler MachineConnected;
/// <summary>
/// Occurs when the machine has disconnected.
/// </summary>
public event EventHandler MachineDisconnected;
private bool _isConnected;
/// <summary>
/// Gets a value indicating whether the machine is currently connected.
/// </summary>
public bool IsConnected
{
get { return _isConnected; }
private set { _isConnected = value; RaisePropertyChangedAuto(); }
}
private Machine _machine;
/// <summary>
/// Gets the database machine entity associated with the current machine.
/// </summary>
public Machine Machine
{
get
{
return _machine;
}
private set
{
_machine = value;
RaisePropertyChangedAuto();
}
}
private IMachineOperator _machineOperator;
/// <summary>
/// Gets the machine operator.
/// </summary>
public IMachineOperator MachineOperator
{
get
{
return _machineOperator;
}
private set
{
_machineOperator = value;
RaisePropertyChangedAuto();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DefaultMachineProvider"/> class.
/// </summary>
public DefaultMachineProvider()
{
var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
MachineOperator = new MachineOperator();
MachineOperator.StatusChanged += MachineOperator_StatusChanged;
MachineOperator.EnableEventsNotification = true;
MachineOperator.EnableJobResume = true;
MachineOperator.UseKeepAlive = true;
MachineOperator.EnableMachineStatusUpdates = true;
MachineOperator.EnableDiagnostics = true;
MachineOperator.EnablePowerUpSequence = true;
MachineOperator.EnableEmbeddedDebugging = settings.EnableEmbeddedDebugLogs;
MachineOperator.EnableAutomaticThreadLoading = settings.EnableAutomaticThreadLoading;
MachineOperator.JobRunsLogger.JobSource = BL.Enumerations.JobSource.Local;
MachineOperator.FirmwareUpgradeMode = Integration.Upgrade.FirmwareUpgradeModes.DFU | Integration.Upgrade.FirmwareUpgradeModes.TFP_PACKAGE;
MachineOperator.JobUploadStrategy = settings.JobUploadStrategy;
MachineOperator.JobUnitsMethod = settings.JobUnitsMethod;
MachineOperator.GradientGenerationConfiguration.IsEnabled = settings.EnableGradientGeneration;
MachineOperator.GradientGenerationConfiguration.ResolutionCM = settings.GradientGenerationResolution;
MachineOperator.EmergencyNotificationProvider.Address = settings.EmergencyComPort;
MachineOperator.EmergencyNotificationProvider.IsEnabled = settings.EnableEmergencyNotifications;
MachineOperator.EnableJobLiquidQuantityValidation = settings.EnableJobLiquidQuantityValidation;
}
private void MachineOperator_StatusChanged(object sender, MachineStatuses status)
{
if (status != MachineStatuses.Disconnected)
{
if (!IsConnected)
{
OnMachineConnected();
}
}
else
{
if (IsConnected)
{
OnMachineConnected();
}
}
}
private async void ConnectionThreadMethod()
{
while (true)
{
if (MachineOperator.State != TransportComponentState.Connected)
{
var fileLogger = LogManager.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger));
try
{
Thread.Sleep(2000);
if (fileLogger != null && disableConnectionFileLogging)
{
fileLogger.Enabled = false;
}
LogManager.Log("Starting machine connection procedure...", LogCategory.Info);
var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
if (!Machine.IsDemo)
{
if (String.IsNullOrWhiteSpace(settings.EmbeddedComPort))
{
TimeSpan timeout = TimeSpan.FromSeconds(SettingsManager.Default.GetOrCreate<PPCSettings>().MachineScanningTimeoutSeconds);
LogManager.Log("Scanning for machine on available serial ports...", LogCategory.Info);
Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse> scanner = new Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse>(UsbSerialBaudRates.BR_115200);
var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, settings.EmbeddedDeviceHint, timeout);
LogManager.Log("Machine discovered on port: " + response.Adapter.Address, LogCategory.Info);
LogManager.Log("Device Information:", LogCategory.Debug);
LogManager.Log(response.Response.DeviceInformation.ToJsonString(), LogCategory.Info);
LogManager.Log("Disconnecting machine operator...", LogCategory.Info);
await MachineOperator.Disconnect();
MachineOperator.Adapter = response.Adapter;
MachineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
LogManager.Log("Connecting machine operator...", LogCategory.Info);
try
{
await MachineOperator.Connect();
if (MachineOperator.DeviceInformation != null)
{
settings.FirmwareVersion = MachineOperator.DeviceInformation.Version;
settings.Save();
}
disableConnectionFileLogging = false;
}
catch (Exception)
{
await response.Adapter.Disconnect();
throw;
}
await Task.Delay(1000);
await MachineOperator.UploadHardwareConfiguration(Machine.Configuration.HardwareVersion, Machine.Configuration);
MachineOperator.UseKeepAlive = true;
}
else
{
LogManager.Log($"Connecting to machine on {settings.EmbeddedComPort}...", LogCategory.Info);
UsbTransportAdapter adapter = new UsbTransportAdapter(settings.EmbeddedComPort, UsbSerialBaudRates.BR_115200);
MachineOperator.Adapter = adapter;
MachineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
try
{
await MachineOperator.Connect();
if (MachineOperator.DeviceInformation != null)
{
settings.FirmwareVersion = MachineOperator.DeviceInformation.Version;
settings.Save();
}
disableConnectionFileLogging = false;
}
catch (Exception)
{
await adapter.Disconnect();
throw;
}
await Task.Delay(1000);
await MachineOperator.UploadHardwareConfiguration(Machine.Configuration.HardwareVersion, Machine.Configuration);
MachineOperator.UseKeepAlive = true;
}
}
else
{
LogManager.Log("Application in demo mode!");
var emulator_channel_name = "emulator-" + Guid.NewGuid().ToString();
LogManager.Log("Starting embedded emulator...");
MachineEmulator emulator = new MachineEmulator(new BasicTransporter(new MemoryTransportAdapter(emulator_channel_name)));
await emulator.Start();
LogManager.Log("Emulator started. Connecting to emulator...");
MemoryTransportAdapter adapter = new MemoryTransportAdapter(emulator_channel_name);
MachineOperator.Adapter = adapter;
MachineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
LogManager.Log("Connecting machine operator...");
await MachineOperator.Connect();
disableConnectionFileLogging = false;
if (MachineOperator.DeviceInformation != null)
{
settings.FirmwareVersion = MachineOperator.DeviceInformation.Version;
settings.Save();
}
await Task.Delay(1000);
await MachineOperator.UploadHardwareConfiguration(Machine.Configuration.HardwareVersion, Machine.Configuration);
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error while trying to scan and connect to the machine.");
LogManager.Log("File logging of further connection attempts is now disabled and will resume when connection is successful.");
disableConnectionFileLogging = true;
}
finally
{
await Task.Delay(100);
if (fileLogger != null)
{
fileLogger.Enabled = true;
}
}
}
Thread.Sleep(5000);
}
}
/// <summary>
/// Initializes this machine provider start machine port scanning and connection.
/// </summary>
public void Init(Machine machine, ObservablesContext context)
{
if (!_isInitialized)
{
LogManager.Log("Initializing machine operator...");
_isInitialized = true;
LogManager.Log("Retrieving first machine database entry...");
_context = context;
Machine = machine;
if (Machine != null)
{
LogManager.Log("First machine entry found. Machine serial number is: " + Machine.SerialNumber + ".");
if (Machine.IsDemo)
{
LogManager.Log("Machine is in demo mode. Changing firmware upgrade mode to TFP package only.");
MachineOperator.FirmwareUpgradeMode = Integration.Upgrade.FirmwareUpgradeModes.TFP_PACKAGE;
}
ConnectToMachine();
}
else
{
LogManager.Log("No machine entry found on database. Could not connect to the embedded device!", LogCategory.Error);
}
}
}
/// <summary>
/// Tries to connect to the machine by scanning all available serial ports.
/// The timeout for a scan cycle is specified on <see cref="PPCSettings.MachineScanningTimeoutSeconds"/>.
/// </summary>
private void ConnectToMachine()
{
if (_connection_thread == null)
{
_connection_thread = new Thread(ConnectionThreadMethod);
_connection_thread.IsBackground = true;
_connection_thread.Start();
}
}
/// <summary>
/// Saves the machine settings.
/// </summary>
/// <returns></returns>
public async Task SaveMachine()
{
await _context.SaveChangesAsync();
Machine = await new MachineBuilder(_context).SetFirst().BuildAsync();
TangoMessenger.Default.Send(new MachineSettingsSavedMessage() { Machine = Machine });
}
/// <summary>
/// Creates and connects a machine operator with no continuous requests at all.
/// </summary>
/// <returns></returns>
public static async Task<IMachineOperator> CreateMinimalMachineOperator(Action<String> onProgress = null)
{
LogManager.Default.Log("Creating minimal machine operator...");
var machineOperator = new MachineOperator();
machineOperator.EnableDiagnostics = false;
machineOperator.EnableEmbeddedDebugging = false;
machineOperator.EnableEventsNotification = false;
machineOperator.EnableMachineStatusUpdates = false;
machineOperator.EnableJobResume = false;
LogManager.Default.Log("Starting machine connection procedure...");
var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
if (String.IsNullOrWhiteSpace(settings.EmbeddedComPort))
{
TimeSpan timeout = TimeSpan.FromSeconds(SettingsManager.Default.GetOrCreate<PPCSettings>().MachineScanningTimeoutSeconds);
onProgress?.Invoke("Scanning for the machine...");
LogManager.Default.Log("Scanning for machine on available serial ports...");
Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse> scanner = new Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse>(UsbSerialBaudRates.BR_115200);
var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, timeout);
onProgress?.Invoke("Machine discovered on port: " + response.Adapter.Address);
LogManager.Default.Log("Machine discovered on port: " + response.Adapter.Address);
LogManager.Default.Log("Device Information:");
LogManager.Default.Log(response.Response.DeviceInformation.ToJsonString());
machineOperator.Adapter = response.Adapter;
machineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
LogManager.Default.Log("Connecting machine operator...");
onProgress?.Invoke("Connecting machine operator...");
await machineOperator.Connect();
}
else
{
LogManager.Default.Log($"Connecting to machine on {settings.EmbeddedComPort}...");
onProgress?.Invoke($"Connecting to machine on {settings.EmbeddedComPort}...");
UsbTransportAdapter adapter = new UsbTransportAdapter(settings.EmbeddedComPort, UsbSerialBaudRates.BR_115200);
machineOperator.Adapter = adapter;
machineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
await machineOperator.Connect();
}
return machineOperator;
}
/// <summary>
/// Called when the machine has connected.
/// </summary>
protected virtual void OnMachineConnected()
{
IsConnected = true;
MachineConnected?.Invoke(this, new EventArgs());
}
/// <summary>
/// Called when the machine has disconnected.
/// </summary>
protected virtual void OnMachineDisconnected()
{
IsConnected = false;
MachineDisconnected?.Invoke(this, new EventArgs());
}
}
}
|